eclipse plugin: Add the interface copied over from...

...the main preferences page
This commit is contained in:
Timotei Dolean 2011-05-22 20:10:38 +00:00
parent 37cd9a6645
commit 039e59b97b
5 changed files with 382 additions and 22 deletions

View file

@ -64,6 +64,9 @@ public class Constants
/** Advanced preferences */
public static final String P_ADV_NO_TERRAIN_GFX = "adv_no_terrain_gfx"; //$NON-NLS-1$
/** Install preferences */
public static final String P_INST_DEFAULT_INSTALL = "inst_default"; //$NON-NLS-1$
/** Wizards Constants **/
public static final int WIZ_TextBoxHeight = 21;
public static final int WIZ_MaxTextBoxesOnPage = 10;

View file

@ -10,8 +10,8 @@ package org.wesnoth.preferences;
import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.jface.preference.IPreferenceStore;
import org.wesnoth.WesnothPlugin;
import org.wesnoth.Constants;
import org.wesnoth.WesnothPlugin;
/**
* Class used to initialize default preference values.
@ -48,6 +48,9 @@ public class Preferences extends AbstractPreferenceInitializer
// advanced
store.setDefault(Constants.P_ADV_NO_TERRAIN_GFX, true);
// installs
store.setDefault(Constants.P_INST_DEFAULT_INSTALL, "none"); // $NON-NLS-1$
}
/**

View file

@ -0,0 +1,263 @@
/*******************************************************************************
* 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.preferences;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.SelectionDialog;
import org.eclipse.xtext.ui.editor.preferences.fields.LabelFieldEditor;
import org.wesnoth.Constants;
import org.wesnoth.Messages;
import org.wesnoth.templates.ReplaceableParameter;
import org.wesnoth.templates.TemplateProvider;
import org.wesnoth.utils.StringUtils;
public class WesnothInstallDialog extends SelectionDialog
{
private Text txtInstallName_;
private Combo cmbVersion_;
private DirectoryFieldEditor wmlToolsField_;
private DirectoryFieldEditor wesnothWorkingDirField_;
private DirectoryFieldEditor wesnothUserDirField_;
private FileFieldEditor wesnothExecutableField_;
private List<String> wmlToolsList_;
private Composite compositeParent_;
/**
* @wbp.parser.constructor
*/
protected WesnothInstallDialog(Shell parentShell)
{
super(parentShell);
wmlToolsList_ = new ArrayList<String>();
wmlToolsList_.add("wmllint"); //$NON-NLS-1$
wmlToolsList_.add("wmlindent"); //$NON-NLS-1$
wmlToolsList_.add("wmlscope"); //$NON-NLS-1$
wmlToolsList_.add("wesnoth_addon_manager"); //$NON-NLS-1$
}
public WesnothInstallDialog(Shell parentShell, String data)
{
this(parentShell);
}
@Override
protected Control createDialogArea(Composite parent)
{
Composite composite = new Composite(parent, 0);
compositeParent_ = composite;
composite.setLayout(new GridLayout(10, false));
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
ModifyListener listener = new ModifyListener() {
@Override
public void modifyText(ModifyEvent e)
{
checkState();
guessDefaultPaths();
}
};
wesnothExecutableField_ = new FileFieldEditor(Constants.P_WESNOTH_EXEC_PATH,
Messages.WesnothPreferencesPage_5, composite);
wesnothExecutableField_.getTextControl(composite).
addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e)
{
checkState();
String wesnothExec = wesnothExecutableField_.getStringValue();
if (wesnothWorkingDirField_.getStringValue().isEmpty() &&
!wesnothExec.isEmpty() &&
new File(wesnothExec.substring(0,
wesnothExec.lastIndexOf(new File(wesnothExec).getName()))).exists())
{
wesnothWorkingDirField_.setStringValue(wesnothExec.substring(0,
wesnothExec.lastIndexOf(new File(wesnothExec).getName()))
);
}
}
@Override
public void focusGained(FocusEvent e)
{
}
});
wesnothExecutableField_.getLabelControl(composite).setToolTipText(Messages.WesnothPreferencesPage_6);
wesnothWorkingDirField_ = new DirectoryFieldEditor(Constants.P_WESNOTH_WORKING_DIR,
Messages.WesnothPreferencesPage_7, composite);
wesnothWorkingDirField_.getTextControl(composite).
addModifyListener(listener);
wesnothWorkingDirField_.getLabelControl(composite).setToolTipText(Messages.WesnothPreferencesPage_8);
wesnothUserDirField_ = new DirectoryFieldEditor(Constants.P_WESNOTH_USER_DIR,
Messages.WesnothPreferencesPage_9, composite);
wesnothUserDirField_.getLabelControl(composite).setToolTipText(Messages.WesnothPreferencesPage_10);
wmlToolsField_ = new DirectoryFieldEditor(Constants.P_WESNOTH_WMLTOOLS_DIR,
Messages.WesnothPreferencesPage_11, composite);
wmlToolsField_.getLabelControl(composite).setToolTipText(Messages.WesnothPreferencesPage_12);
new FileFieldEditor(Constants.P_PYTHON_PATH, Messages.WesnothPreferencesPage_13, composite);
new LabelFieldEditor(Messages.WesnothPreferencesPage_14, composite);
guessDefaultPaths();
return composite;
}
protected void checkState()
{
testWMLToolsPath(wmlToolsField_.getStringValue());
}
/**
* Tries the list of available paths for current os
*/
private void guessDefaultPaths()
{
String os = "linux"; //$NON-NLS-1$
if (Constants.IS_MAC_MACHINE)
os = "mac"; //$NON-NLS-1$
else if (Constants.IS_WINDOWS_MACHINE)
os = "windows"; //$NON-NLS-1$
List<ReplaceableParameter> params = new ArrayList<ReplaceableParameter>();
params.add(new ReplaceableParameter("$$home_path", System.getProperty("user.home"))); //$NON-NLS-1$ //$NON-NLS-2$
testPaths(StringUtils.getLines(
TemplateProvider.getInstance().getProcessedTemplate(os + "_exec", params)), //$NON-NLS-1$
wesnothExecutableField_);
testPaths(StringUtils.getLines(
TemplateProvider.getInstance().getProcessedTemplate(os + "_data", params)), //$NON-NLS-1$
wesnothWorkingDirField_);
testPaths(StringUtils.getLines(
TemplateProvider.getInstance().getProcessedTemplate(os + "_user", params)), //$NON-NLS-1$
wesnothUserDirField_);
// guess the working dir based on executable's path
Text textControl = wesnothWorkingDirField_.getTextControl(compositeParent_);
String wesnothExec = wesnothExecutableField_.getStringValue();
if (wesnothWorkingDirField_.getStringValue().isEmpty() &&
!wesnothExec.isEmpty() &&
new File(wesnothExec.substring(0,
wesnothExec.lastIndexOf(new File(wesnothExec).getName()))).exists())
{
textControl.setText(wesnothExec.substring(0,
wesnothExec.lastIndexOf(new File(wesnothExec).getName()))
);
}
// guess the wmltools path
if (wmlToolsField_.getStringValue().isEmpty() &&
!wesnothWorkingDirField_.getStringValue().isEmpty())
{
String path = wesnothWorkingDirField_.getStringValue() + "/data/tools"; //$NON-NLS-1$
if (testWMLToolsPath(path))
wmlToolsField_.setStringValue(path);
}
// guess the userdata path
if (wesnothUserDirField_.getStringValue().isEmpty() &&
!wesnothWorkingDirField_.getStringValue().isEmpty())
{
String path = wesnothWorkingDirField_.getStringValue() + "/userdata"; //$NON-NLS-1$
testPaths(new String[] { path },wesnothUserDirField_);
}
checkState();
}
/**
* Tests for wmltools in the specified path
* @param path
* @return
*/
private boolean testWMLToolsPath(String path)
{
for(String tool : wmlToolsList_)
{
if (!(new File(path + Path.SEPARATOR + tool).exists()))
{
wmlToolsField_.setErrorMessage(String.format(Messages.WesnothPreferencesPage_24,
tool));
return false;
}
}
return true;
}
/**
* Tests the list of paths and if any path exists it will
* set it as the string value to the field editor
* if the field editor value is empty
* @param list The list to search in
* @param field The field to put the path in
*/
private void testPaths(String[] list, StringFieldEditor field)
{
if (!(field.getStringValue().isEmpty()))
return;
for(String path : list)
{
if (new File(path).exists())
{
field.setStringValue(path);
return;
}
}
}
/**
* This method will unsert invalid properties's values,
* thus saving only valid ones.
*/
private void unsetInvalidProperties()
{
if (!wesnothExecutableField_.isValid())
wesnothExecutableField_.setStringValue(""); //$NON-NLS-1$
if (!wesnothUserDirField_.isValid())
wesnothUserDirField_.setStringValue(""); //$NON-NLS-1$
if (!wesnothWorkingDirField_.isValid())
wesnothWorkingDirField_.setStringValue(""); //$NON-NLS-1$
if (!wmlToolsField_.isValid())
wmlToolsField_.setStringValue(""); //$NON-NLS-1$
}
@Override
protected void okPressed()
{
super.okPressed();
unsetInvalidProperties();
}
}

View file

@ -8,12 +8,19 @@
*******************************************************************************/
package org.wesnoth.preferences;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
@ -42,16 +49,39 @@ public class WesnothInstallsPage extends PreferencePage implements IWorkbenchPre
return null;
}
public String getColumnText(Object element, int columnIndex) {
return element.toString();
if (element instanceof WesnothInstall){
if (columnIndex == 0){ // name
return ((WesnothInstall)element).Name;
}else if (columnIndex == 1){ // version
return ((WesnothInstall)element).Version;
}else if ( columnIndex == 2 ) { // default or not
return ((WesnothInstall)element).Default == true ? "Default" : "";
}
}
return "";
}
}
private Button btnAdd_;
private Button btnEdit_;
private Button btnRemove_;
private Button btnSetAsDefault_;
public static class WesnothInstall
{
public String Name;
public String Version;
public boolean Default;
public WesnothInstall(String name, String version)
{
Name = name;
Version = version;
}
}
private List<WesnothInstall> _installs;
public WesnothInstallsPage()
{
_installs = new ArrayList<WesnothInstall>();
setPreferenceStore(WesnothPlugin.getDefault().getPreferenceStore());
setTitle("Wesnoth Installs Preferences");
}
@ -64,18 +94,25 @@ public class WesnothInstallsPage extends PreferencePage implements IWorkbenchPre
TableViewer tableViewer = new TableViewer(comp, SWT.BORDER | SWT.FULL_SELECTION);
Table table = tableViewer.getTable();
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseDoubleClick(MouseEvent e) {
editInstall();
}
});
table.setHeaderVisible(true);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
TableColumn tblclmnName = new TableColumn(table, SWT.NONE);
tblclmnName.setWidth(100);
tblclmnName.setText("Name");
tblclmnName.setText("Install Name");
TableColumn tblclmnWesnothVersion = new TableColumn(table, SWT.NONE);
tblclmnWesnothVersion.setWidth(100);
tblclmnWesnothVersion.setText("Wesnoth version");
tableViewer.setContentProvider(new ContentProvider());
tableViewer.setLabelProvider(new TableLabelProvider());
tableViewer.setInput(_installs);
Composite composite = new Composite(comp, SWT.NONE);
FillLayout fl_composite = new FillLayout(SWT.VERTICAL);
@ -86,23 +123,83 @@ public class WesnothInstallsPage extends PreferencePage implements IWorkbenchPre
gd_composite.widthHint = 23;
composite.setLayoutData(gd_composite);
btnAdd_ = new Button(composite, SWT.NONE);
btnAdd_.setText("Add");
Button btnAdd = new Button(composite, SWT.NONE);
btnAdd.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
addInstall();
}
});
btnAdd.setText("Add");
btnEdit_ = new Button(composite, SWT.NONE);
btnEdit_.setText("Edit");
Button btnEdit = new Button(composite, SWT.NONE);
btnEdit.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
editInstall();
}
});
btnEdit.setText("Edit");
btnRemove_ = new Button(composite, SWT.NONE);
btnRemove_.setText("Remove");
Button btnRemove = new Button(composite, SWT.NONE);
btnRemove.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
removeInstall();
}
});
btnRemove.setText("Remove");
btnSetAsDefault_ = new Button(composite, SWT.NONE);
btnSetAsDefault_.setText("Set as default");
Button btnSetAsDefault = new Button(composite, SWT.NONE);
btnSetAsDefault.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
setInstallAsDefault();
}
});
btnSetAsDefault.setText("Set as default");
return comp;
}
protected void removeInstall()
{
}
protected void addInstall()
{
WesnothInstallDialog dlg = new WesnothInstallDialog(getShell());
dlg.setTitle("Add a new Wesnoth Install");
dlg.open();
}
protected void editInstall()
{
WesnothInstallDialog dlg = new WesnothInstallDialog(getShell());
dlg.setTitle("Edit the Wesnoth Install");
dlg.open();
}
protected void setInstallAsDefault()
{
}
@Override
public void init(IWorkbench workbench)
{
// load the installs strings
// dummy entries
_installs.add(new WesnothInstall("name1", "1.9.0"));
_installs.add(new WesnothInstall("name2", "1.9.0+svn"));
_installs.add(new WesnothInstall("name3", "1.9.0x"));
}
@Override
public boolean performOk()
{
// pack settings
return true;
}
}

View file

@ -24,9 +24,9 @@ import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.xtext.ui.editor.preferences.fields.LabelFieldEditor;
import org.wesnoth.WesnothPlugin;
import org.wesnoth.Constants;
import org.wesnoth.Messages;
import org.wesnoth.WesnothPlugin;
import org.wesnoth.templates.ReplaceableParameter;
import org.wesnoth.templates.TemplateProvider;
import org.wesnoth.utils.StringUtils;
@ -66,12 +66,6 @@ public class WesnothPreferencesPage extends AbstractPreferencePage
wmlToolsList_.add("wesnoth_addon_manager"); //$NON-NLS-1$
}
/**
* Creates the field editors. Field editors are abstractions of
* the common GUI blocks needed to manipulate various types
* of preferences. Each field editor knows how to save and
* restore itself.
*/
@Override
public void createFieldEditors()
{