ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / dialogs / MultiLineInputDialog.java
CommitLineData
3be2946f 1/*******************************************************************************
c8422608 2 * Copyright (c) 2012, 2013 Ericsson
3be2946f
PT
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
13package org.eclipse.linuxtools.internal.tmf.ui.dialogs;
14
15import org.eclipse.jface.dialogs.IDialogConstants;
16import org.eclipse.jface.dialogs.IInputValidator;
17import org.eclipse.jface.dialogs.InputDialog;
18import org.eclipse.swt.SWT;
19import org.eclipse.swt.events.KeyAdapter;
20import org.eclipse.swt.events.KeyEvent;
21import org.eclipse.swt.events.MouseAdapter;
22import org.eclipse.swt.events.MouseEvent;
23import org.eclipse.swt.graphics.Point;
24import org.eclipse.swt.graphics.Rectangle;
25import org.eclipse.swt.layout.GridData;
26import org.eclipse.swt.widgets.Composite;
27import org.eclipse.swt.widgets.Control;
28import org.eclipse.swt.widgets.Label;
29import org.eclipse.swt.widgets.Shell;
30import 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 *
a0a88f65 37 * @author Patrick Tassé
3be2946f
PT
38 */
39public 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
3be2946f
PT
84 @Override
85 protected Control createDialogArea(Composite parent) {
86 Composite composite = (Composite) super.createDialogArea(parent);
87 final Text text = getText();
88
89 /* create dialog message label here instead because default implementation uses GRAB_VERTICAL */
90 if (dialogMessage != null) {
91 Label label = new Label(composite, SWT.WRAP);
92 label.setText(dialogMessage);
93 GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL
94 | GridData.VERTICAL_ALIGN_CENTER);
95 data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
96 label.setLayoutData(data);
97 label.setFont(parent.getFont());
98 label.moveAbove(text);
99 }
100
101 /* modify text layout data here because default implementation doesn't fill vertically */
102 GridData gridData = new GridData(GridData.FILL_BOTH);
103 gridData.widthHint = convertHorizontalDLUsToPixels(250);
104 gridData.heightHint = convertHeightInCharsToPixels(3);
105 text.setLayoutData(gridData);
106
107 text.addKeyListener(new KeyAdapter() {
108 @Override
109 public void keyPressed(KeyEvent e) {
110 if (e.character == SWT.CR) {
111 if (submitOnCR) {
112 /* submit the dialog */
113 e.doit = false;
114 okPressed();
115 return;
116 }
117 } else if (e.character == SWT.TAB) {
118 /* don't insert a tab character in the text */
119 e.doit = false;
120 text.traverse(SWT.TRAVERSE_TAB_NEXT);
121 }
122 /* don't allow CR to submit anymore */
123 submitOnCR = false;
124 }
125 });
126
127 text.addMouseListener(new MouseAdapter() {
128 @Override
129 public void mouseDown(MouseEvent e) {
130 /* don't allow CR to submit anymore */
131 submitOnCR = false;
132 }
133 });
134
135 return composite;
136 }
137
3be2946f
PT
138 @Override
139 protected Control createContents(Composite parent) {
140 Control control = super.createContents(parent);
141
142 /* set the shell minimum size */
143 Point clientArea = control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
144 Rectangle trim = getShell().computeTrim(0, 0, clientArea.x, clientArea.y);
145 getShell().setMinimumSize(trim.width, trim.height);
146
147 return control;
148 }
149
3be2946f
PT
150 @Override
151 protected int getInputTextStyle() {
152 return SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER;
153 }
154
3be2946f
PT
155 @Override
156 protected boolean isResizable() {
157 return true;
158 }
159
160 private static class NotEmptyValidator implements IInputValidator {
161 @Override
162 public String isValid(String newText) {
163 return (newText == null || newText.trim().length() == 0) ? " " : null; //$NON-NLS-1$
164 }
165 }
166
167}
This page took 0.048423 seconds and 5 git commands to generate.