Commit | Line | Data |
---|---|---|
664dac59 PP |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015, 2016 EfficiOS Inc., Philippe Proulx | |
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 | ||
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.FontDescriptor; | |
17 | import org.eclipse.jface.resource.StringConverter; | |
18 | import org.eclipse.swt.SWT; | |
19 | import org.eclipse.swt.graphics.Color; | |
20 | import org.eclipse.swt.graphics.Font; | |
21 | import org.eclipse.swt.layout.GridData; | |
22 | import org.eclipse.swt.widgets.Button; | |
23 | import org.eclipse.swt.widgets.Composite; | |
24 | import org.eclipse.swt.widgets.Control; | |
25 | import org.eclipse.swt.widgets.Label; | |
26 | import org.eclipse.swt.widgets.Shell; | |
27 | import org.eclipse.swt.widgets.Text; | |
28 | ||
29 | /** | |
30 | * Dialog to set a name and a command when creating a custom | |
31 | * analysis entry. | |
32 | * | |
33 | * @author Philippe Proulx | |
34 | */ | |
35 | @NonNullByDefault({}) | |
36 | class AddAnalysisDialog extends Dialog { | |
37 | ||
38 | private final String title; | |
39 | private String fName = ""; //$NON-NLS-1$ | |
40 | private String fCommand = ""; //$NON-NLS-1$ | |
41 | private final IInputValidator fNameValidator; | |
42 | private final IInputValidator fCommandValidator; | |
43 | private Button fOkButton; | |
44 | private Text fNameText; | |
45 | private Text fCommandText; | |
46 | private Label fNameErrorLabel; | |
47 | private Label fCommandErrorLabel; | |
48 | ||
49 | public AddAnalysisDialog(Shell parentShell, | |
50 | String dialogTitle, | |
51 | IInputValidator nameValidator, | |
52 | IInputValidator commandValidator) { | |
53 | super(parentShell); | |
54 | this.title = dialogTitle; | |
55 | fNameValidator = nameValidator; | |
56 | fCommandValidator = commandValidator; | |
57 | } | |
58 | ||
59 | @Override | |
60 | protected void buttonPressed(int buttonId) { | |
61 | if (buttonId == IDialogConstants.OK_ID) { | |
62 | fName = fNameText.getText(); | |
63 | fCommand = fCommandText.getText(); | |
64 | } else { | |
65 | fName = null; | |
66 | fCommand = null; | |
67 | } | |
68 | super.buttonPressed(buttonId); | |
69 | } | |
70 | ||
71 | @Override | |
72 | protected boolean isResizable() { | |
73 | return true; | |
74 | } | |
75 | ||
76 | @Override | |
77 | protected void configureShell(Shell shell) { | |
78 | super.configureShell(shell); | |
79 | if (title != null) { | |
80 | shell.setText(title); | |
81 | } | |
82 | } | |
83 | ||
84 | @Override | |
85 | protected void createButtonsForButtonBar(Composite parent) { | |
86 | fOkButton = createButton(parent, IDialogConstants.OK_ID, | |
87 | IDialogConstants.OK_LABEL, true); | |
88 | createButton(parent, IDialogConstants.CANCEL_ID, | |
89 | IDialogConstants.CANCEL_LABEL, false); | |
90 | validateInputs(); | |
91 | fNameText.setFocus(); | |
92 | } | |
93 | ||
94 | private static void createSubtitleLabel(Composite parent, String text) { | |
95 | final Label label = new Label(parent, SWT.WRAP); | |
96 | label.setText(text + ':'); | |
97 | final FontDescriptor boldDescriptor = FontDescriptor.createFrom(parent.getFont()).setStyle(SWT.BOLD); | |
98 | final Font boldFont = boldDescriptor.createFont(parent.getDisplay()); | |
99 | label.setFont(boldFont); | |
100 | label.addDisposeListener(event -> boldDescriptor.destroyFont(boldFont)); | |
101 | } | |
102 | ||
103 | private static Label createErrorLabel(Composite parent) { | |
104 | final Label label = new Label(parent, SWT.WRAP); | |
105 | Color color = new Color(parent.getDisplay(), 0xe7, 0x4c, 0x3c); | |
106 | label.setForeground(color); | |
107 | final FontDescriptor fd = FontDescriptor.createFrom(parent.getFont()); | |
664dac59 PP |
108 | Font font = fd.createFont(parent.getDisplay()); |
109 | label.setFont(font); | |
110 | ||
111 | label.addDisposeListener(e -> { | |
112 | color.dispose(); | |
113 | fd.destroyFont(font); | |
114 | }); | |
115 | ||
116 | return label; | |
117 | } | |
118 | ||
119 | @Override | |
120 | protected Control createDialogArea(Composite parent) { | |
121 | // create composite | |
122 | final Composite composite = (Composite) super.createDialogArea(parent); | |
123 | ||
124 | // create label for name text | |
125 | createSubtitleLabel(composite, Messages.AddAnalysisDialog_Name); | |
126 | ||
127 | // create name text | |
128 | fNameText = new Text(composite, getInputTextStyle()); | |
129 | fNameText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | |
130 | | GridData.HORIZONTAL_ALIGN_FILL)); | |
131 | fNameText.addModifyListener(e -> validateInputs()); | |
132 | ||
133 | // create name error text | |
134 | fNameErrorLabel = createErrorLabel(composite); | |
135 | ||
136 | // spacer | |
137 | new Label(composite, SWT.WRAP); | |
138 | ||
139 | // create label for command text | |
140 | createSubtitleLabel(composite, Messages.AddAnalysisDialog_Command); | |
141 | ||
142 | // create command text | |
143 | fCommandText = new Text(composite, getInputTextStyle()); | |
144 | fCommandText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | |
145 | | GridData.HORIZONTAL_ALIGN_FILL)); | |
146 | final Font mono = new Font(parent.getDisplay(), "Monospace", 9, SWT.NONE); //$NON-NLS-1$ | |
147 | fCommandText.setFont(mono); | |
148 | fCommandText.addModifyListener(e -> validateInputs()); | |
149 | fCommandText.addDisposeListener(e -> mono.dispose()); | |
150 | ||
151 | // create command error text | |
152 | fCommandErrorLabel = createErrorLabel(composite); | |
153 | ||
154 | applyDialogFont(composite); | |
155 | return composite; | |
156 | } | |
157 | ||
158 | @Override | |
159 | public void create() { | |
160 | super.create(); | |
161 | Shell shell = getShell(); | |
162 | shell.setMinimumSize(shell.getSize()); | |
163 | } | |
164 | ||
165 | /** | |
166 | * Returns the value of the name text. | |
167 | * | |
168 | * @return the name text's value | |
169 | */ | |
170 | public String getName() { | |
171 | return fName; | |
172 | } | |
173 | ||
174 | /** | |
175 | * Returns the value of the command text. | |
176 | * | |
177 | * @return the command text's value | |
178 | */ | |
179 | public String getCommand() { | |
180 | return fCommand; | |
181 | } | |
182 | ||
183 | protected boolean validateInput(IInputValidator validator, Text text, Label errorLabel) { | |
184 | final String errMsg = validator.isValid(text.getText()); | |
185 | setErrorLabel(errorLabel, errMsg); | |
186 | ||
187 | return errMsg == null; | |
188 | } | |
189 | ||
190 | protected void validateInputs() { | |
191 | boolean valid = true; | |
192 | ||
193 | valid &= validateInput(fNameValidator, fNameText, fNameErrorLabel); | |
194 | valid &= validateInput(fCommandValidator, fCommandText, fCommandErrorLabel); | |
195 | fOkButton.setEnabled(valid); | |
196 | } | |
197 | ||
198 | protected void setErrorLabel(Label label, String errorMessage) { | |
199 | if (label != null && !label.isDisposed()) { | |
200 | label.setText(errorMessage == null ? " \n " : errorMessage); //$NON-NLS-1$ | |
201 | final boolean hasError = errorMessage != null && (StringConverter.removeWhiteSpaces(errorMessage)).length() > 0; | |
202 | label.setEnabled(hasError); | |
203 | label.setVisible(hasError); | |
204 | label.getParent().update(); | |
205 | Control button = getButton(IDialogConstants.OK_ID); | |
206 | ||
207 | if (button != null) { | |
208 | button.setEnabled(errorMessage == null); | |
209 | } | |
210 | } | |
211 | } | |
212 | ||
213 | protected int getInputTextStyle() { | |
214 | return SWT.SINGLE | SWT.BORDER; | |
215 | } | |
216 | } |