]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/dialogs/SingleValue.java
b58f44694e18f084290c191b74fb34f777c0fd5d
[lgpl/argeo-commons.git] / base / runtime / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / ui / dialogs / SingleValue.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.eclipse.ui.dialogs;
17
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.dialogs.IMessageProvider;
20 import org.eclipse.jface.dialogs.TitleAreaDialog;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.swt.widgets.Text;
31
32 /** Dialog retrieve a single value. */
33 public class SingleValue extends TitleAreaDialog {
34 private static final long serialVersionUID = 2843538207460082349L;
35
36 private Text valueT;
37 private String value;
38 private final String title, message, label;
39 private final Boolean multiline;
40
41 public static String ask(String label, String message) {
42 SingleValue svd = new SingleValue(label, message);
43 if (svd.open() == Dialog.OK)
44 return svd.getString();
45 else
46 return null;
47 }
48
49 public static Long askLong(String label, String message) {
50 SingleValue svd = new SingleValue(label, message);
51 if (svd.open() == Dialog.OK)
52 return svd.getLong();
53 else
54 return null;
55 }
56
57 public static Double askDouble(String label, String message) {
58 SingleValue svd = new SingleValue(label, message);
59 if (svd.open() == Dialog.OK)
60 return svd.getDouble();
61 else
62 return null;
63 }
64
65 public SingleValue(String label, String message) {
66 this(Display.getDefault().getActiveShell(), label, message, label,
67 false);
68 }
69
70 public SingleValue(Shell parentShell, String title, String message,
71 String label, Boolean multiline) {
72 super(parentShell);
73 this.title = title;
74 this.message = message;
75 this.label = label;
76 this.multiline = multiline;
77 }
78
79 protected Point getInitialSize() {
80 return new Point(300, 250);
81 }
82
83 protected Control createDialogArea(Composite parent) {
84 Composite dialogarea = (Composite) super.createDialogArea(parent);
85 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
86 Composite composite = new Composite(dialogarea, SWT.NONE);
87 composite.setLayout(new GridLayout(2, false));
88 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
89 valueT = createLT(composite, label);
90
91 setMessage(message, IMessageProvider.NONE);
92
93 parent.pack();
94 return composite;
95 }
96
97 @Override
98 protected void okPressed() {
99 value = valueT.getText();
100 super.okPressed();
101 }
102
103 /** Creates label and text. */
104 protected Text createLT(Composite parent, String label) {
105 new Label(parent, SWT.NONE).setText(label);
106 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
107 | (multiline ? SWT.MULTI : SWT.NONE));
108 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
109 return text;
110 }
111
112 protected void configureShell(Shell shell) {
113 super.configureShell(shell);
114 shell.setText(title);
115 }
116
117 public String getString() {
118 return value;
119 }
120
121 public Long getLong() {
122 return Long.valueOf(getString());
123 }
124
125 public Double getDouble() {
126 return Double.valueOf(getString());
127 }
128 }