1 11 package org.eclipse.debug.internal.ui.views.variables.details; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.LinkedHashSet ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Set ; 22 import java.util.StringTokenizer ; 23 import java.util.Map.Entry; 24 25 import org.eclipse.core.expressions.EvaluationContext; 26 import org.eclipse.core.expressions.EvaluationResult; 27 import org.eclipse.core.expressions.Expression; 28 import org.eclipse.core.expressions.ExpressionConverter; 29 import org.eclipse.core.expressions.ExpressionTagNames; 30 import org.eclipse.core.expressions.IEvaluationContext; 31 import org.eclipse.core.runtime.CoreException; 32 import org.eclipse.core.runtime.IConfigurationElement; 33 import org.eclipse.core.runtime.IExtensionPoint; 34 import org.eclipse.core.runtime.IStatus; 35 import org.eclipse.core.runtime.Platform; 36 import org.eclipse.core.runtime.Status; 37 import org.eclipse.debug.internal.core.IConfigurationElementConstants; 38 import org.eclipse.debug.internal.ui.DebugUIPlugin; 39 import org.eclipse.debug.ui.IDebugUIConstants; 40 import org.eclipse.debug.ui.IDetailPane; 41 import org.eclipse.debug.ui.IDetailPaneFactory; 42 import org.eclipse.jface.viewers.IStructuredSelection; 43 44 53 public class DetailPaneManager { 54 55 60 private class DetailPaneFactoryExtension implements IDetailPaneFactory{ 61 62 private IConfigurationElement fConfigElement; 63 private IDetailPaneFactory fFactory; 64 private Expression fEnablementExpression; 65 66 public DetailPaneFactoryExtension(IConfigurationElement configElement){ 67 fConfigElement = configElement; 68 } 69 70 74 public IDetailPane createDetailPane(String paneID){ 75 if (getFactory() != null){ 76 return getFactory().createDetailPane(paneID); 77 } 78 return null; 79 } 80 81 85 public Set getDetailPaneTypes(IStructuredSelection selection){ 86 if (getFactory() != null){ 87 return getFactory().getDetailPaneTypes(selection); 88 } 89 return new HashSet (0); 90 } 91 92 96 public String getDefaultDetailPane(IStructuredSelection selection) { 97 if (getFactory() != null){ 98 return getFactory().getDefaultDetailPane(selection); 99 } 100 return null; 101 } 102 103 107 public String getDetailPaneName(String paneID) { 108 if (getFactory() != null){ 109 return getFactory().getDetailPaneName(paneID); 110 } 111 return null; 112 } 113 114 118 public String getDetailPaneDescription(String paneID) { 119 if (getFactory() != null){ 120 return getFactory().getDetailPaneDescription(paneID); 121 } 122 return null; 123 } 124 125 128 private IDetailPaneFactory getFactory(){ 129 if (fFactory != null) return fFactory; 130 try{ 131 Object obj = fConfigElement.createExecutableExtension(IConfigurationElementConstants.CLASS); 132 if(obj instanceof IDetailPaneFactory) { 133 fFactory = (IDetailPaneFactory)obj; 134 } else { 135 throw new CoreException(new Status(IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.INTERNAL_ERROR, "org.eclipse.debug.ui.detailFactories extension failed to load a detail factory because the specified class does not implement org.eclipse.debug.ui.IDetailPaneFactory. Class specified was: " + obj, null)); } 137 } catch (CoreException e){ 138 DebugUIPlugin.log(e.getStatus()); 139 fFactory = null; 140 } 141 return fFactory; 142 } 143 144 148 public boolean isEnabled(IStructuredSelection selection) { 149 boolean enabled = false; 150 if (selection == null || selection.isEmpty()){ 152 return "org.eclipse.debug.ui.defaultDetailPaneFactory".equals(fConfigElement.getAttribute(IConfigurationElementConstants.ID)); } 154 Expression expression = getEnablementExpression(); 155 if (expression != null) { 156 List list = selection.toList(); 157 IEvaluationContext context = new EvaluationContext(null, list); 158 context.addVariable("selection", list); enabled = evalEnablementExpression(context, expression); 160 } else { 161 enabled = true; 162 } 163 return enabled; 164 } 165 166 177 private boolean evalEnablementExpression(IEvaluationContext context, Expression exp) { 178 try{ 179 if (exp != null){ 180 EvaluationResult result = exp.evaluate(context); 181 if (result == EvaluationResult.TRUE || result == EvaluationResult.NOT_LOADED){ 182 return true; 183 } 184 } 185 } catch (CoreException e){ 186 DebugUIPlugin.log(e.getStatus()); 187 } 188 return false; 189 } 190 191 197 private Expression getEnablementExpression(){ 198 if (fEnablementExpression == null) { 200 try{ 201 IConfigurationElement[] elements = fConfigElement.getChildren(ExpressionTagNames.ENABLEMENT); 202 IConfigurationElement enablement = elements.length > 0 ? elements[0] : null; 203 if (enablement != null) { 204 fEnablementExpression = ExpressionConverter.getDefault().perform(enablement); 205 } 206 } catch (CoreException e){ 207 DebugUIPlugin.log(e.getStatus()); 208 fEnablementExpression = null; 209 } 210 } 211 return fEnablementExpression; 212 } 213 214 } 215 216 219 private static DetailPaneManager fgSingleton; 220 221 225 private Map fFactoriesByPaneID; 226 227 230 private Map fPreferredDetailPanes; 231 232 235 private Collection fKnownFactories; 236 237 242 public static final String PREF_DETAIL_AREAS = "preferredDetailPanes"; 244 private DetailPaneManager(){ 245 fFactoriesByPaneID = new HashMap (); 246 } 247 248 public static DetailPaneManager getDefault(){ 249 if (fgSingleton == null) fgSingleton = new DetailPaneManager(); 250 return fgSingleton; 251 } 252 253 259 public String getPreferredPaneFromSelection(IStructuredSelection selection){ 260 Collection possibleFactories = getEnabledFactories(selection); 261 Set possiblePaneIDs = getPossiblePaneIDs(possibleFactories, selection); 262 return chooseDetailsAreaIDInSet(possiblePaneIDs, possibleFactories, selection); 263 } 264 265 272 public Set getAvailablePaneIDs(IStructuredSelection selection){ 273 Collection possibleFactories = getEnabledFactories(selection); 274 return getPossiblePaneIDs(possibleFactories, selection); 275 } 276 277 287 public IDetailPane getDetailPaneFromID(String ID){ 288 IDetailPaneFactory factory = (IDetailPaneFactory)fFactoriesByPaneID.get(ID); 289 if (factory != null){ 290 return factory.createDetailPane(ID); 291 } 292 return null; 293 } 294 295 302 public String getNameFromID(String ID){ 303 IDetailPaneFactory factory = (IDetailPaneFactory)fFactoriesByPaneID.get(ID); 304 if (factory != null){ 305 return factory.getDetailPaneName(ID); 306 } 307 return null; 308 } 309 310 317 public String getDescriptionFromID(String ID){ 318 IDetailPaneFactory factory = (IDetailPaneFactory)fFactoriesByPaneID.get(ID); 319 if (factory != null){ 320 return factory.getDetailPaneDescription(ID); 321 } 322 return null; 323 } 324 325 326 334 private Collection getEnabledFactories(IStructuredSelection selection){ 335 Collection factoriesForSelection = new ArrayList (); 336 if (fKnownFactories == null) initializeDetailFactories(); 337 Iterator iter = fKnownFactories.iterator(); 338 while (iter.hasNext()) { 339 IDetailPaneFactory currentFactory = (IDetailPaneFactory) iter.next(); 340 if (currentFactory instanceof DetailPaneFactoryExtension){ 341 if (((DetailPaneFactoryExtension)currentFactory).isEnabled(selection)){ 342 factoriesForSelection.add(currentFactory); 343 } 344 } 345 } 346 return factoriesForSelection; 347 } 348 349 357 private Set getPossiblePaneIDs(Collection factoriesToQuery, IStructuredSelection selection){ 358 Set idsForSelection = new LinkedHashSet (); 359 Iterator factoryIter = factoriesToQuery.iterator(); 360 while (factoryIter.hasNext()) { 361 IDetailPaneFactory currentFactory = (IDetailPaneFactory) factoryIter.next(); 362 Iterator detailAreaTypes = currentFactory.getDetailPaneTypes(selection).iterator(); 363 while (detailAreaTypes.hasNext()) { 364 String currentAreaTypeID = (String ) detailAreaTypes.next(); 365 fFactoriesByPaneID.put(currentAreaTypeID, currentFactory); 366 idsForSelection.add(currentAreaTypeID); 367 } 368 } 369 return idsForSelection; 370 } 371 372 380 private String chooseDetailsAreaIDInSet(Set possiblePaneIDs, Collection enabledFactories, IStructuredSelection selection){ 381 if (possiblePaneIDs == null || possiblePaneIDs.isEmpty()){ 382 return null; 383 } 384 385 String preferredID = getUserPreferredDetailPane(possiblePaneIDs); 386 387 if (preferredID == null){ 388 Iterator factoryIterator = enabledFactories.iterator(); 390 while (preferredID == null && factoryIterator.hasNext()) { 391 IDetailPaneFactory currentFactory = (IDetailPaneFactory) factoryIterator.next(); 392 preferredID = currentFactory.getDefaultDetailPane(selection); 393 } 394 if (preferredID == null){ 396 Iterator paneIterator = possiblePaneIDs.iterator(); 397 preferredID = (String )paneIterator.next(); 399 while (paneIterator.hasNext() && preferredID != DefaultDetailPaneFactory.DEFAULT_DETAIL_PANE_ID) { 400 String currentID = (String ) paneIterator.next(); 401 if (currentID.equals(DefaultDetailPaneFactory.DEFAULT_DETAIL_PANE_ID)){ 402 preferredID = currentID; 403 } 404 } 405 } 406 setPreferredDetailPane(possiblePaneIDs, preferredID); 407 } 408 409 return preferredID; 410 } 411 412 415 private synchronized void initializeDetailFactories(){ 416 if (fKnownFactories == null){ 417 fKnownFactories = new ArrayList (); 418 IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.EXTENSION_POINT_DETAIL_FACTORIES); 419 IConfigurationElement[] infos = extensionPoint.getConfigurationElements(); 420 DetailPaneFactoryExtension delegate = null; 421 for(int i = 0; i < infos.length; i++) { 422 delegate = new DetailPaneFactoryExtension(infos[i]); 423 fKnownFactories.add(delegate); 424 } 425 } 426 } 427 428 434 public String getUserPreferredDetailPane(Set possibleDetailsAreaIDs){ 435 if (fPreferredDetailPanes == null){ 436 loadPreferredDetailsAreas(); 437 } 438 return (String )fPreferredDetailPanes.get(possibleDetailsAreaIDs); 439 440 } 441 442 449 public void setPreferredDetailPane(Set possibleDetailsAreaIDs, String preferredDetailsAreaID){ 450 if (possibleDetailsAreaIDs == null) return; 451 if (fPreferredDetailPanes == null){ 452 loadPreferredDetailsAreas(); 453 } 454 String currentKey = (String )fPreferredDetailPanes.get(possibleDetailsAreaIDs); 455 if (currentKey == null || !currentKey.equals(preferredDetailsAreaID)){ 456 fPreferredDetailPanes.put(possibleDetailsAreaIDs, preferredDetailsAreaID); 457 storePreferredDetailsAreas(); 458 } 459 460 } 461 462 470 private void storePreferredDetailsAreas() { 471 StringBuffer buffer= new StringBuffer (); 472 Iterator iter = fPreferredDetailPanes.entrySet().iterator(); 473 while (iter.hasNext()) { 474 Entry entry = (Entry) iter.next(); 475 Iterator setIter = ((Set )entry.getKey()).iterator(); 476 while (setIter.hasNext()) { 477 String currentID = (String ) setIter.next(); 478 buffer.append(currentID); 479 buffer.append(','); 480 } 481 buffer.deleteCharAt(buffer.length()-1); 482 buffer.append(':'); 483 buffer.append(entry.getValue()); 484 buffer.append('|'); 485 } 486 DebugUIPlugin.getDefault().getPluginPreferences().setValue(PREF_DETAIL_AREAS, buffer.toString()); 487 } 488 489 494 private void loadPreferredDetailsAreas() { 495 fPreferredDetailPanes = new HashMap (); 496 String preferenceValue = DebugUIPlugin.getDefault().getPluginPreferences().getString(PREF_DETAIL_AREAS); 497 StringTokenizer entryTokenizer = new StringTokenizer (preferenceValue,"|"); while (entryTokenizer.hasMoreTokens()){ 499 String token = entryTokenizer.nextToken(); 500 int valueStart = token.indexOf(':'); 501 StringTokenizer keyTokenizer = new StringTokenizer (token.substring(0,valueStart),","); Set keys = new LinkedHashSet (); 503 while (keyTokenizer.hasMoreTokens()){ 504 keys.add(keyTokenizer.nextToken()); 505 } 506 fPreferredDetailPanes.put(keys, token.substring(valueStart+1)); 507 } 508 } 509 510 } 511 | Popular Tags |