Cleaned up Editor: removed warnings; cleaned listener system
[Mograsim.git] / plugins / net.mograsim.logic.model.editor / src / net / mograsim / logic / model / editor / ui / DialogManager.java
1 package net.mograsim.logic.model.editor.ui;
2
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;
14
15 public class DialogManager
16 {
17         private Shell parent;
18
19         public DialogManager(Shell parent)
20         {
21                 this.parent = parent;
22         }
23
24         public void openWarningDialog(String title, String message)
25         {
26                 MessageBox b = new MessageBox(parent, SWT.ICON_WARNING | SWT.OK);
27                 b.setText(title);
28                 b.setMessage(message);
29                 b.open();
30         }
31
32         private static abstract class FlexibleInputsDialog extends Dialog
33         {
34                 private final String title, acceptLabel, cancelLabel;
35
36                 protected String[] result;
37
38                 public FlexibleInputsDialog(String title, String acceptLabel, String cancelLabel)
39                 {
40                         this(false, title, acceptLabel, cancelLabel);
41                 }
42
43                 public FlexibleInputsDialog(boolean resizable, String title, String acceptLabel, String cancelLabel)
44                 {
45                         super(new Shell(SWT.CLOSE | (resizable ? SWT.RESIZE | SWT.MAX : 0) | SWT.TITLE | SWT.MIN | SWT.ON_TOP | SWT.APPLICATION_MODAL));
46                         this.title = title;
47                         this.acceptLabel = acceptLabel;
48                         this.cancelLabel = cancelLabel;
49                 }
50
51                 protected abstract void setupWidgets(Composite parent);
52
53                 protected abstract void buildResult();
54
55                 /**
56                  * @return May be null (if {@link Dialog} was cancelled)
57                  */
58                 public String[] open()
59                 {
60                         Shell shell = getParent();
61                         Display display = shell.getDisplay();
62                         shell.setMinimumSize(500, 150);
63                         shell.setText(title);
64                         GridLayout layout = new GridLayout();
65                         layout.numColumns = 2;
66                         shell.setLayout(layout);
67
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);
77
78                         Button b1 = new Button(shell, SWT.PUSH);
79                         b1.addListener(SWT.Selection, e ->
80                         {
81                                 buildResult();
82                                 shell.dispose();
83                         });
84
85                         b1.setText(acceptLabel);
86                         Button b2 = new Button(shell, SWT.PUSH);
87                         b2.addListener(SWT.Selection, e -> shell.dispose());
88                         b2.setText(cancelLabel);
89
90                         shell.pack();
91
92                         shell.open();
93                         while (!shell.isDisposed())
94                                 if (!display.readAndDispatch())
95                                         display.sleep();
96                         return result;
97                 }
98         }
99
100         private static class MultiTextFieldsDialog extends FlexibleInputsDialog
101         {
102                 private final String[] inputs;
103                 private Text[] textFields;
104
105                 public MultiTextFieldsDialog(String title, String acceptLabel, String cancelLabel, String... inputs)
106                 {
107                         super(title, acceptLabel, cancelLabel);
108                         this.inputs = inputs;
109                 }
110
111                 @Override
112                 protected void setupWidgets(Composite parent)
113                 {
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++)
119                         {
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;
128                         }
129                 }
130
131                 @Override
132                 protected void buildResult()
133                 {
134                         result = new String[textFields.length];
135                         for (int i = 0; i < textFields.length; i++)
136                                 result[i] = textFields[i].getText();
137                 }
138
139         }
140
141         /**
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
143          *         was cancelled.
144          */
145         public static String[] openMultiTextDialog(String title, String acceptLabel, String cancelLabel, String... inputs)
146         {
147                 return new MultiTextFieldsDialog(title, acceptLabel, cancelLabel, inputs).open();
148         }
149
150         public static class MultiLineTextFieldDialog extends FlexibleInputsDialog
151         {
152                 private final String input;
153                 private Text textField;
154
155                 public MultiLineTextFieldDialog(String title, String acceptLabel, String cancelLabel, String input)
156                 {
157                         super(true, title, acceptLabel, cancelLabel);
158                         this.input = input;
159                 }
160
161                 @Override
162                 protected void setupWidgets(Composite parent)
163                 {
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);
169                         l.setText(input);
170                         gd.verticalAlignment = SWT.TOP;
171                         l.setLayoutData(gd);
172                         gd = new GridData();
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;
179                 }
180
181                 @Override
182                 protected void buildResult()
183                 {
184                         result = new String[] { textField.getText() };
185                 }
186         }
187
188         /**
189          * @return The String entered if the dialog was accepted, null if the dialog was cancelled.
190          */
191         public static String openMultiLineTextDialog(String title, String acceptLabel, String cancelLabel, String input)
192         {
193                 String[] result = new MultiLineTextFieldDialog(title, acceptLabel, cancelLabel, input).open();
194                 return result == null ? null : result[0];
195         }
196 }