1 package net.mograsim.logic.model.editor.ui;
3 import java.io.IOException;
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;
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;
29 public class EditorGUI
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;
37 public EditorGUI(Editor editor)
40 display = Display.getDefault();
41 shell = new Shell(display);
43 GridLayout layout = new GridLayout();
44 layout.numColumns = 1;
45 shell.setLayout(layout);
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);
56 GridLayout innerLayout = new GridLayout();
57 innerComp.setLayout(innerLayout);
58 innerLayout.numColumns = 2;
60 GridData d = new GridData();
61 d.grabExcessHorizontalSpace = true;
62 d.horizontalAlignment = SWT.FILL;
63 d.grabExcessVerticalSpace = true;
64 d.verticalAlignment = SWT.FILL;
66 logicCanvas = new EditorCanvas(innerComp, SWT.TRAIL, editor);
67 logicCanvas.setLayoutData(d);
70 d.grabExcessVerticalSpace = true;
71 d.verticalAlignment = SWT.FILL;
73 addList = new List(innerComp, SWT.V_SCROLL);
74 addList.setLayoutData(d);
77 setupBottomToolBar(innerComp);
79 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(logicCanvas);
80 userInput.buttonDrag = 3;
81 userInput.buttonZoom = 2;
82 userInput.enableUserInput();
83 new ZoomableCanvasOverlay(logicCanvas, null).enableScale();
86 private ToolBar setupTopToolBar(Composite parent)
88 GridData d = new GridData();
89 d.grabExcessHorizontalSpace = true;
90 d.horizontalAlignment = SWT.FILL;
92 ToolBar toolBar = new ToolBar(parent, SWT.BORDER);
93 toolBar.setLayoutData(d);
95 ToolItem file = new ToolItem(toolBar, SWT.DROP_DOWN);
98 // DropDownEntry newEntry = new DropDownEntry("New", e -> {
100 DropDownEntry loadEntry = new DropDownEntry("Load", e ->
104 SaveLoadManager.openLoadDialog();
106 catch (IOException e1)
108 editor.dialogManager.openWarningDialog("Failed to load Component!", e1.getMessage());
111 DropDownEntry saveEntry = new DropDownEntry("Save", e -> editor.save());
112 DropDownEntry saveAsEntry = new DropDownEntry("Save as...", e -> editor.saveAs());
114 DropDownEntry[] entries = new DropDownEntry[] { loadEntry, saveEntry, saveAsEntry };
116 setupDrowpDownMenu(file, entries);
118 file.setText("File");
122 private ToolBar setupBottomToolBar(Composite parent)
124 GridData d = new GridData();
125 d.grabExcessHorizontalSpace = true;
126 d.horizontalAlignment = SWT.FILL;
128 ToolBar toolBar = new ToolBar(parent, SWT.BORDER);
129 toolBar.setLayoutData(d);
131 ToolItem snappingLabel = new ToolItem(toolBar, SWT.NONE);
132 snappingLabel.setText("Snapping:");
134 ToolItem snappSelect = new ToolItem(toolBar, SWT.DROP_DOWN);
135 DropDownEntry[] entries = new DropDownEntry[Editor.Snapping.values().length];
137 for (Editor.Snapping sn : Editor.Snapping.values())
139 entries[index++] = new DropDownEntry(sn.toString(), e ->
141 editor.setSnapping(sn);
142 snappSelect.setText(sn.toString());
145 snappSelect.setText(editor.getSnapping().toString());
146 setupDrowpDownMenu(snappSelect, entries);
148 new ToolItem(toolBar, SWT.SEPARATOR);
155 private void setupDrowpDownMenu(ToolItem parent, DropDownEntry[] entries)
157 Menu menu = new Menu(shell, SWT.POP_UP);
158 for (DropDownEntry entry : entries)
160 MenuItem item = new MenuItem(menu, SWT.PUSH);
161 item.addSelectionListener(new SelectionListener()
164 public void widgetSelected(SelectionEvent e)
166 entry.listener.widgetSelected(e);
170 public void widgetDefaultSelected(SelectionEvent e)
175 item.setText(entry.title);
178 parent.addListener(SWT.Selection, new Listener()
180 public void handleEvent(Event event)
182 if (event.detail == SWT.ARROW)
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);
194 private static class DropDownEntry
196 public final String title;
197 public final EntrySelectedListener listener;
199 public DropDownEntry(String title, EntrySelectedListener listener)
203 this.listener = listener;
207 private static interface EntrySelectedListener
209 public void widgetSelected(SelectionEvent e);
212 public void refreshAddList()
214 addList.setItems(IndirectGUIComponentCreator.getStandardComponentIDs().toArray(String[]::new));
218 public String getAddListSelected()
220 String[] selection = addList.getSelection();
221 if (selection.length == 0)
222 throw new IllegalStateException("Selection in the Add Component List may never be empty!");
229 while (!shell.isDisposed())
230 if (!display.readAndDispatch())