]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/SingleValue.java
CMS wizard and error feedback
[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 to 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 valueT.setFocus();
103 return composite;
104 }
105
106 @Override
107 protected void okPressed() {
108 value = valueT.getText();
109 super.okPressed();
110 }
111
112 /** Creates label and text. */
113 protected Text createLT(Composite parent, String label) {
114 new Label(parent, SWT.NONE).setText(label);
115 Text text;
116 if (multiline) {
117 text = new Text(parent, SWT.LEAD | SWT.BORDER | SWT.MULTI);
118 text.setLayoutData(EclipseUiUtils.fillAll());
119 } else {
120 text = new Text(parent, SWT.LEAD | SWT.BORDER | SWT.SINGLE);
121 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
122 }
123 return text;
124 }
125
126 protected void configureShell(Shell shell) {
127 super.configureShell(shell);
128 shell.setText(title);
129 }
130
131 public String getString() {
132 return value;
133 }
134
135 public Long getLong() {
136 return Long.valueOf(getString());
137 }
138
139 public Double getDouble() {
140 return Double.valueOf(getString());
141 }
142 }