1 package net.mograsim.logic.model.editor.ui;
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;
13 import net.mograsim.logic.model.editor.Editor;
15 public class DialogManager
19 public DialogManager(Shell parent)
24 public void openWarningDialog(String title, String message)
26 MessageBox b = new MessageBox(parent, SWT.ICON_WARNING | SWT.OK);
28 b.setMessage(message);
32 public static class InteractiveDialog
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;
41 public InteractiveDialog(String title, String acceptLabel, String cancelLabel, String... inputs)
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);
47 GridLayout layout = new GridLayout();
48 layout.numColumns = 2;
49 shell.setLayout(layout);
51 this.textFields = new Text[inputs.length];
52 for (int i = 0; i < inputs.length; i++)
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;
63 b1 = new Button(shell, SWT.PUSH);
64 b1.addListener(SWT.Selection, e ->
66 state = InteractiveDialogState.ACCEPTED;
70 b1.setText(acceptLabel);
71 b2 = new Button(shell, SWT.PUSH);
72 b2.addListener(SWT.Selection, e ->
74 state = InteractiveDialogState.CANCELLED;
78 b2.setText(cancelLabel);
80 state = InteractiveDialogState.ACTIVE;
85 public String getText()
90 public String getText(int index)
92 if (!shell.isDisposed())
93 return textFields[index].getText();
95 return finalInput[index];
101 while (!shell.isDisposed())
102 if (!display.readAndDispatch())
106 public void dispose()
111 public InteractiveDialog.InteractiveDialogState getState()
116 private void buildFinalInput()
118 finalInput = new String[textFields.length];
119 for (int i = 0; i < textFields.length; i++)
120 finalInput[i] = textFields[i].getText();
123 public static enum InteractiveDialogState
125 ACTIVE, ACCEPTED, CANCELLED;
129 public static void openAddPinDialog(Editor editor, double x, double y)