KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > PlatformUIPreferenceListener


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.ui.internal;
12
13 import java.io.IOException JavaDoc;
14 import java.io.Reader JavaDoc;
15 import java.io.StringReader JavaDoc;
16 import java.util.HashMap JavaDoc;
17
18 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
19 import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.ui.IEditorDescriptor;
22 import org.eclipse.ui.IEditorRegistry;
23 import org.eclipse.ui.IWorkbench;
24 import org.eclipse.ui.IWorkbenchPreferenceConstants;
25 import org.eclipse.ui.IWorkbenchWindow;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.WorkbenchException;
28 import org.eclipse.ui.internal.decorators.DecoratorManager;
29 import org.eclipse.ui.internal.progress.ProgressManager;
30 import org.eclipse.ui.internal.registry.EditorRegistry;
31 import org.eclipse.ui.internal.util.PrefUtil;
32
33 /**
34  * The PlatformUIPreferenceListener is a class that listens to changes in the
35  * preference store and propogates the change for any special cases that require
36  * updating of other values within the workbench.
37  */

38 public class PlatformUIPreferenceListener implements
39         IEclipsePreferences.IPreferenceChangeListener {
40     
41     private static PlatformUIPreferenceListener singleton;
42     
43     public static IEclipsePreferences.IPreferenceChangeListener getSingleton(){
44         if(singleton == null) {
45             singleton = new PlatformUIPreferenceListener();
46         }
47         return singleton;
48     }
49
50     /*
51      * (non-Javadoc)
52      *
53      * @see org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener#preferenceChange(org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent)
54      */

55     public void preferenceChange(PreferenceChangeEvent event) {
56
57         String JavaDoc propertyName = event.getKey();
58         if (IPreferenceConstants.ENABLED_DECORATORS.equals(propertyName)) {
59             DecoratorManager manager = WorkbenchPlugin.getDefault()
60                     .getDecoratorManager();
61             manager.applyDecoratorsPreference();
62             manager.clearCaches();
63             manager.updateForEnablementChange();
64             return;
65         }
66
67         if (IWorkbenchPreferenceConstants.SHOW_SYSTEM_JOBS.equals(propertyName)) {
68             boolean setting = PrefUtil.getAPIPreferenceStore().getBoolean(
69                     IWorkbenchPreferenceConstants.SHOW_SYSTEM_JOBS);
70
71             ProgressManager.getInstance().setShowSystemJobs(setting);
72         }
73         
74         if (IWorkbenchPreferenceConstants.DEFAULT_PERSPECTIVE_ID.equals(propertyName)) {
75             IWorkbench workbench = PlatformUI.getWorkbench();
76
77             workbench.getPerspectiveRegistry().setDefaultPerspective(
78                     PrefUtil.getAPIPreferenceStore().getString(
79                             IWorkbenchPreferenceConstants.DEFAULT_PERSPECTIVE_ID));
80             return;
81         }
82
83         if (IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR
84                 .equals(propertyName)) {
85             IPreferenceStore apiStore = PrefUtil.getAPIPreferenceStore();
86             IWorkbench workbench = PlatformUI.getWorkbench();
87             IWorkbenchWindow[] workbenchWindows = workbench
88                     .getWorkbenchWindows();
89             for (int i = 0; i < workbenchWindows.length; i++) {
90                 IWorkbenchWindow window = workbenchWindows[i];
91                 if (window instanceof WorkbenchWindow) {
92                     ((WorkbenchWindow) window)
93                             .setPerspectiveBarLocation(apiStore
94                                     .getString(IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR));
95                 }
96             }
97             return;
98         }
99
100         // TODO the banner apperance should have its own preference
101
if (IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS
102                 .equals(propertyName)) {
103             boolean newValue = PrefUtil.getAPIPreferenceStore().getBoolean(
104                     IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS);
105
106             IWorkbench workbench = PlatformUI.getWorkbench();
107             IWorkbenchWindow[] workbenchWindows = workbench
108                     .getWorkbenchWindows();
109             for (int i = 0; i < workbenchWindows.length; i++) {
110                 IWorkbenchWindow window = workbenchWindows[i];
111                 if (window instanceof WorkbenchWindow) {
112                     ((WorkbenchWindow) window).setBannerCurve(newValue);
113                 }
114             }
115             return;
116         }
117
118         // Update the file associations if they have changed due to an import
119
if (IPreferenceConstants.RESOURCES.equals(propertyName)) {
120             IEditorRegistry registry = WorkbenchPlugin.getDefault()
121                     .getEditorRegistry();
122             if (registry instanceof EditorRegistry) {
123                 EditorRegistry editorRegistry = (EditorRegistry) registry;
124                 IPreferenceStore store = WorkbenchPlugin.getDefault()
125                         .getPreferenceStore();
126                 Reader JavaDoc reader = null;
127                 try {
128                     String JavaDoc xmlString = store
129                             .getString(IPreferenceConstants.RESOURCES);
130                     if (xmlString != null && xmlString.length() > 0) {
131                         reader = new StringReader JavaDoc(xmlString);
132                         // Build the editor map.
133
HashMap JavaDoc editorMap = new HashMap JavaDoc();
134                         int i = 0;
135                         IEditorDescriptor[] descriptors = editorRegistry
136                                 .getSortedEditorsFromPlugins();
137                         // Get the internal editors
138
for (i = 0; i < descriptors.length; i++) {
139                             IEditorDescriptor descriptor = descriptors[i];
140                             editorMap.put(descriptor.getId(), descriptor);
141                         }
142                         // Get the external (OS) editors
143
descriptors = editorRegistry.getSortedEditorsFromOS();
144                         for (i = 0; i < descriptors.length; i++) {
145                             IEditorDescriptor descriptor = descriptors[i];
146                             editorMap.put(descriptor.getId(), descriptor);
147                         }
148                         // Update the file to editor(s) mappings
149
editorRegistry.readResources(editorMap, reader);
150                     }
151                 } catch (WorkbenchException e) {
152                     e.printStackTrace();
153                 } finally {
154                     if (reader != null) {
155                         try {
156                             reader.close();
157                         } catch (IOException JavaDoc e) {
158                             e.printStackTrace();
159                         }
160                     }
161                 }
162             }
163         }
164     }
165
166 }
167
Popular Tags