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 -> Editor.openNewEditor());
99 DropDownEntry loadEntry = new DropDownEntry("Load", e ->
103 SaveLoadManager.openLoadDialog();
105 catch (IOException e1)
107 editor.dialogManager.openWarningDialog("Failed to load Component!", e1.getMessage());
110 DropDownEntry saveEntry = new DropDownEntry("Save", e -> editor.save());
111 DropDownEntry saveAsEntry = new DropDownEntry("Save as...", e -> editor.saveAs());
113 DropDownEntry[] entries = new DropDownEntry[] { newEntry, loadEntry, saveEntry, saveAsEntry };
115 setupDrowpDownMenu(file, entries);
117 file.setText("File");
121 private ToolBar setupBottomToolBar(Composite parent)
123 GridData d = new GridData();
124 d.grabExcessHorizontalSpace = true;
125 d.horizontalAlignment = SWT.FILL;
127 ToolBar toolBar = new ToolBar(parent, SWT.BORDER);
128 toolBar.setLayoutData(d);
130 ToolItem snappingLabel = new ToolItem(toolBar, SWT.NONE);
131 snappingLabel.setText("Snapping:");
133 ToolItem snappSelect = new ToolItem(toolBar, SWT.DROP_DOWN);
134 DropDownEntry[] entries = new DropDownEntry[Editor.Snapping.values().length];
136 for (Editor.Snapping sn : Editor.Snapping.values())
138 entries[index++] = new DropDownEntry(sn.toString(), e ->
140 editor.setSnapping(sn);
141 snappSelect.setText(sn.toString());
144 snappSelect.setText(editor.getSnapping().toString());
145 setupDrowpDownMenu(snappSelect, entries);
147 new ToolItem(toolBar, SWT.SEPARATOR);
154 private void setupDrowpDownMenu(ToolItem parent, DropDownEntry[] entries)
156 Menu menu = new Menu(shell, SWT.POP_UP);
157 for (DropDownEntry entry : entries)
159 MenuItem item = new MenuItem(menu, SWT.PUSH);
160 item.addSelectionListener(new SelectionListener()
163 public void widgetSelected(SelectionEvent e)
165 entry.listener.widgetSelected(e);
169 public void widgetDefaultSelected(SelectionEvent e)
174 item.setText(entry.title);
177 parent.addListener(SWT.Selection, new Listener()
179 public void handleEvent(Event event)
181 if (event.detail == SWT.ARROW)
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);
193 private static class DropDownEntry
195 public final String title;
196 public final EntrySelectedListener listener;
198 public DropDownEntry(String title, EntrySelectedListener listener)
202 this.listener = listener;
206 private static interface EntrySelectedListener
208 public void widgetSelected(SelectionEvent e);
211 public void refreshAddList()
213 addList.setItems(IndirectGUIComponentCreator.getStandardComponentIDs().keySet().stream().sorted().toArray(String[]::new));
217 public String getAddListSelected()
219 String[] selection = addList.getSelection();
220 if (selection.length == 0)
221 throw new IllegalStateException("Selection in the Add Component List may never be empty!");
228 while (!shell.isDisposed())
229 if (!display.readAndDispatch())