Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 75d6dde2de67770ae6c1938ba022f79c95b9ef93 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package org.eclipse.papyrus.extensionpoints.editors.ui;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.papyrus.extensionpoints.editors.Activator;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

// equivalent au inputDialog (code recopie) mais donne un sourceviewer a la place du label
/**
 *
 */
public class LabelEditorDialog extends Dialog implements ILabelEditorDialog {

	/**
	 * The title of the dialog.
	 */
	protected String title;

	/**
	 * The input value; the empty string by default.
	 */
	protected String value = "";//$NON-NLS-1$

	/**
	 * The input validator, or <code>null</code> if none.
	 */
	protected IInputValidator validator;

	/**
	 * Ok button widget.
	 */
	protected Button okButton;

	/**
	 * Error message label widget.
	 */
	protected CLabel errorMessageText;

	/**
	 * Error message string.
	 */
	protected String errorMessage;

	/**
	 * SourceViewer : area that displays text.
	 */
	protected SourceViewer viewer;

	/**
	 * Creates a dialog with OK and Cancel buttons. Note that the dialog will have no visual
	 * representation (no widgets) until it is told to open.
	 * <p>
	 * Note that the <code>open</code> method blocks for input dialogs.
	 * </p>
	 *
	 * @param dialogTitle
	 *            the dialog title, or <code>null</code> if none
	 * @param validator
	 *            an input validator, or <code>null</code> if none
	 * @param parentShell
	 *            the parent shell, or <code>null</code> to create a top-level shell
	 * @param initialValue
	 *            the initial input value, or <code>null</code> if none (equivalent to the empty
	 *            string)
	 */
	public LabelEditorDialog(Shell parentShell, String dialogTitle, String initialValue, IInputValidator validator) {
		super(parentShell);
		this.title = dialogTitle;
		if (initialValue == null) {
			value = "";//$NON-NLS-1$
		} else {
			value = initialValue;
		}
		this.validator = validator;
	}

	/*
	 * (non-Javadoc) Method declared on Dialog.
	 */
	/**
	 *
	 *
	 * @param buttonId
	 */
	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			value = viewer.getDocument().get();
		} else {
			value = null;
		}
		super.buttonPressed(buttonId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets .Shell)
	 */
	/**
	 *
	 *
	 * @param shell
	 */
	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		if (title != null) {
			shell.setText(title);
		}
	}

	/**
	 * Returns the ok button.
	 *
	 * @return the ok button
	 */
	// @unused
	protected Button getOkButton() {
		return okButton;
	}

	/**
	 * Returns the text area.
	 *
	 * @return the text area
	 */
	// @unused
	protected SourceViewer getSourceViewer() {
		return viewer;
	}

	/**
	 * Returns the validator.
	 *
	 * @return the validator
	 */
	// @unused
	protected IInputValidator getValidator() {
		return validator;
	}

	/**
	 * Returns the string typed into this input dialog.
	 *
	 * @return the input string
	 */
	public String getValue() {
		return value;
	}

	/**
	 * Validates the input.
	 * <p>
	 * The default implementation of this framework method delegates the request to the supplied input validator object; if it finds the input invalid, the error message is displayed in the dialog's message line. This hook method is called whenever the text
	 * changes in the input field.
	 * </p>
	 */
	protected void validateInput() {
		String errorMessage = null;
		if (validator != null) {
			errorMessage = validator.isValid(viewer.getDocument().get());
		}
		// Bug 16256: important not to treat "" (blank error) the same as null
		// (no error)
		setErrorMessage(errorMessage);
	}

	/**
	 * Sets or clears the error message. If not <code>null</code>, the OK button is disabled.
	 *
	 * @param errorMessage
	 *            the error message, or <code>null</code> to clear
	 *
	 * @since 3.0
	 */
	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
		if ((errorMessageText != null) && !errorMessageText.isDisposed()) {
			Image errorImage = Activator.getImage("icons/error.gif");
			errorMessageText.setImage(errorMessage == null ? null : errorImage);
			errorMessageText.setText(errorMessage == null ? "" : errorMessage); //$NON-NLS-1$
			errorMessageText.getParent().update();
			// Access the ok button by id, in case clients have overridden
			// button creation.
			// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=113643
			Control button = getButton(IDialogConstants.OK_ID);
			if (button != null) {
				button.setEnabled(errorMessage == null);
			}
		}
	}
}

Back to the top