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 @ 43524

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

    
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
public class DefaultSymbologySwingManager implements
64
        SymbologySwingManager {
65

    
66
    private static final Logger logger = LoggerFactory.getLogger(DefaultSymbologySwingManager.class);
67

    
68
    private SymbologyManager manager;
69
    private I18nManager i18nmanager = null;
70
    private SymbologyWindowManager windowManager;
71
    private ColorTablesFactory colorTablesFactory;
72

    
73
    private static Map<Integer, List<Class<? extends TypeSymbolEditor>>> symbolEditorRegistry;
74
    private List<Class<? extends ILegendPanel>> legendEditorRegistry = null;
75
    private List<Class<? extends ILabelingStrategyPanel>> labelingEditorRegistry = null;
76
    private Map<String,LabelClassEditorFactory> labelClassEditorFactories = new HashMap<String, LabelClassEditorFactory>();
77
    private List<Action> optionalActionsOfLegendsPanel;
78

    
79
    public DefaultSymbologySwingManager() {
80
        this.manager = SymbologyLocator.getSymbologyManager();
81
        this.windowManager = new DefaultSymbologyWindowManager();
82
        this.colorTablesFactory = new DefaultColorTablesFactory();
83

    
84
        this.legendEditorRegistry = new ArrayList<Class<? extends ILegendPanel>>();
85
        this.labelingEditorRegistry = new ArrayList<Class<? extends ILabelingStrategyPanel>>();
86
    }
87

    
88
    public SymbologyManager getManager() {
89
        return this.manager;
90
    }
91

    
92
    public String getTranslation(String key) {
93
        if (this.i18nmanager == null) {
94
            this.i18nmanager = ToolsLocator.getI18nManager();
95
        }
96
        return this.i18nmanager.getTranslation(key);
97
    }
98

    
99
    public void registerWindowManager(SymbologyWindowManager manager) {
100
        this.windowManager = manager;
101
    }
102

    
103
    public SymbologyWindowManager getWindowManager() {
104
        return this.windowManager;
105
    }
106

    
107
    public void setColorTablesFactory(ColorTablesFactory factory) {
108
        colorTablesFactory = factory;
109
    }
110

    
111
    public List<ColorTablePainter> createColorTables() {
112
        if (colorTablesFactory != null) {
113
            return colorTablesFactory.createColorTables();
114
        }
115
        return null;
116
    }
117

    
118
    public ColorTablesFactory getColorTablesFactory() {
119
        return this.colorTablesFactory;
120
    }
121

    
122
    /**
123
     * @deprecated use registerTypeSymbolEditor
124
     */
125
    public void addSymbolEditorPanel(Class abstractTypeSymbolEditorPanelClass, int shapeType) {
126
        Class<? extends TypeSymbolEditor> symbolEditor = abstractTypeSymbolEditorPanelClass;
127
        this.registerSymbolEditor(symbolEditor, shapeType);
128
    }
129

    
130
    public void registerSymbolEditor(Class<? extends TypeSymbolEditor> symbolEditor, int shapeType) {
131
        if (symbolEditorRegistry == null) {
132
            symbolEditorRegistry = new HashMap<Integer, List<Class<? extends TypeSymbolEditor>>>();
133
        }
134

    
135
        Integer key = new Integer(shapeType);
136
        List<Class<? extends TypeSymbolEditor>> l = symbolEditorRegistry.get(key);
137
        if (l == null) {
138
            l = new ArrayList<Class<? extends TypeSymbolEditor>>();
139
        }
140
        l.add(symbolEditor);
141

    
142
        symbolEditorRegistry.put(key, l);
143
    }
144

    
145
    public List<Class<? extends TypeSymbolEditor>> getSymbolEditorClassesByGeometryType(GeometryType geometryType) {
146
        if (symbolEditorRegistry == null) {
147
            return Collections.emptyList();
148
        }
149
        Iterator it = symbolEditorRegistry.keySet().iterator();
150
        while (it.hasNext()) {
151
            int currentType = (Integer) it.next();
152
            if (geometryType.isTypeOf(currentType)) {
153
                return (List) symbolEditorRegistry.get(currentType);
154
            }
155
        }
156
        return Collections.emptyList();
157
    }
158

    
159
    public void registerLegendEditor(Class<? extends ILegendPanel> legendEditor) {
160
        if (this.legendEditorRegistry == null) {
161
            this.legendEditorRegistry = new ArrayList<Class<? extends ILegendPanel>>();
162
        }
163
        if (!this.legendEditorRegistry.contains(legendEditor)) {
164
            this.legendEditorRegistry.add(legendEditor);
165
        }
166
    }
167

    
168
    public List<Class<? extends ILegendPanel>> getLegendEditorClasses() {
169
        return Collections.unmodifiableList(this.legendEditorRegistry);
170
    }
171

    
172
    public List<ILegendPanel> getLegendEditors(FLayer layer) {
173
        List<ILegendPanel> editors = new ArrayList<ILegendPanel>();
174
        Class<? extends ILegendPanel> legendEditorClass = null;
175
        ILegendPanel editor = null;
176

    
177
        for (int i = 0; i < legendEditorRegistry.size(); i++) {
178
            legendEditorClass = legendEditorRegistry.get(i);
179
            try {
180
                editor = null;
181
                editor = (ILegendPanel) legendEditorClass.newInstance();
182
            } catch (Exception e) {
183
                logger.info("Unable to instantiate legend editor.", e);
184
            }
185
            if( layer.isAvailable() ) {
186
                if (editor != null && editor.isSuitableFor(layer)) {
187
                    editors.add(editor);
188
                }
189
            } else {
190
                try {
191
                    if (editor != null && editor.isSuitableFor(layer)) {
192
                        editors.add(editor);
193
                    }
194
                } catch(Throwable th) {
195

    
196
                }
197
            }
198
        }
199
        return editors;
200
    }
201

    
202
    public void registerLabelingEditor(Class<? extends ILabelingStrategyPanel> labelingEditor) {
203
        if (!this.labelingEditorRegistry.contains(labelingEditor)) {
204
            this.labelingEditorRegistry.add(labelingEditor);
205
        }
206
    }
207

    
208
    public List<ILabelingStrategyPanel> getLabelingEditors() {
209
        List<ILabelingStrategyPanel> labelingEditors = new ArrayList<ILabelingStrategyPanel>();
210
        Iterator<Class<? extends ILabelingStrategyPanel>> it = this.labelingEditorRegistry.iterator();
211
        while (it.hasNext()) {
212
            Class<? extends ILabelingStrategyPanel> labelingEditorClass = it.next();
213
            try {
214
                ILabelingStrategyPanel labelingEditor = labelingEditorClass.newInstance();
215
                labelingEditors.add(labelingEditor);
216
            } catch (Exception ex) {
217
                String msg = "Can't create the labeling editor associated to '"+labelingEditorClass.getName()+"'.";
218
                logger.warn(msg,ex);
219
                throw new RuntimeException(msg,ex);
220
            }
221
        }
222
        return labelingEditors;
223
    }
224

    
225
    public void registerLabelClassEditor(LabelClassEditorFactory factory) {
226
        this.labelClassEditorFactories.put(factory.getID().toLowerCase(), factory);
227
    }
228

    
229
    public Collection<LabelClassEditorFactory> getLabelClassEditorFactories() {
230
        return Collections.unmodifiableCollection( this.labelClassEditorFactories.values());
231
    }
232

    
233
    public LabelClassEditorFactory getLabelClassEditorFactory(String id) {
234
        if( id==null ) {
235
            return null;
236
        }
237
        return this.labelClassEditorFactories.get(id.toLowerCase());
238
    }
239

    
240
    public LabelClassEditorFactory getLabelClassEditorFactory(ILabelClass labelClass) {
241
        Iterator<LabelClassEditorFactory> it = this.labelClassEditorFactories.values().iterator();
242
        while( it.hasNext() ) {
243
            LabelClassEditorFactory labelClassEditorFactory = it.next();
244
            if( labelClassEditorFactory!=null && labelClassEditorFactory.accept(labelClass.getClass()) ) {
245
                return labelClassEditorFactory;
246
            }
247
        }
248
        return null;
249
    }
250

    
251
    public LabelClassEditor createLabelClassEditor(ILabelClass labelClass, FeatureStore store) {
252
        LabelClassEditorFactory f = this.getLabelClassEditorFactory(labelClass);
253
        if( f == null ) {
254
            return null;
255
        }
256
        return f.createEditor(labelClass, store);
257
    }
258

    
259
    @Override
260
    public Iterable<Action> getOptionalActionOfLegendsPanel() {
261
        if( this.optionalActionsOfLegendsPanel==null ) {
262
            this.optionalActionsOfLegendsPanel = new ArrayList<>();
263
        }
264
        return this.optionalActionsOfLegendsPanel;
265
    }
266

    
267
    @Override
268
    public void addOptionalActionToLegendsPanel(Action action) {
269
        if( this.optionalActionsOfLegendsPanel==null ) {
270
            this.optionalActionsOfLegendsPanel = new ArrayList<>();
271
        }
272
        this.optionalActionsOfLegendsPanel.add(action);
273
    }
274

    
275
}