View Javadoc
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.connect.ui.widgets;
17  
18  import org.argeo.eclipse.ui.EclipseUiUtils;
19  import org.eclipse.jface.dialogs.TrayDialog;
20  import org.eclipse.jface.window.Window;
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 to retrieve a single value. */
33  public class SingleQuestion extends TrayDialog {
34  	private static final long serialVersionUID = 2843538207460082349L;
35  
36  	private Text valueT;
37  	private String value;
38  	private final String title, message, defaultValue;
39  	private final Boolean multiline;
40  
41  	public static String ask(String title, String message) {
42  		SingleQuestion svd = new SingleQuestion(title, message, null);
43  		if (svd.open() == Window.OK)
44  			return svd.getString();
45  		else
46  			return null;
47  	}
48  
49  	public static String ask(String title, String message, String defaultValue) {
50  		SingleQuestion svd = new SingleQuestion(title, message, defaultValue);
51  		if (svd.open() == Window.OK)
52  			return svd.getString();
53  		else
54  			return null;
55  	}
56  
57  	public SingleQuestion(String title, String message, String defaultValue) {
58  		this(Display.getCurrent().getActiveShell(), title, message, defaultValue, false);
59  	}
60  
61  	public SingleQuestion(Shell parentShell, String title, String message, String defaultValue, Boolean multiline) {
62  		super(parentShell);
63  		this.title = title;
64  		this.message = message;
65  		this.defaultValue = defaultValue;
66  		this.multiline = multiline;
67  	}
68  
69  	protected Point getInitialSize() {
70  		if (multiline)
71  			return new Point(450, 350);
72  		else
73  			return new Point(400, 200);
74  	}
75  
76  	protected Control createDialogArea(Composite parent) {
77  		Composite dialogarea = (Composite) super.createDialogArea(parent);
78  		dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
79  		Composite composite = new Composite(dialogarea, SWT.NONE);
80  		composite.setLayoutData(EclipseUiUtils.fillAll());
81  		GridLayout layout = new GridLayout();
82  		// layout.marginWidth = layout.marginHeight = 20;
83  		composite.setLayout(layout);
84  
85  		valueT = createLT(composite, message);
86  		if (EclipseUiUtils.notEmpty(defaultValue))
87  			valueT.setText(defaultValue);
88  
89  		parent.pack();
90  		valueT.setFocus();
91  		return composite;
92  	}
93  
94  	@Override
95  	protected void okPressed() {
96  		value = valueT.getText();
97  		super.okPressed();
98  	}
99  
100 	/** Creates label and text. */
101 	protected Text createLT(Composite parent, String label) {
102 		new Label(parent, SWT.NONE).setText(label);
103 		Text text;
104 		if (multiline) {
105 			text = new Text(parent, SWT.LEAD | SWT.BORDER | SWT.MULTI);
106 			text.setLayoutData(EclipseUiUtils.fillAll());
107 		} else {
108 			text = new Text(parent, SWT.LEAD | SWT.BORDER | SWT.SINGLE);
109 			text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
110 		}
111 		return text;
112 	}
113 
114 	protected void configureShell(Shell shell) {
115 		super.configureShell(shell);
116 		shell.setText(title);
117 	}
118 
119 	public String getString() {
120 		return value;
121 	}
122 }