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 @ 47375
History | View | Annotate | Download (10.3 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 |
import javax.swing.Action; |
33 |
import javax.swing.JButton; |
34 |
import javax.swing.text.JTextComponent; |
35 |
import org.gvsig.app.gui.labeling.LabelClassEditor; |
36 |
import org.gvsig.app.gui.labeling.LabelClassEditorFactory; |
37 |
import org.gvsig.app.gui.styling.TypeSymbolEditor; |
38 |
import org.gvsig.app.project.documents.view.legend.gui.ILabelingStrategyPanel; |
39 |
import org.gvsig.app.project.documents.view.legend.gui.ILegendPanel; |
40 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
41 |
import org.gvsig.fmap.geom.type.GeometryType; |
42 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
43 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass; |
44 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
45 |
import org.gvsig.gui.ColorTablePainter; |
46 |
import org.gvsig.gui.ColorTablesFactory; |
47 |
import org.gvsig.gui.DefaultColorTablesFactory; |
48 |
import org.gvsig.symbology.SymbologyLocator; |
49 |
import org.gvsig.symbology.SymbologyManager; |
50 |
import org.gvsig.symbology.swing.SymbologySwingManager; |
51 |
import org.gvsig.symbology.swing.SymbologyWindowManager; |
52 |
import org.gvsig.symbology.swing.impl.pickercontroller.SymbolPickerController; |
53 |
import org.gvsig.tools.ToolsLocator; |
54 |
import org.gvsig.tools.i18n.I18nManager; |
55 |
import org.gvsig.tools.swing.api.pickercontroller.PickerController; |
56 |
import org.slf4j.Logger; |
57 |
import org.slf4j.LoggerFactory; |
58 |
|
59 |
/**
|
60 |
* Default implementation of the {@link SymbologySwingManager}.
|
61 |
*
|
62 |
* @author gvSIG Team
|
63 |
* @version $Id$
|
64 |
*/
|
65 |
@SuppressWarnings("UseSpecificCatch") |
66 |
public class DefaultSymbologySwingManager implements |
67 |
SymbologySwingManager { |
68 |
|
69 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultSymbologySwingManager.class); |
70 |
|
71 |
private static Map<Integer, List<Class<? extends TypeSymbolEditor>>> symbolEditorRegistry; |
72 |
|
73 |
private final SymbologyManager manager; |
74 |
private final Map<String, LabelClassEditorFactory> labelClassEditorFactories = new HashMap<>(); |
75 |
|
76 |
private I18nManager i18nmanager = null; |
77 |
private SymbologyWindowManager windowManager;
|
78 |
private ColorTablesFactory colorTablesFactory;
|
79 |
|
80 |
private List<Class<? extends ILegendPanel>> legendEditorRegistry = null; |
81 |
private List<Class<? extends ILabelingStrategyPanel>> labelingEditorRegistry = null; |
82 |
private List<Action> optionalActionsOfLegendsPanel; |
83 |
|
84 |
public DefaultSymbologySwingManager() {
|
85 |
this.manager = SymbologyLocator.getSymbologyManager();
|
86 |
this.windowManager = new DefaultSymbologyWindowManager(); |
87 |
this.colorTablesFactory = new DefaultColorTablesFactory(); |
88 |
|
89 |
this.legendEditorRegistry = new ArrayList<>(); |
90 |
this.labelingEditorRegistry = new ArrayList<>(); |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public SymbologyManager getManager() {
|
95 |
return this.manager; |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public String getTranslation(String key) { |
100 |
if (this.i18nmanager == null) { |
101 |
this.i18nmanager = ToolsLocator.getI18nManager();
|
102 |
} |
103 |
return this.i18nmanager.getTranslation(key); |
104 |
} |
105 |
|
106 |
@Override
|
107 |
public void registerWindowManager(SymbologyWindowManager manager) { |
108 |
this.windowManager = manager;
|
109 |
} |
110 |
|
111 |
@Override
|
112 |
public SymbologyWindowManager getWindowManager() {
|
113 |
return this.windowManager; |
114 |
} |
115 |
|
116 |
@Override
|
117 |
public void setColorTablesFactory(ColorTablesFactory factory) { |
118 |
colorTablesFactory = factory; |
119 |
} |
120 |
|
121 |
@Override
|
122 |
public List<ColorTablePainter> createColorTables() { |
123 |
if (colorTablesFactory != null) { |
124 |
return colorTablesFactory.createColorTables();
|
125 |
} |
126 |
return null; |
127 |
} |
128 |
|
129 |
@Override
|
130 |
public ColorTablesFactory getColorTablesFactory() {
|
131 |
return this.colorTablesFactory; |
132 |
} |
133 |
|
134 |
/**
|
135 |
* @param abstractTypeSymbolEditorPanelClass
|
136 |
* @param shapeType
|
137 |
* @deprecated use registerTypeSymbolEditor
|
138 |
*/
|
139 |
public void addSymbolEditorPanel(Class abstractTypeSymbolEditorPanelClass, int shapeType) { |
140 |
Class<? extends TypeSymbolEditor> symbolEditor = abstractTypeSymbolEditorPanelClass; |
141 |
this.registerSymbolEditor(symbolEditor, shapeType);
|
142 |
} |
143 |
|
144 |
@Override
|
145 |
public void registerSymbolEditor(Class<? extends TypeSymbolEditor> symbolEditor, int shapeType) { |
146 |
if (symbolEditorRegistry == null) { |
147 |
symbolEditorRegistry = new HashMap<>(); |
148 |
} |
149 |
|
150 |
Integer key = shapeType;
|
151 |
List<Class<? extends TypeSymbolEditor>> l = symbolEditorRegistry.get(key); |
152 |
if (l == null) { |
153 |
l = new ArrayList<>(); |
154 |
} |
155 |
l.add(symbolEditor); |
156 |
|
157 |
symbolEditorRegistry.put(key, l); |
158 |
} |
159 |
|
160 |
@Override
|
161 |
public List<Class<? extends TypeSymbolEditor>> getSymbolEditorClassesByGeometryType(GeometryType geometryType) { |
162 |
if (symbolEditorRegistry == null) { |
163 |
return Collections.emptyList(); |
164 |
} |
165 |
Iterator it = symbolEditorRegistry.keySet().iterator();
|
166 |
while (it.hasNext()) {
|
167 |
int currentType = (Integer) it.next(); |
168 |
if (geometryType.isTypeOf(currentType)) {
|
169 |
return (List) symbolEditorRegistry.get(currentType); |
170 |
} |
171 |
} |
172 |
return Collections.emptyList(); |
173 |
} |
174 |
|
175 |
@Override
|
176 |
public void registerLegendEditor(Class<? extends ILegendPanel> legendEditor) { |
177 |
if (this.legendEditorRegistry == null) { |
178 |
this.legendEditorRegistry = new ArrayList<>(); |
179 |
} |
180 |
if (!this.legendEditorRegistry.contains(legendEditor)) { |
181 |
this.legendEditorRegistry.add(legendEditor);
|
182 |
} |
183 |
} |
184 |
|
185 |
@Override
|
186 |
public List<Class<? extends ILegendPanel>> getLegendEditorClasses() { |
187 |
return Collections.unmodifiableList(this.legendEditorRegistry); |
188 |
} |
189 |
|
190 |
@Override
|
191 |
public List<ILegendPanel> getLegendEditors(FLayer layer) { |
192 |
List<ILegendPanel> editors = new ArrayList<>(); |
193 |
|
194 |
for (int i = 0; i < legendEditorRegistry.size(); i++) { |
195 |
Class<? extends ILegendPanel> legendEditorClass = null; |
196 |
try {
|
197 |
legendEditorClass = legendEditorRegistry.get(i); |
198 |
ILegendPanel editor = (ILegendPanel) legendEditorClass.newInstance(); |
199 |
if (editor.isSuitableFor(layer)) {
|
200 |
editors.add(editor); |
201 |
} |
202 |
} catch (Throwable e) { |
203 |
LOGGER.warn("Unable to initialize legend editor ("+(legendEditorClass==null?"NULL":legendEditorClass.getName())+").", e); |
204 |
} |
205 |
} |
206 |
return editors;
|
207 |
} |
208 |
|
209 |
@Override
|
210 |
public void registerLabelingEditor(Class<? extends ILabelingStrategyPanel> labelingEditor) { |
211 |
if (!this.labelingEditorRegistry.contains(labelingEditor)) { |
212 |
this.labelingEditorRegistry.add(labelingEditor);
|
213 |
} |
214 |
} |
215 |
|
216 |
@Override
|
217 |
public List<ILabelingStrategyPanel> getLabelingEditors() { |
218 |
List<ILabelingStrategyPanel> labelingEditors = new ArrayList<>(); |
219 |
Iterator<Class<? extends ILabelingStrategyPanel>> it = this.labelingEditorRegistry.iterator(); |
220 |
while (it.hasNext()) {
|
221 |
Class<? extends ILabelingStrategyPanel> labelingEditorClass = null; |
222 |
try {
|
223 |
labelingEditorClass = it.next(); |
224 |
ILabelingStrategyPanel labelingEditor = labelingEditorClass.newInstance(); |
225 |
labelingEditors.add(labelingEditor); |
226 |
} catch (Throwable ex) { |
227 |
String msg = "Can't create the labeling editor associated to '" + (labelingEditorClass==null?"NULL":labelingEditorClass.getName()) + "'."; |
228 |
LOGGER.warn(msg, ex); |
229 |
} |
230 |
} |
231 |
return labelingEditors;
|
232 |
} |
233 |
|
234 |
@Override
|
235 |
public void registerLabelClassEditor(LabelClassEditorFactory factory) { |
236 |
this.labelClassEditorFactories.put(factory.getID().toLowerCase(), factory);
|
237 |
} |
238 |
|
239 |
@Override
|
240 |
public Collection<LabelClassEditorFactory> getLabelClassEditorFactories() { |
241 |
return Collections.unmodifiableCollection(this.labelClassEditorFactories.values()); |
242 |
} |
243 |
|
244 |
@Override
|
245 |
public LabelClassEditorFactory getLabelClassEditorFactory(String id) { |
246 |
if (id == null) { |
247 |
return null; |
248 |
} |
249 |
return this.labelClassEditorFactories.get(id.toLowerCase()); |
250 |
} |
251 |
|
252 |
@Override
|
253 |
public LabelClassEditorFactory getLabelClassEditorFactory(ILabelClass labelClass) {
|
254 |
Iterator<LabelClassEditorFactory> it = this.labelClassEditorFactories.values().iterator(); |
255 |
while (it.hasNext()) {
|
256 |
LabelClassEditorFactory labelClassEditorFactory = null;
|
257 |
try {
|
258 |
labelClassEditorFactory = it.next(); |
259 |
if ( labelClassEditorFactory.accept(labelClass.getClass())) {
|
260 |
return labelClassEditorFactory;
|
261 |
} |
262 |
} catch(Throwable th) { |
263 |
LOGGER.warn("Can't check factory '"+(labelClassEditorFactory==null?"NULL":labelClassEditorFactory.getName())+"'.", th); |
264 |
} |
265 |
} |
266 |
return null; |
267 |
} |
268 |
|
269 |
@Override
|
270 |
public LabelClassEditor createLabelClassEditor(ILabelClass labelClass, FeatureStore store) {
|
271 |
LabelClassEditorFactory f = this.getLabelClassEditorFactory(labelClass);
|
272 |
if (f == null) { |
273 |
return null; |
274 |
} |
275 |
return f.createEditor(labelClass, store);
|
276 |
} |
277 |
|
278 |
@Override
|
279 |
public Iterable<Action> getOptionalActionOfLegendsPanel() { |
280 |
if (this.optionalActionsOfLegendsPanel == null) { |
281 |
this.optionalActionsOfLegendsPanel = new ArrayList<>(); |
282 |
} |
283 |
return this.optionalActionsOfLegendsPanel; |
284 |
} |
285 |
|
286 |
@Override
|
287 |
public void addOptionalActionToLegendsPanel(Action action) { |
288 |
if (this.optionalActionsOfLegendsPanel == null) { |
289 |
this.optionalActionsOfLegendsPanel = new ArrayList<>(); |
290 |
} |
291 |
this.optionalActionsOfLegendsPanel.add(action);
|
292 |
} |
293 |
|
294 |
@Override
|
295 |
public PickerController<ISymbol> createSymbolPickerController(JButton btn, ISymbol symbol) { |
296 |
return new SymbolPickerController(btn, null, -1, symbol); |
297 |
} |
298 |
|
299 |
@Override
|
300 |
public PickerController<ISymbol> createSymbolPickerController(JButton btnPreview, JTextComponent txtDescription, int geomType, ISymbol symbol) { |
301 |
return new SymbolPickerController(btnPreview, txtDescription, geomType, symbol); |
302 |
} |
303 |
|
304 |
} |