Added file menu to Editor
authorFabian Stemmler <stemmler@in.tum.de>
Mon, 15 Jul 2019 15:00:31 +0000 (17:00 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Mon, 15 Jul 2019 15:00:31 +0000 (17:00 +0200)
net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/Editor.java
net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/EditorGUI.java

index 37efbae..e213d1f 100644 (file)
@@ -134,6 +134,11 @@ public final class Editor
        {
                saveManager.save();
        }
+       
+       public void saveAs()
+       {
+               saveManager.openSaveAsDialog();
+       }
 
        public void addComponent(double x, double y)
        {
index 2827f24..06f9f4b 100644 (file)
@@ -7,6 +7,7 @@ import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.graphics.Rectangle;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Event;
 import org.eclipse.swt.widgets.List;
@@ -20,6 +21,7 @@ import org.eclipse.swt.widgets.ToolItem;
 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasOverlay;
 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
 import net.mograsim.logic.model.editor.Editor;
+import net.mograsim.logic.model.editor.SaveLoadManager;
 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
 
 public class EditorGUI
@@ -36,29 +38,41 @@ public class EditorGUI
                display = Display.getDefault();
                shell = new Shell(display);
 
-               // Layout
                GridLayout layout = new GridLayout();
+               layout.numColumns = 1;
                shell.setLayout(layout);
-               layout.numColumns = 2;
 
-               GridData d = new GridData();
-               d.grabExcessVerticalSpace = true;
-               d.verticalAlignment = SWT.FILL;
-               d.verticalSpan = 2;
-               addList = new List(shell, SWT.V_SCROLL);
-               addList.setLayoutData(d);
-               refreshAddList();
+               setupTopToolBar(shell);
+               Composite innerComp = new Composite(shell, SWT.NONE);
+               GridData innerCompData = new GridData();
+               innerCompData.grabExcessHorizontalSpace = true;
+               innerCompData.grabExcessVerticalSpace = true;
+               innerCompData.horizontalAlignment = SWT.FILL;
+               innerCompData.verticalAlignment = SWT.FILL;
+               innerComp.setLayoutData(innerCompData);
+               
+               GridLayout innerLayout = new GridLayout();
+               innerComp.setLayout(innerLayout);
+               innerLayout.numColumns = 2;
 
-               d = new GridData();
+               GridData d = new GridData();
                d.grabExcessHorizontalSpace = true;
                d.horizontalAlignment = SWT.FILL;
                d.grabExcessVerticalSpace = true;
                d.verticalAlignment = SWT.FILL;
 
-               logicCanvas = new EditorCanvas(shell, SWT.TRAIL, editor);
+               logicCanvas = new EditorCanvas(innerComp, SWT.TRAIL, editor);
                logicCanvas.setLayoutData(d);
-
-               setupToolBar();
+               
+               d = new GridData();
+               d.grabExcessVerticalSpace = true;
+               d.verticalAlignment = SWT.FILL;
+               d.verticalSpan = 2;
+               addList = new List(innerComp, SWT.V_SCROLL);
+               addList.setLayoutData(d);
+               refreshAddList();
+               
+               setupBottomToolBar(innerComp);
 
                ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(logicCanvas);
                userInput.buttonDrag = 3;
@@ -67,59 +81,120 @@ public class EditorGUI
                new ZoomableCanvasOverlay(logicCanvas, null).enableScale();
        }
 
-       private ToolBar setupToolBar()
+       private ToolBar setupTopToolBar(Composite parent)
        {
                GridData d = new GridData();
                d.grabExcessHorizontalSpace = true;
                d.horizontalAlignment = SWT.FILL;
 
-               ToolBar toolBar = new ToolBar(shell, SWT.BORDER);
+               ToolBar toolBar = new ToolBar(parent, SWT.BORDER);
+               toolBar.setLayoutData(d);
+
+               ToolItem file = new ToolItem(toolBar, SWT.DROP_DOWN);
+
+               //TODO
+//             DropDownEntry newEntry = new DropDownEntry("New", e -> {
+//             });
+               DropDownEntry loadEntry = new DropDownEntry("Load", e -> SaveLoadManager.openLoadDialog());
+               DropDownEntry saveEntry = new DropDownEntry("Save", e -> editor.save());
+               DropDownEntry saveAsEntry = new DropDownEntry("Save as...", e -> editor.saveAs());
+               
+               DropDownEntry[] entries = new DropDownEntry[] { loadEntry, saveEntry, saveAsEntry};
+               
+               setupDrowpDownMenu(file, entries);
+               
+               file.setText("File");
+               return toolBar;
+       }
+
+       private ToolBar setupBottomToolBar(Composite parent)
+       {
+               GridData d = new GridData();
+               d.grabExcessHorizontalSpace = true;
+               d.horizontalAlignment = SWT.FILL;
+
+               ToolBar toolBar = new ToolBar(parent, SWT.BORDER);
                toolBar.setLayoutData(d);
 
                ToolItem snappingLabel = new ToolItem(toolBar, SWT.NONE);
                snappingLabel.setText("Snapping:");
-               
-               Menu menu = new Menu(shell, SWT.POP_UP);
+
                ToolItem snappSelect = new ToolItem(toolBar, SWT.DROP_DOWN);
+               DropDownEntry[] entries = new DropDownEntry[Editor.Snapping.values().length];
+               int index = 0;
                for (Editor.Snapping sn : Editor.Snapping.values())
+               {
+                       entries[index++] = new DropDownEntry(sn.toString(), e ->
+                       {
+                               editor.setSnapping(sn);
+                               snappSelect.setText(sn.toString());
+                       });
+               }
+               snappSelect.setText(editor.getSnapping().toString());
+               setupDrowpDownMenu(snappSelect, entries);
+
+               new ToolItem(toolBar, SWT.SEPARATOR);
+
+               toolBar.pack();
+
+               return toolBar;
+       }
+
+       private void setupDrowpDownMenu(ToolItem parent, DropDownEntry[] entries)
+       {
+               Menu menu = new Menu(shell, SWT.POP_UP);
+               for (DropDownEntry entry : entries)
                {
                        MenuItem item = new MenuItem(menu, SWT.PUSH);
                        item.addSelectionListener(new SelectionListener()
                        {
                                @Override
-                               public void widgetSelected(SelectionEvent arg0)
+                               public void widgetSelected(SelectionEvent e)
                                {
-                                       editor.setSnapping(sn);
-                                       snappSelect.setText(sn.toString());
+                                       entry.listener.widgetSelected(e);
                                }
-                               
+
                                @Override
-                               public void widgetDefaultSelected(SelectionEvent arg0) {}
+                               public void widgetDefaultSelected(SelectionEvent e)
+                               {
+                                       widgetSelected(e);
+                               }
                        });
-                       item.setText(sn.toString());
+                       item.setText(entry.title);
                }
-               
-               snappSelect.setText(editor.getSnapping().toString());
-               snappSelect.addListener(SWT.Selection, new Listener()
+
+               parent.addListener(SWT.Selection, new Listener()
                {
                        public void handleEvent(Event event)
                        {
                                if (event.detail == SWT.ARROW)
                                {
-                                       Rectangle rect = snappSelect.getBounds();
+                                       Rectangle rect = parent.getBounds();
                                        Point pt = new Point(rect.x, rect.y + rect.height);
-                                       pt = toolBar.toDisplay(pt);
+                                       pt = parent.getParent().toDisplay(pt);
                                        menu.setLocation(pt.x, pt.y);
                                        menu.setVisible(true);
                                }
                        }
                });
-               
-               new ToolItem(toolBar, SWT.SEPARATOR);
-               
-               toolBar.pack();
+       }
 
-               return toolBar;
+       private static class DropDownEntry
+       {
+               public final String title;
+               public final EntrySelectedListener listener;
+
+               public DropDownEntry(String title, EntrySelectedListener listener)
+               {
+                       super();
+                       this.title = title;
+                       this.listener = listener;
+               }
+       }
+
+       private static interface EntrySelectedListener
+       {
+               public void widgetSelected(SelectionEvent e);
        }
 
        public void refreshAddList()