Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[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.IndirectModelComponentCreator;
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 -> Editor.openNewEditor());
99                 DropDownEntry loadEntry = new DropDownEntry("Load", e ->
100                 {
101                         try
102                         {
103                                 SaveLoadManager.openLoadDialog();
104                         }
105                         catch (IOException e1)
106                         {
107                                 editor.dialogManager.openWarningDialog("Failed to load Component!", e1.getMessage());
108                         }
109                 });
110                 DropDownEntry saveEntry = new DropDownEntry("Save", e -> editor.save());
111                 DropDownEntry saveAsEntry = new DropDownEntry("Save as...", e -> editor.saveAs());
112
113                 DropDownEntry[] entries = new DropDownEntry[] { newEntry, loadEntry, saveEntry, saveAsEntry };
114
115                 setupDrowpDownMenu(file, entries);
116
117                 file.setText("File");
118                 return toolBar;
119         }
120
121         private ToolBar setupBottomToolBar(Composite parent)
122         {
123                 GridData d = new GridData();
124                 d.grabExcessHorizontalSpace = true;
125                 d.horizontalAlignment = SWT.FILL;
126
127                 ToolBar toolBar = new ToolBar(parent, SWT.BORDER);
128                 toolBar.setLayoutData(d);
129
130                 ToolItem snappingLabel = new ToolItem(toolBar, SWT.NONE);
131                 snappingLabel.setText("Snapping:");
132
133                 ToolItem snappSelect = new ToolItem(toolBar, SWT.DROP_DOWN);
134                 DropDownEntry[] entries = new DropDownEntry[Editor.Snapping.values().length];
135                 int index = 0;
136                 for (Editor.Snapping sn : Editor.Snapping.values())
137                 {
138                         entries[index++] = new DropDownEntry(sn.toString(), e ->
139                         {
140                                 editor.setSnapping(sn);
141                                 snappSelect.setText(sn.toString());
142                         });
143                 }
144                 snappSelect.setText(editor.getSnapping().toString());
145                 setupDrowpDownMenu(snappSelect, entries);
146
147                 new ToolItem(toolBar, SWT.SEPARATOR);
148
149                 toolBar.pack();
150
151                 return toolBar;
152         }
153
154         private void setupDrowpDownMenu(ToolItem parent, DropDownEntry[] entries)
155         {
156                 Menu menu = new Menu(shell, SWT.POP_UP);
157                 for (DropDownEntry entry : entries)
158                 {
159                         MenuItem item = new MenuItem(menu, SWT.PUSH);
160                         item.addSelectionListener(new SelectionListener()
161                         {
162                                 @Override
163                                 public void widgetSelected(SelectionEvent e)
164                                 {
165                                         entry.listener.widgetSelected(e);
166                                 }
167
168                                 @Override
169                                 public void widgetDefaultSelected(SelectionEvent e)
170                                 {
171                                         widgetSelected(e);
172                                 }
173                         });
174                         item.setText(entry.title);
175                 }
176
177                 parent.addListener(SWT.Selection, new Listener()
178                 {
179                         public void handleEvent(Event event)
180                         {
181                                 if (event.detail == SWT.ARROW)
182                                 {
183                                         Rectangle rect = parent.getBounds();
184                                         Point pt = new Point(rect.x, rect.y + rect.height);
185                                         pt = parent.getParent().toDisplay(pt);
186                                         menu.setLocation(pt.x, pt.y);
187                                         menu.setVisible(true);
188                                 }
189                         }
190                 });
191         }
192
193         private static class DropDownEntry
194         {
195                 public final String title;
196                 public final EntrySelectedListener listener;
197
198                 public DropDownEntry(String title, EntrySelectedListener listener)
199                 {
200                         super();
201                         this.title = title;
202                         this.listener = listener;
203                 }
204         }
205
206         private static interface EntrySelectedListener
207         {
208                 public void widgetSelected(SelectionEvent e);
209         }
210
211         public void refreshAddList()
212         {
213                 addList.setItems(IndirectModelComponentCreator.getStandardComponentIDs().keySet().stream().sorted().toArray(String[]::new));
214                 addList.select(0);
215         }
216
217         public String getAddListSelected()
218         {
219                 String[] selection = addList.getSelection();
220                 if (selection.length == 0)
221                         throw new IllegalStateException("Selection in the Add Component List may never be empty!");
222                 return selection[0];
223         }
224
225         public void open()
226         {
227                 shell.open();
228                 while (!shell.isDisposed())
229                         if (!display.readAndDispatch())
230                                 display.sleep();
231         }
232 }