svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.impl / src / main / java / org / gvsig / symbology / swing / impl / DefaultSymbologySwingManager.java @ 44872
History | View | Annotate | Download (9.65 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.symbology.swing.impl; |
24 |
|
25 |
import java.util.ArrayList; |
26 |
import java.util.Collection; |
27 |
import java.util.Collections; |
28 |
import java.util.HashMap; |
29 |
import java.util.Iterator; |
30 |
import java.util.List; |
31 |
import java.util.Map; |
32 |
|
33 |
import javax.swing.Action; |
34 |
|
35 |
import org.slf4j.Logger; |
36 |
import org.slf4j.LoggerFactory; |
37 |
|
38 |
import org.gvsig.app.gui.labeling.LabelClassEditor; |
39 |
import org.gvsig.app.gui.labeling.LabelClassEditorFactory; |
40 |
import org.gvsig.app.gui.styling.TypeSymbolEditor; |
41 |
import org.gvsig.app.project.documents.view.legend.gui.ILabelingStrategyPanel; |
42 |
import org.gvsig.app.project.documents.view.legend.gui.ILegendPanel; |
43 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
44 |
import org.gvsig.fmap.geom.type.GeometryType; |
45 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
46 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass; |
47 |
import org.gvsig.gui.ColorTablePainter; |
48 |
import org.gvsig.gui.ColorTablesFactory; |
49 |
import org.gvsig.gui.DefaultColorTablesFactory; |
50 |
import org.gvsig.symbology.SymbologyLocator; |
51 |
import org.gvsig.symbology.SymbologyManager; |
52 |
import org.gvsig.symbology.swing.SymbologySwingManager; |
53 |
import org.gvsig.symbology.swing.SymbologyWindowManager; |
54 |
import org.gvsig.tools.ToolsLocator; |
55 |
import org.gvsig.tools.i18n.I18nManager; |
56 |
|
57 |
/**
|
58 |
* Default implementation of the {@link SymbologySwingManager}.
|
59 |
*
|
60 |
* @author gvSIG Team
|
61 |
* @version $Id$
|
62 |
*/
|
63 |
@SuppressWarnings("UseSpecificCatch") |
64 |
public class DefaultSymbologySwingManager implements |
65 |
SymbologySwingManager { |
66 |
|
67 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultSymbologySwingManager.class); |
68 |
|
69 |
private static Map<Integer, List<Class<? extends TypeSymbolEditor>>> symbolEditorRegistry; |
70 |
|
71 |
private final SymbologyManager manager; |
72 |
private final Map<String, LabelClassEditorFactory> labelClassEditorFactories = new HashMap<>(); |
73 |
|
74 |
private I18nManager i18nmanager = null; |
75 |
private SymbologyWindowManager windowManager;
|
76 |
private ColorTablesFactory colorTablesFactory;
|
77 |
|
78 |
private List<Class<? extends ILegendPanel>> legendEditorRegistry = null; |
79 |
private List<Class<? extends ILabelingStrategyPanel>> labelingEditorRegistry = null; |
80 |
private List<Action> optionalActionsOfLegendsPanel; |
81 |
|
82 |
public DefaultSymbologySwingManager() {
|
83 |
this.manager = SymbologyLocator.getSymbologyManager();
|
84 |
this.windowManager = new DefaultSymbologyWindowManager(); |
85 |
this.colorTablesFactory = new DefaultColorTablesFactory(); |
86 |
|
87 |
this.legendEditorRegistry = new ArrayList<>(); |
88 |
this.labelingEditorRegistry = new ArrayList<>(); |
89 |
} |
90 |
|
91 |
@Override
|
92 |
public SymbologyManager getManager() {
|
93 |
return this.manager; |
94 |
} |
95 |
|
96 |
@Override
|
97 |
public String getTranslation(String key) { |
98 |
if (this.i18nmanager == null) { |
99 |
this.i18nmanager = ToolsLocator.getI18nManager();
|
100 |
} |
101 |
return this.i18nmanager.getTranslation(key); |
102 |
} |
103 |
|
104 |
@Override
|
105 |
public void registerWindowManager(SymbologyWindowManager manager) { |
106 |
this.windowManager = manager;
|
107 |
} |
108 |
|
109 |
@Override
|
110 |
public SymbologyWindowManager getWindowManager() {
|
111 |
return this.windowManager; |
112 |
} |
113 |
|
114 |
@Override
|
115 |
public void setColorTablesFactory(ColorTablesFactory factory) { |
116 |
colorTablesFactory = factory; |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public List<ColorTablePainter> createColorTables() { |
121 |
if (colorTablesFactory != null) { |
122 |
return colorTablesFactory.createColorTables();
|
123 |
} |
124 |
return null; |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public ColorTablesFactory getColorTablesFactory() {
|
129 |
return this.colorTablesFactory; |
130 |
} |
131 |
|
132 |
/**
|
133 |
* @param abstractTypeSymbolEditorPanelClass
|
134 |
* @param shapeType
|
135 |
* @deprecated use registerTypeSymbolEditor
|
136 |
*/
|
137 |
public void addSymbolEditorPanel(Class abstractTypeSymbolEditorPanelClass, int shapeType) { |
138 |
Class<? extends TypeSymbolEditor> symbolEditor = abstractTypeSymbolEditorPanelClass; |
139 |
this.registerSymbolEditor(symbolEditor, shapeType);
|
140 |
} |
141 |
|
142 |
@Override
|
143 |
public void registerSymbolEditor(Class<? extends TypeSymbolEditor> symbolEditor, int shapeType) { |
144 |
if (symbolEditorRegistry == null) { |
145 |
symbolEditorRegistry = new HashMap<>(); |
146 |
} |
147 |
|
148 |
Integer key = shapeType;
|
149 |
List<Class<? extends TypeSymbolEditor>> l = symbolEditorRegistry.get(key); |
150 |
if (l == null) { |
151 |
l = new ArrayList<>(); |
152 |
} |
153 |
l.add(symbolEditor); |
154 |
|
155 |
symbolEditorRegistry.put(key, l); |
156 |
} |
157 |
|
158 |
@Override
|
159 |
public List<Class<? extends TypeSymbolEditor>> getSymbolEditorClassesByGeometryType(GeometryType geometryType) { |
160 |
if (symbolEditorRegistry == null) { |
161 |
return Collections.emptyList(); |
162 |
} |
163 |
Iterator it = symbolEditorRegistry.keySet().iterator();
|
164 |
while (it.hasNext()) {
|
165 |
int currentType = (Integer) it.next(); |
166 |
if (geometryType.isTypeOf(currentType)) {
|
167 |
return (List) symbolEditorRegistry.get(currentType); |
168 |
} |
169 |
} |
170 |
return Collections.emptyList(); |
171 |
} |
172 |
|
173 |
@Override
|
174 |
public void registerLegendEditor(Class<? extends ILegendPanel> legendEditor) { |
175 |
if (this.legendEditorRegistry == null) { |
176 |
this.legendEditorRegistry = new ArrayList<>(); |
177 |
} |
178 |
if (!this.legendEditorRegistry.contains(legendEditor)) { |
179 |
this.legendEditorRegistry.add(legendEditor);
|
180 |
} |
181 |
} |
182 |
|
183 |
@Override
|
184 |
public List<Class<? extends ILegendPanel>> getLegendEditorClasses() { |
185 |
return Collections.unmodifiableList(this.legendEditorRegistry); |
186 |
} |
187 |
|
188 |
@Override
|
189 |
public List<ILegendPanel> getLegendEditors(FLayer layer) { |
190 |
List<ILegendPanel> editors = new ArrayList<>(); |
191 |
|
192 |
for (int i = 0; i < legendEditorRegistry.size(); i++) { |
193 |
Class<? extends ILegendPanel> legendEditorClass = null; |
194 |
try {
|
195 |
legendEditorClass = legendEditorRegistry.get(i); |
196 |
ILegendPanel editor = (ILegendPanel) legendEditorClass.newInstance(); |
197 |
if (editor.isSuitableFor(layer)) {
|
198 |
editors.add(editor); |
199 |
} |
200 |
} catch (Throwable e) { |
201 |
LOGGER.warn("Unable to initialize legend editor ("+(legendEditorClass==null?"NULL":legendEditorClass.getName())+").", e); |
202 |
} |
203 |
} |
204 |
return editors;
|
205 |
} |
206 |
|
207 |
@Override
|
208 |
public void registerLabelingEditor(Class<? extends ILabelingStrategyPanel> labelingEditor) { |
209 |
if (!this.labelingEditorRegistry.contains(labelingEditor)) { |
210 |
this.labelingEditorRegistry.add(labelingEditor);
|
211 |
} |
212 |
} |
213 |
|
214 |
@Override
|
215 |
public List<ILabelingStrategyPanel> getLabelingEditors() { |
216 |
List<ILabelingStrategyPanel> labelingEditors = new ArrayList<>(); |
217 |
Iterator<Class<? extends ILabelingStrategyPanel>> it = this.labelingEditorRegistry.iterator(); |
218 |
while (it.hasNext()) {
|
219 |
Class<? extends ILabelingStrategyPanel> labelingEditorClass = null; |
220 |
try {
|
221 |
labelingEditorClass = it.next(); |
222 |
ILabelingStrategyPanel labelingEditor = labelingEditorClass.newInstance(); |
223 |
labelingEditors.add(labelingEditor); |
224 |
} catch (Throwable ex) { |
225 |
String msg = "Can't create the labeling editor associated to '" + (labelingEditorClass==null?"NULL":labelingEditorClass.getName()) + "'."; |
226 |
LOGGER.warn(msg, ex); |
227 |
} |
228 |
} |
229 |
return labelingEditors;
|
230 |
} |
231 |
|
232 |
@Override
|
233 |
public void registerLabelClassEditor(LabelClassEditorFactory factory) { |
234 |
this.labelClassEditorFactories.put(factory.getID().toLowerCase(), factory);
|
235 |
} |
236 |
|
237 |
@Override
|
238 |
public Collection<LabelClassEditorFactory> getLabelClassEditorFactories() { |
239 |
return Collections.unmodifiableCollection(this.labelClassEditorFactories.values()); |
240 |
} |
241 |
|
242 |
@Override
|
243 |
public LabelClassEditorFactory getLabelClassEditorFactory(String id) { |
244 |
if (id == null) { |
245 |
return null; |
246 |
} |
247 |
return this.labelClassEditorFactories.get(id.toLowerCase()); |
248 |
} |
249 |
|
250 |
@Override
|
251 |
public LabelClassEditorFactory getLabelClassEditorFactory(ILabelClass labelClass) {
|
252 |
Iterator<LabelClassEditorFactory> it = this.labelClassEditorFactories.values().iterator(); |
253 |
while (it.hasNext()) {
|
254 |
LabelClassEditorFactory labelClassEditorFactory = null;
|
255 |
try {
|
256 |
labelClassEditorFactory = it.next(); |
257 |
if ( labelClassEditorFactory.accept(labelClass.getClass())) {
|
258 |
return labelClassEditorFactory;
|
259 |
} |
260 |
} catch(Throwable th) { |
261 |
LOGGER.warn("Can't check factory '"+(labelClassEditorFactory==null?"NULL":labelClassEditorFactory.getName())+"'.", th); |
262 |
} |
263 |
} |
264 |
return null; |
265 |
} |
266 |
|
267 |
@Override
|
268 |
public LabelClassEditor createLabelClassEditor(ILabelClass labelClass, FeatureStore store) {
|
269 |
LabelClassEditorFactory f = this.getLabelClassEditorFactory(labelClass);
|
270 |
if (f == null) { |
271 |
return null; |
272 |
} |
273 |
return f.createEditor(labelClass, store);
|
274 |
} |
275 |
|
276 |
@Override
|
277 |
public Iterable<Action> getOptionalActionOfLegendsPanel() { |
278 |
if (this.optionalActionsOfLegendsPanel == null) { |
279 |
this.optionalActionsOfLegendsPanel = new ArrayList<>(); |
280 |
} |
281 |
return this.optionalActionsOfLegendsPanel; |
282 |
} |
283 |
|
284 |
@Override
|
285 |
public void addOptionalActionToLegendsPanel(Action action) { |
286 |
if (this.optionalActionsOfLegendsPanel == null) { |
287 |
this.optionalActionsOfLegendsPanel = new ArrayList<>(); |
288 |
} |
289 |
this.optionalActionsOfLegendsPanel.add(action);
|
290 |
} |
291 |
|
292 |
} |