001: package net.refractions.udig.project.ui;
002:
003: import java.util.HashMap;
004: import java.util.List;
005: import java.util.logging.Logger;
006:
007: import net.refractions.udig.core.internal.ExtensionPointList;
008: import net.refractions.udig.project.geoselection.IGeoSelectionChangedListener;
009: import net.refractions.udig.project.geoselection.IGeoSelectionManager;
010: import net.refractions.udig.project.geoselection.IGeoSelectionService;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IConfigurationElement;
014: import org.eclipse.core.runtime.InvalidRegistryObjectException;
015:
016: /**
017: * Default UDIG implementation of IGeoSelectionService with a platform selection manager
018: * working for the currently active MapEditor.
019: * <p>
020: *
021: * @author Vitalus
022: *
023: */
024: final public class GeoSelectionService implements IGeoSelectionService {
025:
026: /**
027: * Logger.
028: */
029: public static Logger LOGGER = Logger
030: .getLogger("net.refractions.udig.project.geoselection"); //$NON-NLS-1$
031:
032: public static final String EXTENSION_POINT_ID = "net.refractions.udig.project.geoselection"; //$NON-NLS-1$
033:
034: private static GeoSelectionService instance;
035:
036: private IGeoSelectionManager platformSelectionManager = null;
037:
038: private HashMap<String, IGeoSelectionManager> registeredManagers = new HashMap<String, IGeoSelectionManager>();
039:
040: private GeoSelectionService() {
041:
042: platformSelectionManager = new PlatformGeoSelectionManager();
043: registerSelectionManager(PlatformGeoSelectionManager.ID,
044: platformSelectionManager);
045: try {
046: processExtensionPoint();
047: } catch (Throwable t) {
048: t.printStackTrace();
049: }
050: }
051:
052: /**
053: * @return
054: */
055: public IGeoSelectionManager getPlatformSelectionManager() {
056: // if (platformSelectionManager == null) {
057: // platformSelectionManager = new PlatformGeoSelectionManager();
058: // registerSelectionManager(PLATFORM_SELECTION_MANAGER_ID, platformSelectionManager);
059: // }
060: return platformSelectionManager;
061: }
062:
063: public static GeoSelectionService getDefault() {
064: if (instance == null) {
065: instance = new GeoSelectionService();
066: }
067: return instance;
068: }
069:
070: /**
071: * @param selectionManager
072: */
073: public void registerSelectionManager(String id,
074: IGeoSelectionManager selectionManager) {
075: if (registeredManagers.containsKey(id))
076: throw new IllegalArgumentException(
077: "The IGeoSelectionManager instance with id= " + id
078: + " is already registered");
079:
080: registeredManagers.put(id, selectionManager);
081: }
082:
083: /**
084: * @param selectionManager
085: */
086: public void unregisterSelectionManager(String id) {
087: registeredManagers.remove(id);
088: }
089:
090: public IGeoSelectionManager getSelectionManager(String id) {
091: return registeredManagers.get(id);
092: }
093:
094: private void processExtensionPoint() {
095: List<IConfigurationElement> extensionList = ExtensionPointList
096: .getExtensionPointList(EXTENSION_POINT_ID);
097:
098: for (IConfigurationElement element : extensionList) {
099: // IExtension extension = element.getDeclaringExtension();
100: String type = element.getName();
101:
102: if (type.equals("geoSelectionListener")) {
103:
104: try {
105:
106: String id = element.getAttribute("id");
107: String managerId = element
108: .getAttribute("managerId");
109:
110: Object listenerObj = element
111: .createExecutableExtension("class");
112: IGeoSelectionChangedListener listener = (IGeoSelectionChangedListener) listenerObj;
113:
114: IGeoSelectionManager manager = getSelectionManager(managerId);
115: if (manager != null) {
116: manager.addListener(listener);
117: }
118:
119: } catch (InvalidRegistryObjectException e) {
120: // TODO Auto-generated catch block
121: e.printStackTrace();
122: } catch (CoreException e) {
123: // TODO Auto-generated catch block
124: e.printStackTrace();
125: }
126: }
127:
128: }
129: }
130:
131: }
|