tmf: Update copyright headers in tmf.ui
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / dialogs / MultiLineInputDialog.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.tmf.ui.dialogs;
14
15 import org.eclipse.jface.dialogs.IDialogConstants;
16 import org.eclipse.jface.dialogs.IInputValidator;
17 import org.eclipse.jface.dialogs.InputDialog;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.KeyAdapter;
20 import org.eclipse.swt.events.KeyEvent;
21 import org.eclipse.swt.events.MouseAdapter;
22 import org.eclipse.swt.events.MouseEvent;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.graphics.Rectangle;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.swt.widgets.Text;
31
32 /**
33 * A simple input dialog for soliciting an input string from the user.
34 *
35 * Overrides InputDialog to support multiple line text input.
36 *
37 * @author Patrick Tassé
38 */
39 public class MultiLineInputDialog extends InputDialog {
40
41 private final String dialogMessage;
42
43 /* flag to indicate if CR can be used to submit the dialog */
44 private boolean submitOnCR = true;
45
46 /**
47 * Creates a multi line input dialog.
48 *
49 * @param parentShell
50 * the parent shell, or <code>null</code> to create a top-level shell
51 * @param dialogTitle
52 * the dialog title, or <code>null</code> if none
53 * @param dialogMessage
54 * the dialog message, or <code>null</code> if none
55 * @param initialValue
56 * the initial input value, or <code>null</code> if none (equivalent to the empty string)
57 * @param validator
58 * an input validator, or <code>null</code> if none
59 */
60 public MultiLineInputDialog(Shell parentShell, String dialogTitle,
61 String dialogMessage, String initialValue, IInputValidator validator) {
62 super(parentShell, dialogTitle, null, initialValue, validator);
63 this.dialogMessage = dialogMessage;
64 }
65
66 /**
67 * Creates a multi line input dialog with a not-empty text validator.
68 *
69 * @param parentShell
70 * the parent shell, or <code>null</code> to create a top-level shell
71 * @param dialogTitle
72 * the dialog title, or <code>null</code> if none
73 * @param dialogMessage
74 * the dialog message, or <code>null</code> if none
75 * @param initialValue
76 * the initial input value, or <code>null</code> if none (equivalent to the empty string)
77 */
78 public MultiLineInputDialog(Shell parentShell, String dialogTitle,
79 String dialogMessage, String initialValue) {
80 super(parentShell, dialogTitle, null, initialValue, new NotEmptyValidator());
81 this.dialogMessage = dialogMessage;
82 }
83
84 /* (non-Javadoc)
85 * @see org.eclipse.jface.dialogs.InputDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
86 */
87 @Override
88 protected Control createDialogArea(Composite parent) {
89 Composite composite = (Composite) super.createDialogArea(parent);
90 final Text text = getText();
91
92 /* create dialog message label here instead because default implementation uses GRAB_VERTICAL */
93 if (dialogMessage != null) {
94 Label label = new Label(composite, SWT.WRAP);
95 label.setText(dialogMessage);
96 GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL
97 | GridData.VERTICAL_ALIGN_CENTER);
98 data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
99 label.setLayoutData(data);
100 label.setFont(parent.getFont());
101 label.moveAbove(text);
102 }
103
104 /* modify text layout data here because default implementation doesn't fill vertically */
105 GridData gridData = new GridData(GridData.FILL_BOTH);
106 gridData.widthHint = convertHorizontalDLUsToPixels(250);
107 gridData.heightHint = convertHeightInCharsToPixels(3);
108 text.setLayoutData(gridData);
109
110 text.addKeyListener(new KeyAdapter() {
111 @Override
112 public void keyPressed(KeyEvent e) {
113 if (e.character == SWT.CR) {
114 if (submitOnCR) {
115 /* submit the dialog */
116 e.doit = false;
117 okPressed();
118 return;
119 }
120 } else if (e.character == SWT.TAB) {
121 /* don't insert a tab character in the text */
122 e.doit = false;
123 text.traverse(SWT.TRAVERSE_TAB_NEXT);
124 }
125 /* don't allow CR to submit anymore */
126 submitOnCR = false;
127 }
128 });
129
130 text.addMouseListener(new MouseAdapter() {
131 @Override
132 public void mouseDown(MouseEvent e) {
133 /* don't allow CR to submit anymore */
134 submitOnCR = false;
135 }
136 });
137
138 return composite;
139 }
140
141 /* (non-Javadoc)
142 * @see org.eclipse.jface.dialogs.Dialog#createContents(org.eclipse.swt.widgets.Composite)
143 */
144 @Override
145 protected Control createContents(Composite parent) {
146 Control control = super.createContents(parent);
147
148 /* set the shell minimum size */
149 Point clientArea = control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
150 Rectangle trim = getShell().computeTrim(0, 0, clientArea.x, clientArea.y);
151 getShell().setMinimumSize(trim.width, trim.height);
152
153 return control;
154 }
155
156 /* (non-Javadoc)
157 * @see org.eclipse.jface.dialogs.InputDialog#getInputTextStyle()
158 */
159 @Override
160 protected int getInputTextStyle() {
161 return SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER;
162 }
163
164 /* (non-Javadoc)
165 * @see org.eclipse.jface.dialogs.Dialog#isResizable()
166 */
167 @Override
168 protected boolean isResizable() {
169 return true;
170 }
171
172 private static class NotEmptyValidator implements IInputValidator {
173 @Override
174 public String isValid(String newText) {
175 return (newText == null || newText.trim().length() == 0) ? " " : null; //$NON-NLS-1$
176 }
177 }
178
179 }
This page took 0.037444 seconds and 5 git commands to generate.