1 package net.mograsim.plugin.nature.properties;
3 import java.util.Optional;
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;
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;
21 public class MograsimNaturePropertyPage extends PropertyPage
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!
29 private MachineCombo machineSelect;
30 private MachineDefinition defaultMachineDefinition;
32 private MachineContext machineContext;
35 * Constructor for SamplePropertyPage.
37 public MograsimNaturePropertyPage()
42 private void addFirstSection(Composite parent)
44 Composite composite = createDefaultComposite(parent);
46 // Label for path field
47 Label pathLabel = new Label(composite, SWT.NONE);
48 pathLabel.setText(WARNING);
51 private void addSeparator(Composite parent)
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);
60 private void addSecondSection(Composite parent)
62 Composite composite = createDefaultComposite(parent);
65 Label ownerLabel = new Label(composite, SWT.NONE);
66 ownerLabel.setText(MACHINE_LABEL);
69 machineSelect = MachineContextSwtTools.createMachineSelector(composite, SWT.NONE);
71 if (currentId.isPresent())
72 machineSelect.add(currentId.get());
74 Optional<MachineDefinition> currentMachineDefinition = machineContext.getMachineDefinition();
76 if (currentMachineDefinition.isPresent())
77 machineSelect.setSelection(currentMachineDefinition.get());
79 defaultMachineDefinition = currentMachineDefinition.orElseGet(() -> MachineRegistry.getMachine(DEFAULT_MACHINE));
83 * @see PreferencePage#createContents(Composite)
85 protected Control createContents(Composite parent)
87 machineContext = ProjectMachineContext.getMachineContextOf(getElement());
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);
96 addFirstSection(composite);
97 addSeparator(composite);
98 addSecondSection(composite);
102 private Composite createDefaultComposite(Composite parent)
104 Composite composite = new Composite(parent, SWT.NULL);
105 GridLayout layout = new GridLayout();
106 layout.numColumns = 2;
107 composite.setLayout(layout);
109 GridData data = new GridData();
110 data.verticalAlignment = GridData.FILL;
111 data.horizontalAlignment = GridData.FILL;
112 composite.setLayoutData(data);
117 protected void performDefaults()
119 super.performDefaults();
120 // Populate the owner text field with the default value
121 machineSelect.setSelection(defaultMachineDefinition);
124 public boolean performOk()
126 return machineContext.setMachineId(machineSelect.getSelection().getId());