KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > util > PrefUtil


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.ui.internal.util;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.jface.preference.IPreferenceStore;
15 import org.eclipse.ui.internal.WorkbenchPlugin;
16
17 /**
18  * Internal utility class to help with getting/setting preferences.
19  * <p>
20  * API preferences are defined in {@link org.eclipse.ui.IWorkbenchPreferenceConstants}
21  * and are obtained from the <code>org.eclipse.ui</code> plug-in's preference store.
22  * </p>
23  * <p>
24  * Internal preferences are defined in {@link org.eclipse.ui.internal.IPreferenceConstants}
25  * and are obtained from the <code>org.eclipse.ui.workbench</code> plug-in's preference store.
26  * </p>
27  *
28  * @since 3.0
29  */

30 public class PrefUtil {
31
32     private PrefUtil() {
33         // prevents instantiation
34
}
35
36     /**
37      * Callback interface to obtain and save the UI preference store.
38      */

39     public static interface ICallback {
40         IPreferenceStore getPreferenceStore();
41
42         void savePreferences();
43     }
44
45     private static ICallback uiCallback;
46
47     private static IPreferenceStore uiPreferenceStore;
48
49     /**
50      * Sets the callback used to obtain and save the UI preference store.
51      */

52     public static final void setUICallback(ICallback callback) {
53         Assert.isTrue(uiCallback == null);
54         uiCallback = callback;
55     }
56
57     /**
58      * Returns the API preference store.
59      *
60      * @return the API preference store
61      */

62     public static IPreferenceStore getAPIPreferenceStore() {
63         if (uiPreferenceStore == null) {
64             Assert.isNotNull(uiCallback);
65             uiPreferenceStore = uiCallback.getPreferenceStore();
66         }
67         return uiPreferenceStore;
68     }
69
70     /**
71      * Returns the internal preference store.
72      *
73      * @return the internal preference store
74      */

75     public static IPreferenceStore getInternalPreferenceStore() {
76         return WorkbenchPlugin.getDefault().getPreferenceStore();
77     }
78
79     /**
80      * Saves both the API and internal preference stores.
81      */

82     public static void savePrefs() {
83         saveAPIPrefs();
84         saveInternalPrefs();
85     }
86
87     /**
88      * Saves the API preference store, if needed.
89      */

90     public static void saveAPIPrefs() {
91         Assert.isNotNull(uiCallback);
92         uiCallback.savePreferences();
93     }
94
95     /**
96      * Saves the internal preference store, if needed.
97      */

98     public static void saveInternalPrefs() {
99         WorkbenchPlugin.getDefault().savePluginPreferences();
100     }
101 }
102
Popular Tags