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 Label machineDescription;
31 private MachineDefinition defaultMachineDefinition;
33 private MachineContext machineContext;
36 * Constructor for SamplePropertyPage.
38 public MograsimNaturePropertyPage()
43 private void addFirstSection(Composite parent)
45 Composite composite = createDefaultComposite(parent, false);
47 // Label for path field
48 Label pathLabel = new Label(composite, SWT.NONE);
49 pathLabel.setText(WARNING);
52 private void addSeparator(Composite parent)
54 Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
55 GridData gridData = new GridData();
56 gridData.horizontalAlignment = GridData.FILL;
57 gridData.grabExcessHorizontalSpace = true;
58 separator.setLayoutData(gridData);
61 private void addSecondSection(Composite parent)
63 Composite composite = createDefaultComposite(parent, true);
66 Label ownerLabel = new Label(composite, SWT.NONE);
67 ownerLabel.setText(MACHINE_LABEL);
70 machineSelect = MachineContextSwtTools.createMachineSelector(composite, SWT.NONE);
72 machineDescription = new Label(composite, SWT.NONE);
73 machineSelect.addListener(md -> machineDescription.setText(md.getDescription()));
74 machineDescription.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
76 Optional<MachineDefinition> currentMachineDefinition = machineContext.getMachineDefinition();
78 if (currentMachineDefinition.isPresent())
79 machineSelect.setSelection(currentMachineDefinition.get());
81 defaultMachineDefinition = currentMachineDefinition.orElseGet(() -> MachineRegistry.getMachine(DEFAULT_MACHINE));
85 * @see PreferencePage#createContents(Composite)
87 protected Control createContents(Composite parent)
89 machineContext = ProjectMachineContext.getMachineContextOf(getElement());
91 Composite composite = new Composite(parent, SWT.NONE);
92 GridLayout layout = new GridLayout();
93 composite.setLayout(layout);
94 GridData data = new GridData(GridData.FILL);
95 data.grabExcessHorizontalSpace = true;
96 composite.setLayoutData(data);
98 addFirstSection(composite);
99 addSeparator(composite);
100 addSecondSection(composite);
104 private Composite createDefaultComposite(Composite parent, boolean grabExcessVerticalSpace)
106 Composite composite = new Composite(parent, SWT.NULL);
107 GridLayout layout = new GridLayout();
108 layout.numColumns = 2;
109 composite.setLayout(layout);
111 GridData data = new GridData();
112 data.verticalAlignment = GridData.FILL;
113 data.horizontalAlignment = GridData.FILL;
114 data.grabExcessHorizontalSpace = true;
115 data.grabExcessVerticalSpace = grabExcessVerticalSpace;
116 composite.setLayoutData(data);
121 protected void performDefaults()
123 super.performDefaults();
124 // Populate the owner text field with the default value
125 machineSelect.setSelection(defaultMachineDefinition);
128 public boolean performOk()
130 return machineContext.setMachineId(machineSelect.getSelection().getId());