io.github.albertus82.jface.preference.IPreference Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jface-utils Show documentation
Show all versions of jface-utils Show documentation
Java SWT/JFace Utility Library including a Preferences Framework, Lightweight HTTP Server and macOS support.
package io.github.albertus82.jface.preference;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.swt.widgets.Composite;
import io.github.albertus82.jface.preference.page.IPageDefinition;
/**
*
* This is a simple example of enum that implements
* {@link io.github.albertus82.jface.preference.IPreference IPreference}:
*
*
*
* public enum MyPreference implements IPreference {
*
* STRING(new PreferenceDetailsBuilder(MyPageDefinition.TEXT).name("string").label("String").defaultValue("Hello World!").build(), new FieldEditorDetailsBuilder(DefaultStringFieldEditor.class).build()),
* WRAP_STRING(new PreferenceDetailsBuilder(MyPageDefinition.TEXT).name("wrapString").label("Wrap String").defaultValue("Long text here.").build(), new FieldEditorDetailsBuilder(WrapStringFieldEditor.class).build()),
*
* INTEGER(new PreferenceDetailsBuilder(MyPageDefinition.TEXT_NUMERIC).name("integer").label("Integer").defaultValue(12345).build(), new FieldEditorDetailsBuilder(EnhancedIntegerFieldEditor.class).emptyStringAllowed(true).numberMinimum(-67890).build()),
* DOUBLE(new PreferenceDetailsBuilder(MyPageDefinition.TEXT_NUMERIC).name("double").label("Double").defaultValue(24680.13579).build(), new FieldEditorDetailsBuilder(DoubleFieldEditor.class).emptyStringAllowed(true).build()),
*
* COMBO(new PreferenceDetailsBuilder(MyPageDefinition.COMBO).name("combo").label("Combo").defaultValue("value 1").build(), new FieldEditorDetailsBuilder(DefaultComboFieldEditor.class).labelsAndValues(new StaticLabelsAndValues("Label 1", "value 1").put("Label 2", "value 2")).build()),
* VALIDATED_COMBO(new PreferenceDetailsBuilder(MyPageDefinition.COMBO).name("validatedCombo").label("Validated Combo").defaultValue("value 5").build(), new FieldEditorDetailsBuilder(ValidatedComboFieldEditor.class).labelsAndValues(new StaticLabelsAndValues("Label 5", "value 5")).emptyStringAllowed(false).build()),
*
* FLOAT_COMBO(new PreferenceDetailsBuilder(MyPageDefinition.COMBO_NUMERIC).separate().name("floatCombo").label("Float Combo").defaultValue(123.456f).build(), new FieldEditorDetailsBuilder(FloatComboFieldEditor.class).labelsAndValues(new StaticLabelsAndValues("float", 1)).emptyStringAllowed(true).numberValidRange(-10000, 20000).build()),
* BIGDECIMAL_COMBO(new PreferenceDetailsBuilder(MyPageDefinition.COMBO_NUMERIC).name("bigDecimalCombo").label("BigDecimal Combo").defaultValue(67890.12345).build(), new FieldEditorDetailsBuilder(BigDecimalComboFieldEditor.class).labelsAndValues(new StaticLabelsAndValues("BigDecimal Value", -10.5).put("invalid", 1000000)).emptyStringAllowed(false).numberValidRange(-1000, 100000).textLimit(20).build()),
*
* BOOLEAN(new PreferenceDetailsBuilder(MyPageDefinition.VARIOUS).name("boolean").label("Boolean").defaultValue(false).build(), new FieldEditorDetailsBuilder(DefaultBooleanFieldEditor.class).build()),
* COLOR(new PreferenceDetailsBuilder(MyPageDefinition.VARIOUS).name("color").label("Color").defaultValue("255,0,0").build(), new FieldEditorDetailsBuilder(ColorFieldEditor.class).build()),
* PASSWORD(new PreferenceDetailsBuilder(MyPageDefinition.VARIOUS).name("password").label("Password").build(), new FieldEditorDetailsBuilder(PasswordFieldEditor.class).build()),
* DATE(new PreferenceDetailsBuilder(MyPageDefinition.VARIOUS).name("date").label("Date").defaultValue("24/12/2015").build(), new FieldEditorDetailsBuilder(DateFieldEditor.class).datePattern("dd/MM/yyyy").dateFrom(new GregorianCalendar(2010, Calendar.JANUARY, 1).getTime()).style(SWT.DROP_DOWN).build()),
*
* EMAIL(new PreferenceDetailsBuilder(MyPageDefinition.PAGE).name("emails").label("Emails").build(), new FieldEditorDetailsBuilder(EmailAddressesListEditor.class).build()),
* URI(new PreferenceDetailsBuilder(MyPageDefinition.PAGE).name("uris").label("URIs").build(), new FieldEditorDetailsBuilder(UriListEditor.class).build());
*
* private static final FieldEditorFactory fieldEditorFactory = new FieldEditorFactory();
*
* private PreferenceDetails preferenceDetails;
* private FieldEditorDetails fieldEditorDetails;
*
* MyPreference(PreferenceDetails preferenceDetails, FieldEditorDetails fieldEditorDetails) {
* this.preferenceDetails = preferenceDetails;
* this.fieldEditorDetails = fieldEditorDetails;
* }
*
* @Override
* public String getName() {
* return preferenceDetails.getName();
* }
*
* @Override
* public String getLabel() {
* return preferenceDetails.getLabel().get();
* }
*
* @Override
* public IPageDefinition getPageDefinition() {
* return preferenceDetails.getPageDefinition();
* }
*
* @Override
* public String getDefaultValue() {
* return preferenceDetails.getDefaultValue();
* }
*
* @Override
* public IPreference getParent() {
* return preferenceDetails.getParent();
* }
*
* @Override
* public boolean isRestartRequired() {
* return preferenceDetails.isRestartRequired();
* }
*
* @Override
* public boolean isSeparate() {
* return preferenceDetails.isSeparate();
* }
*
* @Override
* public IPreference[] getChildren() {
* Set<MyPreference> preferences = EnumSet.noneOf(MyPreference.class);
* for (MyPreference item : MyPreference.values()) {
* if (this.equals(item.getParent())) {
* preferences.add(item);
* }
* }
* return preferences.toArray(new IPreference[] {});
* }
*
* @Override
* public FieldEditor createFieldEditor(Composite parent) {
* return fieldEditorFactory.createFieldEditor(getName(), getLabel(), parent, fieldEditorDetails);
* }
* }
*
*
* You can surely improve this code, for example introducing localization and
* autodetermining name values using the enum names. This example makes
* use of {@link io.github.albertus82.jface.preference.PreferenceDetails
* PreferenceDetails} and
* {@link io.github.albertus82.jface.preference.FieldEditorDetails
* FieldEditorDetails} helper classes and their respective builders.
*
*/
public interface IPreference {
FieldEditor createFieldEditor(Composite parent);
IPreference[] getChildren();
String getDefaultValue();
String getLabel();
String getName();
IPageDefinition getPageDefinition();
IPreference getParent();
boolean isRestartRequired();
boolean isSeparate();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy