1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 EfficiOS Inc., Philippe Proulx
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 *******************************************************************************/
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
.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
;
30 * Dialog to set a name and a command when creating a custom
33 * @author Philippe Proulx
36 class AddAnalysisDialog
extends Dialog
{
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
;
49 public AddAnalysisDialog(Shell parentShell
,
51 IInputValidator nameValidator
,
52 IInputValidator commandValidator
) {
54 this.title
= dialogTitle
;
55 fNameValidator
= nameValidator
;
56 fCommandValidator
= commandValidator
;
60 protected void buttonPressed(int buttonId
) {
61 if (buttonId
== IDialogConstants
.OK_ID
) {
62 fName
= fNameText
.getText();
63 fCommand
= fCommandText
.getText();
68 super.buttonPressed(buttonId
);
72 protected boolean isResizable() {
77 protected void configureShell(Shell shell
) {
78 super.configureShell(shell
);
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);
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
));
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());
109 Font font
= fd
.createFont(parent
.getDisplay());
112 label
.addDisposeListener(e
-> {
114 fd
.destroyFont(font
);
121 protected Control
createDialogArea(Composite parent
) {
123 final Composite composite
= (Composite
) super.createDialogArea(parent
);
125 // create label for name text
126 createSubtitleLabel(composite
, Messages
.AddAnalysisDialog_Name
);
129 fNameText
= new Text(composite
, getInputTextStyle());
130 fNameText
.setLayoutData(new GridData(GridData
.GRAB_HORIZONTAL
131 | GridData
.HORIZONTAL_ALIGN_FILL
));
132 fNameText
.addModifyListener(e
-> validateInputs());
134 // create name error text
135 fNameErrorLabel
= createErrorLabel(composite
);
138 new Label(composite
, SWT
.WRAP
);
140 // create label for command text
141 createSubtitleLabel(composite
, Messages
.AddAnalysisDialog_Command
);
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());
152 // create command error text
153 fCommandErrorLabel
= createErrorLabel(composite
);
155 applyDialogFont(composite
);
160 public void create() {
162 Shell shell
= getShell();
163 shell
.setMinimumSize(shell
.getSize());
167 * Returns the value of the name text.
169 * @return the name text's value
171 public String
getName() {
176 * Returns the value of the command text.
178 * @return the command text's value
180 public String
getCommand() {
184 protected boolean validateInput(IInputValidator validator
, Text text
, Label errorLabel
) {
185 final String errMsg
= validator
.isValid(text
.getText());
186 setErrorLabel(errorLabel
, errMsg
);
188 return errMsg
== null;
191 protected void validateInputs() {
192 boolean valid
= true;
194 valid
&= validateInput(fNameValidator
, fNameText
, fNameErrorLabel
);
195 valid
&= validateInput(fCommandValidator
, fCommandText
, fCommandErrorLabel
);
196 fOkButton
.setEnabled(valid
);
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
);
208 if (button
!= null) {
209 button
.setEnabled(errorMessage
== null);
214 protected int getInputTextStyle() {
215 return SWT
.SINGLE
| SWT
.BORDER
;