From 20dff13a4008a67a28b4410298b79bdb89d5a207 Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Wed, 18 Sep 2019 11:46:23 +0200 Subject: [PATCH] Cleaned up Editor: removed warnings; cleaned listener system --- .../mograsim/logic/model/editor/Editor.java | 1 + .../logic/model/editor/EditorUserInput.java | 112 +++++++----------- .../logic/model/editor/SaveLoadManager.java | 1 + .../model/editor/handles/ComponentHandle.java | 2 +- .../logic/model/editor/handles/Handle.java | 11 +- .../model/editor/handles/HandleManager.java | 20 ++-- .../model/editor/handles/StaticPinHandle.java | 2 +- .../model/editor/states/EditorState.java | 31 ++--- .../model/editor/states/SelectionState.java | 5 +- .../logic/model/editor/ui/DialogManager.java | 11 +- .../logic/model/editor/ui/EditorGUI.java | 63 +++------- 11 files changed, 100 insertions(+), 159 deletions(-) diff --git a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/Editor.java b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/Editor.java index 90baafb5..8c602578 100644 --- a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/Editor.java +++ b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/Editor.java @@ -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); diff --git a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/EditorUserInput.java b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/EditorUserInput.java index feae04f3..333efcd1 100644 --- a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/EditorUserInput.java +++ b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/EditorUserInput.java @@ -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 } }); } diff --git a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/SaveLoadManager.java b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/SaveLoadManager.java index bbe202b1..c2e1b032 100644 --- a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/SaveLoadManager.java +++ b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/SaveLoadManager.java @@ -58,6 +58,7 @@ public class SaveLoadManager } } + @SuppressWarnings("unused") // Editor public static void openLoadDialog() throws IOException { Shell fdShell = new Shell(); diff --git a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/ComponentHandle.java b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/ComponentHandle.java index 14868e94..2330073a 100644 --- a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/ComponentHandle.java +++ b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/ComponentHandle.java @@ -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); diff --git a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/Handle.java b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/Handle.java index cbfe2a2a..8baa1e65 100644 --- a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/Handle.java +++ b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/Handle.java @@ -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 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 reqCopy(Point refPoint) {return Optional.empty();} + public void onSelect() {/**/} + public void onDeselect() {/**/} //@formatter:on public final int getPriority() diff --git a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/HandleManager.java b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/HandleManager.java index 0a33509c..ddde0956 100644 --- a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/HandleManager.java +++ b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/HandleManager.java @@ -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) diff --git a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/StaticPinHandle.java b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/StaticPinHandle.java index 0f4a1524..3ddb58d1 100644 --- a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/StaticPinHandle.java +++ b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/StaticPinHandle.java @@ -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(); } diff --git a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/states/EditorState.java b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/states/EditorState.java index 05e05d85..d7301945 100644 --- a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/states/EditorState.java +++ b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/states/EditorState.java @@ -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 } diff --git a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/states/SelectionState.java b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/states/SelectionState.java index baa2eff9..7bf5961d 100644 --- a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/states/SelectionState.java +++ b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/states/SelectionState.java @@ -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: diff --git a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/DialogManager.java b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/DialogManager.java index df7455e6..509c524e 100644 --- a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/DialogManager.java +++ b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/DialogManager.java @@ -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(); diff --git a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/EditorGUI.java b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/EditorGUI.java index 17e12ebb..b89d587d 100644 --- a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/EditorGUI.java +++ b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/EditorGUI.java @@ -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)); -- 2.17.1