1
/*******************************************************************************
2
* Copyright (c) 2002, 2010 QNX Software Systems and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* QNX Software Systems - Initial API and implementation
10
*******************************************************************************/
12
package org.eclipse.cdt.make.internal.ui.preferences;
14
import java.io.BufferedReader;
15
import java.io.IOException;
16
import java.io.InputStreamReader;
17
import com.ibm.icu.text.MessageFormat;
18
import java.util.HashMap;
19
import java.util.Iterator;
23
import org.eclipse.cdt.make.internal.ui.MakeUIPlugin;
24
import org.eclipse.core.runtime.IStatus;
25
import org.eclipse.jface.dialogs.DialogPage;
26
import org.eclipse.jface.dialogs.IMessageProvider;
27
import org.eclipse.jface.preference.PreferencePage;
28
import org.eclipse.swt.SWT;
29
import org.eclipse.swt.events.ModifyEvent;
30
import org.eclipse.swt.events.ModifyListener;
31
import org.eclipse.swt.events.SelectionEvent;
32
import org.eclipse.swt.events.SelectionListener;
33
import org.eclipse.swt.graphics.Font;
34
import org.eclipse.swt.layout.GridData;
35
import org.eclipse.swt.widgets.Button;
36
import org.eclipse.swt.widgets.Composite;
37
import org.eclipse.swt.widgets.Control;
38
import org.eclipse.swt.widgets.Label;
39
import org.eclipse.swt.widgets.Text;
40
import org.eclipse.ui.IWorkbench;
41
import org.eclipse.ui.IWorkbenchPreferencePage;
44
* AbstraceMakeEditorPreferencePage
46
public abstract class AbstractMakefileEditorPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
47
OverlayPreferenceStore fOverlayStore;
49
Map<Control, String> fCheckBoxes= new HashMap<Control, String>();
50
private SelectionListener fCheckBoxListener= new SelectionListener() {
51
public void widgetDefaultSelected(SelectionEvent e) {
53
public void widgetSelected(SelectionEvent e) {
54
Button button= (Button) e.widget;
55
fOverlayStore.setValue(fCheckBoxes.get(button), button.getSelection());
59
Map<Control, String> fTextFields= new HashMap<Control, String>();
60
private ModifyListener fTextFieldListener= new ModifyListener() {
61
public void modifyText(ModifyEvent e) {
62
Text text= (Text) e.widget;
63
fOverlayStore.setValue(fTextFields.get(text), text.getText());
67
private Map<Text, String[]> fNumberFields= new HashMap<Text, String[]>();
68
private ModifyListener fNumberFieldListener= new ModifyListener() {
69
public void modifyText(ModifyEvent e) {
70
numberFieldChanged((Text) e.widget);
74
public AbstractMakefileEditorPreferencePage() {
76
setPreferenceStore(MakeUIPlugin.getDefault().getPreferenceStore());
77
fOverlayStore= createOverlayStore();
80
protected abstract OverlayPreferenceStore createOverlayStore();
83
* @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
85
public void init(IWorkbench workbench) {
88
protected void initializeFields() {
89
Map<Control, String> checkBoxes= getCheckBoxes();
90
Map<Control, String> textFields= getTextFields();
91
Iterator<Control> e= checkBoxes.keySet().iterator();
93
Button b= (Button) e.next();
94
String key= checkBoxes.get(b);
95
b.setSelection(getOverlayStore().getBoolean(key));
98
e= textFields.keySet().iterator();
100
Text t= (Text) e.next();
101
String key= textFields.get(t);
102
t.setText(getOverlayStore().getString(key));
107
* @see org.eclipse.jface.preference.IPreferencePage#performOk()
110
public boolean performOk() {
111
getOverlayStore().propagate();
112
MakeUIPlugin.getDefault().savePluginPreferences();
116
protected OverlayPreferenceStore getOverlayStore() {
117
return fOverlayStore;
120
protected OverlayPreferenceStore setOverlayStore() {
121
return fOverlayStore;
124
protected Map<Control, String> getCheckBoxes() {
128
protected Map<Control, String> getTextFields() {
132
protected Map<Text, String[]> getNumberFields() {
133
return fNumberFields;
137
* @see org.eclipse.jface.preference.PreferencePage#performDefaults()
140
protected void performDefaults() {
141
getOverlayStore().loadDefaults();
144
super.performDefaults();
147
protected abstract void handleDefaults();
150
* @see org.eclipse.jface.dialogs.IDialogPage#dispose()
153
public void dispose() {
154
if (getOverlayStore() != null) {
155
getOverlayStore().stop();
161
protected Button addCheckBox(Composite parent, String labelText, String key, int indentation) {
162
Button checkBox= new Button(parent, SWT.CHECK);
163
checkBox.setText(labelText);
164
checkBox.setFont(parent.getFont());
166
GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
167
gd.horizontalIndent= indentation;
168
gd.horizontalSpan= 2;
169
checkBox.setLayoutData(gd);
170
checkBox.addSelectionListener(fCheckBoxListener);
172
getCheckBoxes().put(checkBox, key);
177
protected Control addTextField(Composite composite, String labelText, String key, int textLimit, int indentation, String[] errorMessages) {
178
Font font= composite.getFont();
180
Label label= new Label(composite, SWT.NONE);
181
label.setText(labelText);
183
GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
184
gd.horizontalIndent= indentation;
185
label.setLayoutData(gd);
187
Text textControl= new Text(composite, SWT.BORDER | SWT.SINGLE);
188
textControl.setFont(font);
189
gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
190
gd.widthHint= convertWidthInCharsToPixels(textLimit + 1);
191
textControl.setLayoutData(gd);
192
textControl.setTextLimit(textLimit);
193
getTextFields().put(textControl, key);
194
if (errorMessages != null) {
195
getNumberFields().put(textControl, errorMessages);
196
textControl.addModifyListener(fNumberFieldListener);
198
textControl.addModifyListener(fTextFieldListener);
204
void numberFieldChanged(Text textControl) {
205
String number= textControl.getText();
206
IStatus status= validatePositiveNumber(number, getNumberFields().get(textControl));
207
if (!status.matches(IStatus.ERROR)) {
208
getOverlayStore().setValue(getTextFields().get(textControl), number);
210
updateStatus(status);
213
private IStatus validatePositiveNumber(String number, String[] errorMessages) {
214
StatusInfo status= new StatusInfo();
215
if (number.length() == 0) {
216
status.setError(errorMessages[0]);
219
int value= Integer.parseInt(number);
221
status.setError(MessageFormat.format(errorMessages[1], new String[]{number}));
222
} catch (NumberFormatException e) {
223
status.setError(MessageFormat.format(errorMessages[1], new String[]{number}));
229
private void updateStatus(IStatus status) {
230
if (!status.matches(IStatus.ERROR)) {
231
Set<Text> keys= getNumberFields().keySet();
232
for (Iterator<Text> iter = keys.iterator(); iter.hasNext();) {
233
Text text = iter.next();
234
IStatus s= validatePositiveNumber(text.getText(), getNumberFields().get(text));
235
status= s.getSeverity() > status.getSeverity() ? s : status;
238
setValid(!status.matches(IStatus.ERROR));
239
applyToStatusLine(this, status);
243
* Applies the status to the status line of a dialog page.
245
private void applyToStatusLine(DialogPage page, IStatus status) {
246
String message= status.getMessage();
247
switch (status.getSeverity()) {
249
page.setMessage(message, IMessageProvider.NONE);
250
page.setErrorMessage(null);
252
case IStatus.WARNING:
253
page.setMessage(message, IMessageProvider.WARNING);
254
page.setErrorMessage(null);
257
page.setMessage(message, IMessageProvider.INFORMATION);
258
page.setErrorMessage(null);
261
if (message.length() == 0) {
264
page.setMessage(null);
265
page.setErrorMessage(message);
271
* Returns an array of size 2:
272
* - first element is of type <code>Label</code>
273
* - second element is of type <code>Text</code>
274
* Use <code>getLabelControl</code> and <code>getTextControl</code> to get the 2 controls.
276
protected Control[] addLabelledTextField(Composite composite, String label, String key, int textLimit, int indentation, String[] errorMessages) {
277
Label labelControl= new Label(composite, SWT.NONE);
278
labelControl.setText(label);
279
labelControl.setFont(composite.getFont());
280
GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
281
gd.horizontalIndent= indentation;
282
labelControl.setLayoutData(gd);
284
Text textControl= new Text(composite, SWT.BORDER | SWT.SINGLE);
285
gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
286
gd.widthHint= convertWidthInCharsToPixels(textLimit + 1);
287
textControl.setLayoutData(gd);
288
textControl.setTextLimit(textLimit);
289
textControl.setFont(composite.getFont());
290
fTextFields.put(textControl, key);
291
if (errorMessages != null) {
292
fNumberFields.put(textControl, errorMessages);
293
textControl.addModifyListener(fNumberFieldListener);
295
textControl.addModifyListener(fTextFieldListener);
298
return new Control[]{labelControl, textControl};
301
protected String loadPreviewContentFromFile(String filename) {
303
String separator= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
304
StringBuffer buffer= new StringBuffer(512);
305
BufferedReader reader= null;
307
reader= new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(filename)));
308
while ((line= reader.readLine()) != null) {
310
buffer.append(separator);
312
} catch (IOException io) {
313
MakeUIPlugin.log(io);
315
if (reader != null) {
316
try { reader.close(); } catch (IOException e) {}
319
return buffer.toString();