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.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;
15 import net.mograsim.machine.MachineRegistry;
16 import net.mograsim.plugin.nature.MachineContext;
17 import net.mograsim.plugin.nature.ProjectMachineContext;
19 public class MograsimNaturePropertyPage extends PropertyPage
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";
27 private Combo machineSelect;
28 private String defaultId;
30 private MachineContext machineContext;
33 * Constructor for SamplePropertyPage.
35 public MograsimNaturePropertyPage()
40 private void addFirstSection(Composite parent)
42 Composite composite = createDefaultComposite(parent);
44 // Label for path field
45 Label pathLabel = new Label(composite, SWT.NONE);
46 pathLabel.setText(WARNING);
49 private void addSeparator(Composite parent)
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);
58 private void addSecondSection(Composite parent)
60 Composite composite = createDefaultComposite(parent);
63 Label ownerLabel = new Label(composite, SWT.NONE);
64 ownerLabel.setText(MACHINE_LABEL);
67 machineSelect = new Combo(parent, SWT.BORDER);
68 GridData gd = new GridData();
69 machineSelect.setLayoutData(gd);
71 Optional<String> currentId = machineContext.getMachineId();
73 if (currentId.isPresent())
74 machineSelect.add(currentId.get());
76 for (String machineId : MachineRegistry.getInstalledMachines().keySet())
78 if (currentId.isPresent() && currentId.get().equals(machineId))
80 machineSelect.add(machineId);
83 defaultId = currentId.orElse(DEFAULT_MACHINE);
85 machineSelect.select(machineSelect.indexOf(defaultId));
89 * @see PreferencePage#createContents(Composite)
91 protected Control createContents(Composite parent)
93 machineContext = ProjectMachineContext.getMachineContextOf(getElement());
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);
102 addFirstSection(composite);
103 addSeparator(composite);
104 addSecondSection(composite);
108 private Composite createDefaultComposite(Composite parent)
110 Composite composite = new Composite(parent, SWT.NULL);
111 GridLayout layout = new GridLayout();
112 layout.numColumns = 2;
113 composite.setLayout(layout);
115 GridData data = new GridData();
116 data.verticalAlignment = GridData.FILL;
117 data.horizontalAlignment = GridData.FILL;
118 composite.setLayoutData(data);
123 protected void performDefaults()
125 super.performDefaults();
126 // Populate the owner text field with the default value
127 machineSelect.select(machineSelect.indexOf(DEFAULT_MACHINE));
130 public boolean performOk()
132 int selected = machineSelect.getSelectionIndex();
133 String newId = machineSelect.getItem(selected);
134 return machineContext.setMachineId(newId);