Java/SWT JFace Eclipse/Preferences

Материал из Java эксперт
Версия от 05:55, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Demonstrates PreferenceStore

import java.io.IOException;
import org.eclipse.jface.preference.PreferenceStore;
/**
 * This class demonstrates PreferenceStore
 */
public class PreferenceStoreTest {
  public static void main(String[] args) throws IOException {
    // Create the preference store
    PreferenceStore preferenceStore = new PreferenceStore("foo.properties");
    // Load it
    preferenceStore.load();
    // Set some defaults
    preferenceStore.setDefault("name1", true);
    preferenceStore.setDefault("name2", 42);
    preferenceStore.setDefault("name3", "Stack");
    // List the preferences
    preferenceStore.list(System.out);
  }
}
//foo.properties
/*
name1=false
name3=House

*/





JFace preferences

import java.io.IOException;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.preference.PreferenceManager;
import org.eclipse.jface.preference.PreferenceNode;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
/**
 * This class demonstrates JFace preferences
 */
public class ShowPrefs {
  /**
   * Runs the application
   */
  public void run() {
    Display display = new Display();
    // Create the preference manager
    PreferenceManager mgr = new PreferenceManager();
    // Create the nodes
    PreferenceNode one = new PreferenceNode("one", "One", ImageDescriptor
        .createFromFile(ShowPrefs.class, "jexp.gif"),
        PrefPageOne.class.getName());
    PreferenceNode two = new PreferenceNode("two", new PrefPageTwo());
    // Add the nodes
    mgr.addToRoot(one);
    mgr.addTo(one.getId(), two);
    // Create the preferences dialog
    PreferenceDialog dlg = new PreferenceDialog(null, mgr);
    // Set the preference store
    PreferenceStore ps = new PreferenceStore("showprefs.properties");
    try {
      ps.load();
    } catch (IOException e) {
      // Ignore
    }
    dlg.setPreferenceStore(ps);
    // Open the dialog
    dlg.open();
    try {
      // Save the preferences
      ps.save();
    } catch (IOException e) {
      e.printStackTrace();
    }
    display.dispose();
  }
  /**
   * The application entry point
   * 
   * @param args
   *            the command line arguments
   */
  public static void main(String[] args) {
    new ShowPrefs().run();
  }
}
/**
 * This class creates a preference page
 */
class PrefPageOne extends PreferencePage {
  // Names for preferences
  private static final String ONE = "one.one";
  private static final String TWO = "one.two";
  private static final String THREE = "one.three";
  // Text fields for user to enter preferences
  private Text fieldOne;
  private Text fieldTwo;
  private Text fieldThree;
  /**
   * Creates the controls for this page
   */
  protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(2, false));
    // Get the preference store
    IPreferenceStore preferenceStore = getPreferenceStore();
    // Create three text fields.
    // Set the text in each from the preference store
    new Label(composite, SWT.LEFT).setText("Field One:");
    fieldOne = new Text(composite, SWT.BORDER);
    fieldOne.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    fieldOne.setText(preferenceStore.getString(ONE));
    new Label(composite, SWT.LEFT).setText("Field Two:");
    fieldTwo = new Text(composite, SWT.BORDER);
    fieldTwo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    fieldTwo.setText(preferenceStore.getString(TWO));
    new Label(composite, SWT.LEFT).setText("Field Three:");
    fieldThree = new Text(composite, SWT.BORDER);
    fieldThree.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    fieldThree.setText(preferenceStore.getString(THREE));
    return composite;
  }
  /**
   * Called when user clicks Restore Defaults
   */
  protected void performDefaults() {
    // Get the preference store
    IPreferenceStore preferenceStore = getPreferenceStore();
    // Reset the fields to the defaults
    fieldOne.setText(preferenceStore.getDefaultString(ONE));
    fieldTwo.setText(preferenceStore.getDefaultString(TWO));
    fieldThree.setText(preferenceStore.getDefaultString(THREE));
  }
  /**
   * Called when user clicks Apply or OK
   * 
   * @return boolean
   */
  public boolean performOk() {
    // Get the preference store
    IPreferenceStore preferenceStore = getPreferenceStore();
    // Set the values from the fields
    if (fieldOne != null) preferenceStore.setValue(ONE, fieldOne.getText());
    if (fieldTwo != null) preferenceStore.setValue(TWO, fieldTwo.getText());
    if (fieldThree != null)
        preferenceStore.setValue(THREE, fieldThree.getText());
    // Return true to allow dialog to close
    return true;
  }
}

/**
 * This class creates a preference page
 */
class PrefPageTwo extends PreferencePage {
  // Names for preferences
  private static final String ONE = "two.one";
  private static final String TWO = "two.two";
  private static final String THREE = "two.three";
  // The checkboxes
  private Button checkOne;
  private Button checkTwo;
  private Button checkThree;
  /**
   * PrefPageTwo constructor
   */
  public PrefPageTwo() {
    super("Two");
    setDescription("Check the checks");
  }
  /**
   * Creates the controls for this page
   */
  protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new RowLayout(SWT.VERTICAL));
    // Get the preference store
    IPreferenceStore preferenceStore = getPreferenceStore();
    // Create three checkboxes
    checkOne = new Button(composite, SWT.CHECK);
    checkOne.setText("Check One");
    checkOne.setSelection(preferenceStore.getBoolean(ONE));
    checkTwo = new Button(composite, SWT.CHECK);
    checkTwo.setText("Check Two");
    checkTwo.setSelection(preferenceStore.getBoolean(TWO));
    checkThree = new Button(composite, SWT.CHECK);
    checkThree.setText("Check Three");
    checkThree.setSelection(preferenceStore.getBoolean(THREE));
    return composite;
  }
  /**
   * Add buttons
   * 
   * @param parent the parent composite
   */
  protected void contributeButtons(Composite parent) {
    // Add a select all button
    Button selectAll = new Button(parent, SWT.PUSH);
    selectAll.setText("Select All");
    selectAll.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        checkOne.setSelection(true);
        checkTwo.setSelection(true);
        checkThree.setSelection(true);
      }
    });
    // Add a select all button
    Button clearAll = new Button(parent, SWT.PUSH);
    clearAll.setText("Clear All");
    clearAll.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        checkOne.setSelection(false);
        checkTwo.setSelection(false);
        checkThree.setSelection(false);
      }
    });
    // Add two columns to the parent"s layout
    ((GridLayout) parent.getLayout()).numColumns += 2;
  }
  /**
   * Change the description label
   */
  protected Label createDescriptionLabel(Composite parent) {
    Label label = null;
    String description = getDescription();
    if (description != null) {
      // Upper case the description
      description = description.toUpperCase();
      // Right-align the label
      label = new Label(parent, SWT.RIGHT);
      label.setText(description);
    }
    return label;
  }
  /**
   * Called when user clicks Restore Defaults
   */
  protected void performDefaults() {
    // Get the preference store
    IPreferenceStore preferenceStore = getPreferenceStore();
    // Reset the fields to the defaults
    checkOne.setSelection(preferenceStore.getDefaultBoolean(ONE));
    checkTwo.setSelection(preferenceStore.getDefaultBoolean(TWO));
    checkThree.setSelection(preferenceStore.getDefaultBoolean(THREE));
  }
  /**
   * Called when user clicks Apply or OK
   * 
   * @return boolean
   */
  public boolean performOk() {
    // Get the preference store
    IPreferenceStore preferenceStore = getPreferenceStore();
    // Set the values from the fields
    if (checkOne != null) preferenceStore.setValue(ONE, checkOne.getSelection());
    if (checkTwo != null) preferenceStore.setValue(TWO, checkTwo.getSelection());
    if (checkThree != null)
        preferenceStore.setValue(THREE, checkThree.getSelection());
    // Return true to allow dialog to close
    return true;
  }
}





JFace preferences and field editors

import java.io.IOException;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.ColorFieldEditor;
import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.preference.FontFieldEditor;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.jface.preference.PathEditor;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.preference.PreferenceManager;
import org.eclipse.jface.preference.PreferenceNode;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.jface.preference.RadioGroupFieldEditor;
import org.eclipse.jface.preference.ScaleFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.widgets.Display;
/**
 * This class demonstrates JFace preferences and field editors
 */
public class ShowFieldPrefs {
  /**
   * Runs the application
   */
  public void run() {
    Display display = new Display();
    // Create the preference manager
    PreferenceManager mgr = new PreferenceManager();
    // Create the nodes
    PreferenceNode one = new PreferenceNode("one", "One", null,
        FieldEditorPageOne.class.getName());
    PreferenceNode two = new PreferenceNode("two", "Two", null,
        FieldEditorPageTwo.class.getName());
    // Add the nodes
    mgr.addToRoot(one);
    mgr.addToRoot(two);
    // Create the preferences dialog
    PreferenceDialog dlg = new PreferenceDialog(null, mgr);
    // Set the preference store
    PreferenceStore ps = new PreferenceStore("showfieldprefs.properties");
    try {
      ps.load();
    } catch (IOException e) {
      // Ignore
    }
    dlg.setPreferenceStore(ps);
    // Open the dialog
    dlg.open();
    try {
      // Save the preferences
      ps.save();
    } catch (IOException e) {
      e.printStackTrace();
    }
    display.dispose();
  }
  /**
   * The application entry point
   * 
   * @param args
   *            the command line arguments
   */
  public static void main(String[] args) {
    new ShowFieldPrefs().run();
  }
}
/**
 * This class demonstrates field editors
 */
class FieldEditorPageOne extends FieldEditorPreferencePage {
  public FieldEditorPageOne() {
    // Use the "flat" layout
    super(FLAT);
  }
  /**
   * Creates the field editors
   */
  protected void createFieldEditors() {
    // Add a boolean field
    BooleanFieldEditor bfe = new BooleanFieldEditor("myBoolean", "Boolean",
        getFieldEditorParent());
    addField(bfe);
    // Add a color field
    ColorFieldEditor cfe = new ColorFieldEditor("myColor", "Color:",
        getFieldEditorParent());
    addField(cfe);
    // Add a directory field
    DirectoryFieldEditor dfe = new DirectoryFieldEditor("myDirectory",
        "Directory:", getFieldEditorParent());
    addField(dfe);
    // Add a file field
    FileFieldEditor ffe = new FileFieldEditor("myFile", "File:",
        getFieldEditorParent());
    addField(ffe);
    // Add a font field
    FontFieldEditor fontFe = new FontFieldEditor("myFont", "Font:",
        getFieldEditorParent());
    addField(fontFe);
    // Add a radio group field
    RadioGroupFieldEditor rfe = new RadioGroupFieldEditor("myRadioGroup",
        "Radio Group", 2, new String[][] { { "First Value", "first" },
            { "Second Value", "second" },
            { "Third Value", "third" },
            { "Fourth Value", "fourth" } }, getFieldEditorParent(),
        true);
    addField(rfe);
    // Add a path field
    PathEditor pe = new PathEditor("myPath", "Path:", "Choose a Path",
        getFieldEditorParent());
    addField(pe);
  }
}
/**
 * This class demonstrates field editors
 */
class FieldEditorPageTwo extends FieldEditorPreferencePage {
  public FieldEditorPageTwo() {
    // Use the "grid" layout
    super(GRID);
  }
  /**
   * Creates the field editors
   */
  protected void createFieldEditors() {
    // Add an integer field
    IntegerFieldEditor ife = new IntegerFieldEditor("myInt", "Int:",
        getFieldEditorParent());
    addField(ife);
    // Add a scale field
    ScaleFieldEditor sfe = new ScaleFieldEditor("myScale", "Scale:",
        getFieldEditorParent(), 0, 100, 1, 10);
    addField(sfe);
    // Add a string field
    StringFieldEditor stringFe = new StringFieldEditor("myString",
        "String:", getFieldEditorParent());
    addField(stringFe);
  }
}

//showfieldprefs.properties
/*
#Sat Feb 28 16:06:57 GMT-05:00 2004
myPath=C\:\\Documents and Settings\\Owner\\My Documents;C\:\\;
myRadioGroup=
myScale=0
myColor=0,128,0
myFont=1|Terminal|8|0|WINDOWS|1|-13|0|0|0|400|0|0|0|-1|1|2|1|49|Terminal;
myFile=.\\0249f1701.bmp
myString=
myBoolean=true
myDirectory=C\:\\Documents and Settings\\Owner\\My Documents

*/