KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.ibm.icu.text.MessageFormat;
15
16 import org.eclipse.ant.internal.ui.AntUIPlugin;
17 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds;
18 import org.eclipse.ant.internal.ui.IAntUIPreferenceConstants;
19 import org.eclipse.jface.preference.BooleanFieldEditor;
20 import org.eclipse.jface.preference.FieldEditorPreferencePage;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.preference.IntegerFieldEditor;
23 import org.eclipse.jface.preference.PreferenceConverter;
24 import org.eclipse.jface.preference.StringFieldEditor;
25 import org.eclipse.jface.resource.StringConverter;
26 import org.eclipse.jface.util.PropertyChangeEvent;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.graphics.Font;
31 import org.eclipse.swt.graphics.RGB;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.List;
39 import org.eclipse.ui.IWorkbench;
40 import org.eclipse.ui.IWorkbenchPreferencePage;
41 import org.eclipse.ui.PlatformUI;
42
43 public class AntPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
44     
45     private List fConsoleColorList;
46     private ColorEditor fConsoleColorEditor;
47     
48     private BooleanFieldEditor fToolsWarningEditor= null;
49     
50     // Array containing the message to display, the preference key, and the
51
// default value (initialized in storeInitialValues()) for each color preference
52
private final String JavaDoc[][] fAppearanceColorListModel= new String JavaDoc[][] {
53         {AntPreferencesMessages.AntPreferencePage__Error__2, IAntUIPreferenceConstants.CONSOLE_ERROR_COLOR, null},
54         {AntPreferencesMessages.AntPreferencePage__Warning__3, IAntUIPreferenceConstants.CONSOLE_WARNING_COLOR, null},
55         {AntPreferencesMessages.AntPreferencePage_I_nformation__4, IAntUIPreferenceConstants.CONSOLE_INFO_COLOR, null},
56         {AntPreferencesMessages.AntPreferencePage_Ve_rbose__5, IAntUIPreferenceConstants.CONSOLE_VERBOSE_COLOR, null},
57         {AntPreferencesMessages.AntPreferencePage_Deb_ug__6, IAntUIPreferenceConstants.CONSOLE_DEBUG_COLOR, null},
58     };
59
60     /**
61      * Create the Ant page.
62      */

63     public AntPreferencePage() {
64         super(GRID);
65         setDescription(AntPreferencesMessages.AntPreferencePage_General);
66         setPreferenceStore(AntUIPlugin.getDefault().getPreferenceStore());
67     }
68     
69     /* (non-Javadoc)
70      * @see org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors()
71      */

72     protected void createFieldEditors() {
73         storeAppliedValues();
74
75         Font font= getFieldEditorParent().getFont();
76         Label label= new Label(getFieldEditorParent(), SWT.WRAP);
77         label.setText(AntPreferencesMessages.AntPreferencePage_Enter);
78         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
79         gd.horizontalSpan= 3;
80         gd.widthHint= convertWidthInCharsToPixels(60);
81         label.setLayoutData(gd);
82         label.setLayoutData(gd);
83         label.setFont(font);
84         
85         StringFieldEditor editor = new StringFieldEditor(IAntUIPreferenceConstants.ANT_FIND_BUILD_FILE_NAMES, AntPreferencesMessages.AntPreferencePage__Names__3, getFieldEditorParent());
86         addField(editor);
87         
88         IntegerFieldEditor timeout = new IntegerFieldEditor(IAntUIPreferenceConstants.ANT_COMMUNICATION_TIMEOUT, AntPreferencesMessages.AntPreferencePage_13, getFieldEditorParent());
89         int minValue= AntUIPlugin.getDefault().getPreferenceStore().getDefaultInt(IAntUIPreferenceConstants.ANT_COMMUNICATION_TIMEOUT);
90         int maxValue = 1200000;
91         timeout.setValidRange(minValue, maxValue);
92         timeout.setErrorMessage(MessageFormat.format(AntPreferencesMessages.AntPreferencePage_14, new Object JavaDoc[] {new Integer JavaDoc(minValue), new Integer JavaDoc(maxValue)}));
93         addField(timeout);
94         
95         editor = new URLFieldEditor(IAntUIPreferenceConstants.DOCUMENTATION_URL, AntPreferencesMessages.AntPreferencePage_2, getFieldEditorParent());
96         addField(editor);
97         
98         createSpace();
99     
100         if (!AntUIPlugin.isMacOS()) {
101             //the mac does not have a tools.jar Bug 40778
102
label= new Label(getFieldEditorParent(), SWT.WRAP);
103             label.setText(AntPreferencesMessages.AntPreferencePage_0);
104             gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
105             gd.horizontalSpan= 3;
106             gd.widthHint= convertWidthInCharsToPixels(60);
107             label.setLayoutData(gd);
108             label.setFont(font);
109             fToolsWarningEditor= new BooleanFieldEditor(IAntUIPreferenceConstants.ANT_TOOLS_JAR_WARNING, AntPreferencesMessages.AntPreferencePage_1, getFieldEditorParent());
110             addField(fToolsWarningEditor);
111             createSpace();
112         }
113         
114         addField(new BooleanFieldEditor(IAntUIPreferenceConstants.ANT_ERROR_DIALOG, AntPreferencesMessages.AntPreferencePage_12, getFieldEditorParent()));
115         createSpace();
116         
117         
118         addField(new BooleanFieldEditor(IAntUIPreferenceConstants.ANT_CREATE_MARKERS, AntPreferencesMessages.AntPreferencePage_15, getFieldEditorParent()));
119         label= new Label(getFieldEditorParent(), SWT.WRAP);
120         label.setText(AntPreferencesMessages.AntPreferencePage_16);
121         gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
122         gd.horizontalSpan= 3;
123         gd.widthHint= convertWidthInCharsToPixels(60);
124         label.setLayoutData(gd);
125         label.setFont(font);
126         
127         createSpace();
128         createColorComposite();
129         getPreferenceStore().addPropertyChangeListener(this);
130     }
131     
132     private void createSpace() {
133         Label label= new Label(getFieldEditorParent(), SWT.NONE);
134         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
135         gd.horizontalSpan= 3;
136         label.setLayoutData(gd);
137     }
138
139     /**
140      * Stores the initial values of the color preferences. The preference values are updated
141      * on the fly as the user edits them (instead of only when they press "Apply"). We need
142      * to store the old values so that we can reset them when the user chooses "Cancel".
143      */

144     private void storeAppliedValues() {
145         IPreferenceStore store= getPreferenceStore();
146         for (int i = 0; i < fAppearanceColorListModel.length; i++) {
147             String JavaDoc preference = fAppearanceColorListModel[i][1];
148             fAppearanceColorListModel[i][2]= store.getString(preference);
149         }
150     }
151     
152     private void createColorComposite() {
153         Font font= getFieldEditorParent().getFont();
154         Label label= new Label(getFieldEditorParent(), SWT.LEFT);
155         label.setText(AntPreferencesMessages.AntPreferencePage_Ant_Color_Options__6);
156         label.setFont(font);
157         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
158         gd.horizontalSpan= 2;
159         label.setLayoutData(gd);
160                 
161         Composite editorComposite= new Composite(getFieldEditorParent(), SWT.NONE);
162         GridLayout layout= new GridLayout();
163         layout.numColumns= 2;
164         layout.marginHeight= 0;
165         layout.marginWidth= 0;
166         editorComposite.setLayout(layout);
167         editorComposite.setFont(font);
168         gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL);
169         gd.horizontalSpan= 2;
170         editorComposite.setLayoutData(gd);
171
172         fConsoleColorList= new List(editorComposite, SWT.SINGLE | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
173         gd= new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_HORIZONTAL);
174         gd.heightHint= convertHeightInCharsToPixels(8);
175         fConsoleColorList.setLayoutData(gd);
176         fConsoleColorList.setFont(font);
177                 
178         Composite stylesComposite= new Composite(editorComposite, SWT.NONE);
179         layout= new GridLayout();
180         layout.marginHeight= 0;
181         layout.marginWidth= 0;
182         layout.numColumns= 2;
183         stylesComposite.setLayout(layout);
184         stylesComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
185         stylesComposite.setFont(font);
186
187         label= new Label(stylesComposite, SWT.LEFT);
188         label.setText(AntPreferencesMessages.AntPreferencePage_Color__7);
189         label.setFont(font);
190         gd= new GridData();
191         gd.horizontalAlignment= GridData.BEGINNING;
192         label.setLayoutData(gd);
193
194         fConsoleColorEditor= new ColorEditor(stylesComposite);
195         Button foregroundColorButton= fConsoleColorEditor.getButton();
196         gd= new GridData(GridData.FILL_HORIZONTAL);
197         gd.horizontalAlignment= GridData.BEGINNING;
198         foregroundColorButton.setLayoutData(gd);
199         foregroundColorButton.setFont(font);
200
201         fConsoleColorList.addSelectionListener(new SelectionAdapter() {
202             public void widgetSelected(SelectionEvent e) {
203                 handleAppearanceColorListSelection();
204             }
205         });
206         foregroundColorButton.addSelectionListener(new SelectionAdapter() {
207             public void widgetSelected(SelectionEvent e) {
208                 int i= fConsoleColorList.getSelectionIndex();
209                 if (i == -1) { //bug 85590
210
return;
211                 }
212                 String JavaDoc key= fAppearanceColorListModel[i][1];
213                 PreferenceConverter.setValue(getPreferenceStore(), key, fConsoleColorEditor.getColorValue());
214             }
215         });
216     }
217     
218     /**
219      * Restore all color preferences to their values when the page was opened.
220      */

221     public boolean performCancel() {
222         for (int i = 0; i < fAppearanceColorListModel.length; i++) {
223             String JavaDoc preference = fAppearanceColorListModel[i][1];
224             PreferenceConverter.setValue(getPreferenceStore(), preference, StringConverter.asRGB(fAppearanceColorListModel[i][2]));
225         }
226         return super.performCancel();
227     }
228     
229     /**
230      * When the user applies the preferences, update the set of stored
231      * preferences so that we will fall back to the applied values on Cancel.
232      */

233     public boolean performOk() {
234         storeAppliedValues();
235         return super.performOk();
236     }
237     
238     private void handleAppearanceColorListSelection() {
239         int i= fConsoleColorList.getSelectionIndex();
240         if (i == -1) { //bug 85590
241
return;
242         }
243         String JavaDoc key= fAppearanceColorListModel[i][1];
244         RGB rgb= PreferenceConverter.getColor(getPreferenceStore(), key);
245         fConsoleColorEditor.setColorValue(rgb);
246     }
247     
248     /**
249      * @see FieldEditorPreferencePage#createContents(org.eclipse.swt.widgets.Composite)
250      */

251     protected Control createContents(Composite parent) {
252         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IAntUIHelpContextIds.ANT_PREFERENCE_PAGE);
253         return super.createContents(parent);
254     }
255
256     /**
257      * @see IWorkbenchPreferencePage#init(IWorkbench)
258      */

259     public void init(IWorkbench workbench) {
260     }
261     
262     /**
263      * @see org.eclipse.jface.preference.FieldEditorPreferencePage#initialize()
264      */

265     protected void initialize() {
266         super.initialize();
267         for (int i= 0; i < fAppearanceColorListModel.length; i++) {
268             fConsoleColorList.add(fAppearanceColorListModel[i][0]);
269         }
270         fConsoleColorList.getDisplay().asyncExec(new Runnable JavaDoc() {
271             public void run() {
272                 if (fConsoleColorList != null && !fConsoleColorList.isDisposed()) {
273                     fConsoleColorList.select(0);
274                     handleAppearanceColorListSelection();
275                 }
276             }
277         });
278     }
279     /**
280      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
281      */

282     protected void performDefaults() {
283         for (int i = 0; i < fAppearanceColorListModel.length; i++) {
284             String JavaDoc key= fAppearanceColorListModel[i][1];
285             PreferenceConverter.setValue(getPreferenceStore(), key, PreferenceConverter.getDefaultColor(getPreferenceStore(), key));
286         }
287         handleAppearanceColorListSelection();
288         
289         super.performDefaults();
290     }
291     
292     /* (non-Javadoc)
293      * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
294      */

295     public void dispose() {
296         getPreferenceStore().removePropertyChangeListener(this);
297     }
298
299     /* (non-Javadoc)
300      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
301      */

302     public void propertyChange(PropertyChangeEvent event) {
303         if (event.getProperty().equals(IAntUIPreferenceConstants.ANT_TOOLS_JAR_WARNING)) {
304             if (fToolsWarningEditor != null) {
305                 fToolsWarningEditor.load();
306             }
307         } else {
308             super.propertyChange(event);
309         }
310     }
311 }
312
Popular Tags