1 11 package org.eclipse.help.ui.internal.views; 12 13 import java.io.*; 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.help.ui.internal.HelpUIPlugin; 18 import org.eclipse.jface.dialogs.IDialogSettings; 19 20 23 public class ScopeSetManager { 24 private ScopeSet activeSet; 25 26 private ScopeSet lastExplicitSet; 27 28 private static final String ACTIVE_SET = "activeScopeSet"; 30 private ArrayList sets; 31 32 public ScopeSetManager() { 33 ensureLocation(); 34 loadScopeSets(); 35 } 36 37 public void add(ScopeSet set) { 38 sets.add(set); 39 } 40 41 public void remove(ScopeSet set) { 42 sets.remove(set); 43 set.dispose(); 44 } 45 46 public void setActiveSet(ScopeSet set) { 47 if (this.activeSet != null) { 48 this.activeSet.save(); 49 } 50 this.activeSet = set; 51 if (!activeSet.isImplicit()) 52 lastExplicitSet = set; 53 } 54 55 public boolean restoreLastExplicitSet() { 56 if (activeSet != null && activeSet.isImplicit() 57 && lastExplicitSet != null) { 58 setActiveSet(lastExplicitSet); 59 return true; 60 } 61 return false; 62 } 63 64 public static void ensureLocation() { 65 IPath location = HelpUIPlugin.getDefault().getStateLocation(); 66 location = location.append(ScopeSet.SCOPE_DIR_NAME); 67 File dir = location.toFile(); 68 if (dir.exists() == false) 69 dir.mkdir(); 70 } 71 72 public void save() { 73 ensureLocation(); 74 for (int i = 0; i < sets.size(); i++) { 75 ScopeSet set = (ScopeSet) sets.get(i); 76 set.save(); 77 } 78 IDialogSettings settings = HelpUIPlugin.getDefault() 79 .getDialogSettings(); 80 if (activeSet != null) 81 settings.put(ACTIVE_SET, activeSet.getName()); 82 } 83 84 public ScopeSet[] getScopeSets(boolean implicit) { 85 ArrayList result = new ArrayList (); 86 for (int i = 0; i < sets.size(); i++) { 87 ScopeSet set = (ScopeSet) sets.get(i); 88 if (set.isImplicit() == implicit) 89 result.add(set); 90 if (!implicit && set.isImplicit() && activeSet==set) 91 result.add(set); 92 } 93 return (ScopeSet[]) result.toArray(new ScopeSet[result.size()]); 94 } 95 96 private void loadScopeSets() { 97 sets = new ArrayList (); 98 IPath location = HelpUIPlugin.getDefault().getStateLocation(); 99 location = location.append("scope_sets"); File dir = location.toFile(); 101 ScopeSet defSet = null; 102 if (dir.exists() && dir.isDirectory()) { 103 File[] files = dir.listFiles(new FilenameFilter() { 104 public boolean accept(File dir, String name) { 105 return name.endsWith(ScopeSet.EXT) 106 || name.endsWith(HistoryScopeSet.EXT); 107 } 108 }); 109 for (int i = 0; i < files.length; i++) { 110 File file = files[i]; 111 String name = file.getName(); 112 int loc = name.lastIndexOf(ScopeSet.EXT); 113 if (loc != -1) { 114 ScopeSet set = new ScopeSet(name.substring(0, loc)); 115 sets.add(set); 116 if (set.isDefault()) 117 defSet = set; 118 continue; 119 } 120 loc = name.lastIndexOf(HistoryScopeSet.EXT); 121 if (loc != -1) { 122 HistoryScopeSet set = new HistoryScopeSet(name.substring(0, 123 loc), null); 124 sets.add(set); 125 } 126 } 127 } 128 if (sets.size() == 1) { 129 activeSet = (ScopeSet) sets.get(0); 130 } 131 if (defSet == null) 132 sets.add(new ScopeSet()); 133 } 134 135 138 public ScopeSet getActiveSet() { 139 if (activeSet == null) { 140 IDialogSettings settings = HelpUIPlugin.getDefault() 141 .getDialogSettings(); 142 String name = settings.get(ACTIVE_SET); 143 activeSet = findSet(name); 144 if (!activeSet.isImplicit()) 145 lastExplicitSet = activeSet; 146 } 147 return activeSet; 148 } 149 150 public ScopeSet findSet(String name) { 151 return findSet(name, false); 152 } 153 154 public HistoryScopeSet findSearchSet(String expression) { 155 for (int i = 0; i < sets.size(); i++) { 156 ScopeSet set = (ScopeSet) sets.get(i); 157 if (!set.isImplicit() || !(set instanceof HistoryScopeSet)) 158 continue; 159 HistoryScopeSet sset = (HistoryScopeSet) set; 160 if (sset.getExpression().equals(expression)) 161 return sset; 162 } 163 return null; 164 } 165 166 public ScopeSet findSet(String name, boolean implicit) { 167 ScopeSet defaultSet = null; 168 for (int i = 0; i < sets.size(); i++) { 169 ScopeSet set = (ScopeSet) sets.get(i); 170 if (name != null && set.isImplicit() == implicit) { 171 if (set.getName().equals(name)) 172 return set; 173 } else if (set.isDefault()) 174 defaultSet = set; 175 } 176 if (!implicit) 177 return defaultSet; 178 return null; 179 } 180 } | Popular Tags |