]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/dialogs/SingleValue.java
c15a76736528b0fdd849b892071923e05e3507c1
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / ui / dialogs / SingleValue.java
1 package org.argeo.eclipse.ui.dialogs;
2
3 import org.eclipse.jface.dialogs.Dialog;
4 import org.eclipse.jface.dialogs.IMessageProvider;
5 import org.eclipse.jface.dialogs.TitleAreaDialog;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.graphics.Point;
8 import org.eclipse.swt.layout.GridData;
9 import org.eclipse.swt.layout.GridLayout;
10 import org.eclipse.swt.widgets.Composite;
11 import org.eclipse.swt.widgets.Control;
12 import org.eclipse.swt.widgets.Display;
13 import org.eclipse.swt.widgets.Label;
14 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.swt.widgets.Text;
16
17 /** Dialog retrieve a single value. */
18 public class SingleValue extends TitleAreaDialog {
19 private Text valueT;
20 private String value;
21 private final String title, message, label;
22 private final Boolean multiline;
23
24 public static String ask(String label, String message) {
25 SingleValue svd = new SingleValue(label, message);
26 if (svd.open() == Dialog.OK)
27 return svd.getString();
28 else
29 return null;
30 }
31
32 public static Long askLong(String label, String message) {
33 SingleValue svd = new SingleValue(label, message);
34 if (svd.open() == Dialog.OK)
35 return svd.getLong();
36 else
37 return null;
38 }
39
40 public static Double askDouble(String label, String message) {
41 SingleValue svd = new SingleValue(label, message);
42 if (svd.open() == Dialog.OK)
43 return svd.getDouble();
44 else
45 return null;
46 }
47
48 public SingleValue(String label, String message) {
49 this(Display.getDefault().getActiveShell(), label, message, label,
50 false);
51 }
52
53 public SingleValue(Shell parentShell, String title, String message,
54 String label, Boolean multiline) {
55 super(parentShell);
56 this.title = title;
57 this.message = message;
58 this.label = label;
59 this.multiline = multiline;
60 }
61
62 protected Point getInitialSize() {
63 return new Point(300, 250);
64 }
65
66 protected Control createDialogArea(Composite parent) {
67 Composite dialogarea = (Composite) super.createDialogArea(parent);
68 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
69 Composite composite = new Composite(dialogarea, SWT.NONE);
70 composite.setLayout(new GridLayout(2, false));
71 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
72 valueT = createLT(composite, label);
73
74 setMessage(message, IMessageProvider.NONE);
75 parent.pack();
76 return composite;
77 }
78
79 @Override
80 protected void okPressed() {
81 value = valueT.getText();
82 super.okPressed();
83 }
84
85 /** Creates label and text. */
86 protected Text createLT(Composite parent, String label) {
87 new Label(parent, SWT.NONE).setText(label);
88 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
89 | (multiline ? SWT.MULTI : SWT.NONE));
90 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
91 return text;
92 }
93
94 protected void configureShell(Shell shell) {
95 super.configureShell(shell);
96 shell.setText(title);
97 }
98
99 public String getString() {
100 return value;
101 }
102
103 public Long getLong() {
104 return Long.valueOf(getString());
105 }
106
107 public Double getDouble() {
108 return Double.valueOf(getString());
109 }
110 }