1 11 package org.eclipse.ltk.core.refactoring.participants; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IConfigurationElement; 21 import org.eclipse.core.runtime.IExtensionRegistry; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.Platform; 24 25 import org.eclipse.core.expressions.EvaluationContext; 26 27 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 28 29 import org.eclipse.ltk.internal.core.refactoring.Messages; 30 import org.eclipse.ltk.internal.core.refactoring.ParticipantDescriptor; 31 import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages; 32 import org.eclipse.ltk.internal.core.refactoring.RefactoringCorePlugin; 33 34 class ParticipantExtensionPoint { 35 36 private String fName; 37 38 private String fParticipantID; 39 private List fParticipants; 40 private Class fParticipantClass; 41 42 56 57 public ParticipantExtensionPoint(String name, String participantId, Class clazz) { 58 Assert.isNotNull(name); 59 Assert.isNotNull(participantId); 60 Assert.isNotNull(clazz); 61 fName= name; 62 fParticipantID= participantId; 63 fParticipantClass= clazz; 64 } 65 66 public String getName() { 67 return fName; 68 } 69 70 public RefactoringParticipant[] getParticipants(RefactoringStatus status, RefactoringProcessor processor, Object element, RefactoringArguments arguments, IParticipantDescriptorFilter filter, String [] affectedNatures, SharableParticipants shared) { 71 if (fParticipants == null) 72 init(); 73 74 EvaluationContext evalContext= createEvaluationContext(processor, element, affectedNatures); 75 List result= new ArrayList (); 76 for (Iterator iter= fParticipants.iterator(); iter.hasNext();) { 77 ParticipantDescriptor descriptor= (ParticipantDescriptor)iter.next(); 78 if (!descriptor.isEnabled()) { 79 iter.remove(); 80 } else { 81 try { 82 RefactoringStatus filterStatus= new RefactoringStatus(); 83 if (descriptor.matches(evalContext, filter, filterStatus)) { 84 RefactoringParticipant participant= shared.get(descriptor); 85 if (participant != null) { 86 ((ISharableParticipant)participant).addElement(element, arguments); 87 } else { 88 participant= descriptor.createParticipant(); 89 if (fParticipantClass.isInstance(participant)) { 90 if (participant.initialize(processor, element, arguments)) { 91 participant.setDescriptor(descriptor); 92 result.add(participant); 93 if (participant instanceof ISharableParticipant) 94 shared.put(descriptor, participant); 95 } 96 } else { 97 status.addError(Messages.format( 98 RefactoringCoreMessages.ParticipantExtensionPoint_participant_removed, 99 descriptor.getName())); 100 RefactoringCorePlugin.logErrorMessage( 101 Messages.format( 102 RefactoringCoreMessages.ParticipantExtensionPoint_wrong_type, 103 new String [] {descriptor.getName(), fParticipantClass.getName()})); 104 iter.remove(); 105 } 106 } 107 } else { 108 status.merge(filterStatus); 109 } 110 } catch (CoreException e) { 111 logMalfunctioningParticipant(status, descriptor, e); 112 iter.remove(); 113 } catch (RuntimeException e) { 114 logMalfunctioningParticipant(status, descriptor, e); 115 iter.remove(); 116 } 117 } 118 } 119 120 return (RefactoringParticipant[])result.toArray(new RefactoringParticipant[result.size()]); 121 } 122 123 private void logMalfunctioningParticipant(RefactoringStatus status, ParticipantDescriptor descriptor, Throwable e) { 124 status.addError(Messages.format( 125 RefactoringCoreMessages.ParticipantExtensionPoint_participant_removed, 126 descriptor.getName())); 127 RefactoringCorePlugin.logRemovedParticipant(descriptor, e); 128 } 129 130 private void init() { 131 IExtensionRegistry registry= Platform.getExtensionRegistry(); 132 IConfigurationElement[] ces= registry.getConfigurationElementsFor( 133 RefactoringCorePlugin.getPluginId(), 134 fParticipantID); 135 fParticipants= new ArrayList (ces.length); 136 for (int i= 0; i < ces.length; i++) { 137 ParticipantDescriptor descriptor= new ParticipantDescriptor(ces[i]); 138 IStatus status= descriptor.checkSyntax(); 139 switch (status.getSeverity()) { 140 case IStatus.ERROR: 141 RefactoringCorePlugin.log(status); 142 break; 143 case IStatus.WARNING: 144 case IStatus.INFO: 145 RefactoringCorePlugin.log(status); 146 default: 148 fParticipants.add(descriptor); 149 } 150 } 151 } 152 153 155 private static EvaluationContext createEvaluationContext(RefactoringProcessor processor, Object element, String [] affectedNatures) { 156 EvaluationContext result= new EvaluationContext(null, element); 157 result.addVariable("element", element); result.addVariable("affectedNatures", Arrays.asList(affectedNatures)); result.addVariable("processorIdentifier", processor.getIdentifier()); return result; 161 } 162 } 163 | Popular Tags |