Merge remote-tracking branch 'origin/development' into development
[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                 protected abstract void buildResult();
53
54                 /**
55                  * @return May be null (if {@link Dialog} was cancelled)
56                  */
57                 public String[] open()
58                 {
59                         Shell shell = getParent();
60                         Display display = shell.getDisplay();
61                         shell.setMinimumSize(500, 150);
62                         shell.setText(title);
63                         GridLayout layout = new GridLayout();
64                         layout.numColumns = 2;
65                         shell.setLayout(layout);
66
67                         Composite inputContainer = new Composite(shell, SWT.BORDER);
68                         GridData gd = new GridData();
69                         gd.horizontalSpan = 2;
70                         gd.horizontalAlignment = SWT.FILL;
71                         gd.grabExcessHorizontalSpace = true;
72                         gd.verticalAlignment = SWT.FILL;
73                         gd.grabExcessVerticalSpace = true;
74                         inputContainer.setLayoutData(gd);
75                         setupWidgets(inputContainer);
76                         
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 was cancelled.
146          */
147         public static String[] openMultiTextDialog(String title, String acceptLabel, String cancelLabel, String... inputs)
148         {
149                 return new MultiTextFieldsDialog(title, acceptLabel, cancelLabel, inputs).open();
150         }
151         
152         public static class MultiLineTextFieldDialog extends FlexibleInputsDialog
153         {
154                 private final String input;
155                 private Text textField;
156                 
157                 public MultiLineTextFieldDialog(String title, String acceptLabel, String cancelLabel, String input)
158                 {
159                         super(true, title, acceptLabel, cancelLabel);
160                         this.input = input;
161                 }
162                 
163                 @Override
164                 protected void setupWidgets(Composite parent)
165                 {
166                         GridLayout layout = new GridLayout();
167                         layout.numColumns = 2;
168                         parent.setLayout(layout);
169                         GridData gd = new GridData();
170                         Label l = new Label(parent, SWT.NONE);
171                         l.setText(input);
172                         gd.verticalAlignment = SWT.TOP;
173                         l.setLayoutData(gd);
174                         gd = new GridData();
175                         textField = new Text(parent, SWT.V_SCROLL);
176                         textField.setLayoutData(gd);
177                         gd.grabExcessHorizontalSpace = true;
178                         gd.grabExcessVerticalSpace = true;
179                         gd.horizontalAlignment = SWT.FILL;
180                         gd.verticalAlignment = SWT.FILL;
181                 }
182
183                 @Override
184                 protected void buildResult()
185                 {
186                         result = new String[] { textField.getText() };
187                 }       
188         }
189
190         /**
191          * @return The String entered if the dialog was accepted, null if the dialog was cancelled.
192          */
193         public static String openMultiLineTextDialog(String title, String acceptLabel, String cancelLabel, String input)
194         {
195                 String[] result = new MultiLineTextFieldDialog(title, acceptLabel, cancelLabel, input).open();
196                 return result == null ? null : result[0];
197         }
198 }