1 11 12 package org.eclipse.jdt.internal.ui.preferences.formatter; 13 14 import java.io.File ; 15 import java.io.FileReader ; 16 import java.io.IOException ; 17 import java.util.ArrayList ; 18 import java.util.List ; 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 ; 37 38 39 40 public class FormatterProfileStore extends ProfileStore { 41 42 45 private static final String PREF_FORMATTER_PROFILES= "org.eclipse.jdt.ui.formatterprofiles"; 47 private final IProfileVersioner fProfileVersioner; 48 49 public FormatterProfileStore(IProfileVersioner profileVersioner) { 50 super(PREF_FORMATTER_PROFILES, profileVersioner); 51 fProfileVersioner= profileVersioner; 52 } 53 54 57 public List readProfiles(IScopeContext scope) throws CoreException { 58 List profiles= super.readProfiles(scope); 59 if (profiles == null) { 60 profiles= readOldForCompatibility(scope); 61 } 62 return profiles; 63 } 64 65 70 private List readOldForCompatibility(IScopeContext instanceScope) { 71 72 final String STORE_FILE= "code_formatter_profiles.xml"; 75 File file= JavaPlugin.getDefault().getStateLocation().append(STORE_FILE).toFile(); 76 if (!file.exists()) 77 return null; 78 79 try { 80 final FileReader reader= new FileReader (file); 82 try { 83 List res= readProfilesFromStream(new InputSource (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(); return res; 92 } finally { 93 reader.close(); 94 } 95 } catch (CoreException e) { 96 JavaPlugin.log(e); } catch (IOException e) { 98 JavaPlugin.log(e); } 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; } 114 try { 115 List profiles= (new FormatterProfileStore(profileVersioner)).readProfiles(instanceScope); 116 if (profiles == null) { 117 profiles= new ArrayList (); 118 } 119 ProfileManager manager= new FormatterProfileManager(profiles, instanceScope, access, profileVersioner); 120 if (manager.getSelected() instanceof CustomProfile) { 121 manager.commitChanges(instanceScope); } 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); 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 |