Commit | Line | Data |
---|---|---|
4208b510 AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2016 IBM Corporation and others. | |
3 | * | |
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 | *******************************************************************************/ | |
9 | ||
10 | package org.eclipse.tracecompass.internal.provisional.analysis.lami.ui.handler; | |
11 | ||
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; | |
27 | ||
28 | /** | |
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 | |
31 | * parameters. | |
32 | * | |
33 | * @author Alexandre Montplaisir | |
34 | */ | |
35 | @NonNullByDefault({}) | |
36 | class ParameterDialog extends Dialog { | |
37 | ||
38 | private static final Color GRAY_COLOR = Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY); | |
39 | ||
40 | private String fTitle; | |
41 | private String fMessage; | |
42 | private String fValue = "";//$NON-NLS-1$ | |
43 | private IInputValidator fValidator; | |
44 | private Button fOkButton; | |
45 | private Text fText; | |
46 | private Text fErrorMessageText; | |
47 | private String fErrorMessage; | |
48 | ||
49 | private Text fBaseCommandText; | |
50 | private final String fBaseCommand; | |
51 | ||
52 | public ParameterDialog(Shell parentShell, | |
53 | String dialogTitle, | |
54 | String dialogMessage, | |
55 | String baseCommand, | |
56 | IInputValidator validator) { | |
57 | super(parentShell); | |
58 | fTitle = dialogTitle; | |
59 | fMessage = dialogMessage; | |
60 | fValue = "";//$NON-NLS-1$ | |
61 | fValidator = validator; | |
62 | fBaseCommand = baseCommand; | |
63 | } | |
64 | ||
65 | @Override | |
66 | protected void buttonPressed(int buttonId) { | |
67 | if (buttonId == IDialogConstants.OK_ID) { | |
68 | fValue = fText.getText(); | |
69 | } else { | |
70 | fValue = null; | |
71 | } | |
72 | super.buttonPressed(buttonId); | |
73 | } | |
74 | ||
75 | @Override | |
76 | protected void configureShell(Shell shell) { | |
77 | super.configureShell(shell); | |
78 | if (fTitle != null) { | |
79 | shell.setText(fTitle); | |
80 | } | |
81 | } | |
82 | ||
83 | @Override | |
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); | |
89 | fText.setFocus(); | |
90 | if (fValue != null) { | |
91 | fText.setText(fValue); | |
92 | fText.selectAll(); | |
93 | } | |
94 | } | |
95 | ||
96 | @Override | |
97 | protected Control createDialogArea(Composite parent) { | |
98 | // create composite | |
99 | Composite composite = (Composite) super.createDialogArea(parent); | |
100 | ||
101 | Label label = new Label(composite, SWT.WRAP); | |
102 | label.setText(Messages.ParameterDialog_BaseCommand + ':'); | |
103 | ||
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); | |
109 | ||
110 | // create message | |
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()); | |
120 | } | |
121 | ||
122 | ||
123 | ||
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); | |
134 | ||
135 | applyDialogFont(composite); | |
136 | return composite; | |
137 | } | |
138 | ||
139 | /** | |
140 | * Returns the ok button. | |
141 | * | |
142 | * @return the ok button | |
143 | */ | |
144 | protected Button getOkButton() { | |
145 | return fOkButton; | |
146 | } | |
147 | ||
148 | /** | |
149 | * Returns the string typed into this input dialog. | |
150 | * | |
151 | * @return the input string | |
152 | */ | |
153 | public String getValue() { | |
154 | return fValue; | |
155 | } | |
156 | ||
157 | /** | |
158 | * Validates the input. | |
159 | * <p> | |
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. | |
164 | * </p> | |
165 | */ | |
166 | protected void validateInput() { | |
167 | String errMsg = null; | |
168 | if (fValidator != null) { | |
169 | errMsg = fValidator.isValid(fText.getText()); | |
170 | } | |
171 | setErrorMessage(errMsg); | |
172 | } | |
173 | ||
174 | /** | |
175 | * Sets or clears the error message. | |
176 | * If not <code>null</code>, the OK button is disabled. | |
177 | * | |
178 | * @param errorMessage | |
179 | * the error message, or <code>null</code> to clear | |
180 | */ | |
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); | |
192 | } | |
193 | } | |
194 | } | |
195 | ||
196 | /** | |
197 | * Returns the style bits that should be used for the input text field. | |
198 | * Defaults to a single line entry. Subclasses may override. | |
199 | * | |
200 | * @return the integer style bits that should be used when creating the | |
201 | * input text | |
202 | */ | |
203 | protected int getInputTextStyle() { | |
204 | return SWT.SINGLE | SWT.BORDER; | |
205 | } | |
206 | } |