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