0ce1044bce00fa3a38d15c5d0205b0abb8a9f7a4
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.lami.ui / src / org / eclipse / tracecompass / internal / provisional / analysis / lami / ui / handler / AddAnalysisDialog.java
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());
108 fd.setHeight(9);
109 Font font = fd.createFont(parent.getDisplay());
110 label.setFont(font);
111
112 label.addDisposeListener(e -> {
113 color.dispose();
114 fd.destroyFont(font);
115 });
116
117 return label;
118 }
119
120 @Override
121 protected Control createDialogArea(Composite parent) {
122 // create composite
123 final Composite composite = (Composite) super.createDialogArea(parent);
124
125 // create label for name text
126 createSubtitleLabel(composite, Messages.AddAnalysisDialog_Name);
127
128 // create name text
129 fNameText = new Text(composite, getInputTextStyle());
130 fNameText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
131 | GridData.HORIZONTAL_ALIGN_FILL));
132 fNameText.addModifyListener(e -> validateInputs());
133
134 // create name error text
135 fNameErrorLabel = createErrorLabel(composite);
136
137 // spacer
138 new Label(composite, SWT.WRAP);
139
140 // create label for command text
141 createSubtitleLabel(composite, Messages.AddAnalysisDialog_Command);
142
143 // create command text
144 fCommandText = new Text(composite, getInputTextStyle());
145 fCommandText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
146 | GridData.HORIZONTAL_ALIGN_FILL));
147 final Font mono = new Font(parent.getDisplay(), "Monospace", 9, SWT.NONE); //$NON-NLS-1$
148 fCommandText.setFont(mono);
149 fCommandText.addModifyListener(e -> validateInputs());
150 fCommandText.addDisposeListener(e -> mono.dispose());
151
152 // create command error text
153 fCommandErrorLabel = createErrorLabel(composite);
154
155 applyDialogFont(composite);
156 return composite;
157 }
158
159 @Override
160 public void create() {
161 super.create();
162 Shell shell = getShell();
163 shell.setMinimumSize(shell.getSize());
164 }
165
166 /**
167 * Returns the value of the name text.
168 *
169 * @return the name text's value
170 */
171 public String getName() {
172 return fName;
173 }
174
175 /**
176 * Returns the value of the command text.
177 *
178 * @return the command text's value
179 */
180 public String getCommand() {
181 return fCommand;
182 }
183
184 protected boolean validateInput(IInputValidator validator, Text text, Label errorLabel) {
185 final String errMsg = validator.isValid(text.getText());
186 setErrorLabel(errorLabel, errMsg);
187
188 return errMsg == null;
189 }
190
191 protected void validateInputs() {
192 boolean valid = true;
193
194 valid &= validateInput(fNameValidator, fNameText, fNameErrorLabel);
195 valid &= validateInput(fCommandValidator, fCommandText, fCommandErrorLabel);
196 fOkButton.setEnabled(valid);
197 }
198
199 protected void setErrorLabel(Label label, String errorMessage) {
200 if (label != null && !label.isDisposed()) {
201 label.setText(errorMessage == null ? " \n " : errorMessage); //$NON-NLS-1$
202 final boolean hasError = errorMessage != null && (StringConverter.removeWhiteSpaces(errorMessage)).length() > 0;
203 label.setEnabled(hasError);
204 label.setVisible(hasError);
205 label.getParent().update();
206 Control button = getButton(IDialogConstants.OK_ID);
207
208 if (button != null) {
209 button.setEnabled(errorMessage == null);
210 }
211 }
212 }
213
214 protected int getInputTextStyle() {
215 return SWT.SINGLE | SWT.BORDER;
216 }
217 }
This page took 0.041688 seconds and 4 git commands to generate.