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