Merge branch 'development' of https://gitlab.lrz.de/lrr-tum/students/eragp-misim...
authorFabian Stemmler <stemmler@in.tum.de>
Mon, 15 Jul 2019 15:00:57 +0000 (17:00 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Mon, 15 Jul 2019 15:47:27 +0000 (17:47 +0200)
1  2 
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

@@@ -1,5 -1,6 +1,6 @@@
  package net.mograsim.logic.model.editor;
  
+ import java.io.IOException;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Map;
@@@ -134,11 -135,6 +135,11 @@@ public final class Edito
        {
                saveManager.save();
        }
 +      
 +      public void saveAs()
 +      {
 +              saveManager.openSaveAsDialog();
 +      }
  
        public void addComponent(double x, double y)
        {
                }
        }
        
-       public static void main(String[] args)
+       public static void main(String[] args) throws IOException
        {
                SaveLoadManager.openLoadDialog();
        }
@@@ -1,5 -1,5 +1,7 @@@
  package net.mograsim.logic.model.editor.ui;
  
++import java.io.IOException;
++
  import org.eclipse.swt.SWT;
  import org.eclipse.swt.events.SelectionEvent;
  import org.eclipse.swt.events.SelectionListener;
@@@ -7,7 -7,6 +9,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;
@@@ -21,7 -20,6 +23,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
                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;
                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(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 loadEntry = new DropDownEntry("Load", e -> {
++                      try
++                      {
++                              SaveLoadManager.openLoadDialog();
++                      } catch (IOException e1)
++                      {
++                              editor.dialogManager.openWarningDialog("Failed to load Component!", e1.getMessage());
++                      }
++              });
 +              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(shell, SWT.BORDER);
 +              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()