c51aa01a76c3c5712b5ca6801b33af0faa2e25f4
[Mograsim.git] /
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 Label machineDescription;
31         private MachineDefinition defaultMachineDefinition;
32
33         private MachineContext machineContext;
34
35         /**
36          * Constructor for SamplePropertyPage.
37          */
38         public MograsimNaturePropertyPage()
39         {
40                 super();
41         }
42
43         private void addFirstSection(Composite parent)
44         {
45                 Composite composite = createDefaultComposite(parent, false);
46
47                 // Label for path field
48                 Label pathLabel = new Label(composite, SWT.NONE);
49                 pathLabel.setText(WARNING);
50         }
51
52         private void addSeparator(Composite parent)
53         {
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);
59         }
60
61         private void addSecondSection(Composite parent)
62         {
63                 Composite composite = createDefaultComposite(parent, true);
64
65                 // Label for machine
66                 Label ownerLabel = new Label(composite, SWT.NONE);
67                 ownerLabel.setText(MACHINE_LABEL);
68
69                 // Machine choice
70                 machineSelect = MachineContextSwtTools.createMachineSelector(composite, SWT.NONE);
71
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));
75
76                 Optional<MachineDefinition> currentMachineDefinition = machineContext.getMachineDefinition();
77
78                 if (currentMachineDefinition.isPresent())
79                         machineSelect.setSelection(currentMachineDefinition.get());
80
81                 defaultMachineDefinition = currentMachineDefinition.orElseGet(() -> MachineRegistry.getMachine(DEFAULT_MACHINE));
82         }
83
84         /**
85          * @see PreferencePage#createContents(Composite)
86          */
87         protected Control createContents(Composite parent)
88         {
89                 machineContext = ProjectMachineContext.getMachineContextOf(getElement());
90
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);
97
98                 addFirstSection(composite);
99                 addSeparator(composite);
100                 addSecondSection(composite);
101                 return composite;
102         }
103
104         private Composite createDefaultComposite(Composite parent, boolean grabExcessVerticalSpace)
105         {
106                 Composite composite = new Composite(parent, SWT.NULL);
107                 GridLayout layout = new GridLayout();
108                 layout.numColumns = 2;
109                 composite.setLayout(layout);
110
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);
117
118                 return composite;
119         }
120
121         protected void performDefaults()
122         {
123                 super.performDefaults();
124                 // Populate the owner text field with the default value
125                 machineSelect.setSelection(defaultMachineDefinition);
126         }
127
128         public boolean performOk()
129         {
130                 return machineContext.setMachineId(machineSelect.getSelection().getId());
131         }
132
133 }