]> git.argeo.org Git - lgpl/argeo-commons.git/blob - SingleValue.java
5c27b06442924b056fb9bf321fd636ee8bfb3fec
[lgpl/argeo-commons.git] / SingleValue.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 Text valueT;
35 private String value;
36 private final String title, message, label;
37 private final Boolean multiline;
38
39 public static String ask(String label, String message) {
40 SingleValue svd = new SingleValue(label, message);
41 if (svd.open() == Dialog.OK)
42 return svd.getString();
43 else
44 return null;
45 }
46
47 public static Long askLong(String label, String message) {
48 SingleValue svd = new SingleValue(label, message);
49 if (svd.open() == Dialog.OK)
50 return svd.getLong();
51 else
52 return null;
53 }
54
55 public static Double askDouble(String label, String message) {
56 SingleValue svd = new SingleValue(label, message);
57 if (svd.open() == Dialog.OK)
58 return svd.getDouble();
59 else
60 return null;
61 }
62
63 public SingleValue(String label, String message) {
64 this(Display.getDefault().getActiveShell(), label, message, label,
65 false);
66 }
67
68 public SingleValue(Shell parentShell, String title, String message,
69 String label, Boolean multiline) {
70 super(parentShell);
71 this.title = title;
72 this.message = message;
73 this.label = label;
74 this.multiline = multiline;
75 }
76
77 protected Point getInitialSize() {
78 return new Point(300, 250);
79 }
80
81 protected Control createDialogArea(Composite parent) {
82 Composite dialogarea = (Composite) super.createDialogArea(parent);
83 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
84 Composite composite = new Composite(dialogarea, SWT.NONE);
85 composite.setLayout(new GridLayout(2, false));
86 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
87 valueT = createLT(composite, label);
88
89 setMessage(message, IMessageProvider.NONE);
90
91 parent.pack();
92 return composite;
93 }
94
95 @Override
96 protected void okPressed() {
97 value = valueT.getText();
98 super.okPressed();
99 }
100
101 /** Creates label and text. */
102 protected Text createLT(Composite parent, String label) {
103 new Label(parent, SWT.NONE).setText(label);
104 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
105 | (multiline ? SWT.MULTI : SWT.NONE));
106 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
107 return text;
108 }
109
110 protected void configureShell(Shell shell) {
111 super.configureShell(shell);
112 shell.setText(title);
113 }
114
115 public String getString() {
116 return value;
117 }
118
119 public Long getLong() {
120 return Long.valueOf(getString());
121 }
122
123 public Double getDouble() {
124 return Double.valueOf(getString());
125 }
126 }