eclipse plugin: Refactor a bit the InstallsPage,

adding a new util class to handle the installs persistance
This commit is contained in:
Timotei Dolean 2011-06-20 15:33:07 +00:00
parent 1b309b0992
commit 4a46cf94be
2 changed files with 89 additions and 42 deletions

View file

@ -49,13 +49,14 @@ import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.xtext.ui.editor.preferences.fields.LabelFieldEditor;
import org.wesnoth.Constants;
import org.wesnoth.Logger;
import org.wesnoth.Messages;
import org.wesnoth.WesnothPlugin;
import org.wesnoth.templates.ReplaceableParameter;
import org.wesnoth.templates.TemplateProvider;
import org.wesnoth.utils.GUIUtils;
import org.wesnoth.utils.StringUtils;
import org.wesnoth.utils.WesnothInstallsUtils;
import org.wesnoth.utils.WesnothInstallsUtils.WesnothInstall;
public class WesnothInstallsPage extends AbstractPreferencePage
{
@ -91,19 +92,10 @@ public class WesnothInstallsPage extends AbstractPreferencePage
// add the default install first
installs_.put( "Default", new WesnothInstall( "Default", "" ) ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
// unpack installs
String[] installs = Preferences.getString( Constants.P_INST_INSTALL_LIST ).split( ";" ); //$NON-NLS-1$
for ( String str : installs ){
if ( str.isEmpty() )
continue;
String[] tokens = str.split( ":" ); //$NON-NLS-1$
if ( tokens.length != 2 ) {
Logger.getInstance().logError( "invalid install [" + str + "] in installs list." ); //$NON-NLS-1$
continue;
}
installs_.put( tokens[0], new WesnothInstall( tokens[0], tokens[1] ) );
List<WesnothInstall> installs = WesnothInstallsUtils.getInstalls( );
for ( WesnothInstall wesnothInstall : installs )
{
installs_.put( wesnothInstall.Name, wesnothInstall );
}
}
@ -520,22 +512,7 @@ public class WesnothInstallsPage extends AbstractPreferencePage
wmlToolsField_.setStringValue(""); //$NON-NLS-1$
saveInstall();
// pack back the installs
StringBuilder installs = new StringBuilder();
for ( WesnothInstall install : installs_.values() ) {
// don't save the default install
if ( install.Name.equals( "Default" ) )
continue;
if ( installs.length() > 0 )
installs.append( ";" ); //$NON-NLS-1$
installs.append( install.Name );
installs.append( ":" ); //$NON-NLS-1$
installs.append( install.Version );
}
Preferences.getPreferences().setValue( Constants.P_INST_INSTALL_LIST, installs.toString() );
WesnothInstallsUtils.setInstalls( installs_.values( ) );
}
@Override
@ -584,16 +561,4 @@ public class WesnothInstallsPage extends AbstractPreferencePage
return ""; //$NON-NLS-1$
}
}
public static class WesnothInstall
{
public String Name;
public String Version;
public WesnothInstall(String name, String version)
{
Name = name;
Version = version;
}
}
}

View file

@ -0,0 +1,82 @@
/*******************************************************************************
* Copyright (c) 2011 by Timotei Dolean <timotei21@gmail.com>
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.wesnoth.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.wesnoth.Constants;
import org.wesnoth.Logger;
import org.wesnoth.preferences.Preferences;
public class WesnothInstallsUtils
{
public static class WesnothInstall
{
public String Name;
public String Version;
public WesnothInstall(String name, String version)
{
Name = name;
Version = version;
}
}
/**
* Returns a list of the current wesnoth installations available
* in the preferences store
* @return A list with Wesnoth Installs
*/
public static List<WesnothInstall> getInstalls()
{
List<WesnothInstall> installsList = new ArrayList<WesnothInstall>();
// unpack installs
String[] installs = Preferences.getString( Constants.P_INST_INSTALL_LIST ).split( ";" ); //$NON-NLS-1$
for ( String str : installs ){
if ( str.isEmpty() )
continue;
String[] tokens = str.split( ":" ); //$NON-NLS-1$
if ( tokens.length != 2 ) {
Logger.getInstance().logError( "invalid install [" + str + "] in installs list." ); //$NON-NLS-1$
continue;
}
installsList.add( new WesnothInstall( tokens[0], tokens[1] ) );
}
return installsList;
}
/**
* Sets the specified Installs list in the preferences store
* @param installsList The list to replace / set the installs list
*/
public static void setInstalls( Collection<WesnothInstall> installsList )
{
// pack back the installs
StringBuilder installs = new StringBuilder();
for ( WesnothInstall install : installsList ) {
// don't save the default install
if ( install.Name.equals( "Default" ) )
continue;
if ( installs.length() > 0 )
installs.append( ";" ); //$NON-NLS-1$
installs.append( install.Name );
installs.append( ":" ); //$NON-NLS-1$
installs.append( install.Version );
}
Preferences.getPreferences().setValue( Constants.P_INST_INSTALL_LIST, installs.toString() );
}
}