Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / MapContextLocator.java @ 44297

History | View | Annotate | Download (7.08 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40559 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 40435 jjdelcerro
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8 40559 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * of the License, or (at your option) any later version.
10 40559 jjdelcerro
 *
11 40435 jjdelcerro
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15 40559 jjdelcerro
 *
16 40435 jjdelcerro
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18 40559 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 40435 jjdelcerro
 * MA  02110-1301, USA.
20 40559 jjdelcerro
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 40435 jjdelcerro
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2009 {DiSiD Technologies}  {{Task}}
27
 */
28
package org.gvsig.fmap.mapcontext;
29
30 40713 jldominguez
import java.util.ArrayList;
31
import java.util.Iterator;
32
import java.util.List;
33
34
import org.gvsig.fmap.mapcontext.layers.order.LayerOrderManager;
35 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
36 40713 jldominguez
import org.gvsig.tools.ToolsLocator;
37
import org.gvsig.tools.extensionpoint.ExtensionPoint;
38
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
39
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
40 40435 jjdelcerro
import org.gvsig.tools.locator.BaseLocator;
41
import org.gvsig.tools.locator.Locator;
42
import org.gvsig.tools.locator.LocatorException;
43 40713 jldominguez
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
45 40435 jjdelcerro
46
/**
47
 * Locator for the MapContext library.
48
 *
49
 * @author <a href="mailto:cordinyana@gvsig.org">C?sar Ordi?ana</a>
50
 */
51
public class MapContextLocator extends BaseLocator {
52
53 40713 jldominguez
        private static Logger logger = LoggerFactory.getLogger(MapContextLocator.class);
54
55 40435 jjdelcerro
        public static final String MAPCONTEXT_MANAGER_NAME = "mapcontextlocator.manager";
56
        private static final String MAPCONTEXT_MANAGER_DESCRIPTION = "MapContext Library manager";
57
58
        public static final String SYMBOL_MANAGER_NAME = "symbol.manager";
59
        private static final String SYMBOL_MANAGER_DESCRIPTION = "Symbols manager";
60
61 40713 jldominguez
        public static final String DEFAULT_LAYER_ORDER_MANAGER_NAME = "default.layer.order.manager";
62
        private static final String DEFAULT_LAYER_ORDER_MANAGER_DESCRIPTION = "Default layer order manager";
63
        public static final String LAYER_ORDER_MANAGER_NAME =
64
                        "layer.order.manager.name";
65
        public static final String LAYER_ORDER_MANAGER_EXT_POINT =
66
                        "layer.order.manager.extension.point";
67
68 40435 jjdelcerro
        private static final MapContextLocator instance = new MapContextLocator();
69
70
        /**
71
         * Private constructor, we don't want it to be able to be instantiated
72
         * outside this class.
73
         */
74
        private MapContextLocator() {
75
                // Nothing to do
76
        }
77
78
        public static MapContextLocator getInstance() {
79
                return instance;
80
        }
81
82
        /**
83
         * Return a reference to MapContextManager.
84
         *
85
         * @return a reference to MapContextManager
86
         * @throws LocatorException
87
         *             if there is no access to the class or the class cannot be
88
         *             instantiated
89
         * @see Locator#get(String)
90
         */
91
        public static MapContextManager getMapContextManager()
92
                        throws LocatorException {
93
                return (MapContextManager) getInstance().get(MAPCONTEXT_MANAGER_NAME);
94
        }
95
96
        /**
97
         * Registers the Class implementing the MapContextManager interface.
98
         *
99
         * @param clazz
100
         *            implementing the MapContextManager interface
101
         */
102
        public static void registerMapContextManager(Class clazz) {
103
                getInstance().register(MAPCONTEXT_MANAGER_NAME,
104
                                MAPCONTEXT_MANAGER_DESCRIPTION, clazz);
105
        }
106
107
        /**
108
         * Return a reference to the {@link SymbolManager}.
109
         *
110
         * @return a reference to the SymbolManager.
111
         * @throws LocatorException
112
         *             if there is no access to the class or the class cannot be
113
         *             instantiated
114
         * @see Locator#get(String)
115
         */
116
        public static SymbolManager getSymbolManager() throws LocatorException {
117
                return (SymbolManager) getInstance().get(SYMBOL_MANAGER_NAME);
118
        }
119
120
        /**
121
         * Registers the Class implementing the SymbolManager interface.
122
         *
123
         * @param clazz
124
         *            implementing the SymbolManager interface
125
         */
126
        public static void registerSymbolManager(Class clazz) {
127
                getInstance().register(SYMBOL_MANAGER_NAME, SYMBOL_MANAGER_DESCRIPTION,
128
                                clazz);
129
        }
130 40713 jldominguez
131
        /* *************************************
132
         * Layer order management
133
         * *************************************
134
         */
135
136
        /**
137
         * Register the class as the default order manager.
138
         * This will only work if there is not a default order manager already.
139
         * Also registers the class in the list of available order managers.
140
         *
141
         * @param lom_class
142
         * @throws LocatorException
143
         */
144
        public static void registerDefaultOrderManager(Class lom_class)
145
                        throws LocatorException {
146
147
                if (LayerOrderManager.class.isAssignableFrom(lom_class)) {
148
                        getInstance().registerDefault(
149
                                        DEFAULT_LAYER_ORDER_MANAGER_NAME,
150
                                        DEFAULT_LAYER_ORDER_MANAGER_DESCRIPTION,
151
                                        lom_class);
152
153
                        // Also register as available order manager
154
                        registerOrderManager(lom_class);
155
156
                } else {
157
                        // not valid class passed:
158
                        throw new LocatorException(
159
                                        "Class '" +
160
                        (lom_class == null ? "NULL" : lom_class.getClass().getName()) +
161
                        "' does not implement LayerOrderManager.",
162
                                        "", -1);
163
                }
164
        }
165
166
        /**
167
         * Adds the class to the list of available order managers
168
         *
169
         * @param lom_class
170
         */
171
        public static void registerOrderManager(Class lom_class) {
172
173
                if (LayerOrderManager.class.isAssignableFrom(lom_class)) {
174
175
                        ExtensionPointManager epMan = ToolsLocator.getExtensionPointManager();
176
                        ExtensionPoint ep = epMan.add(LAYER_ORDER_MANAGER_EXT_POINT, "");
177
                        ep.append(LAYER_ORDER_MANAGER_NAME + "." + lom_class.getName(),
178
                                        "", lom_class);
179
                } else {
180
                        // not valid class passed:
181
                        throw new LocatorException(
182
                                        "Class '"
183
                        + (lom_class == null ? "NULL" : lom_class.getClass().getName())
184
                        + "' does not implement LayerOrderManager.",
185
                                        "", -1);
186
                }
187
        }
188
189
        /**
190
         * Returns the default order manager or null if none was
191
         * registered as default.
192
         *
193
         * @return
194
         * @throws LocatorException
195
         */
196
        public static LayerOrderManager getDefaultOrderManager()
197
                        throws LocatorException {
198
199
                LayerOrderManager resp = null;
200
                try {
201
                        resp = (LayerOrderManager) getInstance().get(
202
                                        DEFAULT_LAYER_ORDER_MANAGER_NAME);
203
                } catch (Exception ex) {
204
                        logger.info("Cant get default layer order manager", ex);
205
                }
206
                return resp;
207
        }
208
209
210
        /**
211
         * Gets list of available order managers.
212
         *
213
         * @return
214
         */
215
        public static List getOrderManagers() {
216
217
                ExtensionPointManager epMan = ToolsLocator.getExtensionPointManager();
218
                ExtensionPoint ep = epMan.get(LAYER_ORDER_MANAGER_EXT_POINT);
219
220
                List managers = new ArrayList();
221
                Iterator iter = ep.iterator();
222
223
224
                while (iter.hasNext()) {
225
                        Extension extension = (Extension) iter.next();
226
                        LayerOrderManager lom = null;
227
                        try {
228
                                lom = (LayerOrderManager) extension.create();
229
                        } catch (Exception e) {
230
                                logger.warn("Cant create LayerOrderManager " + extension.getName(), e);
231
                                continue;
232
                        }
233
                        managers.add(lom);
234
                }
235
                return managers;
236
        }
237
238 40435 jjdelcerro
}