Restructured Mograsim project nature and introduced project context
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / nature / properties / MograsimNaturePropertyPage.java
1 package net.mograsim.plugin.nature.properties;
2
3 import java.util.Optional;
4
5 import org.eclipse.jface.preference.PreferencePage;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.layout.GridData;
8 import org.eclipse.swt.layout.GridLayout;
9 import org.eclipse.swt.widgets.Combo;
10 import org.eclipse.swt.widgets.Composite;
11 import org.eclipse.swt.widgets.Control;
12 import org.eclipse.swt.widgets.Label;
13 import org.eclipse.ui.dialogs.PropertyPage;
14
15 import net.mograsim.machine.MachineRegistry;
16 import net.mograsim.plugin.nature.MachineContext;
17 import net.mograsim.plugin.nature.ProjectMachineContext;
18
19 public class MograsimNaturePropertyPage extends PropertyPage
20 {
21
22         private static final String WARNING = "Changing the Mograsim machine can completely break your project. Be careful.";
23         private static final String MACHINE_LABEL = "Machine ID";
24         private static final String MACHINE_PROPERTY = "net.mograsim.projectMachineId";
25         private static final String DEFAULT_MACHINE = "Am2900";
26
27         private Combo machineSelect;
28         private String defaultId;
29
30         private MachineContext machineContext;
31
32         /**
33          * Constructor for SamplePropertyPage.
34          */
35         public MograsimNaturePropertyPage()
36         {
37                 super();
38         }
39
40         private void addFirstSection(Composite parent)
41         {
42                 Composite composite = createDefaultComposite(parent);
43
44                 // Label for path field
45                 Label pathLabel = new Label(composite, SWT.NONE);
46                 pathLabel.setText(WARNING);
47         }
48
49         private void addSeparator(Composite parent)
50         {
51                 Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
52                 GridData gridData = new GridData();
53                 gridData.horizontalAlignment = GridData.FILL;
54                 gridData.grabExcessHorizontalSpace = true;
55                 separator.setLayoutData(gridData);
56         }
57
58         private void addSecondSection(Composite parent)
59         {
60                 Composite composite = createDefaultComposite(parent);
61
62                 // Label for machine
63                 Label ownerLabel = new Label(composite, SWT.NONE);
64                 ownerLabel.setText(MACHINE_LABEL);
65
66                 // Machine choice
67                 machineSelect = new Combo(parent, SWT.BORDER);
68                 GridData gd = new GridData();
69                 machineSelect.setLayoutData(gd);
70
71                 Optional<String> currentId = machineContext.getMachineId();
72
73                 if (currentId.isPresent())
74                         machineSelect.add(currentId.get());
75
76                 for (String machineId : MachineRegistry.getInstalledMachines().keySet())
77                 {
78                         if (currentId.isPresent() && currentId.get().equals(machineId))
79                                 continue;
80                         machineSelect.add(machineId);
81                 }
82
83                 defaultId = currentId.orElse(DEFAULT_MACHINE);
84
85                 machineSelect.select(machineSelect.indexOf(defaultId));
86         }
87
88         /**
89          * @see PreferencePage#createContents(Composite)
90          */
91         protected Control createContents(Composite parent)
92         {
93                 machineContext = ProjectMachineContext.getMachineContextOf(getElement());
94
95                 Composite composite = new Composite(parent, SWT.NONE);
96                 GridLayout layout = new GridLayout();
97                 composite.setLayout(layout);
98                 GridData data = new GridData(GridData.FILL);
99                 data.grabExcessHorizontalSpace = true;
100                 composite.setLayoutData(data);
101
102                 addFirstSection(composite);
103                 addSeparator(composite);
104                 addSecondSection(composite);
105                 return composite;
106         }
107
108         private Composite createDefaultComposite(Composite parent)
109         {
110                 Composite composite = new Composite(parent, SWT.NULL);
111                 GridLayout layout = new GridLayout();
112                 layout.numColumns = 2;
113                 composite.setLayout(layout);
114
115                 GridData data = new GridData();
116                 data.verticalAlignment = GridData.FILL;
117                 data.horizontalAlignment = GridData.FILL;
118                 composite.setLayoutData(data);
119
120                 return composite;
121         }
122
123         protected void performDefaults()
124         {
125                 super.performDefaults();
126                 // Populate the owner text field with the default value
127                 machineSelect.select(machineSelect.indexOf(DEFAULT_MACHINE));
128         }
129
130         public boolean performOk()
131         {
132                 int selected = machineSelect.getSelectionIndex();
133                 String newId = machineSelect.getItem(selected);
134                 return machineContext.setMachineId(newId);
135         }
136
137 }