KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > formatter > FormatterProfileStore


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
12 package org.eclipse.jdt.internal.ui.preferences.formatter;
13
14 import java.io.File JavaDoc;
15 import java.io.FileReader JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
22 import org.eclipse.core.runtime.preferences.IScopeContext;
23
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.resources.ResourcesPlugin;
26
27 import org.eclipse.jdt.core.JavaCore;
28
29 import org.eclipse.jdt.ui.JavaUI;
30
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32 import org.eclipse.jdt.internal.ui.preferences.PreferencesAccess;
33 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile;
34
35 import org.osgi.service.prefs.BackingStoreException;
36 import org.xml.sax.InputSource JavaDoc;
37
38
39
40 public class FormatterProfileStore extends ProfileStore {
41
42     /**
43      * Preference key where all profiles are stored
44      */

45     private static final String JavaDoc PREF_FORMATTER_PROFILES= "org.eclipse.jdt.ui.formatterprofiles"; //$NON-NLS-1$
46

47     private final IProfileVersioner fProfileVersioner;
48         
49     public FormatterProfileStore(IProfileVersioner profileVersioner) {
50         super(PREF_FORMATTER_PROFILES, profileVersioner);
51         fProfileVersioner= profileVersioner;
52     }
53     
54     /**
55      * {@inheritDoc}
56      */

57     public List JavaDoc readProfiles(IScopeContext scope) throws CoreException {
58         List JavaDoc profiles= super.readProfiles(scope);
59         if (profiles == null) {
60             profiles= readOldForCompatibility(scope);
61         }
62         return profiles;
63     }
64
65     /**
66      * Read the available profiles from the internal XML file and return them
67      * as collection.
68      * @return returns a list of <code>CustomProfile</code> or <code>null</code>
69      */

70     private List JavaDoc readOldForCompatibility(IScopeContext instanceScope) {
71         
72         // in 3.0 M9 and less the profiles were stored in a file in the plugin's meta data
73
final String JavaDoc STORE_FILE= "code_formatter_profiles.xml"; //$NON-NLS-1$
74

75         File JavaDoc file= JavaPlugin.getDefault().getStateLocation().append(STORE_FILE).toFile();
76         if (!file.exists())
77             return null;
78         
79         try {
80             // note that it's wrong to use a file reader when XML declares UTF-8: Kept for compatibility
81
final FileReader JavaDoc reader= new FileReader JavaDoc(file);
82             try {
83                 List JavaDoc res= readProfilesFromStream(new InputSource JavaDoc(reader));
84                 if (res != null) {
85                     for (int i= 0; i < res.size(); i++) {
86                         fProfileVersioner.update((CustomProfile) res.get(i));
87                     }
88                     writeProfiles(res, instanceScope);
89                 }
90                 file.delete(); // remove after successful write
91
return res;
92             } finally {
93                 reader.close();
94             }
95         } catch (CoreException e) {
96             JavaPlugin.log(e); // log but ignore
97
} catch (IOException JavaDoc e) {
98             JavaPlugin.log(e); // log but ignore
99
}
100         return null;
101     }
102     
103     
104     public static void checkCurrentOptionsVersion() {
105         PreferencesAccess access= PreferencesAccess.getOriginalPreferences();
106         ProfileVersioner profileVersioner= new ProfileVersioner();
107         
108         IScopeContext instanceScope= access.getInstanceScope();
109         IEclipsePreferences uiPreferences= instanceScope.getNode(JavaUI.ID_PLUGIN);
110         int version= uiPreferences.getInt(PREF_FORMATTER_PROFILES + VERSION_KEY_SUFFIX, 0);
111         if (version >= profileVersioner.getCurrentVersion()) {
112             return; // is up to date
113
}
114         try {
115             List JavaDoc profiles= (new FormatterProfileStore(profileVersioner)).readProfiles(instanceScope);
116             if (profiles == null) {
117                 profiles= new ArrayList JavaDoc();
118             }
119             ProfileManager manager= new FormatterProfileManager(profiles, instanceScope, access, profileVersioner);
120             if (manager.getSelected() instanceof CustomProfile) {
121                 manager.commitChanges(instanceScope); // updates JavaCore options
122
}
123             uiPreferences.putInt(PREF_FORMATTER_PROFILES + VERSION_KEY_SUFFIX, profileVersioner.getCurrentVersion());
124             savePreferences(instanceScope);
125                         
126             IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects();
127             for (int i= 0; i < projects.length; i++) {
128                 IScopeContext scope= access.getProjectScope(projects[i]);
129                 if (manager.hasProjectSpecificSettings(scope)) {
130                     manager= new FormatterProfileManager(profiles, scope, access, profileVersioner);
131                     manager.commitChanges(scope); // updates JavaCore project options
132
savePreferences(scope);
133                 }
134             }
135         } catch (CoreException e) {
136             JavaPlugin.log(e);
137         } catch (BackingStoreException e) {
138             JavaPlugin.log(e);
139         }
140     }
141     
142     private static void savePreferences(final IScopeContext context) throws BackingStoreException {
143         try {
144             context.getNode(JavaUI.ID_PLUGIN).flush();
145         } finally {
146             context.getNode(JavaCore.PLUGIN_ID).flush();
147         }
148     }
149 }
150
Popular Tags