| 1 | package de.uka.ipd.sdq.cip.completions; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.List; |
| 6 | |
| 7 | import org.eclipse.core.runtime.IConfigurationElement; |
| 8 | import org.eclipse.core.runtime.IExtensionRegistry; |
| 9 | import org.eclipse.core.runtime.InvalidRegistryObjectException; |
| 10 | import org.eclipse.core.runtime.Platform; |
| 11 | import org.eclipse.jface.viewers.IBaseLabelProvider; |
| 12 | import org.eclipse.jface.viewers.ITableLabelProvider; |
| 13 | import org.eclipse.jface.viewers.LabelProvider; |
| 14 | import org.eclipse.swt.graphics.Image; |
| 15 | |
| 16 | import de.uka.ipd.sdq.cip.CipPlugin; |
| 17 | import de.uka.ipd.sdq.cip.ConstantsContainer; |
| 18 | |
| 19 | /** |
| 20 | * @author Thomas Schuischel |
| 21 | * |
| 22 | */ |
| 23 | public class RegisteredCompletions { |
| 24 | |
| 25 | /** |
| 26 | * Cache enumerated completions for performance |
| 27 | */ |
| 28 | private static Collection<RegisteredCompletion> cachedCompletions; |
| 29 | |
| 30 | public static RegisteredCompletion findCompletion(String ID) { |
| 31 | if(cachedCompletions == null) |
| 32 | getCompletions(); |
| 33 | |
| 34 | for(RegisteredCompletion completion : cachedCompletions) |
| 35 | { |
| 36 | if(completion.getId().equals(ID)) |
| 37 | return completion; |
| 38 | } |
| 39 | |
| 40 | return null; |
| 41 | } |
| 42 | |
| 43 | public static Collection<RegisteredCompletion> getCompletions() { |
| 44 | if(cachedCompletions != null) |
| 45 | return cachedCompletions; |
| 46 | |
| 47 | List<RegisteredCompletion> found = new ArrayList<RegisteredCompletion>(); |
| 48 | IExtensionRegistry reg = Platform.getExtensionRegistry(); |
| 49 | try { |
| 50 | IConfigurationElement[] elements = reg.getConfigurationElementsFor(CipPlugin.PLUGIN_ID,ConstantsContainer.COMPLETION_EXTENSION_POINT_ID); |
| 51 | for(int i = 0; i < elements.length; i++) { |
| 52 | IConfigurationElement element = elements[i]; |
| 53 | if(element.getName().equals(ConstantsContainer.NODE_COMPLETION)){ |
| 54 | RegisteredCompletion rc = RegisteredCompletion.parseCompletion(element,element.getContributor().getName()); |
| 55 | if(rc != null) |
| 56 | found.add(rc); |
| 57 | |
| 58 | } |
| 59 | } |
| 60 | cachedCompletions = found; |
| 61 | } |
| 62 | catch (InvalidRegistryObjectException e) { |
| 63 | //logger.logError(msg,e); |
| 64 | } |
| 65 | |
| 66 | return cachedCompletions; |
| 67 | } |
| 68 | |
| 69 | private static class RegisteredCompletionLabelProvider extends LabelProvider implements ITableLabelProvider { |
| 70 | |
| 71 | @Override |
| 72 | public Image getColumnImage(Object element, int columnIndex) { |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String getColumnText(Object element, int columnIndex) { |
| 78 | RegisteredCompletion rc = (RegisteredCompletion) element; |
| 79 | switch (columnIndex) { |
| 80 | case 0: |
| 81 | return rc.getName(); |
| 82 | case 1: |
| 83 | return rc.getId(); |
| 84 | |
| 85 | default: |
| 86 | return "unkown " + columnIndex; |
| 87 | } |
| 88 | |
| 89 | } |
| 90 | |
| 91 | } |
| 92 | |
| 93 | public static IBaseLabelProvider getLabelProvider() { |
| 94 | return new RegisteredCompletions.RegisteredCompletionLabelProvider(); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | } |