bffceac45eb803247e465a6b37cb38d5c0db42e3
[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.Composite;
10 import org.eclipse.swt.widgets.Control;
11 import org.eclipse.swt.widgets.Label;
12 import org.eclipse.ui.dialogs.PropertyPage;
13
14 import net.mograsim.machine.MachineDefinition;
15 import net.mograsim.machine.MachineRegistry;
16 import net.mograsim.plugin.nature.MachineContext;
17 import net.mograsim.plugin.nature.MachineContextSwtTools;
18 import net.mograsim.plugin.nature.MachineContextSwtTools.MachineCombo;
19 import net.mograsim.plugin.nature.ProjectMachineContext;
20
21 public class MograsimNaturePropertyPage extends PropertyPage
22 {
23         // TODO i10n
24         private static final String WARNING = "Changing the Mograsim machine can completely break your project. Be careful.";
25         private static final String MACHINE_LABEL = "Machine definition";
26         private static final String MACHINE_PROPERTY = "net.mograsim.projectMachineId";
27         private static final String DEFAULT_MACHINE = "Am2900Simple";// TODO don't hardcode that here!
28
29         private MachineCombo machineSelect;
30         private MachineDefinition defaultMachineDefinition;
31
32         private MachineContext machineContext;
33
34         /**
35          * Constructor for SamplePropertyPage.
36          */
37         public MograsimNaturePropertyPage()
38         {
39                 super();
40         }
41
42         private void addFirstSection(Composite parent)
43         {
44                 Composite composite = createDefaultComposite(parent);
45
46                 // Label for path field
47                 Label pathLabel = new Label(composite, SWT.NONE);
48                 pathLabel.setText(WARNING);
49         }
50
51         private void addSeparator(Composite parent)
52         {
53                 Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
54                 GridData gridData = new GridData();
55                 gridData.horizontalAlignment = GridData.FILL;
56                 gridData.grabExcessHorizontalSpace = true;
57                 separator.setLayoutData(gridData);
58         }
59
60         private void addSecondSection(Composite parent)
61         {
62                 Composite composite = createDefaultComposite(parent);
63
64                 // Label for machine
65                 Label ownerLabel = new Label(composite, SWT.NONE);
66                 ownerLabel.setText(MACHINE_LABEL);
67
68                 // Machine choice
69                 machineSelect = MachineContextSwtTools.createMachineSelector(composite, SWT.NONE);
70
71                 if (currentId.isPresent())
72                         machineSelect.add(currentId.get());
73
74                 Optional<MachineDefinition> currentMachineDefinition = machineContext.getMachineDefinition();
75
76                 if (currentMachineDefinition.isPresent())
77                         machineSelect.setSelection(currentMachineDefinition.get());
78
79                 defaultMachineDefinition = currentMachineDefinition.orElseGet(() -> MachineRegistry.getMachine(DEFAULT_MACHINE));
80         }
81
82         /**
83          * @see PreferencePage#createContents(Composite)
84          */
85         protected Control createContents(Composite parent)
86         {
87                 machineContext = ProjectMachineContext.getMachineContextOf(getElement());
88
89                 Composite composite = new Composite(parent, SWT.NONE);
90                 GridLayout layout = new GridLayout();
91                 composite.setLayout(layout);
92                 GridData data = new GridData(GridData.FILL);
93                 data.grabExcessHorizontalSpace = true;
94                 composite.setLayoutData(data);
95
96                 addFirstSection(composite);
97                 addSeparator(composite);
98                 addSecondSection(composite);
99                 return composite;
100         }
101
102         private Composite createDefaultComposite(Composite parent)
103         {
104                 Composite composite = new Composite(parent, SWT.NULL);
105                 GridLayout layout = new GridLayout();
106                 layout.numColumns = 2;
107                 composite.setLayout(layout);
108
109                 GridData data = new GridData();
110                 data.verticalAlignment = GridData.FILL;
111                 data.horizontalAlignment = GridData.FILL;
112                 composite.setLayoutData(data);
113
114                 return composite;
115         }
116
117         protected void performDefaults()
118         {
119                 super.performDefaults();
120                 // Populate the owner text field with the default value
121                 machineSelect.setSelection(defaultMachineDefinition);
122         }
123
124         public boolean performOk()
125         {
126                 return machineContext.setMachineId(machineSelect.getSelection().getId());
127         }
128
129 }