Statistics
| Revision:

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 40560 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40560 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40435 jjdelcerro
 *
6 41400 jjdelcerro
 * 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 40435 jjdelcerro
 *
11 41400 jjdelcerro
 * 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 40435 jjdelcerro
 *
16 41400 jjdelcerro
 * 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 40435 jjdelcerro
 *
20 41400 jjdelcerro
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22 40435 jjdelcerro
 */
23
package org.gvsig.symbology.swing.impl;
24
25 41400 jjdelcerro
import java.util.ArrayList;
26 41670 jjdelcerro
import java.util.Collection;
27 41400 jjdelcerro
import java.util.Collections;
28
import java.util.HashMap;
29
import java.util.Iterator;
30 40435 jjdelcerro
import java.util.List;
31 41400 jjdelcerro
import java.util.Map;
32 43392 jjdelcerro
import javax.swing.Action;
33 47375 fdiaz
import javax.swing.JButton;
34
import javax.swing.text.JTextComponent;
35 41670 jjdelcerro
import org.gvsig.app.gui.labeling.LabelClassEditor;
36
import org.gvsig.app.gui.labeling.LabelClassEditorFactory;
37 41400 jjdelcerro
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 41670 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStore;
41 41400 jjdelcerro
import org.gvsig.fmap.geom.type.GeometryType;
42
import org.gvsig.fmap.mapcontext.layers.FLayer;
43 41670 jjdelcerro
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass;
44 47375 fdiaz
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
45 40435 jjdelcerro
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 47375 fdiaz
import org.gvsig.symbology.swing.impl.pickercontroller.SymbolPickerController;
53 41400 jjdelcerro
import org.gvsig.tools.ToolsLocator;
54
import org.gvsig.tools.i18n.I18nManager;
55 47375 fdiaz
import org.gvsig.tools.swing.api.pickercontroller.PickerController;
56
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
58 40435 jjdelcerro
59
/**
60
 * Default implementation of the {@link SymbologySwingManager}.
61 41400 jjdelcerro
 *
62 40435 jjdelcerro
 * @author gvSIG Team
63
 * @version $Id$
64
 */
65 44872 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
66 40435 jjdelcerro
public class DefaultSymbologySwingManager implements
67 41400 jjdelcerro
        SymbologySwingManager {
68 40435 jjdelcerro
69 44872 jjdelcerro
  private static final Logger LOGGER = LoggerFactory.getLogger(DefaultSymbologySwingManager.class);
70 41400 jjdelcerro
71 44872 jjdelcerro
  private static Map<Integer, List<Class<? extends TypeSymbolEditor>>> symbolEditorRegistry;
72 40435 jjdelcerro
73 44872 jjdelcerro
  private final SymbologyManager manager;
74
  private final Map<String, LabelClassEditorFactory> labelClassEditorFactories = new HashMap<>();
75 41400 jjdelcerro
76 44872 jjdelcerro
  private I18nManager i18nmanager = null;
77
  private SymbologyWindowManager windowManager;
78
  private ColorTablesFactory colorTablesFactory;
79 41400 jjdelcerro
80 44872 jjdelcerro
  private List<Class<? extends ILegendPanel>> legendEditorRegistry = null;
81
  private List<Class<? extends ILabelingStrategyPanel>> labelingEditorRegistry = null;
82
  private List<Action> optionalActionsOfLegendsPanel;
83 40435 jjdelcerro
84 44872 jjdelcerro
  public DefaultSymbologySwingManager() {
85
    this.manager = SymbologyLocator.getSymbologyManager();
86
    this.windowManager = new DefaultSymbologyWindowManager();
87
    this.colorTablesFactory = new DefaultColorTablesFactory();
88 40435 jjdelcerro
89 44872 jjdelcerro
    this.legendEditorRegistry = new ArrayList<>();
90
    this.labelingEditorRegistry = new ArrayList<>();
91
  }
92 40435 jjdelcerro
93 44872 jjdelcerro
  @Override
94
  public SymbologyManager getManager() {
95
    return this.manager;
96
  }
97 40435 jjdelcerro
98 44872 jjdelcerro
  @Override
99
  public String getTranslation(String key) {
100
    if (this.i18nmanager == null) {
101
      this.i18nmanager = ToolsLocator.getI18nManager();
102 40435 jjdelcerro
    }
103 44872 jjdelcerro
    return this.i18nmanager.getTranslation(key);
104
  }
105 40435 jjdelcerro
106 44872 jjdelcerro
  @Override
107
  public void registerWindowManager(SymbologyWindowManager manager) {
108
    this.windowManager = manager;
109
  }
110 40435 jjdelcerro
111 44872 jjdelcerro
  @Override
112
  public SymbologyWindowManager getWindowManager() {
113
    return this.windowManager;
114
  }
115 41400 jjdelcerro
116 44872 jjdelcerro
  @Override
117
  public void setColorTablesFactory(ColorTablesFactory factory) {
118
    colorTablesFactory = factory;
119
  }
120 41400 jjdelcerro
121 44872 jjdelcerro
  @Override
122
  public List<ColorTablePainter> createColorTables() {
123
    if (colorTablesFactory != null) {
124
      return colorTablesFactory.createColorTables();
125 41400 jjdelcerro
    }
126 44872 jjdelcerro
    return null;
127
  }
128 41400 jjdelcerro
129 44872 jjdelcerro
  @Override
130
  public ColorTablesFactory getColorTablesFactory() {
131
    return this.colorTablesFactory;
132
  }
133 41400 jjdelcerro
134 44872 jjdelcerro
  /**
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 41400 jjdelcerro
144 44872 jjdelcerro
  @Override
145
  public void registerSymbolEditor(Class<? extends TypeSymbolEditor> symbolEditor, int shapeType) {
146
    if (symbolEditorRegistry == null) {
147
      symbolEditorRegistry = new HashMap<>();
148 41400 jjdelcerro
    }
149
150 44872 jjdelcerro
    Integer key = shapeType;
151
    List<Class<? extends TypeSymbolEditor>> l = symbolEditorRegistry.get(key);
152
    if (l == null) {
153
      l = new ArrayList<>();
154 41400 jjdelcerro
    }
155 44872 jjdelcerro
    l.add(symbolEditor);
156 41400 jjdelcerro
157 44872 jjdelcerro
    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 41400 jjdelcerro
    }
165 44872 jjdelcerro
    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 41400 jjdelcerro
175 44872 jjdelcerro
  @Override
176
  public void registerLegendEditor(Class<? extends ILegendPanel> legendEditor) {
177
    if (this.legendEditorRegistry == null) {
178
      this.legendEditorRegistry = new ArrayList<>();
179 41400 jjdelcerro
    }
180 44872 jjdelcerro
    if (!this.legendEditorRegistry.contains(legendEditor)) {
181
      this.legendEditorRegistry.add(legendEditor);
182
    }
183
  }
184 41400 jjdelcerro
185 44872 jjdelcerro
  @Override
186
  public List<Class<? extends ILegendPanel>> getLegendEditorClasses() {
187
    return Collections.unmodifiableList(this.legendEditorRegistry);
188
  }
189 41400 jjdelcerro
190 44872 jjdelcerro
  @Override
191
  public List<ILegendPanel> getLegendEditors(FLayer layer) {
192
    List<ILegendPanel> editors = new ArrayList<>();
193 43524 fdiaz
194 44872 jjdelcerro
    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 41400 jjdelcerro
        }
202 44872 jjdelcerro
      } catch (Throwable e) {
203
        LOGGER.warn("Unable to initialize legend editor ("+(legendEditorClass==null?"NULL":legendEditorClass.getName())+").", e);
204
      }
205 41400 jjdelcerro
    }
206 44872 jjdelcerro
    return editors;
207
  }
208 41400 jjdelcerro
209 44872 jjdelcerro
  @Override
210
  public void registerLabelingEditor(Class<? extends ILabelingStrategyPanel> labelingEditor) {
211
    if (!this.labelingEditorRegistry.contains(labelingEditor)) {
212
      this.labelingEditorRegistry.add(labelingEditor);
213 41400 jjdelcerro
    }
214 44872 jjdelcerro
  }
215 41400 jjdelcerro
216 44872 jjdelcerro
  @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 41400 jjdelcerro
    }
231 44872 jjdelcerro
    return labelingEditors;
232
  }
233 41400 jjdelcerro
234 44872 jjdelcerro
  @Override
235
  public void registerLabelClassEditor(LabelClassEditorFactory factory) {
236
    this.labelClassEditorFactories.put(factory.getID().toLowerCase(), factory);
237
  }
238 41670 jjdelcerro
239 44872 jjdelcerro
  @Override
240
  public Collection<LabelClassEditorFactory> getLabelClassEditorFactories() {
241
    return Collections.unmodifiableCollection(this.labelClassEditorFactories.values());
242
  }
243 41670 jjdelcerro
244 44872 jjdelcerro
  @Override
245
  public LabelClassEditorFactory getLabelClassEditorFactory(String id) {
246
    if (id == null) {
247
      return null;
248 41670 jjdelcerro
    }
249 44872 jjdelcerro
    return this.labelClassEditorFactories.get(id.toLowerCase());
250
  }
251 41670 jjdelcerro
252 44872 jjdelcerro
  @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 41670 jjdelcerro
        }
262 44872 jjdelcerro
      } catch(Throwable th) {
263
        LOGGER.warn("Can't check factory '"+(labelClassEditorFactory==null?"NULL":labelClassEditorFactory.getName())+"'.", th);
264
      }
265 41670 jjdelcerro
    }
266 44872 jjdelcerro
    return null;
267
  }
268 41670 jjdelcerro
269 44872 jjdelcerro
  @Override
270
  public LabelClassEditor createLabelClassEditor(ILabelClass labelClass, FeatureStore store) {
271
    LabelClassEditorFactory f = this.getLabelClassEditorFactory(labelClass);
272
    if (f == null) {
273
      return null;
274 41670 jjdelcerro
    }
275 44872 jjdelcerro
    return f.createEditor(labelClass, store);
276
  }
277 43392 jjdelcerro
278 44872 jjdelcerro
  @Override
279
  public Iterable<Action> getOptionalActionOfLegendsPanel() {
280
    if (this.optionalActionsOfLegendsPanel == null) {
281
      this.optionalActionsOfLegendsPanel = new ArrayList<>();
282 43392 jjdelcerro
    }
283 44872 jjdelcerro
    return this.optionalActionsOfLegendsPanel;
284
  }
285 43392 jjdelcerro
286 44872 jjdelcerro
  @Override
287
  public void addOptionalActionToLegendsPanel(Action action) {
288
    if (this.optionalActionsOfLegendsPanel == null) {
289
      this.optionalActionsOfLegendsPanel = new ArrayList<>();
290 43392 jjdelcerro
    }
291 44872 jjdelcerro
    this.optionalActionsOfLegendsPanel.add(action);
292
  }
293 47375 fdiaz
294
  @Override
295
  public PickerController<ISymbol> createSymbolPickerController(JButton btn, ISymbol symbol) {
296
      return new SymbolPickerController(btn, null, -1, symbol);
297
  }
298 43524 fdiaz
299 47375 fdiaz
  @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 40435 jjdelcerro
}