Fixed a typo
[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 groupComposite = new Composite(parent, SWT.LEFT);
68 //              GridDataFactory.fillDefaults().grab(true, false).applyTo(groupComposite);
69 //              GridLayoutFactory.fillDefaults().numColumns(2).applyTo(groupComposite);
70 //              GridLayout gl = ((GridLayout) groupComposite.getLayout());
71 //              gl.horizontalSpacing = 0;
72 //
73 //              Group grpWindowTitle = new Group(groupComposite, SWT.NONE);
74 //              grpWindowTitle.setText("Line dashes");
75 //              grpWindowTitle.setLayout(new GridLayout(1, false));
76 //              grpWindowTitle.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
77 //
78 //              // show workspace name
79 //              Composite content = new Composite(grpWindowTitle, SWT.NONE);
80 //              GridDataFactory.defaultsFor(content).indent(0, 0).grab(true, false).applyTo(content);
81 //              GridLayout locationNameLayout = new GridLayout(2, false);
82 //              content.setLayout(locationNameLayout);
83 //              locationNameLayout.marginWidth = locationNameLayout.marginHeight = 0;
84
85                 Composite dashesGroupParent = new Composite(parent, SWT.NONE);
86                 dashesGroupParent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
87                 GridLayout dashesGroupParentLayout = new GridLayout();
88                 dashesGroupParentLayout.marginWidth = 0;
89                 dashesGroupParentLayout.marginHeight = 0;
90                 dashesGroupParent.setLayout(dashesGroupParentLayout);
91
92                 Group dashesGroup = new Group(dashesGroupParent, SWT.NONE);
93                 dashesGroup.setText("Line dashes");
94                 dashesGroup.setLayout(new GridLayout());
95                 dashesGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
96
97                 Composite content = new Composite(dashesGroup, SWT.NONE);
98                 content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
99                 GridLayout contentLayout = new GridLayout(2, false);
100                 contentLayout.marginWidth = 0;
101                 contentLayout.marginHeight = 0;
102                 content.setLayout(contentLayout);
103
104                 Label dashesDescription = new Label(content, SWT.WRAP);
105                 dashesDescription.setText("Line dashes for single-bit wires displaying the respective value.\n"
106                                 + "Format: A comma-separated list of doubles. The first value is the width of the first segment,\n"
107                                 + "the second is the spacing between the first and second segments,\n"
108                                 + "the third is the width of the second segment and so on.");
109                 dashesDescription.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));
110                 addString(BIT_ZERO_DASH, "0", content);
111                 addString(BIT_Z_DASH, "Z", content);
112                 addString(BIT_X_DASH, "X", content);
113                 addString(BIT_U_DASH, "U", content);
114                 addString(BIT_ONE_DASH, "1", content);
115         }
116
117         private void addBoolean(String name, String label, Composite parent)
118         {
119                 defaultsPreferenceStore(name).getBoolean(name);
120                 addField(new BooleanFieldEditor(name, label, parent));
121         }
122
123         private void addInt(String name, String label, Composite parent)
124         {
125                 defaultsPreferenceStore(name).getInt(name);
126                 addField(new IntegerFieldEditor(name, label, parent));
127         }
128
129         private void addIntCombo(String name, String label, String[][] entryNamesAndValues, Composite parent)
130         {
131                 addCombo(name, label, entryNamesAndValues, parent, defaultsPreferenceStore(name)::getInt);
132         }
133
134         private void addCombo(String name, String label, String[][] entryNamesAndValues, Composite parent, Consumer<String> initializeDefault)
135         {
136                 initializeDefault.accept(name);
137                 addField(new ComboFieldEditor(name, label, entryNamesAndValues, parent));
138         }
139
140         private void addString(String name, String label, Composite parent)
141         {
142                 defaultsPreferenceStore(name).getString(name);
143                 addField(new StringFieldEditor(name, label, parent));
144         }
145
146         private static Preferences defaultsPreferenceStore(String name)
147         {
148                 // TODO is it a good idea to list all preference systems here?
149                 // Maybe rather split the page into one per preference system
150                 MograsimActivator mograsim = MograsimActivator.instance();
151                 if (name.startsWith(RenderPreferences.PREFIX))
152                         return mograsim.getRenderPrefs();
153                 else if (name.startsWith(PluginPreferences.PREFIX))
154                         return mograsim.getPluginPrefs();
155                 else
156                         throw new IllegalArgumentException("The preference system containing " + name + " is not known");
157         }
158 }