Implement MultiLineInputDialog for Add Bookmark action.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / dialogs / MultiLineInputDialog.java
1 /*******************************************************************************
2 * Copyright (c) 2012 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 Tasse
38 *
39 */
40 public class MultiLineInputDialog extends InputDialog {
41
42 private final String dialogMessage;
43
44 /* flag to indicate if CR can be used to submit the dialog */
45 private boolean submitOnCR = true;
46
47 /**
48 * Creates a multi line input dialog.
49 *
50 * @param parentShell
51 * the parent shell, or <code>null</code> to create a top-level shell
52 * @param dialogTitle
53 * the dialog title, or <code>null</code> if none
54 * @param dialogMessage
55 * the dialog message, or <code>null</code> if none
56 * @param initialValue
57 * the initial input value, or <code>null</code> if none (equivalent to the empty string)
58 * @param validator
59 * an input validator, or <code>null</code> if none
60 */
61 public MultiLineInputDialog(Shell parentShell, String dialogTitle,
62 String dialogMessage, String initialValue, IInputValidator validator) {
63 super(parentShell, dialogTitle, null, initialValue, validator);
64 this.dialogMessage = dialogMessage;
65 }
66
67 /**
68 * Creates a multi line input dialog with a not-empty text validator.
69 *
70 * @param parentShell
71 * the parent shell, or <code>null</code> to create a top-level shell
72 * @param dialogTitle
73 * the dialog title, or <code>null</code> if none
74 * @param dialogMessage
75 * the dialog message, or <code>null</code> if none
76 * @param initialValue
77 * the initial input value, or <code>null</code> if none (equivalent to the empty string)
78 */
79 public MultiLineInputDialog(Shell parentShell, String dialogTitle,
80 String dialogMessage, String initialValue) {
81 super(parentShell, dialogTitle, null, initialValue, new NotEmptyValidator());
82 this.dialogMessage = dialogMessage;
83 }
84
85 /* (non-Javadoc)
86 * @see org.eclipse.jface.dialogs.InputDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
87 */
88 @Override
89 protected Control createDialogArea(Composite parent) {
90 Composite composite = (Composite) super.createDialogArea(parent);
91 final Text text = getText();
92
93 /* create dialog message label here instead because default implementation uses GRAB_VERTICAL */
94 if (dialogMessage != null) {
95 Label label = new Label(composite, SWT.WRAP);
96 label.setText(dialogMessage);
97 GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL
98 | GridData.VERTICAL_ALIGN_CENTER);
99 data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
100 label.setLayoutData(data);
101 label.setFont(parent.getFont());
102 label.moveAbove(text);
103 }
104
105 /* modify text layout data here because default implementation doesn't fill vertically */
106 GridData gridData = new GridData(GridData.FILL_BOTH);
107 gridData.widthHint = convertHorizontalDLUsToPixels(250);
108 gridData.heightHint = convertHeightInCharsToPixels(3);
109 text.setLayoutData(gridData);
110
111 text.addKeyListener(new KeyAdapter() {
112 @Override
113 public void keyPressed(KeyEvent e) {
114 if (e.character == SWT.CR) {
115 if (submitOnCR) {
116 /* submit the dialog */
117 e.doit = false;
118 okPressed();
119 return;
120 }
121 } else if (e.character == SWT.TAB) {
122 /* don't insert a tab character in the text */
123 e.doit = false;
124 text.traverse(SWT.TRAVERSE_TAB_NEXT);
125 }
126 /* don't allow CR to submit anymore */
127 submitOnCR = false;
128 }
129 });
130
131 text.addMouseListener(new MouseAdapter() {
132 @Override
133 public void mouseDown(MouseEvent e) {
134 /* don't allow CR to submit anymore */
135 submitOnCR = false;
136 }
137 });
138
139 return composite;
140 }
141
142 /* (non-Javadoc)
143 * @see org.eclipse.jface.dialogs.Dialog#createContents(org.eclipse.swt.widgets.Composite)
144 */
145 @Override
146 protected Control createContents(Composite parent) {
147 Control control = super.createContents(parent);
148
149 /* set the shell minimum size */
150 Point clientArea = control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
151 Rectangle trim = getShell().computeTrim(0, 0, clientArea.x, clientArea.y);
152 getShell().setMinimumSize(trim.width, trim.height);
153
154 return control;
155 }
156
157 /* (non-Javadoc)
158 * @see org.eclipse.jface.dialogs.InputDialog#getInputTextStyle()
159 */
160 @Override
161 protected int getInputTextStyle() {
162 return SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER;
163 }
164
165 /* (non-Javadoc)
166 * @see org.eclipse.jface.dialogs.Dialog#isResizable()
167 */
168 @Override
169 protected boolean isResizable() {
170 return true;
171 }
172
173 private static class NotEmptyValidator implements IInputValidator {
174 @Override
175 public String isValid(String newText) {
176 return (newText == null || newText.trim().length() == 0) ? " " : null; //$NON-NLS-1$
177 }
178 }
179
180 }
This page took 0.033695 seconds and 5 git commands to generate.