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 / SingleSymbolLegend.java @ 43326

History | View | Annotate | Download (6.1 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 org.gvsig.fmap.dal.exception.DataException;
27
import org.gvsig.fmap.dal.feature.Feature;
28
import org.gvsig.fmap.dal.feature.FeatureStore;
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.mapcontext.MapContextLocator;
31
import org.gvsig.fmap.mapcontext.MapContextManager;
32
import org.gvsig.fmap.mapcontext.rendering.legend.ISingleSymbolLegend;
33
import org.gvsig.fmap.mapcontext.rendering.legend.events.SymbolLegendEvent;
34
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
35
import org.gvsig.tools.ToolsLocator;
36
import org.gvsig.tools.dynobject.DynStruct;
37
import org.gvsig.tools.persistence.PersistenceManager;
38
import org.gvsig.tools.persistence.PersistentState;
39
import org.gvsig.tools.persistence.exception.PersistenceException;
40
import org.gvsig.tools.util.Callable;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
/**
45
 * Implements a legend composed by single symbols.
46
 * 
47
 */
48
public class SingleSymbolLegend extends AbstractVectorialLegend implements
49
                ISingleSymbolLegend {
50

    
51
        final static private Logger LOG = LoggerFactory.getLogger(VectorialUniqueValueLegend.class);
52

    
53
        public static final String SINGLE_SYMBOL_PERSISTENCE_DEFINITION_NAME =
54
                        "SimgleSymbolLegend";
55
        
56
        private ISymbol defaultSymbol;
57
    private int shapeType = Geometry.TYPES.SURFACE; // Por defecto, tipo pol?gono
58

    
59
        /**
60
         * Constructor method, needed by persistence.
61
         */
62
        public SingleSymbolLegend() {
63
                super();
64
        }
65

    
66
        /**
67
         * Convenience fast constructor.
68
         *
69
         * @param style S?mbolo.
70
         */
71
        public SingleSymbolLegend(ISymbol style) {
72
                super();
73
                setDefaultSymbol(style);
74
        }
75

    
76

    
77
    @Override
78
        public void setDefaultSymbol(ISymbol s) {
79
                if (s == null) {
80
            throw new NullPointerException("Default symbol cannot be null");
81
        }
82
                ISymbol old = defaultSymbol;
83
                defaultSymbol = s;
84
                fireDefaultSymbolChangedEvent(new SymbolLegendEvent(old, s));
85
        }
86

    
87

    
88
        public ISymbol getSymbol(int recordIndex) {
89
                return defaultSymbol;
90
        }
91

    
92
    @Override
93
        public ISymbol getDefaultSymbol() {
94
                if(defaultSymbol==null) {
95
                        defaultSymbol = getSymbolManager().createSymbol(shapeType);
96
                }
97
                return defaultSymbol;
98
        }
99

    
100

    
101

    
102
    @Override
103
        public int getShapeType() {
104
                return shapeType;
105
        }
106

    
107
    @Override
108
        public void setShapeType(int shapeType) {
109
                if (this.shapeType != shapeType) {
110
                        if(defaultSymbol==null || defaultSymbol.getSymbolType()!=shapeType){
111
                                defaultSymbol = getSymbolManager().createSymbol(shapeType);
112
                        }
113
                        this.shapeType = shapeType;
114
                }
115
        }
116

    
117
    @Override
118
    public ISymbol getSymbolByFeature(Feature feat) {
119
        return getDefaultSymbol();
120
    }
121

    
122
    @Override
123
        public void useDefaultSymbol(boolean b) {
124
                LOG.warn("TODO: SingleSymbolLegend.useDefaultSymbol");
125
        }
126

    
127
    public String[] getUsedFields() {
128
        return new String[0];
129
    }
130

    
131
    @Override
132
    public boolean isUseDefaultSymbol() {
133
            return true;
134

    
135
    }
136

    
137

    
138
    public String getClassName() {
139
                return getClass().getName();
140
        }
141

    
142
    @Override
143
    public boolean isSuitableForShapeType(int shapeType) {
144
                return getShapeType() == shapeType;
145
        }
146

    
147

    
148
        public void setFeatureStore(FeatureStore fs) throws DataException {
149
                LOG.warn("TODO: SingleSymbolLegend.useDefaultSymbol");
150
        }
151
        
152
    @Override
153
        protected String[] getRequiredFeatureAttributeNames(
154
                        FeatureStore featureStore) throws DataException {
155
                // We only need the default Geometry to draw
156
                return new String[] { featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName() };
157
        }
158

    
159
    @Override
160
        public Object clone() throws CloneNotSupportedException {
161
                SingleSymbolLegend clone = (SingleSymbolLegend) super.clone();
162

    
163
                // Clone default symbol
164
                if (defaultSymbol != null) {
165
                        clone.defaultSymbol = (ISymbol) defaultSymbol.clone();
166
                }
167

    
168
                return clone;
169
        }
170

    
171
    @Override
172
        public void loadFromState(PersistentState state)
173
                        throws PersistenceException {
174
                super.loadFromState(state);
175
        }
176

    
177
    @Override
178
        public void saveToState(PersistentState state) throws PersistenceException {
179
                super.saveToState(state);
180
        }
181

    
182
        public static class RegisterPersistence implements Callable {
183

    
184
        @Override
185
                public Object call() throws Exception {
186
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
187
                        if( manager.getDefinition(SINGLE_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) {
188
                                DynStruct definition = manager.addDefinition(
189
                                                SingleSymbolLegend.class,
190
                                                SINGLE_SYMBOL_PERSISTENCE_DEFINITION_NAME,
191
                                                SINGLE_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
192
                                                null, 
193
                                                null
194
                                );
195
                                // Extend the Vectorial Legend base definition
196
                                definition.extend(manager.getDefinition(VECTORIAL_LEGEND_PERSISTENCE_DEFINITION_NAME));
197
                        }
198
                        return Boolean.TRUE;
199
                }
200
                
201
        }
202

    
203
        public static class RegisterLegend implements Callable {
204

    
205
        @Override
206
                public Object call() throws Exception {
207
                MapContextManager manager = MapContextLocator.getMapContextManager();
208

    
209
            manager.registerLegend(ISingleSymbolLegend.LEGEND_NAME,
210
                    SingleSymbolLegend.class);
211

    
212
                        return Boolean.TRUE;
213
                }
214
                
215
        }
216

    
217
}