001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 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.ui.internal.util;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.jface.preference.IPreferenceStore;
014: import org.eclipse.ui.internal.WorkbenchPlugin;
015:
016: /**
017: * Internal utility class to help with getting/setting preferences.
018: * <p>
019: * API preferences are defined in {@link org.eclipse.ui.IWorkbenchPreferenceConstants}
020: * and are obtained from the <code>org.eclipse.ui</code> plug-in's preference store.
021: * </p>
022: * <p>
023: * Internal preferences are defined in {@link org.eclipse.ui.internal.IPreferenceConstants}
024: * and are obtained from the <code>org.eclipse.ui.workbench</code> plug-in's preference store.
025: * </p>
026: *
027: * @since 3.0
028: */
029: public class PrefUtil {
030:
031: private PrefUtil() {
032: // prevents instantiation
033: }
034:
035: /**
036: * Callback interface to obtain and save the UI preference store.
037: */
038: public static interface ICallback {
039: IPreferenceStore getPreferenceStore();
040:
041: void savePreferences();
042: }
043:
044: private static ICallback uiCallback;
045:
046: private static IPreferenceStore uiPreferenceStore;
047:
048: /**
049: * Sets the callback used to obtain and save the UI preference store.
050: */
051: public static final void setUICallback(ICallback callback) {
052: Assert.isTrue(uiCallback == null);
053: uiCallback = callback;
054: }
055:
056: /**
057: * Returns the API preference store.
058: *
059: * @return the API preference store
060: */
061: public static IPreferenceStore getAPIPreferenceStore() {
062: if (uiPreferenceStore == null) {
063: Assert.isNotNull(uiCallback);
064: uiPreferenceStore = uiCallback.getPreferenceStore();
065: }
066: return uiPreferenceStore;
067: }
068:
069: /**
070: * Returns the internal preference store.
071: *
072: * @return the internal preference store
073: */
074: public static IPreferenceStore getInternalPreferenceStore() {
075: return WorkbenchPlugin.getDefault().getPreferenceStore();
076: }
077:
078: /**
079: * Saves both the API and internal preference stores.
080: */
081: public static void savePrefs() {
082: saveAPIPrefs();
083: saveInternalPrefs();
084: }
085:
086: /**
087: * Saves the API preference store, if needed.
088: */
089: public static void saveAPIPrefs() {
090: Assert.isNotNull(uiCallback);
091: uiCallback.savePreferences();
092: }
093:
094: /**
095: * Saves the internal preference store, if needed.
096: */
097: public static void saveInternalPrefs() {
098: WorkbenchPlugin.getDefault().savePluginPreferences();
099: }
100: }
|