KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > AbstractAntEditorPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.preferences;
12
13
14 import java.io.BufferedReader JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import com.ibm.icu.text.MessageFormat;
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.eclipse.ant.internal.ui.AntUIPlugin;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.jface.dialogs.DialogPage;
28 import org.eclipse.jface.dialogs.IMessageProvider;
29 import org.eclipse.jface.preference.PreferencePage;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.ModifyEvent;
32 import org.eclipse.swt.events.ModifyListener;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.events.SelectionListener;
35 import org.eclipse.swt.graphics.Font;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Label;
41 import org.eclipse.swt.widgets.Text;
42 import org.eclipse.ui.IWorkbench;
43 import org.eclipse.ui.IWorkbenchPreferencePage;
44
45 public abstract class AbstractAntEditorPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
46     
47     private OverlayPreferenceStore fOverlayStore;
48     protected List JavaDoc fStatusList;
49     private boolean fInitialized= false;
50     
51     private Map JavaDoc fCheckBoxes= new HashMap JavaDoc();
52     private SelectionListener fCheckBoxListener= new SelectionListener() {
53         public void widgetDefaultSelected(SelectionEvent e) {
54         }
55         public void widgetSelected(SelectionEvent e) {
56             Button button= (Button) e.widget;
57             fOverlayStore.setValue((String JavaDoc) fCheckBoxes.get(button), button.getSelection());
58         }
59     };
60     
61     private Map JavaDoc fTextFields= new HashMap JavaDoc();
62     private ModifyListener fTextFieldListener= new ModifyListener() {
63         public void modifyText(ModifyEvent e) {
64             if (fInitialized) {
65                 Text text= (Text) e.widget;
66                 fOverlayStore.setValue((String JavaDoc) fTextFields.get(text), text.getText());
67             }
68         }
69     };
70
71     private Map JavaDoc fNumberFields= new HashMap JavaDoc();
72     private ModifyListener fNumberFieldListener= new ModifyListener() {
73         public void modifyText(ModifyEvent e) {
74             if (fInitialized) {
75                 numberFieldChanged((Text) e.widget);
76             }
77         }
78     };
79             
80     public AbstractAntEditorPreferencePage() {
81         super();
82         setPreferenceStore(AntUIPlugin.getDefault().getPreferenceStore());
83         fOverlayStore= createOverlayStore();
84     }
85     
86     protected abstract OverlayPreferenceStore createOverlayStore();
87     
88     /* (non-Javadoc)
89      * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
90      */

91     public void init(IWorkbench workbench) {
92     }
93     
94     protected void initializeFields() {
95         Map JavaDoc checkBoxes= getCheckBoxes();
96         Map JavaDoc textFields= getTextFields();
97         Iterator JavaDoc e= checkBoxes.keySet().iterator();
98         while (e.hasNext()) {
99             Button b= (Button) e.next();
100             String JavaDoc key= (String JavaDoc) checkBoxes.get(b);
101             b.setSelection(getOverlayStore().getBoolean(key));
102         }
103         
104         e= textFields.keySet().iterator();
105         while (e.hasNext()) {
106             Text t= (Text) e.next();
107             String JavaDoc key= (String JavaDoc) textFields.get(t);
108             t.setText(getOverlayStore().getString(key));
109         }
110         fInitialized= true;
111     }
112     
113     /* (non-Javadoc)
114      * @see org.eclipse.jface.preference.IPreferencePage#performOk()
115      */

116     public boolean performOk() {
117         getOverlayStore().propagate();
118         AntUIPlugin.getDefault().savePluginPreferences();
119         return true;
120     }
121     
122     protected OverlayPreferenceStore getOverlayStore() {
123         return fOverlayStore;
124     }
125     
126     protected OverlayPreferenceStore setOverlayStore() {
127         return fOverlayStore;
128     }
129     
130     protected Map JavaDoc getCheckBoxes() {
131         return fCheckBoxes;
132     }
133     
134     protected Map JavaDoc getTextFields() {
135         return fTextFields;
136     }
137     
138     protected Map JavaDoc getNumberFields() {
139         return fNumberFields;
140     }
141     
142     /* (non-Javadoc)
143      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
144      */

145     protected void performDefaults() {
146         getOverlayStore().loadDefaults();
147         initializeFields();
148         handleDefaults();
149         super.performDefaults();
150     }
151     
152     protected abstract void handleDefaults();
153     
154     /* (non-Javadoc)
155      * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
156      */

157     public void dispose() {
158         if (getOverlayStore() != null) {
159             getOverlayStore().stop();
160             fOverlayStore= null;
161         }
162         super.dispose();
163     }
164     
165     protected Button addCheckBox(Composite parent, String JavaDoc labelText, String JavaDoc key, int indentation) {
166         Button checkBox= new Button(parent, SWT.CHECK);
167         checkBox.setText(labelText);
168         checkBox.setFont(parent.getFont());
169         
170         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
171         gd.horizontalIndent= indentation;
172         gd.horizontalSpan= 2;
173         checkBox.setLayoutData(gd);
174         checkBox.addSelectionListener(fCheckBoxListener);
175         
176         getCheckBoxes().put(checkBox, key);
177         
178         return checkBox;
179     }
180     
181     protected Text addTextField(Composite composite, String JavaDoc labelText, String JavaDoc key, int textLimit, int indentation, String JavaDoc[] errorMessages) {
182         Font font= composite.getFont();
183         
184         Label label= new Label(composite, SWT.NONE);
185         label.setText(labelText);
186         label.setFont(font);
187         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
188         gd.horizontalIndent= indentation;
189         label.setLayoutData(gd);
190         
191         Text textControl= new Text(composite, SWT.BORDER | SWT.SINGLE);
192         textControl.setFont(font);
193         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
194         gd.widthHint= convertWidthInCharsToPixels(textLimit + 1);
195         textControl.setLayoutData(gd);
196         textControl.setTextLimit(textLimit);
197         getTextFields().put(textControl, key);
198         if (errorMessages != null) {
199             getNumberFields().put(textControl, errorMessages);
200             textControl.addModifyListener(fNumberFieldListener);
201         } else {
202             textControl.addModifyListener(fTextFieldListener);
203         }
204             
205         return textControl;
206     }
207     
208     private void numberFieldChanged(Text textControl) {
209         String JavaDoc number= textControl.getText();
210         IStatus status= validatePositiveNumber(number, (String JavaDoc[])getNumberFields().get(textControl));
211         if (!status.matches(IStatus.ERROR)) {
212             getOverlayStore().setValue((String JavaDoc) getTextFields().get(textControl), number);
213         }
214         updateStatus(status);
215     }
216     
217     private IStatus validatePositiveNumber(String JavaDoc number, String JavaDoc[] errorMessages) {
218         StatusInfo status= new StatusInfo();
219         if (number.length() == 0) {
220             status.setError(errorMessages[0]);
221         } else {
222             try {
223                 int value= Integer.parseInt(number);
224                 if (value < 0)
225                     status.setError(MessageFormat.format(errorMessages[1], new String JavaDoc[]{number}));
226             } catch (NumberFormatException JavaDoc e) {
227                 status.setError(MessageFormat.format(errorMessages[1], new String JavaDoc[]{number}));
228             }
229         }
230         return status;
231     }
232     
233     protected void updateStatus(IStatus status) {
234         if (!status.matches(IStatus.ERROR)) {
235             Set JavaDoc keys= getNumberFields().keySet();
236             for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();) {
237                 Text text = (Text) iter.next();
238                 IStatus s= validatePositiveNumber(text.getText(), (String JavaDoc[])getNumberFields().get(text));
239                 status= s.getSeverity() > status.getSeverity() ? s : status;
240             }
241         }
242         
243         List JavaDoc statusList= getStatusList();
244         if (statusList != null) {
245             List JavaDoc temp= new ArrayList JavaDoc(statusList.size() + 1);
246             temp.add(status);
247             temp.addAll(statusList);
248             status= getMostSevere(temp);
249         }
250         setValid(!status.matches(IStatus.ERROR));
251         applyToStatusLine(this, status);
252     }
253     
254     protected List JavaDoc getStatusList() {
255         return fStatusList;
256     }
257     
258     /**
259      * Finds the most severe status from a array of stati.
260      * An error is more severe than a warning, and a warning is more severe
261      * than ok.
262      */

263     private IStatus getMostSevere(List JavaDoc statusList) {
264         IStatus max= null;
265         for (int i= 0; i < statusList.size(); i++) {
266             IStatus curr= (IStatus)statusList.get(i);
267             if (curr.matches(IStatus.ERROR)) {
268                 return curr;
269             }
270             if (max == null || curr.getSeverity() > max.getSeverity()) {
271                 max= curr;
272             }
273         }
274         return max;
275     }
276
277     /*
278      * Applies the status to the status line of a dialog page.
279      */

280     private void applyToStatusLine(DialogPage page, IStatus status) {
281         String JavaDoc message= status.getMessage();
282         switch (status.getSeverity()) {
283             case IStatus.OK:
284                 page.setMessage(message, IMessageProvider.NONE);
285                 page.setErrorMessage(null);
286                 break;
287             case IStatus.WARNING:
288                 page.setMessage(message, IMessageProvider.WARNING);
289                 page.setErrorMessage(null);
290                 break;
291             case IStatus.INFO:
292                 page.setMessage(message, IMessageProvider.INFORMATION);
293                 page.setErrorMessage(null);
294                 break;
295             default:
296                 if (message.length() == 0) {
297                     message= null;
298                 }
299                 page.setMessage(null);
300                 page.setErrorMessage(message);
301                 break;
302         }
303     }
304     
305     /**
306      * Returns an array of size 2:
307      * - first element is of type <code>Label</code>
308      * - second element is of type <code>Text</code>
309      * Use <code>getLabelControl</code> and <code>getTextControl</code> to get the 2 controls.
310      */

311     protected Control[] addLabelledTextField(Composite composite, String JavaDoc label, String JavaDoc key, int textLimit, int indentation, String JavaDoc[] errorMessages) {
312         Label labelControl= new Label(composite, SWT.NONE);
313         labelControl.setText(label);
314         labelControl.setFont(composite.getFont());
315         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
316         gd.horizontalIndent= indentation;
317         labelControl.setLayoutData(gd);
318     
319         Text textControl= new Text(composite, SWT.BORDER | SWT.SINGLE);
320         gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
321         if (textLimit > -1) {
322             gd.widthHint= convertWidthInCharsToPixels(textLimit + 1);
323             textControl.setTextLimit(textLimit);
324         } else {
325             gd.widthHint= convertWidthInCharsToPixels(50);
326         }
327         textControl.setLayoutData(gd);
328         textControl.setFont(composite.getFont());
329         fTextFields.put(textControl, key);
330         if (errorMessages != null) {
331             fNumberFields.put(textControl, errorMessages);
332             textControl.addModifyListener(fNumberFieldListener);
333         } else {
334             textControl.addModifyListener(fTextFieldListener);
335         }
336         
337         return new Control[]{labelControl, textControl};
338     }
339     
340     protected String JavaDoc loadPreviewContentFromFile(String JavaDoc filename) {
341         String JavaDoc line;
342         String JavaDoc separator= System.getProperty("line.separator"); //$NON-NLS-1$
343
StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(512);
344         BufferedReader JavaDoc reader= null;
345         try {
346             reader= new BufferedReader JavaDoc(new InputStreamReader JavaDoc(getClass().getResourceAsStream(filename)));
347             while ((line= reader.readLine()) != null) {
348                 buffer.append(line);
349                 buffer.append(separator);
350             }
351         } catch (IOException JavaDoc io) {
352             AntUIPlugin.log(io);
353         } finally {
354             if (reader != null) {
355                 try { reader.close(); } catch (IOException JavaDoc e) {}
356             }
357         }
358         return buffer.toString();
359     }
360
361     protected Label getLabelControl(Control[] labelledTextField) {
362         return (Label)labelledTextField[0];
363     }
364
365     protected Text getTextControl(Control[] labelledTextField) {
366         return (Text)labelledTextField[1];
367     }
368 }
369
Popular Tags