Editor's add list is now sorted alphabetically
[Mograsim.git] / net.mograsim.logic.model.editor / src / net / mograsim / logic / model / editor / ui / EditorGUI.java
1 package net.mograsim.logic.model.editor.ui;
2
3 import java.io.IOException;
4
5 import org.eclipse.swt.SWT;
6 import org.eclipse.swt.events.SelectionEvent;
7 import org.eclipse.swt.events.SelectionListener;
8 import org.eclipse.swt.graphics.Point;
9 import org.eclipse.swt.graphics.Rectangle;
10 import org.eclipse.swt.layout.GridData;
11 import org.eclipse.swt.layout.GridLayout;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.swt.widgets.Display;
14 import org.eclipse.swt.widgets.Event;
15 import org.eclipse.swt.widgets.List;
16 import org.eclipse.swt.widgets.Listener;
17 import org.eclipse.swt.widgets.Menu;
18 import org.eclipse.swt.widgets.MenuItem;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.swt.widgets.ToolBar;
21 import org.eclipse.swt.widgets.ToolItem;
22
23 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;
24 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
25 import net.mograsim.logic.model.editor.Editor;
26 import net.mograsim.logic.model.editor.SaveLoadManager;
27 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
28
29 public class EditorGUI
30 {
31         public final Display display;
32         public final Shell shell;
33         public final EditorCanvas logicCanvas;
34         private final List addList;
35         private final Editor editor;
36
37         public EditorGUI(Editor editor)
38         {
39                 this.editor = editor;
40                 display = Display.getDefault();
41                 shell = new Shell(display);
42
43                 GridLayout layout = new GridLayout();
44                 layout.numColumns = 1;
45                 shell.setLayout(layout);
46
47                 setupTopToolBar(shell);
48                 Composite innerComp = new Composite(shell, SWT.NONE);
49                 GridData innerCompData = new GridData();
50                 innerCompData.grabExcessHorizontalSpace = true;
51                 innerCompData.grabExcessVerticalSpace = true;
52                 innerCompData.horizontalAlignment = SWT.FILL;
53                 innerCompData.verticalAlignment = SWT.FILL;
54                 innerComp.setLayoutData(innerCompData);
55
56                 GridLayout innerLayout = new GridLayout();
57                 innerComp.setLayout(innerLayout);
58                 innerLayout.numColumns = 2;
59
60                 GridData d = new GridData();
61                 d.grabExcessHorizontalSpace = true;
62                 d.horizontalAlignment = SWT.FILL;
63                 d.grabExcessVerticalSpace = true;
64                 d.verticalAlignment = SWT.FILL;
65
66                 logicCanvas = new EditorCanvas(innerComp, SWT.TRAIL, editor);
67                 logicCanvas.setLayoutData(d);
68
69                 d = new GridData();
70                 d.grabExcessVerticalSpace = true;
71                 d.verticalAlignment = SWT.FILL;
72                 d.verticalSpan = 2;
73                 addList = new List(innerComp, SWT.V_SCROLL);
74                 addList.setLayoutData(d);
75                 refreshAddList();
76
77                 setupBottomToolBar(innerComp);
78
79                 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(logicCanvas);
80                 userInput.buttonDrag = 3;
81                 userInput.buttonZoom = 2;
82                 userInput.enableUserInput();
83                 new ZoomableCanvasOverlay(logicCanvas, null).enableScale();
84         }
85
86         private ToolBar setupTopToolBar(Composite parent)
87         {
88                 GridData d = new GridData();
89                 d.grabExcessHorizontalSpace = true;
90                 d.horizontalAlignment = SWT.FILL;
91
92                 ToolBar toolBar = new ToolBar(parent, SWT.BORDER);
93                 toolBar.setLayoutData(d);
94
95                 ToolItem file = new ToolItem(toolBar, SWT.DROP_DOWN);
96
97                 // TODO
98 //              DropDownEntry newEntry = new DropDownEntry("New", e -> {
99 //              });
100                 DropDownEntry loadEntry = new DropDownEntry("Load", e ->
101                 {
102                         try
103                         {
104                                 SaveLoadManager.openLoadDialog();
105                         }
106                         catch (IOException e1)
107                         {
108                                 editor.dialogManager.openWarningDialog("Failed to load Component!", e1.getMessage());
109                         }
110                 });
111                 DropDownEntry saveEntry = new DropDownEntry("Save", e -> editor.save());
112                 DropDownEntry saveAsEntry = new DropDownEntry("Save as...", e -> editor.saveAs());
113
114                 DropDownEntry[] entries = new DropDownEntry[] { loadEntry, saveEntry, saveAsEntry };
115
116                 setupDrowpDownMenu(file, entries);
117
118                 file.setText("File");
119                 return toolBar;
120         }
121
122         private ToolBar setupBottomToolBar(Composite parent)
123         {
124                 GridData d = new GridData();
125                 d.grabExcessHorizontalSpace = true;
126                 d.horizontalAlignment = SWT.FILL;
127
128                 ToolBar toolBar = new ToolBar(parent, SWT.BORDER);
129                 toolBar.setLayoutData(d);
130
131                 ToolItem snappingLabel = new ToolItem(toolBar, SWT.NONE);
132                 snappingLabel.setText("Snapping:");
133
134                 ToolItem snappSelect = new ToolItem(toolBar, SWT.DROP_DOWN);
135                 DropDownEntry[] entries = new DropDownEntry[Editor.Snapping.values().length];
136                 int index = 0;
137                 for (Editor.Snapping sn : Editor.Snapping.values())
138                 {
139                         entries[index++] = new DropDownEntry(sn.toString(), e ->
140                         {
141                                 editor.setSnapping(sn);
142                                 snappSelect.setText(sn.toString());
143                         });
144                 }
145                 snappSelect.setText(editor.getSnapping().toString());
146                 setupDrowpDownMenu(snappSelect, entries);
147
148                 new ToolItem(toolBar, SWT.SEPARATOR);
149
150                 toolBar.pack();
151
152                 return toolBar;
153         }
154
155         private void setupDrowpDownMenu(ToolItem parent, DropDownEntry[] entries)
156         {
157                 Menu menu = new Menu(shell, SWT.POP_UP);
158                 for (DropDownEntry entry : entries)
159                 {
160                         MenuItem item = new MenuItem(menu, SWT.PUSH);
161                         item.addSelectionListener(new SelectionListener()
162                         {
163                                 @Override
164                                 public void widgetSelected(SelectionEvent e)
165                                 {
166                                         entry.listener.widgetSelected(e);
167                                 }
168
169                                 @Override
170                                 public void widgetDefaultSelected(SelectionEvent e)
171                                 {
172                                         widgetSelected(e);
173                                 }
174                         });
175                         item.setText(entry.title);
176                 }
177
178                 parent.addListener(SWT.Selection, new Listener()
179                 {
180                         public void handleEvent(Event event)
181                         {
182                                 if (event.detail == SWT.ARROW)
183                                 {
184                                         Rectangle rect = parent.getBounds();
185                                         Point pt = new Point(rect.x, rect.y + rect.height);
186                                         pt = parent.getParent().toDisplay(pt);
187                                         menu.setLocation(pt.x, pt.y);
188                                         menu.setVisible(true);
189                                 }
190                         }
191                 });
192         }
193
194         private static class DropDownEntry
195         {
196                 public final String title;
197                 public final EntrySelectedListener listener;
198
199                 public DropDownEntry(String title, EntrySelectedListener listener)
200                 {
201                         super();
202                         this.title = title;
203                         this.listener = listener;
204                 }
205         }
206
207         private static interface EntrySelectedListener
208         {
209                 public void widgetSelected(SelectionEvent e);
210         }
211
212         public void refreshAddList()
213         {
214                 addList.setItems(IndirectGUIComponentCreator.getStandardComponentIDs().stream().sorted().toArray(String[]::new));
215                 addList.select(0);
216         }
217
218         public String getAddListSelected()
219         {
220                 String[] selection = addList.getSelection();
221                 if (selection.length == 0)
222                         throw new IllegalStateException("Selection in the Add Component List may never be empty!");
223                 return selection[0];
224         }
225
226         public void open()
227         {
228                 shell.open();
229                 while (!shell.isDisposed())
230                         if (!display.readAndDispatch())
231                                 display.sleep();
232         }
233
234 }