Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4af18ad307d44959cface6e94ee86fe852ee2045 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*******************************************************************************
 * Copyright (c) 2001, 2005 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.sse.ui.internal.preferences.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wst.sse.ui.internal.SSEUIMessages;
import org.eclipse.wst.sse.ui.internal.preferences.OverlayPreferenceStore;

/**
 * Generic preference tab page that contains helpful methods
 * 
 * @author amywu
 */
abstract public class AbstractPreferenceTab implements IPreferenceTab {

	Map fCheckBoxes = new HashMap();
	private SelectionListener fCheckBoxListener = new SelectionListener() {
		public void widgetDefaultSelected(SelectionEvent e) {
		}

		public void widgetSelected(SelectionEvent e) {
			Button button = (Button) e.widget;
			fOverlayStore.setValue((String) fCheckBoxes.get(button), button.getSelection());
		}
	};
	private PreferencePage fMainPreferencePage;
	private ModifyListener fNumberFieldListener = new ModifyListener() {
		public void modifyText(ModifyEvent e) {
			numberFieldChanged((Text) e.widget);
		}
	};
	private ArrayList fNumberFields = new ArrayList();
	OverlayPreferenceStore fOverlayStore;
	private ModifyListener fTextFieldListener = new ModifyListener() {
		public void modifyText(ModifyEvent e) {
			Text text = (Text) e.widget;
			fOverlayStore.setValue((String) fTextFields.get(text), text.getText());
		}
	};
	Map fTextFields = new HashMap();

	protected Button addCheckBox(Composite parent, String label, String key, int indentation) {
		Button checkBox = new Button(parent, SWT.CHECK);
		checkBox.setText(label);

		GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gd.horizontalIndent = indentation;
		gd.horizontalSpan = 2;
		checkBox.setLayoutData(gd);
		checkBox.addSelectionListener(fCheckBoxListener);

		fCheckBoxes.put(checkBox, key);

		return checkBox;
	}

	/**
	 * Returns an array of size 2: - first element is of type
	 * <code>Label</code>- second element is of type <code>Text</code>
	 * Use <code>getLabelControl</code> and <code>getTextControl</code> to
	 * get the 2 controls.
	 */
	private Control[] addLabelledTextField(Composite composite, String label, String key, int textLimit, int indentation, boolean isNumber) {
		Label labelControl = new Label(composite, SWT.NONE);
		labelControl.setText(label);
		GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gd.horizontalIndent = indentation;
		labelControl.setLayoutData(gd);

		Text textControl = new Text(composite, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gd.widthHint = convertWidthInCharsToPixels(textControl, textLimit + 1);
		textControl.setLayoutData(gd);
		textControl.setTextLimit(textLimit);
		fTextFields.put(textControl, key);
		if (isNumber) {
			fNumberFields.add(textControl);
			textControl.addModifyListener(fNumberFieldListener);
		} else {
			textControl.addModifyListener(fTextFieldListener);
		}

		return new Control[]{labelControl, textControl};
	}

	protected Text addTextField(Composite composite, String label, String key, int textLimit, int indentation, boolean isNumber) {
		return getTextControl(addLabelledTextField(composite, label, key, textLimit, indentation, isNumber));
	}

	/**
	 * Applies the status to the status line of a dialog page.
	 */
	private void applyToStatusLine(IStatus status) {
		String message = status.getMessage();
		switch (status.getSeverity()) {
			case IStatus.OK :
				fMainPreferencePage.setMessage(message, IMessageProvider.NONE);
				fMainPreferencePage.setErrorMessage(null);
				break;
			case IStatus.WARNING :
				fMainPreferencePage.setMessage(message, IMessageProvider.WARNING);
				fMainPreferencePage.setErrorMessage(null);
				break;
			case IStatus.INFO :
				fMainPreferencePage.setMessage(message, IMessageProvider.INFORMATION);
				fMainPreferencePage.setErrorMessage(null);
				break;
			default :
				if (message.length() == 0) {
					message = null;
				}
				fMainPreferencePage.setMessage(null);
				fMainPreferencePage.setErrorMessage(message);
				break;
		}
	}

	/**
	 * Returns the number of pixels corresponding to the width of the given
	 * number of characters. This method was copied from
	 * org.eclipse.jface.dialogs.DialogPage
	 * <p>
	 * 
	 * @param a
	 *            control in the page
	 * @param chars
	 *            the number of characters
	 * @return the number of pixels
	 */
	private int convertWidthInCharsToPixels(Control testControl, int chars) {
		// Compute and store a font metric
		GC gc = new GC(testControl);
		gc.setFont(JFaceResources.getDialogFont());
		FontMetrics fontMetrics = gc.getFontMetrics();
		gc.dispose();

		// test for failure to initialize for backward compatibility
		if (fontMetrics == null)
			return 0;
		return Dialog.convertWidthInCharsToPixels(fontMetrics, chars);
	}

	/**
	 * @return Returns the fMainPreferencePage.
	 */
	protected PreferencePage getMainPreferencePage() {
		return fMainPreferencePage;
	}

	/**
	 * @return Returns the fOverlayStore.
	 */
	protected OverlayPreferenceStore getOverlayStore() {
		return fOverlayStore;
	}

	private Text getTextControl(Control[] labelledTextField) {
		return (Text) labelledTextField[1];
	}

	protected void initializeFields() {
		Iterator e = fCheckBoxes.keySet().iterator();
		while (e.hasNext()) {
			Button b = (Button) e.next();
			String key = (String) fCheckBoxes.get(b);
			b.setSelection(fOverlayStore.getBoolean(key));
		}

		e = fTextFields.keySet().iterator();
		while (e.hasNext()) {
			Text t = (Text) e.next();
			String key = (String) fTextFields.get(t);
			t.setText(fOverlayStore.getString(key));
		}
	}

	void numberFieldChanged(Text textControl) {
		String number = textControl.getText();
		IStatus status = validatePositiveNumber(number);
		if (!status.matches(IStatus.ERROR))
			fOverlayStore.setValue((String) fTextFields.get(textControl), number);
		updateStatus(status);
	}

	/**
	 * @param mainPreferencePage
	 *            The fMainPreferencePage to set.
	 */
	protected void setMainPreferencePage(PreferencePage mainPreferencePage) {
		fMainPreferencePage = mainPreferencePage;
	}

	/**
	 * @param overlayStore
	 *            The fOverlayStore to set.
	 */
	protected void setOverlayStore(OverlayPreferenceStore overlayStore) {
		fOverlayStore = overlayStore;
	}

	/**
	 * Update status of main preference page
	 * 
	 * @param status
	 */
	protected void updateStatus(IStatus status) {
		if (!status.matches(IStatus.ERROR)) {
			for (int i = 0; i < fNumberFields.size(); i++) {
				Text text = (Text) fNumberFields.get(i);
				IStatus s = validatePositiveNumber(text.getText());
				status = s.getSeverity() > status.getSeverity() ? s : status;
			}
		}

		fMainPreferencePage.setValid(!status.matches(IStatus.ERROR));
		applyToStatusLine(status);
	}

	private IStatus validatePositiveNumber(String number) {
		StatusInfo status = new StatusInfo();
		if (number.length() == 0) {
			status.setError(SSEUIMessages.StructuredTextEditorPreferencePage_37);
		} else {
			try {
				int value = Integer.parseInt(number);
				if (value < 0)
					status.setError(number + SSEUIMessages.StructuredTextEditorPreferencePage_38);
			} catch (NumberFormatException e) {
				status.setError(number + SSEUIMessages.StructuredTextEditorPreferencePage_38);
			}
		}
		return status;
	}
}

Back to the top