1
/*******************************************************************************
2
* Copyright (c) 2002, 2008 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 fCheckBoxes= new HashMap();
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((String) fCheckBoxes.get(button), button.getSelection());
59
Map fTextFields= new HashMap();
60
private ModifyListener fTextFieldListener= new ModifyListener() {
61
public void modifyText(ModifyEvent e) {
62
Text text= (Text) e.widget;
63
fOverlayStore.setValue((String) fTextFields.get(text), text.getText());
67
private Map fNumberFields= new HashMap();
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 checkBoxes= getCheckBoxes();
90
Map textFields= getTextFields();
91
Iterator e= checkBoxes.keySet().iterator();
93
Button b= (Button) e.next();
94
String key= (String) checkBoxes.get(b);
95
b.setSelection(getOverlayStore().getBoolean(key));
98
e= textFields.keySet().iterator();
100
Text t= (Text) e.next();
101
String key= (String) textFields.get(t);
102
t.setText(getOverlayStore().getString(key));
107
* @see org.eclipse.jface.preference.IPreferencePage#performOk()
109
public boolean performOk() {
110
getOverlayStore().propagate();
111
MakeUIPlugin.getDefault().savePluginPreferences();
115
protected OverlayPreferenceStore getOverlayStore() {
116
return fOverlayStore;
119
protected OverlayPreferenceStore setOverlayStore() {
120
return fOverlayStore;
123
protected Map getCheckBoxes() {
127
protected Map getTextFields() {
131
protected Map getNumberFields() {
132
return fNumberFields;
136
* @see org.eclipse.jface.preference.PreferencePage#performDefaults()
138
protected void performDefaults() {
139
getOverlayStore().loadDefaults();
142
super.performDefaults();
145
protected abstract void handleDefaults();
148
* @see org.eclipse.jface.dialogs.IDialogPage#dispose()
150
public void dispose() {
151
if (getOverlayStore() != null) {
152
getOverlayStore().stop();
158
protected Button addCheckBox(Composite parent, String labelText, String key, int indentation) {
159
Button checkBox= new Button(parent, SWT.CHECK);
160
checkBox.setText(labelText);
161
checkBox.setFont(parent.getFont());
163
GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
164
gd.horizontalIndent= indentation;
165
gd.horizontalSpan= 2;
166
checkBox.setLayoutData(gd);
167
checkBox.addSelectionListener(fCheckBoxListener);
169
getCheckBoxes().put(checkBox, key);
174
protected Control addTextField(Composite composite, String labelText, String key, int textLimit, int indentation, String[] errorMessages) {
175
Font font= composite.getFont();
177
Label label= new Label(composite, SWT.NONE);
178
label.setText(labelText);
180
GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
181
gd.horizontalIndent= indentation;
182
label.setLayoutData(gd);
184
Text textControl= new Text(composite, SWT.BORDER | SWT.SINGLE);
185
textControl.setFont(font);
186
gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
187
gd.widthHint= convertWidthInCharsToPixels(textLimit + 1);
188
textControl.setLayoutData(gd);
189
textControl.setTextLimit(textLimit);
190
getTextFields().put(textControl, key);
191
if (errorMessages != null) {
192
getNumberFields().put(textControl, errorMessages);
193
textControl.addModifyListener(fNumberFieldListener);
195
textControl.addModifyListener(fTextFieldListener);
201
void numberFieldChanged(Text textControl) {
202
String number= textControl.getText();
203
IStatus status= validatePositiveNumber(number, (String[])getNumberFields().get(textControl));
204
if (!status.matches(IStatus.ERROR)) {
205
getOverlayStore().setValue((String) getTextFields().get(textControl), number);
207
updateStatus(status);
210
private IStatus validatePositiveNumber(String number, String[] errorMessages) {
211
StatusInfo status= new StatusInfo();
212
if (number.length() == 0) {
213
status.setError(errorMessages[0]);
216
int value= Integer.parseInt(number);
218
status.setError(MessageFormat.format(errorMessages[1], new String[]{number}));
219
} catch (NumberFormatException e) {
220
status.setError(MessageFormat.format(errorMessages[1], new String[]{number}));
226
private void updateStatus(IStatus status) {
227
if (!status.matches(IStatus.ERROR)) {
228
Set keys= getNumberFields().keySet();
229
for (Iterator iter = keys.iterator(); iter.hasNext();) {
230
Text text = (Text) iter.next();
231
IStatus s= validatePositiveNumber(text.getText(), (String[])getNumberFields().get(text));
232
status= s.getSeverity() > status.getSeverity() ? s : status;
235
setValid(!status.matches(IStatus.ERROR));
236
applyToStatusLine(this, status);
240
* Applies the status to the status line of a dialog page.
242
private void applyToStatusLine(DialogPage page, IStatus status) {
243
String message= status.getMessage();
244
switch (status.getSeverity()) {
246
page.setMessage(message, IMessageProvider.NONE);
247
page.setErrorMessage(null);
249
case IStatus.WARNING:
250
page.setMessage(message, IMessageProvider.WARNING);
251
page.setErrorMessage(null);
254
page.setMessage(message, IMessageProvider.INFORMATION);
255
page.setErrorMessage(null);
258
if (message.length() == 0) {
261
page.setMessage(null);
262
page.setErrorMessage(message);
268
* Returns an array of size 2:
269
* - first element is of type <code>Label</code>
270
* - second element is of type <code>Text</code>
271
* Use <code>getLabelControl</code> and <code>getTextControl</code> to get the 2 controls.
273
protected Control[] addLabelledTextField(Composite composite, String label, String key, int textLimit, int indentation, String[] errorMessages) {
274
Label labelControl= new Label(composite, SWT.NONE);
275
labelControl.setText(label);
276
labelControl.setFont(composite.getFont());
277
GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
278
gd.horizontalIndent= indentation;
279
labelControl.setLayoutData(gd);
281
Text textControl= new Text(composite, SWT.BORDER | SWT.SINGLE);
282
gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
283
gd.widthHint= convertWidthInCharsToPixels(textLimit + 1);
284
textControl.setLayoutData(gd);
285
textControl.setTextLimit(textLimit);
286
textControl.setFont(composite.getFont());
287
fTextFields.put(textControl, key);
288
if (errorMessages != null) {
289
fNumberFields.put(textControl, errorMessages);
290
textControl.addModifyListener(fNumberFieldListener);
292
textControl.addModifyListener(fTextFieldListener);
295
return new Control[]{labelControl, textControl};
298
protected String loadPreviewContentFromFile(String filename) {
300
String separator= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
301
StringBuffer buffer= new StringBuffer(512);
302
BufferedReader reader= null;
304
reader= new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(filename)));
305
while ((line= reader.readLine()) != null) {
307
buffer.append(separator);
309
} catch (IOException io) {
310
MakeUIPlugin.log(io);
312
if (reader != null) {
313
try { reader.close(); } catch (IOException e) {}
316
return buffer.toString();