1 11 package org.eclipse.help.ui.internal.views; 12 13 import java.io.*; 14 15 import org.eclipse.core.runtime.IPath; 16 import org.eclipse.help.ui.internal.*; 17 import org.eclipse.jface.preference.*; 18 19 22 public class ScopeSet { 23 public static final String SCOPE_DIR_NAME = "scope_sets"; private static final String KEY_DEFAULT = "__DEFAULT__"; public static final String EXT = ".pref"; private String name; 27 private PreferenceStore preferenceStore; 28 private boolean needsSaving; 29 private int defaultSet = -1; 30 31 public ScopeSet() { 32 this(Messages.ScopeSet_default); 33 defaultSet = 1; 34 } 35 36 public ScopeSet(String name) { 37 this.needsSaving = true; 38 this.name = name; 39 } 40 41 public boolean isEditable() { 42 return !isDefault(); 43 } 44 45 public boolean isDefault() { 46 if (defaultSet==1) 47 return true; 48 return getPreferenceStore().getBoolean(KEY_DEFAULT); 49 } 50 51 public boolean isImplicit() { 52 return false; 53 } 54 55 public ScopeSet(ScopeSet set) { 56 this(set.getName()+"_new"); copyFrom(set); 58 } 59 60 public void copyFrom(ScopeSet set) { 61 copy((PreferenceStore)set.getPreferenceStore()); 62 } 63 64 public void dispose() { 65 File file = new File(getFileName(name)); 66 if (file.exists()) 67 file.delete(); 68 } 69 70 public IPreferenceStore getPreferenceStore() { 71 if (preferenceStore==null) { 72 preferenceStore = new PreferenceStore(getFileName(this.name)); 73 try { 74 File file = new File(getFileName(this.name)); 75 if (file.exists()) { 76 preferenceStore.load(); 77 } 78 } 79 catch (IOException e) { 80 String message = Messages.bind(Messages.ScopeSet_errorLoading, name); 81 HelpUIPlugin.logError(message, e); 82 } 83 } 84 return preferenceStore; 85 } 86 87 protected String encodeFileName(String name) { 88 return name; 89 } 90 91 private String getFileName(String name) { 92 IPath location = HelpUIPlugin.getDefault().getStateLocation(); 93 location = location.append(SCOPE_DIR_NAME); 94 location = location.append(encodeFileName(name)+getExtension()); 95 return location.toOSString(); 96 } 97 98 protected String getExtension() { 99 return EXT; 100 } 101 102 private void copy(PreferenceStore store) { 103 try { 104 File file = File.createTempFile("sset", null); FileOutputStream fos = new FileOutputStream(file); 106 store.save(fos, ""); fos.close(); 108 FileInputStream fis = new FileInputStream(file); 109 getPreferenceStore(); 110 preferenceStore.load(fis); 111 preferenceStore.setValue(KEY_DEFAULT, false); 114 fis.close(); 115 } 116 catch (IOException e) { 117 } 118 } 119 122 public String getName() { 123 return name; 124 } 125 128 public void setName(String name) { 129 String oldFileName = getFileName(this.name); 130 File oldFile = new File(oldFileName); 131 if (oldFile.exists()) { 132 if (preferenceStore==null) { 134 oldFile.renameTo(new File(getFileName(name))); 136 } 137 else { 138 oldFile.delete(); 141 preferenceStore.setFilename(getFileName(name)); 142 try { 143 preferenceStore.save(); 144 } 145 catch (IOException e) { 146 String message = Messages.bind(Messages.ScopeSet_errorSaving, name); 147 HelpUIPlugin.logError(message, e); 148 } 149 } 150 } 151 this.name = name; 152 } 153 154 public void save() { 155 getPreferenceStore(); 156 if (preferenceStore.needsSaving() || needsSaving) { 157 try { 158 if (defaultSet != -1) 159 preferenceStore.setValue(KEY_DEFAULT, defaultSet>0); 160 preferenceStore.save(); 161 needsSaving = false; 162 } 163 catch (IOException e) { 164 String message = Messages.bind(Messages.ScopeSet_errorSaving, name); 165 HelpUIPlugin.logError(message, e); 166 } 167 } 168 } 169 170 public boolean getEngineEnabled(EngineDescriptor desc) { 171 IPreferenceStore store = getPreferenceStore(); 172 String key = getMasterKey(desc.getId()); 173 if (store.contains(key)) 174 return store.getBoolean(key); 175 store.setValue(key, desc.isEnabled()); 176 return desc.isEnabled(); 177 } 178 public void setEngineEnabled(EngineDescriptor desc, boolean value) { 179 IPreferenceStore store = getPreferenceStore(); 180 String key = getMasterKey(desc.getId()); 181 store.setValue(key, value); 182 } 183 public static String getMasterKey(String id) { 184 return id + ".master"; } 186 public static String getLabelKey(String id) { 187 return id+".label"; } 189 public static String getDescKey(String id) { 190 return id+".desc"; } 192 } 193 | Popular Tags |