]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/SingleValue.java
[maven-release-plugin] prepare release argeo-commons-2.1.35
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / 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.argeo.eclipse.ui.EclipseUiUtils;
19 import org.eclipse.jface.dialogs.IMessageProvider;
20 import org.eclipse.jface.dialogs.TitleAreaDialog;
21 import org.eclipse.jface.window.Window;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.swt.widgets.Text;
32
33 /** Dialog retrieve a single value. */
34 public class SingleValue extends TitleAreaDialog {
35 private static final long serialVersionUID = 2843538207460082349L;
36
37 private Text valueT;
38 private String value;
39 private final String title, message, label;
40 private final Boolean multiline;
41
42 public static String ask(String label, String message) {
43 SingleValue svd = new SingleValue(label, message);
44 if (svd.open() == Window.OK)
45 return svd.getString();
46 else
47 return null;
48 }
49
50 public static Long askLong(String label, String message) {
51 SingleValue svd = new SingleValue(label, message);
52 if (svd.open() == Window.OK)
53 return svd.getLong();
54 else
55 return null;
56 }
57
58 public static Double askDouble(String label, String message) {
59 SingleValue svd = new SingleValue(label, message);
60 if (svd.open() == Window.OK)
61 return svd.getDouble();
62 else
63 return null;
64 }
65
66 public SingleValue(String label, String message) {
67 this(Display.getDefault().getActiveShell(), label, message, label,
68 false);
69 }
70
71 public SingleValue(Shell parentShell, String title, String message,
72 String label, Boolean multiline) {
73 super(parentShell);
74 this.title = title;
75 this.message = message;
76 this.label = label;
77 this.multiline = multiline;
78 }
79
80 protected Point getInitialSize() {
81 if (multiline)
82 return new Point(450, 350);
83
84 else
85 return new Point(400, 270);
86 }
87
88 protected Control createDialogArea(Composite parent) {
89 Composite dialogarea = (Composite) super.createDialogArea(parent);
90 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
91 Composite composite = new Composite(dialogarea, SWT.NONE);
92 composite.setLayoutData(EclipseUiUtils.fillAll());
93 GridLayout layout = new GridLayout(2, false);
94 layout.marginWidth = layout.marginHeight = 20;
95 composite.setLayout(layout);
96
97 valueT = createLT(composite, label);
98
99 setMessage(message, IMessageProvider.NONE);
100
101 parent.pack();
102 return composite;
103 }
104
105 @Override
106 protected void okPressed() {
107 value = valueT.getText();
108 super.okPressed();
109 }
110
111 /** Creates label and text. */
112 protected Text createLT(Composite parent, String label) {
113 new Label(parent, SWT.NONE).setText(label);
114 Text text;
115 if (multiline) {
116 text = new Text(parent, SWT.LEAD | SWT.BORDER | SWT.MULTI);
117 text.setLayoutData(EclipseUiUtils.fillAll());
118 } else {
119 text = new Text(parent, SWT.LEAD | SWT.BORDER | SWT.SINGLE);
120 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
121 }
122 return text;
123 }
124
125 protected void configureShell(Shell shell) {
126 super.configureShell(shell);
127 shell.setText(title);
128 }
129
130 public String getString() {
131 return value;
132 }
133
134 public Long getLong() {
135 return Long.valueOf(getString());
136 }
137
138 public Double getDouble() {
139 return Double.valueOf(getString());
140 }
141 }