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)
40 this(false, title, acceptLabel, cancelLabel);
43 public FlexibleInputsDialog(boolean resizable, String title, String acceptLabel, String cancelLabel)
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 -> shell.dispose());
88 b2.setText(cancelLabel);
93 while (!shell.isDisposed())
94 if (!display.readAndDispatch())
100 private static class MultiTextFieldsDialog extends FlexibleInputsDialog
102 private final String[] inputs;
103 private Text[] textFields;
105 public MultiTextFieldsDialog(String title, String acceptLabel, String cancelLabel, String... inputs)
107 super(title, acceptLabel, cancelLabel);
108 this.inputs = inputs;
112 protected void setupWidgets(Composite parent)
114 GridLayout layout = new GridLayout();
115 layout.numColumns = 2;
116 parent.setLayout(layout);
117 this.textFields = new Text[inputs.length];
118 for (int i = 0; i < inputs.length; i++)
120 Label textFieldName = new Label(parent, SWT.NONE);
121 textFieldName.setText(inputs[i].concat(":"));
122 GridData g = new GridData();
123 g.grabExcessHorizontalSpace = true;
124 g.horizontalAlignment = SWT.FILL;
125 Text newTextField = new Text(parent, SWT.BORDER);
126 newTextField.setLayoutData(g);
127 textFields[i] = newTextField;
132 protected void buildResult()
134 result = new String[textFields.length];
135 for (int i = 0; i < textFields.length; i++)
136 result[i] = textFields[i].getText();
142 * @return The Strings entered, in order of the input labels the dialog was opened with, if the dialog was accepted, null if the dialog
145 public static String[] openMultiTextDialog(String title, String acceptLabel, String cancelLabel, String... inputs)
147 return new MultiTextFieldsDialog(title, acceptLabel, cancelLabel, inputs).open();
150 public static class MultiLineTextFieldDialog extends FlexibleInputsDialog
152 private final String input;
153 private Text textField;
155 public MultiLineTextFieldDialog(String title, String acceptLabel, String cancelLabel, String input)
157 super(true, title, acceptLabel, cancelLabel);
162 protected void setupWidgets(Composite parent)
164 GridLayout layout = new GridLayout();
165 layout.numColumns = 2;
166 parent.setLayout(layout);
167 GridData gd = new GridData();
168 Label l = new Label(parent, SWT.NONE);
170 gd.verticalAlignment = SWT.TOP;
173 textField = new Text(parent, SWT.V_SCROLL);
174 textField.setLayoutData(gd);
175 gd.grabExcessHorizontalSpace = true;
176 gd.grabExcessVerticalSpace = true;
177 gd.horizontalAlignment = SWT.FILL;
178 gd.verticalAlignment = SWT.FILL;
182 protected void buildResult()
184 result = new String[] { textField.getText() };
189 * @return The String entered if the dialog was accepted, null if the dialog was cancelled.
191 public static String openMultiLineTextDialog(String title, String acceptLabel, String cancelLabel, String input)
193 String[] result = new MultiLineTextFieldDialog(title, acceptLabel, cancelLabel, input).open();
194 return result == null ? null : result[0];