From 412caa5f9abe02a58b8127ce898982d573347559 Mon Sep 17 00:00:00 2001 From: Fabian Stemmler Date: Mon, 15 Jul 2019 15:13:42 +0200 Subject: [PATCH] Added menu for adding components with parameters Also restructured DialogManager and fixed some issues with DialogManager and EditorGUI --- .../mograsim/logic/model/editor/Editor.java | 30 ++- .../logic/model/editor/SaveLoadManager.java | 17 +- .../model/editor/states/SelectionState.java | 11 +- .../logic/model/editor/ui/DialogManager.java | 195 ++++++++++++------ .../logic/model/editor/ui/EditorGUI.java | 2 +- 5 files changed, 168 insertions(+), 87 deletions(-) diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/Editor.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/Editor.java index e285574e..37efbae2 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/Editor.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/Editor.java @@ -7,7 +7,9 @@ import java.util.Optional; import java.util.Set; import com.google.gson.JsonElement; -import com.google.gson.JsonObject; +import com.google.gson.JsonNull; +import com.google.gson.JsonParser; +import com.google.gson.JsonSyntaxException; import net.haspamelodica.swt.helper.swtobjectwrappers.Point; import net.mograsim.logic.model.editor.handles.ComponentHandle; @@ -135,10 +137,28 @@ public final class Editor public void addComponent(double x, double y) { - GUIComponent c = addComponent(gui.getAddListSelected(), new JsonObject()); - selection.clear(); - selection.add(handleManager.getHandle(c)); - moveSelection(x, y); + boolean successful = false; + JsonElement params = JsonNull.INSTANCE; + outer: + while(!successful) + { + String selected = gui.getAddListSelected(); + try + { + GUIComponent c = addComponent(selected, params); + selection.clear(); + selection.add(handleManager.getHandle(c)); + moveSelection(x, y); + successful = true; + } + catch(UnsupportedOperationException | JsonSyntaxException | NumberFormatException e) + { + String result = DialogManager.openMultiLineTextDialog("Add component", "Create", "Cancel", "Parameters:"); + if(result == null) + break outer; + params = new JsonParser().parse(result); + } + } } private GUIComponent addComponent(String identifier, JsonElement params) diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/SaveLoadManager.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/SaveLoadManager.java index 1ba1d252..f4a2eca4 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/SaveLoadManager.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/SaveLoadManager.java @@ -2,7 +2,7 @@ package net.mograsim.logic.model.editor; import java.io.IOException; -import net.mograsim.logic.model.editor.ui.DialogManager.InteractiveDialog; +import net.mograsim.logic.model.editor.ui.DialogManager; import net.mograsim.logic.model.model.ViewModelModifiable; import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent; import net.mograsim.logic.model.serializing.SubmodelComponentDeserializer; @@ -28,12 +28,10 @@ public class SaveLoadManager public void openSaveAsDialog() { - InteractiveDialog d = new InteractiveDialog("Save as...", "Save", "Cancel", "Path"); - d.open(); - - if(InteractiveDialog.InteractiveDialogState.ACCEPTED.equals(d.getState())) + String result[] = DialogManager.openMultiTextDialog("Save as...", "Save", "Cancel", "Path"); + if(result != null) { - savePath = d.getText(); + savePath = result[0]; innerSave(); } } @@ -58,12 +56,11 @@ public class SaveLoadManager public static void openLoadDialog() { - InteractiveDialog load = new InteractiveDialog("Load Component...", "Load", "Cancel", "Path"); - load.open(); - if(InteractiveDialog.InteractiveDialogState.ACCEPTED.equals(load.getState())) + String[] result = DialogManager.openMultiTextDialog("Load Component...", "Load", "Cancel", "Path"); + if(result != null) { new Editor((DeserializedSubmodelComponent) SubmodelComponentDeserializer - .create(new ViewModelModifiable(), load.getText())); + .create(new ViewModelModifiable(), result[0])); } } } diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/states/SelectionState.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/states/SelectionState.java index 2a66e476..95721c39 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/states/SelectionState.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/states/SelectionState.java @@ -12,7 +12,7 @@ import net.mograsim.logic.model.editor.handles.PinHandle; import net.mograsim.logic.model.editor.handles.WireHandle; import net.mograsim.logic.model.editor.handles.Handle.HandleClickInfo; import net.mograsim.logic.model.editor.handles.WireHandle.WireHandleClickInfo; -import net.mograsim.logic.model.editor.ui.DialogManager.InteractiveDialog; +import net.mograsim.logic.model.editor.ui.DialogManager; import net.mograsim.logic.model.model.wires.MovablePin; import net.mograsim.logic.model.model.wires.Pin; @@ -98,14 +98,13 @@ public class SelectionState extends EditorState editor.getSelection().clear(); if ((stateMask & SWT.ALT) == SWT.ALT) { - InteractiveDialog pinAdd = new InteractiveDialog("Add Pin...", "Add", "Cancel", "Name", "Logic Width"); - pinAdd.open(); - if (pinAdd.getState().equals(InteractiveDialog.InteractiveDialogState.ACCEPTED)) + String[] result = DialogManager.openMultiTextDialog("Add Pin...", "Add", "Cancel", "Name", "Logic Width"); + if (result != null) { try { - Pin p = editor.toBeEdited.addSubmodelInterface(new MovablePin(editor.toBeEdited, pinAdd.getText(), - Integer.parseInt(pinAdd.getText(1)), clicked.x, clicked.y)); + Pin p = editor.toBeEdited.addSubmodelInterface(new MovablePin(editor.toBeEdited, result[0], + Integer.parseInt(result[1]), clicked.x, clicked.y)); editor.handleManager.getInterfacePinHandle(p).reqMove(clicked.x, clicked.y); } catch (NumberFormatException e) { diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/DialogManager.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/DialogManager.java index 7d982e01..b6d6ad9c 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/DialogManager.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/DialogManager.java @@ -4,14 +4,14 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Dialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; -import net.mograsim.logic.model.editor.Editor; - public class DialogManager { private Shell parent; @@ -28,106 +28,171 @@ public class DialogManager b.setMessage(message); b.open(); } - - public static class InteractiveDialog + + private static abstract class FlexibleInputsDialog extends Dialog { - private String[] finalInput; - private final Display display; - private final Shell shell; - private final Button b1, b2; - private Text[] textFields; - private InteractiveDialog.InteractiveDialogState state; + private final String title, acceptLabel, cancelLabel; + + protected String[] result; - public InteractiveDialog(String title, String acceptLabel, String cancelLabel, String... inputs) + public FlexibleInputsDialog(String title, String acceptLabel, String cancelLabel, String... inputs) { - display = Display.getDefault(); - shell = new Shell(SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.ON_TOP | SWT.APPLICATION_MODAL); + this(false, title, acceptLabel, cancelLabel, inputs); + } + + public FlexibleInputsDialog(boolean resizable, String title, String acceptLabel, String cancelLabel, String... inputs) + { + super(new Shell(SWT.CLOSE | (resizable ? SWT.RESIZE | SWT.MAX : 0) | SWT.TITLE | SWT.MIN | SWT.ON_TOP | SWT.APPLICATION_MODAL)); + this.title = title; + this.acceptLabel = acceptLabel; + this.cancelLabel = cancelLabel; + } + + protected abstract void setupWidgets(Composite parent); + protected abstract void buildResult(); + + /** + * @return May be null (if {@link Dialog} was cancelled) + */ + public String[] open() + { + Shell shell = getParent(); + Display display = shell.getDisplay(); shell.setMinimumSize(500, 150); shell.setText(title); GridLayout layout = new GridLayout(); layout.numColumns = 2; shell.setLayout(layout); - this.textFields = new Text[inputs.length]; - for (int i = 0; i < inputs.length; i++) - { - Label textFieldName = new Label(shell, SWT.NONE); - textFieldName.setText(inputs[i].concat(":")); - GridData g = new GridData(); - g.grabExcessHorizontalSpace = true; - g.horizontalAlignment = SWT.FILL; - Text newTextField = new Text(shell, SWT.BORDER); - newTextField.setLayoutData(g); - textFields[i] = newTextField; - } - b1 = new Button(shell, SWT.PUSH); + Composite inputContainer = new Composite(shell, SWT.BORDER); + GridData gd = new GridData(); + gd.horizontalSpan = 2; + gd.horizontalAlignment = SWT.FILL; + gd.grabExcessHorizontalSpace = true; + gd.verticalAlignment = SWT.FILL; + gd.grabExcessVerticalSpace = true; + inputContainer.setLayoutData(gd); + setupWidgets(inputContainer); + + + Button b1 = new Button(shell, SWT.PUSH); b1.addListener(SWT.Selection, e -> { - state = InteractiveDialogState.ACCEPTED; - buildFinalInput(); - dispose(); + buildResult(); + shell.dispose(); }); + b1.setText(acceptLabel); - b2 = new Button(shell, SWT.PUSH); + Button b2 = new Button(shell, SWT.PUSH); b2.addListener(SWT.Selection, e -> { - state = InteractiveDialogState.CANCELLED; - buildFinalInput(); - dispose(); + shell.dispose(); }); b2.setText(cancelLabel); - state = InteractiveDialogState.ACTIVE; - shell.pack(); - } - public String getText() - { - return getText(0); - } - - public String getText(int index) - { - if (!shell.isDisposed()) - return textFields[index].getText(); - else - return finalInput[index]; - } - - public void open() - { shell.open(); while (!shell.isDisposed()) if (!display.readAndDispatch()) display.sleep(); + return result; } + } - public void dispose() + private static class MultiTextFieldsDialog extends FlexibleInputsDialog + { + private final String[] inputs; + private Text[] textFields; + + public MultiTextFieldsDialog(String title, String acceptLabel, String cancelLabel, String... inputs) { - shell.dispose(); + super(title, acceptLabel, cancelLabel); + this.inputs = inputs; } - - public InteractiveDialog.InteractiveDialogState getState() + + @Override + protected void setupWidgets(Composite parent) { - return state; + GridLayout layout = new GridLayout(); + layout.numColumns = 2; + parent.setLayout(layout); + this.textFields = new Text[inputs.length]; + for (int i = 0; i < inputs.length; i++) + { + Label textFieldName = new Label(parent, SWT.NONE); + textFieldName.setText(inputs[i].concat(":")); + GridData g = new GridData(); + g.grabExcessHorizontalSpace = true; + g.horizontalAlignment = SWT.FILL; + Text newTextField = new Text(parent, SWT.BORDER); + newTextField.setLayoutData(g); + textFields[i] = newTextField; + } } - private void buildFinalInput() + @Override + protected void buildResult() { - finalInput = new String[textFields.length]; + result = new String[textFields.length]; for (int i = 0; i < textFields.length; i++) - finalInput[i] = textFields[i].getText(); - } - - public static enum InteractiveDialogState - { - ACTIVE, ACCEPTED, CANCELLED; + result[i] = textFields[i].getText(); } + + } + + /** + * @return The Strings entered, in order of the input labels the dialog was opened with, if the dialog was accepted, null if the dialog was cancelled. + */ + public static String[] openMultiTextDialog(String title, String acceptLabel, String cancelLabel, String... inputs) + { + return new MultiTextFieldsDialog(title, acceptLabel, cancelLabel, inputs).open(); } - public static void openAddPinDialog(Editor editor, double x, double y) + public static class MultiLineTextFieldDialog extends FlexibleInputsDialog { + private final String input; + private Text textField; + public MultiLineTextFieldDialog(String title, String acceptLabel, String cancelLabel, String input) + { + super(true, title, acceptLabel, cancelLabel); + this.input = input; + } + + @Override + protected void setupWidgets(Composite parent) + { + GridLayout layout = new GridLayout(); + layout.numColumns = 2; + parent.setLayout(layout); + GridData gd = new GridData(); + Label l = new Label(parent, SWT.NONE); + l.setText(input); + gd.verticalAlignment = SWT.TOP; + l.setLayoutData(gd); + gd = new GridData(); + textField = new Text(parent, SWT.V_SCROLL); + textField.setLayoutData(gd); + gd.grabExcessHorizontalSpace = true; + gd.grabExcessVerticalSpace = true; + gd.horizontalAlignment = SWT.FILL; + gd.verticalAlignment = SWT.FILL; + } + + @Override + protected void buildResult() + { + result = new String[] { textField.getText() }; + } + } + + /** + * @return The String entered if the dialog was accepted, null if the dialog was cancelled. + */ + public static String openMultiLineTextDialog(String title, String acceptLabel, String cancelLabel, String input) + { + String[] result = new MultiLineTextFieldDialog(title, acceptLabel, cancelLabel, input).open(); + return result == null ? null : result[0]; } } diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/EditorGUI.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/EditorGUI.java index 4d5236f6..2827f249 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/EditorGUI.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/ui/EditorGUI.java @@ -45,7 +45,7 @@ public class EditorGUI d.grabExcessVerticalSpace = true; d.verticalAlignment = SWT.FILL; d.verticalSpan = 2; - addList = new List(shell, SWT.FILL); + addList = new List(shell, SWT.V_SCROLL); addList.setLayoutData(d); refreshAddList(); -- 2.17.1