1 /*******************************************************************************
2 * Copyright (c) 2000, 2016 IBM Corporation and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.ui
.handler
;
12 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
13 import org
.eclipse
.jface
.dialogs
.Dialog
;
14 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
15 import org
.eclipse
.jface
.dialogs
.IInputValidator
;
16 import org
.eclipse
.jface
.resource
.StringConverter
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.graphics
.Color
;
19 import org
.eclipse
.swt
.layout
.GridData
;
20 import org
.eclipse
.swt
.widgets
.Button
;
21 import org
.eclipse
.swt
.widgets
.Composite
;
22 import org
.eclipse
.swt
.widgets
.Control
;
23 import org
.eclipse
.swt
.widgets
.Display
;
24 import org
.eclipse
.swt
.widgets
.Label
;
25 import org
.eclipse
.swt
.widgets
.Shell
;
26 import org
.eclipse
.swt
.widgets
.Text
;
29 * Adaptation of {@link InputDialog}, to show the command in a grayed out,
30 * read-only input field, and a editable input field for the user to add extra
33 * @author Alexandre Montplaisir
36 class ParameterDialog
extends Dialog
{
38 private static final Color GRAY_COLOR
= Display
.getCurrent().getSystemColor(SWT
.COLOR_DARK_GRAY
);
40 private String fTitle
;
41 private String fMessage
;
42 private String fValue
= "";//$NON-NLS-1$
43 private IInputValidator fValidator
;
44 private Button fOkButton
;
46 private Text fErrorMessageText
;
47 private String fErrorMessage
;
49 private Text fBaseCommandText
;
50 private final String fBaseCommand
;
52 public ParameterDialog(Shell parentShell
,
56 IInputValidator validator
) {
59 fMessage
= dialogMessage
;
60 fValue
= "";//$NON-NLS-1$
61 fValidator
= validator
;
62 fBaseCommand
= baseCommand
;
66 protected void buttonPressed(int buttonId
) {
67 if (buttonId
== IDialogConstants
.OK_ID
) {
68 fValue
= fText
.getText();
72 super.buttonPressed(buttonId
);
76 protected void configureShell(Shell shell
) {
77 super.configureShell(shell
);
79 shell
.setText(fTitle
);
84 protected void createButtonsForButtonBar(Composite parent
) {
85 fOkButton
= createButton(parent
, IDialogConstants
.OK_ID
,
86 IDialogConstants
.OK_LABEL
, true);
87 createButton(parent
, IDialogConstants
.CANCEL_ID
,
88 IDialogConstants
.CANCEL_LABEL
, false);
91 fText
.setText(fValue
);
97 protected Control
createDialogArea(Composite parent
) {
99 Composite composite
= (Composite
) super.createDialogArea(parent
);
101 Label label
= new Label(composite
, SWT
.WRAP
);
102 label
.setText(Messages
.ParameterDialog_BaseCommand
+ ':');
104 fBaseCommandText
= new Text(composite
, getInputTextStyle() | SWT
.READ_ONLY
);
105 fBaseCommandText
.setLayoutData(new GridData(GridData
.GRAB_HORIZONTAL
106 | GridData
.HORIZONTAL_ALIGN_FILL
));
107 fBaseCommandText
.setText(fBaseCommand
);
108 fBaseCommandText
.setForeground(GRAY_COLOR
);
111 if (fMessage
!= null) {
112 label
= new Label(composite
, SWT
.WRAP
);
113 label
.setText(fMessage
);
114 GridData data
= new GridData(GridData
.GRAB_HORIZONTAL
115 | GridData
.GRAB_VERTICAL
| GridData
.HORIZONTAL_ALIGN_FILL
116 | GridData
.VERTICAL_ALIGN_CENTER
);
117 data
.widthHint
= convertHorizontalDLUsToPixels(IDialogConstants
.MINIMUM_MESSAGE_AREA_WIDTH
);
118 label
.setLayoutData(data
);
119 label
.setFont(parent
.getFont());
124 fText
= new Text(composite
, getInputTextStyle());
125 fText
.setLayoutData(new GridData(GridData
.GRAB_HORIZONTAL
126 | GridData
.HORIZONTAL_ALIGN_FILL
));
127 fText
.addModifyListener(e
-> validateInput());
128 fErrorMessageText
= new Text(composite
, SWT
.READ_ONLY
| SWT
.WRAP
);
129 fErrorMessageText
.setLayoutData(new GridData(GridData
.GRAB_HORIZONTAL
130 | GridData
.HORIZONTAL_ALIGN_FILL
));
131 fErrorMessageText
.setBackground(fErrorMessageText
.getDisplay()
132 .getSystemColor(SWT
.COLOR_WIDGET_BACKGROUND
));
133 setErrorMessage(fErrorMessage
);
135 applyDialogFont(composite
);
140 * Returns the ok button.
142 * @return the ok button
144 protected Button
getOkButton() {
149 * Returns the string typed into this input dialog.
151 * @return the input string
153 public String
getValue() {
158 * Validates the input.
160 * The default implementation of this framework method delegates the request
161 * to the supplied input validator object; if it finds the input invalid,
162 * the error message is displayed in the dialog's message line. This hook
163 * method is called whenever the text changes in the input field.
166 protected void validateInput() {
167 String errMsg
= null;
168 if (fValidator
!= null) {
169 errMsg
= fValidator
.isValid(fText
.getText());
171 setErrorMessage(errMsg
);
175 * Sets or clears the error message.
176 * If not <code>null</code>, the OK button is disabled.
178 * @param errorMessage
179 * the error message, or <code>null</code> to clear
181 private void setErrorMessage(String errorMessage
) {
182 this.fErrorMessage
= errorMessage
;
183 if (fErrorMessageText
!= null && !fErrorMessageText
.isDisposed()) {
184 fErrorMessageText
.setText(errorMessage
== null ?
" \n " : errorMessage
); //$NON-NLS-1$
185 boolean hasError
= errorMessage
!= null && (StringConverter
.removeWhiteSpaces(errorMessage
)).length() > 0;
186 fErrorMessageText
.setEnabled(hasError
);
187 fErrorMessageText
.setVisible(hasError
);
188 fErrorMessageText
.getParent().update();
189 Control button
= getButton(IDialogConstants
.OK_ID
);
190 if (button
!= null) {
191 button
.setEnabled(errorMessage
== null);
197 * Returns the style bits that should be used for the input text field.
198 * Defaults to a single line entry. Subclasses may override.
200 * @return the integer style bits that should be used when creating the
203 protected int getInputTextStyle() {
204 return SWT
.SINGLE
| SWT
.BORDER
;