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