MainPreferencePage: Added fields for line widths
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / MainPreferencePage.java
1 package net.mograsim.plugin;
2
3 import static net.mograsim.logic.model.preferences.RenderPreferences.ACTION_BUTTON;
4 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_ONE_DASH;
5 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_U_DASH;
6 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_X_DASH;
7 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_ZERO_DASH;
8 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_Z_DASH;
9 import static net.mograsim.logic.model.preferences.RenderPreferences.DEBUG_HLSSHELL_DEPTH;
10 import static net.mograsim.logic.model.preferences.RenderPreferences.DEBUG_OPEN_HLSSHELL;
11 import static net.mograsim.logic.model.preferences.RenderPreferences.DEFAULT_LINE_WIDTH;
12 import static net.mograsim.logic.model.preferences.RenderPreferences.DRAG_BUTTON;
13 import static net.mograsim.logic.model.preferences.RenderPreferences.WIRE_WIDTH_MULTIBIT;
14 import static net.mograsim.logic.model.preferences.RenderPreferences.WIRE_WIDTH_SINGLEBIT;
15 import static net.mograsim.logic.model.preferences.RenderPreferences.ZOOM_BUTTON;
16 import static net.mograsim.plugin.preferences.PluginPreferences.MPM_EDITOR_BITS_AS_COLUMN_NAME;
17
18 import java.util.function.Consumer;
19
20 import org.eclipse.jface.preference.BooleanFieldEditor;
21 import org.eclipse.jface.preference.ComboFieldEditor;
22 import org.eclipse.jface.preference.FieldEditorPreferencePage;
23 import org.eclipse.jface.preference.IntegerFieldEditor;
24 import org.eclipse.jface.preference.StringFieldEditor;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Group;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.ui.IWorkbench;
32 import org.eclipse.ui.IWorkbenchPreferencePage;
33
34 import net.mograsim.logic.model.preferences.RenderPreferences;
35 import net.mograsim.plugin.preferences.PluginPreferences;
36 import net.mograsim.plugin.util.DoubleFieldEditor;
37 import net.mograsim.preferences.Preferences;
38
39 public class MainPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage
40 {
41         private static final String[][] MOUSE_BUTTONS = { { "left", "1" }, { "middle", "2" }, { "right", "3" }, { "4th", "4" },
42                         { "5th", "5" } };
43
44         public MainPreferencePage()
45         {
46                 super(GRID);
47         }
48
49         @Override
50         public void init(IWorkbench workbench)
51         {
52                 setPreferenceStore(MograsimActivator.instance().getPreferenceStore());
53         }
54
55         @Override
56         protected void createFieldEditors()
57         {
58                 Composite parent = getFieldEditorParent();
59                 addBoolean(DEBUG_OPEN_HLSSHELL, "Open the debug HLS shell", parent);
60                 addInt(DEBUG_HLSSHELL_DEPTH, "Depth of components to list in the debug HLS shell (0: unbounded)", parent);
61                 addIntCombo(ACTION_BUTTON, "Mouse button for actions", MOUSE_BUTTONS, parent);
62                 addIntCombo(DRAG_BUTTON, "Mouse button for dragging", MOUSE_BUTTONS, parent);
63                 addIntCombo(ZOOM_BUTTON, "Mouse button for zooming", MOUSE_BUTTONS, parent);
64                 addBoolean(MPM_EDITOR_BITS_AS_COLUMN_NAME, "Use the raw bit indices of MPM columns as column titles in the MPM editor", parent);
65
66                 insertSeparator(parent);
67                 addDouble(WIRE_WIDTH_SINGLEBIT, "Line width for single-bit wires", parent);
68                 addDouble(WIRE_WIDTH_MULTIBIT, "Line width for multi-bit wires", parent);
69                 addDouble(DEFAULT_LINE_WIDTH, "Line width for other wires", parent);
70                 addDashesGroup(parent);
71                 // TODO add other preferences
72         }
73
74         private void addDashesGroup(Composite parent)
75         {
76                 Composite dashesGroupParent = new Composite(parent, SWT.NONE);
77                 dashesGroupParent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
78                 GridLayout dashesGroupParentLayout = new GridLayout();
79                 dashesGroupParentLayout.marginWidth = 0;
80                 dashesGroupParentLayout.marginHeight = 0;
81                 dashesGroupParent.setLayout(dashesGroupParentLayout);
82
83                 Group dashesGroup = new Group(dashesGroupParent, SWT.NONE);
84                 dashesGroup.setText("Line dashes");
85                 dashesGroup.setLayout(new GridLayout());
86                 dashesGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
87
88                 Composite content = new Composite(dashesGroup, SWT.NONE);
89                 content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
90                 GridLayout contentLayout = new GridLayout(2, false);
91                 contentLayout.marginWidth = 0;
92                 contentLayout.marginHeight = 0;
93                 content.setLayout(contentLayout);
94
95                 Label dashesDescription = new Label(content, SWT.WRAP);
96                 dashesDescription.setText("Line dashes for single-bit wires displaying the respective value.\n"
97                                 + "Format: A comma-separated list of doubles. The first value is the width of the first segment,\n"
98                                 + "the second is the spacing between the first and second segments,\n"
99                                 + "the third is the width of the second segment and so on.");
100                 dashesDescription.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));
101                 addString(BIT_ZERO_DASH, "0", content);
102                 addString(BIT_Z_DASH, "Z", content);
103                 addString(BIT_X_DASH, "X", content);
104                 addString(BIT_U_DASH, "U", content);
105                 addString(BIT_ONE_DASH, "1", content);
106         }
107
108         private void addBoolean(String name, String label, Composite parent)
109         {
110                 defaultsPreferenceStore(name).getBoolean(name);
111                 addField(new BooleanFieldEditor(name, label, parent));
112         }
113
114         private void addInt(String name, String label, Composite parent)
115         {
116                 defaultsPreferenceStore(name).getInt(name);
117                 addField(new IntegerFieldEditor(name, label, parent));
118         }
119
120         private void addDouble(String name, String label, Composite parent)
121         {
122                 defaultsPreferenceStore(name).getDouble(name);
123                 addField(new DoubleFieldEditor(name, label, parent));
124         }
125
126         private void addIntCombo(String name, String label, String[][] entryNamesAndValues, Composite parent)
127         {
128                 addCombo(name, label, entryNamesAndValues, parent, defaultsPreferenceStore(name)::getInt);
129         }
130
131         private void addCombo(String name, String label, String[][] entryNamesAndValues, Composite parent, Consumer<String> initializeDefault)
132         {
133                 initializeDefault.accept(name);
134                 addField(new ComboFieldEditor(name, label, entryNamesAndValues, parent));
135         }
136
137         private void addString(String name, String label, Composite parent)
138         {
139                 defaultsPreferenceStore(name).getString(name);
140                 addField(new StringFieldEditor(name, label, parent));
141         }
142
143         private static void insertSeparator(Composite parent)
144         {
145                 new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
146         }
147
148         private static Preferences defaultsPreferenceStore(String name)
149         {
150                 // TODO is it a good idea to list all preference systems here?
151                 // Maybe rather split the page into one per preference system
152                 MograsimActivator mograsim = MograsimActivator.instance();
153                 if (name.startsWith(RenderPreferences.PREFIX))
154                         return mograsim.getRenderPrefs();
155                 else if (name.startsWith(PluginPreferences.PREFIX))
156                         return mograsim.getPluginPrefs();
157                 else
158                         throw new IllegalArgumentException("The preference system containing " + name + " is not known");
159         }
160 }