Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / legend / impl / AbstractClassifiedVectorLegend.java @ 34294

History | View | Annotate | Download (6.08 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * 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
 * 
16
 * 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
package org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl;
24

    
25
import org.gvsig.fmap.dal.exception.DataException;
26
import org.gvsig.fmap.dal.feature.FeatureStore;
27
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedVectorLegend;
28
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendClearEvent;
29
import org.gvsig.fmap.mapcontext.rendering.legend.events.SymbolLegendEvent;
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.dynobject.DynStruct;
32
import org.gvsig.tools.persistence.PersistenceManager;
33
import org.gvsig.tools.persistence.PersistentState;
34
import org.gvsig.tools.persistence.exception.PersistenceException;
35
import org.gvsig.tools.util.Callable;
36

    
37
/**
38
 * Abstract class that implements the interface for legends composed by
39
 * classified symbols.It will have two methods that will be executed depending
40
 * on the action that had been done with the legend (addition of a new symbol or
41
 * clear the legend).
42
 * 
43
 * @author 2008- pepe vidal salvador - jose.vidal.salvador@iver.es
44
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
45
 */
46
public abstract class AbstractClassifiedVectorLegend extends
47
                AbstractVectorialLegend implements IClassifiedVectorLegend {
48

    
49
        public static final String CLASSIFIED_VECTOR_LEGEND_PERSISTENCE_DEFINITION_NAME =
50
                        "ClassifiedVectorLegend";
51

    
52
        private static final String FIELD_FIELD_NAMES = "fieldNames";
53
        private static final String FIELD_FIELD_TYPES = "fieldTypes";
54

    
55
        private String[] fieldNames;
56
        private int[] fieldTypes;
57

    
58
        /**
59
         * Looks for a change in a classified symbol of a legend. To perform it, the
60
         * Array of LegendListeners is scaned and when the corresponding listener is
61
         * true, the method is invoked and the change will be done.
62
         * 
63
         * @param event
64
         */
65
        public void fireClassifiedSymbolChangeEvent(SymbolLegendEvent event) {
66
                for (int i = 0; i < getListeners().length; i++) {
67
                        getListeners()[i].symbolChanged(event);
68
                }
69
        }
70

    
71
        /**
72
         * Looks for a change in a legend of classified symbols. In this case if the
73
         * specific legend is cleared.
74
         * 
75
         * @param event
76
         */
77
        public void fireLegendClearEvent(LegendClearEvent event) {
78
                for (int i = 0; i < getListeners().length; i++) {
79
                        getListeners()[i].legendCleared(event);
80
                }
81
        }
82

    
83
        public String[] getClassifyingFieldNames() {
84
                return fieldNames;
85
        }
86

    
87
        public void setClassifyingFieldNames(String[] fieldNames) {
88
                this.fieldNames = fieldNames;
89
        }
90

    
91
        public int[] getClassifyingFieldTypes() {
92
                return fieldTypes;
93
        }
94

    
95
        public void setClassifyingFieldTypes(int[] fieldTypes) {
96
                this.fieldTypes = fieldTypes;
97
        }
98

    
99
        public boolean isSuitableForShapeType(int shapeType) {
100
                return getShapeType() == shapeType;
101
        }
102

    
103
        protected String[] getRequiredFeatureAttributeNames(
104
                        FeatureStore featureStore) throws DataException {
105
                String[] requiredFieldNames = null;
106
                String[] classified = getClassifyingFieldNames();
107
                requiredFieldNames = new String[classified.length + 1];
108
                requiredFieldNames[0] =
109
                                featureStore.getDefaultFeatureType()
110
                                                .getDefaultGeometryAttributeName();
111
                for (int i = 1; i < requiredFieldNames.length; i++) {
112
                        requiredFieldNames[i] = classified[i - 1];
113
                }
114

    
115
                return requiredFieldNames;
116
        }
117

    
118
        public Object clone() throws CloneNotSupportedException {
119
                AbstractClassifiedVectorLegend clone =
120
                                (AbstractClassifiedVectorLegend) super.clone();
121

    
122
                // Clone fieldNames
123
                clone.fieldNames = new String[fieldNames.length];
124
                System.arraycopy(fieldNames, 0, clone.fieldNames, 0, fieldNames.length);
125
                // Clone fieldTypes
126
                clone.fieldTypes = new int[fieldTypes.length];
127
                System.arraycopy(fieldTypes, 0, clone.fieldTypes, 0, fieldTypes.length);
128

    
129
                return clone;
130
        }
131

    
132
        public void loadFromState(PersistentState state)
133
                        throws PersistenceException {
134
                // Set parent properties
135
                super.loadFromState(state);
136
                // Set own properties
137
                this.fieldNames = state.getStringArray(FIELD_FIELD_NAMES);
138
                this.fieldTypes = state.getIntArray(FIELD_FIELD_TYPES);
139
        }
140

    
141
        public void saveToState(PersistentState state) throws PersistenceException {
142
                // Save parent properties
143
                super.saveToState(state);
144
                // Save own properties
145
                state.set(FIELD_FIELD_NAMES, this.fieldNames);
146
                state.set(FIELD_FIELD_TYPES, this.fieldTypes);
147
        }
148

    
149
        public static class RegisterPersistence implements Callable {
150

    
151
                public Object call() throws Exception {
152
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
153
                        if( manager.getDefinition(CLASSIFIED_VECTOR_LEGEND_PERSISTENCE_DEFINITION_NAME)==null ) {
154
                                DynStruct definition = manager.addDefinition(
155
                                                AbstractClassifiedVectorLegend.class,
156
                                                CLASSIFIED_VECTOR_LEGEND_PERSISTENCE_DEFINITION_NAME,
157
                                                CLASSIFIED_VECTOR_LEGEND_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
158
                                                null, 
159
                                                null
160
                                );
161
                                // Extend the Vectorial Legend base definition
162
                                definition.extend(manager.getDefinition(VECTORIAL_LEGEND_PERSISTENCE_DEFINITION_NAME));
163

    
164
                                // Field names
165
                                definition.addDynFieldArray(FIELD_FIELD_NAMES)
166
                                        .setClassOfItems(String.class);
167
                                // Field types
168
                                definition.addDynFieldArray(FIELD_FIELD_TYPES)
169
                                        .setClassOfItems(int.class);
170
                        }
171
                        return Boolean.TRUE;
172
                }
173
                
174
        }
175

    
176
}