1 package net.mograsim.logic.model.editor.ui;
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.layout.GridData;
5 import org.eclipse.swt.layout.GridLayout;
6 import org.eclipse.swt.widgets.Button;
7 import org.eclipse.swt.widgets.Composite;
8 import org.eclipse.swt.widgets.Dialog;
9 import org.eclipse.swt.widgets.Display;
10 import org.eclipse.swt.widgets.Label;
11 import org.eclipse.swt.widgets.MessageBox;
12 import org.eclipse.swt.widgets.Shell;
13 import org.eclipse.swt.widgets.Text;
15 public class DialogManager
19 public DialogManager(Shell parent)
24 public void openWarningDialog(String title, String message)
26 MessageBox b = new MessageBox(parent, SWT.ICON_WARNING | SWT.OK);
28 b.setMessage(message);
32 private static abstract class FlexibleInputsDialog extends Dialog
34 private final String title, acceptLabel, cancelLabel;
36 protected String[] result;
38 public FlexibleInputsDialog(String title, String acceptLabel, String cancelLabel, String... inputs)
40 this(false, title, acceptLabel, cancelLabel, inputs);
43 public FlexibleInputsDialog(boolean resizable, String title, String acceptLabel, String cancelLabel, String... inputs)
45 super(new Shell(SWT.CLOSE | (resizable ? SWT.RESIZE | SWT.MAX : 0) | SWT.TITLE | SWT.MIN | SWT.ON_TOP | SWT.APPLICATION_MODAL));
47 this.acceptLabel = acceptLabel;
48 this.cancelLabel = cancelLabel;
51 protected abstract void setupWidgets(Composite parent);
53 protected abstract void buildResult();
56 * @return May be null (if {@link Dialog} was cancelled)
58 public String[] open()
60 Shell shell = getParent();
61 Display display = shell.getDisplay();
62 shell.setMinimumSize(500, 150);
64 GridLayout layout = new GridLayout();
65 layout.numColumns = 2;
66 shell.setLayout(layout);
68 Composite inputContainer = new Composite(shell, SWT.BORDER);
69 GridData gd = new GridData();
70 gd.horizontalSpan = 2;
71 gd.horizontalAlignment = SWT.FILL;
72 gd.grabExcessHorizontalSpace = true;
73 gd.verticalAlignment = SWT.FILL;
74 gd.grabExcessVerticalSpace = true;
75 inputContainer.setLayoutData(gd);
76 setupWidgets(inputContainer);
78 Button b1 = new Button(shell, SWT.PUSH);
79 b1.addListener(SWT.Selection, e ->
85 b1.setText(acceptLabel);
86 Button b2 = new Button(shell, SWT.PUSH);
87 b2.addListener(SWT.Selection, e ->
91 b2.setText(cancelLabel);
96 while (!shell.isDisposed())
97 if (!display.readAndDispatch())
103 private static class MultiTextFieldsDialog extends FlexibleInputsDialog
105 private final String[] inputs;
106 private Text[] textFields;
108 public MultiTextFieldsDialog(String title, String acceptLabel, String cancelLabel, String... inputs)
110 super(title, acceptLabel, cancelLabel);
111 this.inputs = inputs;
115 protected void setupWidgets(Composite parent)
117 GridLayout layout = new GridLayout();
118 layout.numColumns = 2;
119 parent.setLayout(layout);
120 this.textFields = new Text[inputs.length];
121 for (int i = 0; i < inputs.length; i++)
123 Label textFieldName = new Label(parent, SWT.NONE);
124 textFieldName.setText(inputs[i].concat(":"));
125 GridData g = new GridData();
126 g.grabExcessHorizontalSpace = true;
127 g.horizontalAlignment = SWT.FILL;
128 Text newTextField = new Text(parent, SWT.BORDER);
129 newTextField.setLayoutData(g);
130 textFields[i] = newTextField;
135 protected void buildResult()
137 result = new String[textFields.length];
138 for (int i = 0; i < textFields.length; i++)
139 result[i] = textFields[i].getText();
145 * @return The Strings entered, in order of the input labels the dialog was opened with, if the dialog was accepted, null if the dialog
148 public static String[] openMultiTextDialog(String title, String acceptLabel, String cancelLabel, String... inputs)
150 return new MultiTextFieldsDialog(title, acceptLabel, cancelLabel, inputs).open();
153 public static class MultiLineTextFieldDialog extends FlexibleInputsDialog
155 private final String input;
156 private Text textField;
158 public MultiLineTextFieldDialog(String title, String acceptLabel, String cancelLabel, String input)
160 super(true, title, acceptLabel, cancelLabel);
165 protected void setupWidgets(Composite parent)
167 GridLayout layout = new GridLayout();
168 layout.numColumns = 2;
169 parent.setLayout(layout);
170 GridData gd = new GridData();
171 Label l = new Label(parent, SWT.NONE);
173 gd.verticalAlignment = SWT.TOP;
176 textField = new Text(parent, SWT.V_SCROLL);
177 textField.setLayoutData(gd);
178 gd.grabExcessHorizontalSpace = true;
179 gd.grabExcessVerticalSpace = true;
180 gd.horizontalAlignment = SWT.FILL;
181 gd.verticalAlignment = SWT.FILL;
185 protected void buildResult()
187 result = new String[] { textField.getText() };
192 * @return The String entered if the dialog was accepted, null if the dialog was cancelled.
194 public static String openMultiLineTextDialog(String title, String acceptLabel, String cancelLabel, String input)
196 String[] result = new MultiLineTextFieldDialog(title, acceptLabel, cancelLabel, input).open();
197 return result == null ? null : result[0];