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.Display;
8 import org.eclipse.swt.widgets.Label;
9 import org.eclipse.swt.widgets.MessageBox;
10 import org.eclipse.swt.widgets.Shell;
11 import org.eclipse.swt.widgets.Text;
12
13 import net.mograsim.logic.model.editor.Editor;
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         public static class InteractiveDialog
33         {
34                 private String[] finalInput;
35                 private final Display display;
36                 private final Shell shell;
37                 private final Button b1, b2;
38                 private Text[] textFields;
39                 private InteractiveDialog.InteractiveDialogState state;
40
41                 public InteractiveDialog(String title, String acceptLabel, String cancelLabel, String... inputs)
42                 {
43                         display = Display.getDefault();
44                         shell = new Shell(SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.ON_TOP | SWT.APPLICATION_MODAL);
45                         shell.setMinimumSize(500, 150);
46                         shell.setText(title);
47                         GridLayout layout = new GridLayout();
48                         layout.numColumns = 2;
49                         shell.setLayout(layout);
50
51                         this.textFields = new Text[inputs.length];
52                         for (int i = 0; i < inputs.length; i++)
53                         {
54                                 Label textFieldName = new Label(shell, SWT.NONE);
55                                 textFieldName.setText(inputs[i].concat(":"));
56                                 GridData g = new GridData();
57                                 g.grabExcessHorizontalSpace = true;
58                                 g.horizontalAlignment = SWT.FILL;
59                                 Text newTextField = new Text(shell, SWT.BORDER);
60                                 newTextField.setLayoutData(g);
61                                 textFields[i] = newTextField;
62                         }
63                         b1 = new Button(shell, SWT.PUSH);
64                         b1.addListener(SWT.Selection, e ->
65                         {
66                                 state = InteractiveDialogState.ACCEPTED;
67                                 buildFinalInput();
68                                 dispose();
69                         });
70                         b1.setText(acceptLabel);
71                         b2 = new Button(shell, SWT.PUSH);
72                         b2.addListener(SWT.Selection, e ->
73                         {
74                                 state = InteractiveDialogState.CANCELLED;
75                                 buildFinalInput();
76                                 dispose();
77                         });
78                         b2.setText(cancelLabel);
79
80                         state = InteractiveDialogState.ACTIVE;
81
82                         shell.pack();
83                 }
84
85                 public String getText()
86                 {
87                         return getText(0);
88                 }
89
90                 public String getText(int index)
91                 {
92                         if (!shell.isDisposed())
93                                 return textFields[index].getText();
94                         else
95                                 return finalInput[index];
96                 }
97
98                 public void open()
99                 {
100                         shell.open();
101                         while (!shell.isDisposed())
102                                 if (!display.readAndDispatch())
103                                         display.sleep();
104                 }
105
106                 public void dispose()
107                 {
108                         shell.dispose();
109                 }
110
111                 public InteractiveDialog.InteractiveDialogState getState()
112                 {
113                         return state;
114                 }
115
116                 private void buildFinalInput()
117                 {
118                         finalInput = new String[textFields.length];
119                         for (int i = 0; i < textFields.length; i++)
120                                 finalInput[i] = textFields[i].getText();
121                 }
122
123                 public static enum InteractiveDialogState
124                 {
125                         ACTIVE, ACCEPTED, CANCELLED;
126                 }
127         }
128         
129         public static void openAddPinDialog(Editor editor, double x, double y)
130         {
131                 
132         }
133 }