1 package net.mograsim.logic.model.editor.ui;
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;
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;
27 public class EditorGUI
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;
35 public EditorGUI(Editor editor)
38 display = Display.getDefault();
39 shell = new Shell(display);
41 GridLayout layout = new GridLayout();
42 layout.numColumns = 1;
43 shell.setLayout(layout);
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);
54 GridLayout innerLayout = new GridLayout();
55 innerComp.setLayout(innerLayout);
56 innerLayout.numColumns = 2;
58 GridData d = new GridData();
59 d.grabExcessHorizontalSpace = true;
60 d.horizontalAlignment = SWT.FILL;
61 d.grabExcessVerticalSpace = true;
62 d.verticalAlignment = SWT.FILL;
64 logicCanvas = new EditorCanvas(innerComp, SWT.TRAIL, editor);
65 logicCanvas.setLayoutData(d);
68 d.grabExcessVerticalSpace = true;
69 d.verticalAlignment = SWT.FILL;
71 addList = new List(innerComp, SWT.V_SCROLL);
72 addList.setLayoutData(d);
75 setupBottomToolBar(innerComp);
77 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(logicCanvas);
78 userInput.buttonDrag = 3;
79 userInput.buttonZoom = 2;
80 userInput.enableUserInput();
81 new ZoomableCanvasOverlay(logicCanvas, null).enableScale();
84 private ToolBar setupTopToolBar(Composite parent)
86 GridData d = new GridData();
87 d.grabExcessHorizontalSpace = true;
88 d.horizontalAlignment = SWT.FILL;
90 ToolBar toolBar = new ToolBar(parent, SWT.BORDER);
91 toolBar.setLayoutData(d);
93 ToolItem file = new ToolItem(toolBar, SWT.DROP_DOWN);
96 // DropDownEntry newEntry = new DropDownEntry("New", e -> {
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());
102 DropDownEntry[] entries = new DropDownEntry[] { loadEntry, saveEntry, saveAsEntry};
104 setupDrowpDownMenu(file, entries);
106 file.setText("File");
110 private ToolBar setupBottomToolBar(Composite parent)
112 GridData d = new GridData();
113 d.grabExcessHorizontalSpace = true;
114 d.horizontalAlignment = SWT.FILL;
116 ToolBar toolBar = new ToolBar(parent, SWT.BORDER);
117 toolBar.setLayoutData(d);
119 ToolItem snappingLabel = new ToolItem(toolBar, SWT.NONE);
120 snappingLabel.setText("Snapping:");
122 ToolItem snappSelect = new ToolItem(toolBar, SWT.DROP_DOWN);
123 DropDownEntry[] entries = new DropDownEntry[Editor.Snapping.values().length];
125 for (Editor.Snapping sn : Editor.Snapping.values())
127 entries[index++] = new DropDownEntry(sn.toString(), e ->
129 editor.setSnapping(sn);
130 snappSelect.setText(sn.toString());
133 snappSelect.setText(editor.getSnapping().toString());
134 setupDrowpDownMenu(snappSelect, entries);
136 new ToolItem(toolBar, SWT.SEPARATOR);
143 private void setupDrowpDownMenu(ToolItem parent, DropDownEntry[] entries)
145 Menu menu = new Menu(shell, SWT.POP_UP);
146 for (DropDownEntry entry : entries)
148 MenuItem item = new MenuItem(menu, SWT.PUSH);
149 item.addSelectionListener(new SelectionListener()
152 public void widgetSelected(SelectionEvent e)
154 entry.listener.widgetSelected(e);
158 public void widgetDefaultSelected(SelectionEvent e)
163 item.setText(entry.title);
166 parent.addListener(SWT.Selection, new Listener()
168 public void handleEvent(Event event)
170 if (event.detail == SWT.ARROW)
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);
182 private static class DropDownEntry
184 public final String title;
185 public final EntrySelectedListener listener;
187 public DropDownEntry(String title, EntrySelectedListener listener)
191 this.listener = listener;
195 private static interface EntrySelectedListener
197 public void widgetSelected(SelectionEvent e);
200 public void refreshAddList()
202 addList.setItems(IndirectGUIComponentCreator.getStandardComponentIDs().toArray(String[]::new));
206 public String getAddListSelected()
208 String[] selection = addList.getSelection();
209 if (selection.length == 0)
210 throw new IllegalStateException("Selection in the Add Component List may never be empty!");
217 while (!shell.isDisposed())
218 if (!display.readAndDispatch())