OLD | NEW |
(Empty) | |
| 1 /** |
| 2 * |
| 3 */ |
| 4 package com.xored.glance.internal.ui.sources; |
| 5 |
| 6 import java.util.ArrayList; |
| 7 import java.util.Collections; |
| 8 import java.util.Comparator; |
| 9 import java.util.HashMap; |
| 10 import java.util.List; |
| 11 import java.util.Map; |
| 12 |
| 13 import org.eclipse.core.runtime.IConfigurationElement; |
| 14 import org.eclipse.core.runtime.Platform; |
| 15 |
| 16 import com.xored.glance.internal.ui.GlancePlugin; |
| 17 import com.xored.glance.ui.sources.ITextSourceDescriptor; |
| 18 |
| 19 /** |
| 20 * @author Yuri Strot |
| 21 */ |
| 22 public class TextSourceManager { |
| 23 |
| 24 public static TextSourceManager getInstance() { |
| 25 if (instance == null) |
| 26 instance = new TextSourceManager(); |
| 27 return instance; |
| 28 } |
| 29 |
| 30 public void addSourceProviderListener(ISourceProviderListener listener) { |
| 31 assert listener != null; |
| 32 getListener().addSourceProviderListener(listener); |
| 33 } |
| 34 |
| 35 public void removeSourceProviderListener(ISourceProviderListener listener) { |
| 36 getListener().removeSourceProviderListener(listener); |
| 37 } |
| 38 |
| 39 public TextSourceMaker getSource() { |
| 40 return getListener().getSelection(); |
| 41 } |
| 42 |
| 43 private TextSourceListener getListener() { |
| 44 if (listener == null) { |
| 45 listener = new TextSourceListener(getDescriptors()); |
| 46 } |
| 47 return listener; |
| 48 } |
| 49 |
| 50 private ITextSourceDescriptor[] getDescriptors() { |
| 51 if (descriptors == null) { |
| 52 |
| 53 List<Class<?>> excludedClasses = new ArrayList<Class<?>>(); |
| 54 |
| 55 IConfigurationElement[] excludedConfigs = Platform.getExtensionRegistry().
getConfigurationElementsFor( |
| 56 EXT_EXCLUDED_SOURCES); |
| 57 |
| 58 for (IConfigurationElement config : excludedConfigs) { |
| 59 try { |
| 60 ITextSourceDescriptor descriptor = (ITextSourceDescriptor) config.crea
teExecutableExtension(ATTR_CLASS); |
| 61 excludedClasses.add(descriptor.getClass()); |
| 62 } catch (Exception e) { |
| 63 GlancePlugin.log(e); |
| 64 } |
| 65 } |
| 66 |
| 67 IConfigurationElement[] configs = Platform.getExtensionRegistry().getConfi
gurationElementsFor( |
| 68 EXT_SOURCES); |
| 69 List<ITextSourceDescriptor> list = new ArrayList<ITextSourceDescriptor>(); |
| 70 final Map<ITextSourceDescriptor, Integer> prior = new HashMap<ITextSourceD
escriptor, Integer>(); |
| 71 |
| 72 for (IConfigurationElement config : configs) { |
| 73 try { |
| 74 ITextSourceDescriptor descriptor = (ITextSourceDescriptor) config.crea
teExecutableExtension(ATTR_CLASS); |
| 75 if (excludedClasses.contains(descriptor.getClass())) { |
| 76 continue; |
| 77 } |
| 78 int priority = toInt(config.getAttribute(ATTR_PRIORITY)); |
| 79 list.add(descriptor); |
| 80 prior.put(descriptor, priority); |
| 81 } catch (Exception e) { |
| 82 GlancePlugin.log(e); |
| 83 } |
| 84 } |
| 85 |
| 86 Collections.sort(list, new Comparator<ITextSourceDescriptor>() { |
| 87 |
| 88 public int compare(ITextSourceDescriptor o1, ITextSourceDescriptor o2) { |
| 89 return prior.get(o2) - prior.get(o1); |
| 90 } |
| 91 |
| 92 }); |
| 93 descriptors = list.toArray(new ITextSourceDescriptor[list.size()]); |
| 94 } |
| 95 return descriptors; |
| 96 } |
| 97 |
| 98 private int toInt(String priority) { |
| 99 try { |
| 100 if (priority == null || priority.length() == 0) |
| 101 return 1; |
| 102 return Integer.parseInt(priority); |
| 103 } catch (Exception e) { |
| 104 GlancePlugin.log(e); |
| 105 } |
| 106 return 1; |
| 107 } |
| 108 |
| 109 private TextSourceManager() { |
| 110 } |
| 111 |
| 112 private final static String ATTR_PRIORITY = "priority"; |
| 113 private final static String ATTR_CLASS = "class"; |
| 114 private final static String EXT_SOURCES = GlancePlugin.PLUGIN_ID + ".sources"; |
| 115 private final static String EXT_EXCLUDED_SOURCES = GlancePlugin.PLUGIN_ID + ".
excludedSources"; |
| 116 |
| 117 private static TextSourceManager instance; |
| 118 |
| 119 private TextSourceListener listener; |
| 120 private ITextSourceDescriptor[] descriptors; |
| 121 |
| 122 } |
OLD | NEW |