Cleaned up Editor: removed warnings; cleaned listener system
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Wed, 18 Sep 2019 09:46:23 +0000 (11:46 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Wed, 18 Sep 2019 09:46:23 +0000 (11:46 +0200)
plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/Editor.java
plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/EditorUserInput.java
plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/SaveLoadManager.java
plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/ComponentHandle.java
plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/Handle.java
plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/HandleManager.java
plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/StaticPinHandle.java
plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/states/EditorState.java
plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/states/SelectionState.java
plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/DialogManager.java
plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/EditorGUI.java

index 90baafb..8c60257 100644 (file)
@@ -232,6 +232,7 @@ public final class Editor
 //             SaveLoadManager.openLoadDialog();
        }
 
+       @SuppressWarnings("unused") // Editor
        public static void openNewEditor()
        {
                DeserializedSubmodelComponent toBeEdited = new DeserializedSubmodelComponent(new LogicModelModifiable(), null, null, null);
index feae04f..333efcd 100644 (file)
@@ -1,10 +1,6 @@
 package net.mograsim.logic.model.editor;
 
 import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.KeyEvent;
-import org.eclipse.swt.events.KeyListener;
-import org.eclipse.swt.events.MouseEvent;
-import org.eclipse.swt.events.MouseListener;
 
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
 import net.mograsim.logic.model.editor.ui.EditorGUI;
@@ -16,85 +12,61 @@ public class EditorUserInput
        public EditorUserInput(Editor editor)
        {
                this.gui = editor.gui;
-               gui.logicCanvas.addMouseListener(new MouseListener()
+               gui.logicCanvas.addListener(SWT.MouseDown, e ->
                {
-                       @Override
-                       public void mouseDoubleClick(MouseEvent e)
+                       Point clicked = editor.gui.logicCanvas.canvasToWorldCoords(e.x, e.y);
+                       switch (e.button)
                        {
-                               // TODO Auto-generated method stub
+                       case 1:
+                               editor.handleManager.click(clicked, e.stateMask);
+                               break;
+                       default:
+                               // don't react
                        }
 
-                       @Override
-                       public void mouseDown(MouseEvent e)
-                       {
-                               Point clicked = editor.gui.logicCanvas.canvasToWorldCoords(e.x, e.y);
-                               switch (e.button)
-                               {
-                               case 1:
-                                       editor.handleManager.click(clicked, e.stateMask);
-                                       break;
-                               }
-
-                       }
-
-                       @Override
-                       public void mouseUp(MouseEvent e)
-                       {
-                       }
                });
 
-               gui.logicCanvas.addMouseMoveListener((e) ->
+               gui.logicCanvas.addMouseMoveListener(e ->
                {
                        Point dest = editor.gui.logicCanvas.canvasToWorldCoords(e.x, e.y);
                        editor.stateManager.mouseMoved(dest.x, dest.y);
                });
 
-               gui.logicCanvas.addKeyListener(new KeyListener()
+               gui.logicCanvas.addListener(SWT.KeyDown, e ->
                {
-
-                       @Override
-                       public void keyReleased(KeyEvent e)
-                       {
-                               // TODO Auto-generated method stub
-
-                       }
-
-                       @Override
-                       public void keyPressed(KeyEvent e)
+                       switch (e.keyCode)
                        {
-                               switch (e.keyCode)
-                               {
-                               case 'c':
-                                       if ((e.stateMask & SWT.CTRL) == SWT.CTRL)
-                                               editor.stateManager.copy();
-                                       break;
-                               case 'v':
-                                       if ((e.stateMask & SWT.CTRL) == SWT.CTRL)
-                                               editor.stateManager.paste();
-                                       break;
-                               case 'd':
-                                       if ((e.stateMask & SWT.SHIFT) == SWT.SHIFT)
-                                               editor.stateManager.duplicate();
-                                       break;
-                               case 'g':
-                                       editor.stateManager.grab();
-                                       break;
-                               case 'r':
-                                       editor.stateManager.delete();
-                                       break;
-                               case 's':
-                                       if ((e.stateMask & SWT.CTRL) == SWT.CTRL)
-                                               editor.save();
-                                       break;
-                               case 'a':
-                                       if ((e.stateMask & SWT.SHIFT) == SWT.SHIFT)
-                                               editor.stateManager.add();
-                                       break;
-                               case 'h':
-                                       editor.stateManager.boxSelect();
-                                       break;
-                               }
-
+                       case 'c':
+                               if ((e.stateMask & SWT.CTRL) == SWT.CTRL)
+                                       editor.stateManager.copy();
+                               break;
+                       case 'v':
+                               if ((e.stateMask & SWT.CTRL) == SWT.CTRL)
+                                       editor.stateManager.paste();
+                               break;
+                       case 'd':
+                               if ((e.stateMask & SWT.SHIFT) == SWT.SHIFT)
+                                       editor.stateManager.duplicate();
+                               break;
+                       case 'g':
+                               editor.stateManager.grab();
+                               break;
+                       case 'r':
+                               editor.stateManager.delete();
+                               break;
+                       case 's':
+                               if ((e.stateMask & SWT.CTRL) == SWT.CTRL)
+                                       editor.save();
+                               break;
+                       case 'a':
+                               if ((e.stateMask & SWT.SHIFT) == SWT.SHIFT)
+                                       editor.stateManager.add();
+                               break;
+                       case 'h':
+                               editor.stateManager.boxSelect();
+                               break;
+                       default:
+                               // don't react
                        }
                });
        }
index bbe202b..c2e1b03 100644 (file)
@@ -58,6 +58,7 @@ public class SaveLoadManager
                }
        }
 
+       @SuppressWarnings("unused") // Editor
        public static void openLoadDialog() throws IOException
        {
                Shell fdShell = new Shell();
index 14868e9..2330073 100644 (file)
@@ -29,7 +29,7 @@ public class ComponentHandle extends Handle
                this.parent = parent;
                Rectangle bounds = parent.getBounds();
                setSize(bounds.width, bounds.height);
-               parent.addComponentResizedListener((c) ->
+               parent.addComponentResizedListener(c ->
                {
                        Rectangle pBounds = c.getBounds();
                        setSize(pBounds.width, pBounds.height);
index cbfe2a2..8baa1e6 100644 (file)
@@ -116,12 +116,13 @@ public abstract class Handle
                return false;
        }
 
+       // These methods are intended to be overridden
        //@formatter:off
-    public void reqMove(double x, double y) {}
-    public void reqDelete() {}
-    public Optional<ComponentInfo> reqCopy(Point refPoint) { return Optional.empty(); }
-    public void onSelect() {}
-    public void onDeselect() {}
+       @SuppressWarnings("unused") public void reqMove(double x, double y) {/**/}
+    public void reqDelete() {/**/}
+    @SuppressWarnings({ "unused", "static-method" }) public Optional<ComponentInfo> reqCopy(Point refPoint) {return Optional.empty();}
+    public void onSelect() {/**/}
+    public void onDeselect() {/**/}
     //@formatter:on
 
        public final int getPriority()
index 0a33509..ddde095 100644 (file)
@@ -54,17 +54,11 @@ public class HandleManager
 
                LogicModelModifiable model = editor.getSubmodel();
 
-               model.addComponentAddedListener(c -> registerComponent(c));
+               model.addComponentAddedListener(this::registerComponent);
 
-               model.addComponentRemovedListener(c ->
-               {
-                       removeComponentHandle(c);
-               });
+               model.addComponentRemovedListener(this::removeComponentHandle);
 
-               model.addWireAddedListener(w ->
-               {
-                       registerWire(w);
-               });
+               model.addWireAddedListener(this::registerWire);
 
                model.addWireRemovedListener(w ->
                {
@@ -99,8 +93,8 @@ public class HandleManager
        private void registerInterfaceComponent(ModelComponent c)
        {
                c.getPins().values().forEach(p -> addInterfacePinHandle(p));
-               c.addPinAddedListener(p -> addInterfacePinHandle(p));
-               c.addPinRemovedListener(p -> removeInterfacePinHandle(p));
+               c.addPinAddedListener(this::addInterfacePinHandle);
+               c.addPinRemovedListener(this::removeInterfacePinHandle);
        }
 
        private void registerComponent(ModelComponent c)
@@ -109,8 +103,8 @@ public class HandleManager
 
                c.getPins().values().forEach(p -> addPinHandle(p));
 
-               c.addPinAddedListener(p -> addPinHandle(p));
-               c.addPinRemovedListener(p -> removePinHandle(p));
+               c.addPinAddedListener(this::addPinHandle);
+               c.addPinRemovedListener(this::removePinHandle);
        }
 
        private void registerWire(ModelWire wire)
index 0f4a152..3ddb58d 100644 (file)
@@ -18,7 +18,7 @@ public class StaticPinHandle extends PinHandle
                super(1);
                this.parent = parent;
                setSize(CIRCLE_DIAM, CIRCLE_DIAM);
-               parent.addPinMovedListener((p) -> updatePos());
+               parent.addPinMovedListener(p -> updatePos());
                updatePos();
        }
 
index 05e05d8..d730194 100644 (file)
@@ -16,21 +16,22 @@ public abstract class EditorState
                this.manager = manager;
        }
 
+       // These methods are intended to be overridden
        //@formatter:off
-    public void add() {}
-    public void delete() {}
-    public void copy() {}
-    public void paste() {}
-    public void duplicate() {}
-    public void grab() {}
-    public void mouseMoved(double x, double y) {}    
-    public void select(Point pos, boolean additive) {}
-    public void boxSelect() {}
-    public void onEntry() {}
-    public void onExit() {}
-       public void clicked(InterfacePinHandle interfacePinHandle, int stateMask) {}
-       public void clickedEmpty(Point clicked, int stateMask) {}
-       public void clicked(Point clicked, int stateMask) {}
-       public boolean clickedHandle(HandleClickInfo handleClickInfo) { return false; }
+    public void add() {/**/}
+    public void delete() {/**/}
+    public void copy() {/**/}
+    public void paste() {/**/}
+    public void duplicate() {/**/}
+    public void grab() {/**/}
+    @SuppressWarnings("unused") public void mouseMoved(double x, double y) {/**/}    
+    @SuppressWarnings("unused") public void select(Point pos, boolean additive) {/**/}
+    public void boxSelect() {/**/}
+    public void onEntry() {/**/}
+    public void onExit() {/**/}
+    @SuppressWarnings("unused") public void clicked(InterfacePinHandle interfacePinHandle, int stateMask) {/**/}
+    @SuppressWarnings("unused") public void clickedEmpty(Point clicked, int stateMask) {/**/}
+    @SuppressWarnings("unused") public void clicked(Point clicked, int stateMask) {/**/}
+    @SuppressWarnings({ "unused", "static-method" }) public boolean clickedHandle(HandleClickInfo handleClickInfo) {return false;}
        //@formatter:on
 }
index baa2eff..7bf5961 100644 (file)
@@ -108,11 +108,11 @@ public class SelectionState extends EditorState
                                                        Integer.parseInt(result[1]), PinUsage.valueOf(result[2]), clicked.x, clicked.y));
                                        editor.handleManager.getInterfacePinHandle(p).reqMove(clicked.x, clicked.y);
                                }
-                               catch (NumberFormatException e)
+                               catch (@SuppressWarnings("unused") NumberFormatException e)
                                {
                                        editor.dialogManager.openWarningDialog("Failed to create Pin!", "Bit width must be a number!");
                                }
-                               catch (IllegalArgumentException e)
+                               catch (@SuppressWarnings("unused") IllegalArgumentException e)
                                {
                                        editor.dialogManager.openWarningDialog("Failed to create Pin!", "Usage must be one of INPUT, OUTPUT, TRISTATE!");
                                }
@@ -131,6 +131,7 @@ public class SelectionState extends EditorState
                                manager.setState(new CreateWireState(editor, manager, (PinHandle) handleClickInfo.clicked));
                                break;
                        }
+                       //$FALL-THROUGH$
                case CORNER:
                case COMPONENT:
                case WIRE_POINT:
index df7455e..509c524 100644 (file)
@@ -35,12 +35,12 @@ public class DialogManager
 
                protected String[] result;
 
-               public FlexibleInputsDialog(String title, String acceptLabel, String cancelLabel, String... inputs)
+               public FlexibleInputsDialog(String title, String acceptLabel, String cancelLabel)
                {
-                       this(false, title, acceptLabel, cancelLabel, inputs);
+                       this(false, title, acceptLabel, cancelLabel);
                }
 
-               public FlexibleInputsDialog(boolean resizable, String title, String acceptLabel, String cancelLabel, String... inputs)
+               public FlexibleInputsDialog(boolean resizable, String title, String acceptLabel, String cancelLabel)
                {
                        super(new Shell(SWT.CLOSE | (resizable ? SWT.RESIZE | SWT.MAX : 0) | SWT.TITLE | SWT.MIN | SWT.ON_TOP | SWT.APPLICATION_MODAL));
                        this.title = title;
@@ -84,10 +84,7 @@ public class DialogManager
 
                        b1.setText(acceptLabel);
                        Button b2 = new Button(shell, SWT.PUSH);
-                       b2.addListener(SWT.Selection, e ->
-                       {
-                               shell.dispose();
-                       });
+                       b2.addListener(SWT.Selection, e -> shell.dispose());
                        b2.setText(cancelLabel);
 
                        shell.pack();
index 17e12eb..b89d587 100644 (file)
@@ -3,17 +3,13 @@ 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;
 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;
-import org.eclipse.swt.widgets.Listener;
 import org.eclipse.swt.widgets.Menu;
 import org.eclipse.swt.widgets.MenuItem;
 import org.eclipse.swt.widgets.Shell;
@@ -95,20 +91,20 @@ public class EditorGUI
                ToolItem file = new ToolItem(toolBar, SWT.DROP_DOWN);
 
                // TODO
-               DropDownEntry newEntry = new DropDownEntry("New", e -> Editor.openNewEditor());
-               DropDownEntry loadEntry = new DropDownEntry("Load", e ->
+               DropDownEntry newEntry = new DropDownEntry("New", Editor::openNewEditor);
+               DropDownEntry loadEntry = new DropDownEntry("Load", () ->
                {
                        try
                        {
                                SaveLoadManager.openLoadDialog();
                        }
-                       catch (IOException e1)
+                       catch (IOException e)
                        {
-                               editor.dialogManager.openWarningDialog("Failed to load Component!", e1.getMessage());
+                               editor.dialogManager.openWarningDialog("Failed to load Component!", e.getMessage());
                        }
                });
-               DropDownEntry saveEntry = new DropDownEntry("Save", e -> editor.save());
-               DropDownEntry saveAsEntry = new DropDownEntry("Save as...", e -> editor.saveAs());
+               DropDownEntry saveEntry = new DropDownEntry("Save", editor::save);
+               DropDownEntry saveAsEntry = new DropDownEntry("Save as...", editor::saveAs);
 
                DropDownEntry[] entries = new DropDownEntry[] { newEntry, loadEntry, saveEntry, saveAsEntry };
 
@@ -118,6 +114,7 @@ public class EditorGUI
                return toolBar;
        }
 
+       @SuppressWarnings("unused") // ToolItem
        private ToolBar setupBottomToolBar(Composite parent)
        {
                GridData d = new GridData();
@@ -135,7 +132,7 @@ public class EditorGUI
                int index = 0;
                for (Editor.Snapping sn : Editor.Snapping.values())
                {
-                       entries[index++] = new DropDownEntry(sn.toString(), e ->
+                       entries[index++] = new DropDownEntry(sn.toString(), () ->
                        {
                                editor.setSnapping(sn);
                                snappSelect.setText(sn.toString());
@@ -157,57 +154,33 @@ public class EditorGUI
                for (DropDownEntry entry : entries)
                {
                        MenuItem item = new MenuItem(menu, SWT.PUSH);
-                       item.addSelectionListener(new SelectionListener()
-                       {
-                               @Override
-                               public void widgetSelected(SelectionEvent e)
-                               {
-                                       entry.listener.widgetSelected(e);
-                               }
-
-                               @Override
-                               public void widgetDefaultSelected(SelectionEvent e)
-                               {
-                                       widgetSelected(e);
-                               }
-                       });
+                       item.addListener(SWT.Selection, e -> entry.onSelected.run());
                        item.setText(entry.title);
                }
 
-               parent.addListener(SWT.Selection, new Listener()
+               parent.addListener(SWT.Selection, e ->
                {
-                       public void handleEvent(Event event)
-                       {
-                               if (event.detail == SWT.ARROW)
-                               {
-                                       Rectangle rect = parent.getBounds();
-                                       Point pt = new Point(rect.x, rect.y + rect.height);
-                                       pt = parent.getParent().toDisplay(pt);
-                                       menu.setLocation(pt.x, pt.y);
-                                       menu.setVisible(true);
-                               }
-                       }
+                       Rectangle rect = parent.getBounds();
+                       Point pt = new Point(rect.x, rect.y + rect.height);
+                       pt = parent.getParent().toDisplay(pt);
+                       menu.setLocation(pt.x, pt.y);
+                       menu.setVisible(true);
                });
        }
 
        private static class DropDownEntry
        {
                public final String title;
-               public final EntrySelectedListener listener;
+               public final Runnable onSelected;
 
-               public DropDownEntry(String title, EntrySelectedListener listener)
+               public DropDownEntry(String title, Runnable onSelected)
                {
                        super();
                        this.title = title;
-                       this.listener = listener;
+                       this.onSelected = onSelected;
                }
        }
 
-       private static interface EntrySelectedListener
-       {
-               public void widgetSelected(SelectionEvent e);
-       }
-
        public void refreshAddList()
        {
                addList.setItems(IndirectModelComponentCreator.getStandardComponentIDs().keySet().stream().sorted().toArray(String[]::new));