001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.preferences;
011:
012: import java.util.Map;
013:
014: import org.eclipse.core.runtime.IPath;
015:
016: import org.eclipse.swt.widgets.Composite;
017: import org.eclipse.swt.widgets.Control;
018:
019: import org.eclipse.jface.dialogs.Dialog;
020: import org.eclipse.jface.dialogs.MessageDialog;
021: import org.eclipse.jface.preference.PreferencePage;
022:
023: import org.eclipse.ui.IWorkbench;
024: import org.eclipse.ui.IWorkbenchPreferencePage;
025: import org.eclipse.ui.PlatformUI;
026:
027: import org.eclipse.jdt.core.JavaCore;
028:
029: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
030: import org.eclipse.jdt.internal.ui.JavaPlugin;
031: import org.eclipse.jdt.internal.ui.wizards.buildpaths.VariableBlock;
032:
033: public class ClasspathVariablesPreferencePage extends PreferencePage
034: implements IWorkbenchPreferencePage {
035:
036: public static final String ID = "org.eclipse.jdt.ui.preferences.ClasspathVariablesPreferencePage"; //$NON-NLS-1$
037:
038: public static final String DATA_SELECT_VARIABLE = "ClasspathVariablesPreferencePage.select_var"; //$NON-NLS-1$
039:
040: private VariableBlock fVariableBlock;
041: private String fStoredSettings;
042:
043: /**
044: * Constructor for ClasspathVariablesPreferencePage
045: */
046: public ClasspathVariablesPreferencePage() {
047: setPreferenceStore(JavaPlugin.getDefault().getPreferenceStore());
048: fVariableBlock = new VariableBlock(true, null);
049: fStoredSettings = null;
050:
051: // title only used when page is shown programatically
052: setTitle(PreferencesMessages.ClasspathVariablesPreferencePage_title);
053: setDescription(PreferencesMessages.ClasspathVariablesPreferencePage_description);
054: noDefaultAndApplyButton();
055: }
056:
057: /*
058: * (non-Javadoc)
059: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
060: */
061: public void createControl(Composite parent) {
062: super .createControl(parent);
063: PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
064: IJavaHelpContextIds.CP_VARIABLES_PREFERENCE_PAGE);
065: }
066:
067: /*
068: * @see PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
069: */
070: protected Control createContents(Composite parent) {
071: Control result = fVariableBlock.createContents(parent);
072: Dialog.applyDialogFont(result);
073: return result;
074: }
075:
076: /*
077: * @see IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
078: */
079: public void init(IWorkbench workbench) {
080: }
081:
082: /*
083: * @see PreferencePage#performDefaults()
084: */
085: protected void performDefaults() {
086: // not used (constructor calls noDefaultAndApplyButton())
087: // fVariableBlock.performDefaults();
088: super .performDefaults();
089: }
090:
091: /*
092: * @see PreferencePage#performOk()
093: */
094: public boolean performOk() {
095: JavaPlugin.getDefault().savePluginPreferences();
096: return fVariableBlock.performOk();
097: }
098:
099: /* (non-Javadoc)
100: * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
101: */
102: public void setVisible(boolean visible) {
103: // check if the stored settings have changed
104: if (visible) {
105: if (fStoredSettings != null
106: && !fStoredSettings.equals(getCurrentSettings())) {
107: fVariableBlock.refresh(null);
108: }
109: } else {
110: if (fVariableBlock.hasChanges()) {
111: String title = PreferencesMessages.ClasspathVariablesPreferencePage_savechanges_title;
112: String message = PreferencesMessages.ClasspathVariablesPreferencePage_savechanges_message;
113: if (MessageDialog.openQuestion(getShell(), title,
114: message)) {
115: performOk();
116: }
117: fVariableBlock.setChanges(false); // forget
118: }
119: fStoredSettings = getCurrentSettings();
120: }
121: super .setVisible(visible);
122: }
123:
124: private String getCurrentSettings() {
125: StringBuffer buf = new StringBuffer();
126: String[] names = JavaCore.getClasspathVariableNames();
127: for (int i = 0; i < names.length; i++) {
128: String curr = names[i];
129: buf.append(curr).append('\0');
130: IPath val = JavaCore.getClasspathVariable(curr);
131: if (val != null) {
132: buf.append(val.toString());
133: }
134: buf.append('\0');
135: }
136: return buf.toString();
137: }
138:
139: /* (non-Javadoc)
140: * @see org.eclipse.jface.preference.PreferencePage#applyData(java.lang.Object)
141: */
142: public void applyData(Object data) {
143: if (data instanceof Map) {
144: Object id = ((Map) data).get(DATA_SELECT_VARIABLE);
145: if (id instanceof String) {
146: fVariableBlock.setSelection((String) id);
147: }
148: }
149: super.applyData(data);
150: }
151:
152: }
|