MainPreferencePage: Added fields for line widths
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Mon, 18 May 2020 16:42:58 +0000 (18:42 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Mon, 18 May 2020 16:45:55 +0000 (18:45 +0200)
Fixes #3

plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/MainPreferencePage.java
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/util/DoubleFieldEditor.java [new file with mode: 0644]

index 6616b40..ce2ccd0 100644 (file)
@@ -8,7 +8,10 @@ import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_ZERO_DA
 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_Z_DASH;
 import static net.mograsim.logic.model.preferences.RenderPreferences.DEBUG_HLSSHELL_DEPTH;
 import static net.mograsim.logic.model.preferences.RenderPreferences.DEBUG_OPEN_HLSSHELL;
+import static net.mograsim.logic.model.preferences.RenderPreferences.DEFAULT_LINE_WIDTH;
 import static net.mograsim.logic.model.preferences.RenderPreferences.DRAG_BUTTON;
+import static net.mograsim.logic.model.preferences.RenderPreferences.WIRE_WIDTH_MULTIBIT;
+import static net.mograsim.logic.model.preferences.RenderPreferences.WIRE_WIDTH_SINGLEBIT;
 import static net.mograsim.logic.model.preferences.RenderPreferences.ZOOM_BUTTON;
 import static net.mograsim.plugin.preferences.PluginPreferences.MPM_EDITOR_BITS_AS_COLUMN_NAME;
 
@@ -30,6 +33,7 @@ import org.eclipse.ui.IWorkbenchPreferencePage;
 
 import net.mograsim.logic.model.preferences.RenderPreferences;
 import net.mograsim.plugin.preferences.PluginPreferences;
+import net.mograsim.plugin.util.DoubleFieldEditor;
 import net.mograsim.preferences.Preferences;
 
 public class MainPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage
@@ -58,6 +62,11 @@ public class MainPreferencePage extends FieldEditorPreferencePage implements IWo
                addIntCombo(DRAG_BUTTON, "Mouse button for dragging", MOUSE_BUTTONS, parent);
                addIntCombo(ZOOM_BUTTON, "Mouse button for zooming", MOUSE_BUTTONS, parent);
                addBoolean(MPM_EDITOR_BITS_AS_COLUMN_NAME, "Use the raw bit indices of MPM columns as column titles in the MPM editor", parent);
+
+               insertSeparator(parent);
+               addDouble(WIRE_WIDTH_SINGLEBIT, "Line width for single-bit wires", parent);
+               addDouble(WIRE_WIDTH_MULTIBIT, "Line width for multi-bit wires", parent);
+               addDouble(DEFAULT_LINE_WIDTH, "Line width for other wires", parent);
                addDashesGroup(parent);
                // TODO add other preferences
        }
@@ -108,6 +117,12 @@ public class MainPreferencePage extends FieldEditorPreferencePage implements IWo
                addField(new IntegerFieldEditor(name, label, parent));
        }
 
+       private void addDouble(String name, String label, Composite parent)
+       {
+               defaultsPreferenceStore(name).getDouble(name);
+               addField(new DoubleFieldEditor(name, label, parent));
+       }
+
        private void addIntCombo(String name, String label, String[][] entryNamesAndValues, Composite parent)
        {
                addCombo(name, label, entryNamesAndValues, parent, defaultsPreferenceStore(name)::getInt);
@@ -125,6 +140,11 @@ public class MainPreferencePage extends FieldEditorPreferencePage implements IWo
                addField(new StringFieldEditor(name, label, parent));
        }
 
+       private static void insertSeparator(Composite parent)
+       {
+               new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
+       }
+
        private static Preferences defaultsPreferenceStore(String name)
        {
                // TODO is it a good idea to list all preference systems here?
diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/util/DoubleFieldEditor.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/util/DoubleFieldEditor.java
new file mode 100644 (file)
index 0000000..acf9927
--- /dev/null
@@ -0,0 +1,148 @@
+package net.mograsim.plugin.util;
+
+import org.eclipse.jface.preference.IntegerFieldEditor;
+import org.eclipse.jface.preference.StringFieldEditor;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Text;
+
+/**
+ * A field editor for an double type preference.<br>
+ * Adapted from {@link IntegerFieldEditor}.
+ */
+public class DoubleFieldEditor extends StringFieldEditor
+{
+       private double minValidValue = 0;
+
+       private double maxValidValue = Double.MAX_VALUE;
+
+       private static final int DEFAULT_TEXT_LIMIT = 10;
+
+       /**
+        * Creates a new double field editor
+        */
+       protected DoubleFieldEditor()
+       {
+       }
+
+       /**
+        * Creates an double field editor.
+        *
+        * @param name      the name of the preference this field editor works on
+        * @param labelText the label text of the field editor
+        * @param parent    the parent of the field editor's control
+        */
+       public DoubleFieldEditor(String name, String labelText, Composite parent)
+       {
+               this(name, labelText, parent, DEFAULT_TEXT_LIMIT);
+       }
+
+       /**
+        * Creates an double field editor.
+        *
+        * @param name      the name of the preference this field editor works on
+        * @param labelText the label text of the field editor
+        * @param parent    the parent of the field editor's control
+        * @param textLimit the maximum number of characters in the text.
+        */
+       public DoubleFieldEditor(String name, String labelText, Composite parent, int textLimit)
+       {
+               init(name, labelText);
+               setTextLimit(textLimit);
+               setEmptyStringAllowed(false);
+               setErrorMessage("Value must be a double");
+               createControl(parent);
+       }
+
+       /**
+        * Sets the range of valid values for this field.
+        *
+        * @param min the minimum allowed value (inclusive)
+        * @param max the maximum allowed value (inclusive)
+        */
+       public void setValidRange(int min, int max)
+       {
+               minValidValue = min;
+               maxValidValue = max;
+               setErrorMessage("Value must be a double between " + min + " and " + max);
+       }
+
+       @Override
+       protected boolean checkState()
+       {
+               Text text = getTextControl();
+
+               if (text == null)
+               {
+                       return false;
+               }
+
+               String numberString = text.getText();
+               try
+               {
+                       double number = Double.parseDouble(numberString);
+                       if (number >= minValidValue && number <= maxValidValue)
+                       {
+                               clearErrorMessage();
+                               return true;
+                       }
+
+                       showErrorMessage();
+                       return false;
+
+               }
+               catch (@SuppressWarnings("unused") NumberFormatException e1)
+               {
+                       showErrorMessage();
+               }
+
+               return false;
+       }
+
+       @Override
+       protected void doLoad()
+       {
+               Text text = getTextControl();
+               if (text != null)
+               {
+                       double value = getPreferenceStore().getDouble(getPreferenceName());
+                       String valueAsString = String.valueOf(value);
+                       text.setText(valueAsString);
+                       oldValue = valueAsString;
+               }
+
+       }
+
+       @Override
+       protected void doLoadDefault()
+       {
+               Text text = getTextControl();
+               if (text != null)
+               {
+                       double value = getPreferenceStore().getDefaultDouble(getPreferenceName());
+                       text.setText(String.valueOf(value));
+               }
+               valueChanged();
+       }
+
+       @Override
+       protected void doStore()
+       {
+               Text text = getTextControl();
+               if (text != null)
+               {
+                       Double d = Double.valueOf(text.getText());
+                       getPreferenceStore().setValue(getPreferenceName(), d.intValue());
+               }
+       }
+
+       /**
+        * Returns this field editor's current value as a double.
+        *
+        * @return the value
+        * @exception NumberFormatException if the <code>String</code> does not contain a parsable double
+        */
+       public double getDoubleValue() throws NumberFormatException
+       {
+               return Double.valueOf(getStringValue()).doubleValue();
+       }
+}