Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / 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 @ 45523

History | View | Annotate | Download (7.58 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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl;
25

    
26
import java.util.ArrayList;
27
import java.util.Arrays;
28
import java.util.HashSet;
29
import java.util.List;
30
import java.util.Set;
31
import org.gvsig.fmap.dal.exception.DataException;
32
import org.gvsig.fmap.dal.feature.FeatureStore;
33
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedVectorLegend;
34
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendClearEvent;
35
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendContentsChangedListener;
36
import org.gvsig.fmap.mapcontext.rendering.legend.events.SymbolLegendEvent;
37
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
38
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.AbstractSymbol;
39
import org.gvsig.tools.ToolsLocator;
40
import org.gvsig.tools.dynobject.DynStruct;
41
import org.gvsig.tools.persistence.PersistenceManager;
42
import org.gvsig.tools.persistence.PersistentState;
43
import org.gvsig.tools.persistence.exception.PersistenceException;
44
import org.gvsig.tools.util.Callable;
45

    
46
/**
47
 * Abstract class that implements the interface for legends composed by
48
 * classified symbols.It will have two methods that will be executed depending
49
 * on the action that had been done with the legend (addition of a new symbol or
50
 * clear the legend).
51
 *
52
 * @author gvSIG team
53
 */
54
public abstract class AbstractClassifiedVectorLegend extends
55
        AbstractVectorialLegend implements IClassifiedVectorLegend {
56

    
57
    public static final String CLASSIFIED_VECTOR_LEGEND_PERSISTENCE_DEFINITION_NAME
58
            = "ClassifiedVectorLegend";
59

    
60
    private static final String FIELD_FIELD_NAMES = "fieldNames";
61
    private static final String FIELD_FIELD_TYPES = "fieldTypes";
62

    
63
    private String[] fieldNames;
64
    private int[] fieldTypes;
65

    
66
    /**
67
     * Looks for a change in a classified symbol of a legend. To perform it, the
68
     * Array of LegendListeners is scaned and when the corresponding listener is
69
     * true, the method is invoked and the change will be done.
70
     *
71
     * @param event
72
     */
73
    public void fireClassifiedSymbolChangeEvent(SymbolLegendEvent event) {
74
        for (LegendContentsChangedListener listener : getListeners()) {
75
            listener.symbolChanged(event);
76
        }
77
    }
78

    
79
    /**
80
     * Looks for a change in a legend of classified symbols. In this case if the
81
     * specific legend is cleared.
82
     *
83
     * @param event
84
     */
85
    public void fireLegendClearEvent(LegendClearEvent event) {
86
        for (LegendContentsChangedListener listener : getListeners()) {
87
            listener.legendCleared(event);
88
        }
89
    }
90

    
91
    @Override
92
    public String[] getClassifyingFieldNames() {
93
        return fieldNames;
94
    }
95

    
96
    @Override
97
    public void setClassifyingFieldNames(String[] fieldNames) {
98
        this.fieldNames = fieldNames;
99
    }
100

    
101
    @Override
102
    public int[] getClassifyingFieldTypes() {
103
        return fieldTypes;
104
    }
105

    
106
    @Override
107
    public void setClassifyingFieldTypes(int[] fieldTypes) {
108
        this.fieldTypes = fieldTypes;
109
    }
110

    
111
    @Override
112
    public boolean isSuitableForShapeType(int shapeType) {
113
        return getShapeType() == shapeType;
114
    }
115

    
116
    private String[] requiredFieldNames = null;
117
    
118
    @Override
119
    protected String[] getRequiredFeatureAttributeNames(FeatureStore featureStore) 
120
            throws DataException 
121
        {
122
        if( this.requiredFieldNames == null ) {
123
            Set<String> theRequiredFieldNames = new HashSet<>();
124
            theRequiredFieldNames.add(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName());
125
            theRequiredFieldNames.addAll(Arrays.asList(getClassifyingFieldNames()));
126
            for (ISymbol symbol : this.getSymbols()) {
127
                if( symbol instanceof AbstractSymbol ) {
128
                    String[] names = ((AbstractSymbol) symbol).getRequiredFeatureAttributeNames(featureStore);
129
                    theRequiredFieldNames.addAll(Arrays.asList(names));
130
                }
131
            }
132
            this.requiredFieldNames = theRequiredFieldNames.toArray(new String[theRequiredFieldNames.size()]);
133
        }
134
        return this.requiredFieldNames;
135
    }
136

    
137
    @Override
138
    public Object clone() throws CloneNotSupportedException {
139
        AbstractClassifiedVectorLegend clone
140
                = (AbstractClassifiedVectorLegend) super.clone();
141

    
142
        // Clone fieldNames
143
        clone.fieldNames = new String[fieldNames.length];
144
        System.arraycopy(fieldNames, 0, clone.fieldNames, 0, fieldNames.length);
145
        // Clone fieldTypes
146
        clone.fieldTypes = new int[fieldTypes.length];
147
        System.arraycopy(fieldTypes, 0, clone.fieldTypes, 0, fieldTypes.length);
148

    
149
        return clone;
150
    }
151

    
152
    @Override
153
    public void loadFromState(PersistentState state)
154
            throws PersistenceException {
155
        // Set parent properties
156
        super.loadFromState(state);
157
        // Set own properties
158
        this.fieldNames = state.getStringArray(FIELD_FIELD_NAMES);
159
        this.fieldTypes = state.getIntArray(FIELD_FIELD_TYPES);
160
    }
161

    
162
    @Override
163
    public void saveToState(PersistentState state) throws PersistenceException {
164
        // Save parent properties
165
        super.saveToState(state);
166
        // Save own properties
167
        state.set(FIELD_FIELD_NAMES, this.fieldNames);
168
        state.set(FIELD_FIELD_TYPES, this.fieldTypes);
169
    }
170

    
171
    public static class RegisterPersistence implements Callable {
172

    
173
        @Override
174
        public Object call() throws Exception {
175
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
176
            if (manager.getDefinition(CLASSIFIED_VECTOR_LEGEND_PERSISTENCE_DEFINITION_NAME) == null) {
177
                DynStruct definition = manager.addDefinition(
178
                        AbstractClassifiedVectorLegend.class,
179
                        CLASSIFIED_VECTOR_LEGEND_PERSISTENCE_DEFINITION_NAME,
180
                        CLASSIFIED_VECTOR_LEGEND_PERSISTENCE_DEFINITION_NAME + " Persistence definition",
181
                        null,
182
                        null
183
                );
184
                // Extend the Vectorial Legend base definition
185
                definition.extend(manager.getDefinition(VECTORIAL_LEGEND_PERSISTENCE_DEFINITION_NAME));
186

    
187
                // Field names
188
                definition.addDynFieldArray(FIELD_FIELD_NAMES)
189
                        .setClassOfItems(String.class);
190
                // Field types
191
                definition.addDynFieldArray(FIELD_FIELD_TYPES)
192
                        .setClassOfItems(int.class);
193
            }
194
            return true;
195
        }
196

    
197
    }
198

    
199
}