X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model.editor%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Feditor%2Fui%2FDialogManager.java;h=df7455e66e2fa12420c83fb249fa91640d39f55d;hb=8bed58cd47f4e53a0a83e066d38864aa6875502f;hp=7d982e01c93f3fe767583d859efdb99ebc98acc1;hpb=a393b0a2a9899707af54c9ee77a01f28ac967bd1;p=Mograsim.git 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..df7455e6 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,18 +4,18 @@ 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; - + public DialogManager(Shell parent) { this.parent = parent; @@ -28,106 +28,172 @@ 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 FlexibleInputsDialog(String title, String acceptLabel, String cancelLabel, String... inputs) + { + this(false, title, acceptLabel, cancelLabel, inputs); + } - public InteractiveDialog(String title, String acceptLabel, String cancelLabel, String... inputs) + public FlexibleInputsDialog(boolean resizable, 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); + 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); + shell.open(); + while (!shell.isDisposed()) + if (!display.readAndDispatch()) + display.sleep(); + return result; } + } + + private static class MultiTextFieldsDialog extends FlexibleInputsDialog + { + private final String[] inputs; + private Text[] textFields; - public String getText(int index) + public MultiTextFieldsDialog(String title, String acceptLabel, String cancelLabel, String... inputs) { - if (!shell.isDisposed()) - return textFields[index].getText(); - else - return finalInput[index]; + super(title, acceptLabel, cancelLabel); + this.inputs = inputs; } - public void open() + @Override + protected void setupWidgets(Composite parent) { - shell.open(); - while (!shell.isDisposed()) - if (!display.readAndDispatch()) - display.sleep(); + 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; + } } - public void dispose() + @Override + protected void buildResult() { - shell.dispose(); + result = new String[textFields.length]; + for (int i = 0; i < textFields.length; i++) + result[i] = textFields[i].getText(); } - public InteractiveDialog.InteractiveDialogState getState() + } + + /** + * @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 class MultiLineTextFieldDialog extends FlexibleInputsDialog + { + private final String input; + private Text textField; + + public MultiLineTextFieldDialog(String title, String acceptLabel, String cancelLabel, String input) { - return state; + super(true, title, acceptLabel, cancelLabel); + this.input = input; } - private void buildFinalInput() + @Override + protected void setupWidgets(Composite parent) { - finalInput = new String[textFields.length]; - for (int i = 0; i < textFields.length; i++) - finalInput[i] = textFields[i].getText(); + 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; } - public static enum InteractiveDialogState + @Override + protected void buildResult() { - ACTIVE, ACCEPTED, CANCELLED; + result = new String[] { textField.getText() }; } } - - public static void openAddPinDialog(Editor editor, double x, double y) + + /** + * @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]; } }