1 11 package org.eclipse.debug.internal.core; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.StringTokenizer ; 19 import java.util.Map.Entry; 20 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IConfigurationElement; 23 import org.eclipse.core.runtime.IExtensionPoint; 24 import org.eclipse.core.runtime.Platform; 25 import org.eclipse.debug.core.DebugPlugin; 26 import org.eclipse.debug.core.ILogicalStructureType; 27 import org.eclipse.debug.core.model.IValue; 28 29 34 public class LogicalStructureManager { 35 36 private static LogicalStructureManager fgDefault; 37 private List fTypes = null; 38 private List fTypeProviders; 39 40 48 private Map fStructureTypeSelections= null; 49 55 private List fStructureTypeIds= null; 56 57 63 public static final String PREF_STRUCTURE_SELECTIONS= "selectedStructures"; 70 public static final String PREF_STRUCTURE_IDS= "allStructures"; 72 public static LogicalStructureManager getDefault() { 73 if (fgDefault == null) { 74 fgDefault = new LogicalStructureManager(); 75 } 76 return fgDefault; 77 } 78 79 84 public ILogicalStructureType[] getLogicalStructureTypes(IValue value) { 85 initialize(); 86 Iterator iterator = fTypes.iterator(); 88 List select = new ArrayList (); 89 while (iterator.hasNext()) { 90 ILogicalStructureType type = (ILogicalStructureType)iterator.next(); 91 if (type.providesLogicalStructure(value)) { 92 select.add(type); 93 } 94 } 95 for (Iterator iter= fTypeProviders.iterator(); iter.hasNext();) { 97 ILogicalStructureType[] logicalStructures= ((LogicalStructureProvider) iter.next()).getLogicalStructures(value); 98 for (int i= 0; i < logicalStructures.length; i++) { 99 select.add(logicalStructures[i]); 100 } 101 } 102 return (ILogicalStructureType[]) select.toArray(new ILogicalStructureType[select.size()]); 103 } 104 105 108 private void loadStructureTypeSelections() { 109 fStructureTypeSelections= new HashMap (); 110 String selections= DebugPlugin.getDefault().getPluginPreferences().getString(PREF_STRUCTURE_SELECTIONS); 111 StringTokenizer tokenizer= new StringTokenizer (selections, "|"); while (tokenizer.hasMoreTokens()) { 115 String selection = tokenizer.nextToken(); 116 int i = selection.lastIndexOf(','); 119 if (i > 0 && i < selection.length() - 1) { 120 String comboKey= selection.substring(0, i + 1); 121 String selected= selection.substring(i + 1, selection.length()); 122 fStructureTypeSelections.put(comboKey, new Integer (Integer.parseInt(selected))); 123 } 124 } 125 } 126 127 130 private void storeStructureTypeSelections() { 131 StringBuffer buffer= new StringBuffer (); 132 Iterator iter = fStructureTypeSelections.entrySet().iterator(); 133 while (iter.hasNext()) { 134 Map.Entry entry = (Entry) iter.next(); 135 buffer.append(entry.getKey()); 136 buffer.append(entry.getValue()); 137 buffer.append('|'); 138 } 139 DebugPlugin.getDefault().getPluginPreferences().setValue(PREF_STRUCTURE_SELECTIONS, buffer.toString()); 140 } 141 142 145 private void loadStructureTypeIds() { 146 fStructureTypeIds= new ArrayList (); 147 String types= DebugPlugin.getDefault().getPluginPreferences().getString(PREF_STRUCTURE_IDS); 149 StringTokenizer tokenizer= new StringTokenizer (types, ","); while (tokenizer.hasMoreTokens()) { 151 String id= tokenizer.nextToken(); 152 if (id.length() > 0) { 153 fStructureTypeIds.add(id); 154 } 155 } 156 } 157 158 161 private void storeStructureTypeIds() { 162 StringBuffer buffer= new StringBuffer (); 163 Iterator iter = fStructureTypeIds.iterator(); 164 while (iter.hasNext()) { 165 buffer.append(iter.next()).append(','); 166 } 167 DebugPlugin.getDefault().getPluginPreferences().setValue(PREF_STRUCTURE_IDS, buffer.toString()); 168 } 169 170 178 public ILogicalStructureType getSelectedStructureType(ILogicalStructureType[] structureTypes) { 179 if (structureTypes.length == 0) { 180 return null; 181 } 182 String combo= getComboString(structureTypes); 183 Integer index = (Integer ) fStructureTypeSelections.get(combo); 185 if (index == null) { 186 return structureTypes[0]; 189 } else if (index.intValue() == -1) { 190 return null; 192 } 193 String id= (String ) fStructureTypeIds.get(index.intValue()); 195 for (int i = 0; i < structureTypes.length; i++) { 196 ILogicalStructureType type = structureTypes[i]; 198 if (type.getId().equals(id)) { 199 return type; 200 } 201 } 202 return structureTypes[0]; 203 } 204 205 211 public void setEnabledType(ILogicalStructureType[] types, ILogicalStructureType selected) { 212 String combo= getComboString(types); 213 int index= -1; if (selected != null) { 215 index= fStructureTypeIds.indexOf(selected.getId()); 216 } 217 Integer integer= new Integer (index); 218 fStructureTypeSelections.put(combo, integer); 219 storeStructureTypeSelections(); 220 storeStructureTypeIds(); 221 } 222 223 233 protected String getComboString(ILogicalStructureType[] types) { 234 StringBuffer comboKey= new StringBuffer (); 235 for (int i = 0; i < types.length; i++) { 236 ILogicalStructureType type = types[i]; 237 int typeIndex = fStructureTypeIds.indexOf(type.getId()); 238 if (typeIndex == -1) { 239 typeIndex= fStructureTypeIds.size(); 240 fStructureTypeIds.add(type.getId()); 241 } 242 comboKey.append(typeIndex).append(','); 243 } 244 return comboKey.toString(); 245 } 246 247 private synchronized void initialize() { 248 if (fTypes == null) { 249 IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(DebugPlugin.getUniqueIdentifier(), DebugPlugin.EXTENSION_POINT_LOGICAL_STRUCTURE_TYPES); 251 IConfigurationElement[] extensions = point.getConfigurationElements(); 252 fTypes = new ArrayList (extensions.length); 253 for (int i = 0; i < extensions.length; i++) { 254 IConfigurationElement extension = extensions[i]; 255 LogicalStructureType type; 256 try { 257 type = new LogicalStructureType(extension); 258 fTypes.add(type); 259 } catch (CoreException e) { 260 DebugPlugin.log(e); 261 } 262 } 263 point= Platform.getExtensionRegistry().getExtensionPoint(DebugPlugin.getUniqueIdentifier(), DebugPlugin.EXTENSION_POINT_LOGICAL_STRUCTURE_PROVIDERS); 265 extensions= point.getConfigurationElements(); 266 fTypeProviders= new ArrayList (extensions.length); 267 for (int i= 0; i < extensions.length; i++) { 268 try { 269 fTypeProviders.add(new LogicalStructureProvider(extensions[i])); 270 } catch (CoreException e) { 271 DebugPlugin.log(e); 272 } 273 } 274 } 275 if (fStructureTypeSelections == null) { 276 loadStructureTypeSelections(); 277 } 278 if (fStructureTypeIds == null) { 279 loadStructureTypeIds(); 280 } 281 } 282 } 283 | Popular Tags |