Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[Mograsim.git] / 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, String... inputs)
39                 {
40                         this(false, title, acceptLabel, cancelLabel, inputs);
41                 }
42
43                 public FlexibleInputsDialog(boolean resizable, String title, String acceptLabel, String cancelLabel, String... inputs)
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 ->
88                         {
89                                 shell.dispose();
90                         });
91                         b2.setText(cancelLabel);
92
93                         shell.pack();
94
95                         shell.open();
96                         while (!shell.isDisposed())
97                                 if (!display.readAndDispatch())
98                                         display.sleep();
99                         return result;
100                 }
101         }
102
103         private static class MultiTextFieldsDialog extends FlexibleInputsDialog
104         {
105                 private final String[] inputs;
106                 private Text[] textFields;
107
108                 public MultiTextFieldsDialog(String title, String acceptLabel, String cancelLabel, String... inputs)
109                 {
110                         super(title, acceptLabel, cancelLabel);
111                         this.inputs = inputs;
112                 }
113
114                 @Override
115                 protected void setupWidgets(Composite parent)
116                 {
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++)
122                         {
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;
131                         }
132                 }
133
134                 @Override
135                 protected void buildResult()
136                 {
137                         result = new String[textFields.length];
138                         for (int i = 0; i < textFields.length; i++)
139                                 result[i] = textFields[i].getText();
140                 }
141
142         }
143
144         /**
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
146          *         was cancelled.
147          */
148         public static String[] openMultiTextDialog(String title, String acceptLabel, String cancelLabel, String... inputs)
149         {
150                 return new MultiTextFieldsDialog(title, acceptLabel, cancelLabel, inputs).open();
151         }
152
153         public static class MultiLineTextFieldDialog extends FlexibleInputsDialog
154         {
155                 private final String input;
156                 private Text textField;
157
158                 public MultiLineTextFieldDialog(String title, String acceptLabel, String cancelLabel, String input)
159                 {
160                         super(true, title, acceptLabel, cancelLabel);
161                         this.input = input;
162                 }
163
164                 @Override
165                 protected void setupWidgets(Composite parent)
166                 {
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);
172                         l.setText(input);
173                         gd.verticalAlignment = SWT.TOP;
174                         l.setLayoutData(gd);
175                         gd = new GridData();
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;
182                 }
183
184                 @Override
185                 protected void buildResult()
186                 {
187                         result = new String[] { textField.getText() };
188                 }
189         }
190
191         /**
192          * @return The String entered if the dialog was accepted, null if the dialog was cancelled.
193          */
194         public static String openMultiLineTextDialog(String title, String acceptLabel, String cancelLabel, String input)
195         {
196                 String[] result = new MultiLineTextFieldDialog(title, acceptLabel, cancelLabel, input).open();
197                 return result == null ? null : result[0];
198         }
199 }