KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > ClasspathVariablesPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.ui.preferences;
12
13 import java.util.Map JavaDoc;
14
15 import org.eclipse.core.runtime.IPath;
16
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19
20 import org.eclipse.jface.dialogs.Dialog;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.jface.preference.PreferencePage;
23
24 import org.eclipse.ui.IWorkbench;
25 import org.eclipse.ui.IWorkbenchPreferencePage;
26 import org.eclipse.ui.PlatformUI;
27
28 import org.eclipse.jdt.core.JavaCore;
29
30 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32 import org.eclipse.jdt.internal.ui.wizards.buildpaths.VariableBlock;
33
34 public class ClasspathVariablesPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
35
36     public static final String JavaDoc ID= "org.eclipse.jdt.ui.preferences.ClasspathVariablesPreferencePage"; //$NON-NLS-1$
37

38     public static final String JavaDoc DATA_SELECT_VARIABLE= "ClasspathVariablesPreferencePage.select_var"; //$NON-NLS-1$
39

40     private VariableBlock fVariableBlock;
41     private String JavaDoc fStoredSettings;
42     
43     /**
44      * Constructor for ClasspathVariablesPreferencePage
45      */

46     public ClasspathVariablesPreferencePage() {
47         setPreferenceStore(JavaPlugin.getDefault().getPreferenceStore());
48         fVariableBlock= new VariableBlock(true, null);
49         fStoredSettings= null;
50         
51         // title only used when page is shown programatically
52
setTitle(PreferencesMessages.ClasspathVariablesPreferencePage_title);
53         setDescription(PreferencesMessages.ClasspathVariablesPreferencePage_description);
54         noDefaultAndApplyButton();
55     }
56     
57     /*
58      * (non-Javadoc)
59      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
60      */

61     public void createControl(Composite parent) {
62         super.createControl(parent);
63         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.CP_VARIABLES_PREFERENCE_PAGE);
64     }
65
66     /*
67      * @see PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
68      */

69     protected Control createContents(Composite parent) {
70         Control result= fVariableBlock.createContents(parent);
71         Dialog.applyDialogFont(result);
72         return result;
73     }
74     
75     /*
76      * @see IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
77      */

78     public void init(IWorkbench workbench) {
79     }
80     
81     /*
82      * @see PreferencePage#performDefaults()
83      */

84     protected void performDefaults() {
85         // not used (constructor calls noDefaultAndApplyButton())
86
// fVariableBlock.performDefaults();
87
super.performDefaults();
88     }
89
90     /*
91      * @see PreferencePage#performOk()
92      */

93     public boolean performOk() {
94         JavaPlugin.getDefault().savePluginPreferences();
95         return fVariableBlock.performOk();
96     }
97     
98     /* (non-Javadoc)
99      * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
100      */

101     public void setVisible(boolean visible) {
102         // check if the stored settings have changed
103
if (visible) {
104             if (fStoredSettings != null && !fStoredSettings.equals(getCurrentSettings())) {
105                 fVariableBlock.refresh(null);
106             }
107         } else {
108             if (fVariableBlock.hasChanges()) {
109                 String JavaDoc title= PreferencesMessages.ClasspathVariablesPreferencePage_savechanges_title;
110                 String JavaDoc message= PreferencesMessages.ClasspathVariablesPreferencePage_savechanges_message;
111                 if (MessageDialog.openQuestion(getShell(), title, message)) {
112                     performOk();
113                 }
114                 fVariableBlock.setChanges(false); // forget
115
}
116             fStoredSettings= getCurrentSettings();
117         }
118         super.setVisible(visible);
119     }
120     
121     private String JavaDoc getCurrentSettings() {
122         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
123         String JavaDoc[] names= JavaCore.getClasspathVariableNames();
124         for (int i= 0; i < names.length; i++) {
125             String JavaDoc curr= names[i];
126             buf.append(curr).append('\0');
127             IPath val= JavaCore.getClasspathVariable(curr);
128             if (val != null) {
129                 buf.append(val.toString());
130             }
131             buf.append('\0');
132         }
133         return buf.toString();
134     }
135     
136     /* (non-Javadoc)
137      * @see org.eclipse.jface.preference.PreferencePage#applyData(java.lang.Object)
138      */

139     public void applyData(Object JavaDoc data) {
140         if (data instanceof Map JavaDoc) {
141             Object JavaDoc id= ((Map JavaDoc) data).get(DATA_SELECT_VARIABLE);
142             if (id instanceof String JavaDoc) {
143                 fVariableBlock.setSelection((String JavaDoc) id);
144             }
145         }
146         super.applyData(data);
147     }
148
149 }
150
Popular Tags