Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extRasterTools-SE / src / org / gvsig / fmap / raster / legend / ColorTableLegend.java @ 32880

History | View | Annotate | Download (6.93 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.fmap.raster.legend;
20

    
21
import java.awt.Color;
22
import java.util.Arrays;
23
import java.util.List;
24

    
25
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedLegend;
26
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
27
import org.gvsig.fmap.mapcontext.rendering.legend.IRasterLegend;
28
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendContentsChangedListener;
29
import org.gvsig.fmap.mapcontext.rendering.legend.events.SymbolLegendEvent;
30
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
31
import org.gvsig.raster.datastruct.ColorItem;
32
import org.gvsig.raster.datastruct.ColorTable;
33
import org.gvsig.raster.util.MathUtils;
34
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.SimpleFillSymbol;
35
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.impl.SimpleLineSymbol;
36
import org.gvsig.tools.ToolsLocator;
37
import org.gvsig.tools.dynobject.DynStruct;
38
import org.gvsig.tools.persistence.PersistenceManager;
39
import org.gvsig.tools.persistence.PersistentState;
40
import org.gvsig.tools.persistence.exception.PersistenceException;
41

    
42
/**
43
 * Leyenda para tablas de color aplicadas a un raster.
44
 *
45
 * @version 27/06/2007
46
 * @author Nacho Brodin (nachobrodin@gmail.com)
47
 */
48
public class ColorTableLegend implements IClassifiedLegend, IRasterLegend {
49
        public static final String COLOR_TABLE_LEGEND_DYNCLASS_NAME =
50
                        "ColorTableLegend";
51

    
52
        private static final String FIELD_SYMBOLS = "symbols";
53
        private static final String FIELD_DESCRIPTIONS = "descriptions";
54

    
55
        ISymbol[] symbols = null;
56
        String[] desc = null;
57

    
58
        /**
59
         * Crea una leyenda de tipo ColorTableLegend a partir de un objeto ColorTable
60
         * @param colorTable
61
         * @return ColorTableLegend
62
         */
63
        public static ColorTableLegend createLegend(ColorTable colorTable) {
64
                if ((colorTable == null) || (colorTable.getColorItems() == null))
65
                        return null;
66

    
67
                SimpleLineSymbol line = new SimpleLineSymbol();
68
                line.setLineColor(Color.BLACK);
69
                ISymbol[] symbol = new ISymbol[colorTable.getColorItems().size()];
70
                String[] desc = new String[colorTable.getColorItems().size()];
71

    
72
                String nameClass = null;
73
                for (int i = 0; i < colorTable.getColorItems().size(); i++) {
74
                        SimpleFillSymbol s = new SimpleFillSymbol();
75
                        s.setOutline(line);
76
                        s.setFillColor(((ColorItem) colorTable.getColorItems().get(i)).getColor());
77
                        nameClass = ((ColorItem) colorTable.getColorItems().get(i)).getNameClass();
78
                        if ((nameClass == null) || (nameClass.equals(""))){
79
                                if (i < (colorTable.getColorItems().size() - 1)){
80
                                        desc[i] = "[" + MathUtils.format(((ColorItem) colorTable.getColorItems().get(i)).getValue(), 2) + " , " + MathUtils.format(((ColorItem) colorTable.getColorItems().get(i + 1)).getValue(), 2) + "[ ";
81
                                }else{
82
                                        desc[i] = "[" + MathUtils.format(((ColorItem) colorTable.getColorItems().get(i)).getValue(), 2) + "] ";
83
                                }
84
                        }else{
85
                                desc[i] = ((ColorItem) colorTable.getColorItems().get(i)).getNameClass();
86
                        }        
87
                        symbol[i] = s;
88
                }
89

    
90
                return new ColorTableLegend(symbol, desc);
91
        }
92

    
93
        /**
94
         * Leyenda para tablas de color raster.
95
         * @param s Lista de simbolos
96
         * @param d Lista de descripciones de simbolos
97
         */
98
        public ColorTableLegend(ISymbol[] s, String[] d) {
99
                symbols = s;
100
                desc = d;
101
        }
102

    
103
        /**
104
         * Empty constructor used only for persistence purposes.
105
         */
106
        public ColorTableLegend() {
107
        }
108

    
109
        /*
110
         * (non-Javadoc)
111
         * @see com.iver.cit.gvsig.fmap.rendering.IClassifiedLegend#getDescriptions()
112
         */
113
        public String[] getDescriptions() {
114
                return desc;
115
        }
116

    
117
        /*
118
         * (non-Javadoc)
119
         * @see com.iver.cit.gvsig.fmap.rendering.IClassifiedLegend#getSymbols()
120
         */
121
        public ISymbol[] getSymbols() {
122
                return symbols;
123
        }
124

    
125
        /*
126
         * (non-Javadoc)
127
         * @see com.iver.cit.gvsig.fmap.rendering.IClassifiedLegend#getValues()
128
         */
129
        public Object[] getValues() {
130
                return desc;
131
        }
132

    
133
        /*
134
         * (non-Javadoc)
135
         * @see com.iver.cit.gvsig.fmap.rendering.ILegend#cloneLegend()
136
         */
137
        public ILegend cloneLegend() {
138
                return null;
139
        }
140

    
141
        /*
142
         * (non-Javadoc)
143
         * @see com.iver.cit.gvsig.fmap.rendering.ILegend#getDefaultSymbol()
144
         */
145
        public ISymbol getDefaultSymbol() {
146
                return null;
147
        }
148

    
149
        /*
150
         * (non-Javadoc)
151
         * @see com.iver.cit.gvsig.fmap.rendering.ILegend#getSLDString(java.lang.String)
152
         */
153
        public String getSLDString(String layerName) {
154
                return null;
155
        }
156

    
157
        /*
158
         * (non-Javadoc)
159
         * @see com.iver.utiles.IPersistance#getClassName()
160
         */
161
        public String getClassName() {
162
                return null;
163
        }
164

    
165
        public void addLegendListener(LegendContentsChangedListener listener) {
166
                // TODO Auto-generated method stub
167
        }
168

    
169
        public void fireDefaultSymbolChangedEvent(SymbolLegendEvent event) {
170
                // TODO Auto-generated method stub
171
                
172
        }
173

    
174
        public LegendContentsChangedListener[] getListeners() {
175
                // TODO Auto-generated method stub
176
                return null;
177
        }
178

    
179
        
180
        public void removeLegendListener(LegendContentsChangedListener listener) {
181
                // TODO Auto-generated method stub
182
        }
183

    
184
        @Override
185
        public Object clone() throws CloneNotSupportedException {
186
                // TODO Auto-generated method stub
187
                return super.clone();
188
        }
189

    
190
        private void setDescriptions(String[] descriptions) {
191
                this.desc = descriptions;
192
        }
193

    
194
        private void setSymbols(ISymbol[] symbols) {
195
                this.symbols = symbols;
196
        }
197

    
198
        @SuppressWarnings("unchecked")
199
        public void loadFromState(PersistentState state)
200
                        throws PersistenceException {
201
                List<ISymbol> symbols = (List<ISymbol>) state.get(FIELD_SYMBOLS);
202
                setSymbols(symbols.toArray(new ISymbol[symbols.size()]));
203

    
204
                List<String> descriptions =
205
                                (List<String>) state.get(FIELD_DESCRIPTIONS);
206
                setDescriptions(descriptions.toArray(new String[descriptions.size()]));
207
        }
208

    
209
        public void saveToState(PersistentState state) throws PersistenceException {
210
                state.set(FIELD_SYMBOLS, Arrays.asList(getSymbols()));
211
                state.set(FIELD_DESCRIPTIONS, Arrays.asList(getDescriptions()));
212
        }
213

    
214
        public static void registerPersistence() {
215
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
216
                DynStruct definition = manager.addDefinition(
217
                                ColorTableLegend.class,
218
                                COLOR_TABLE_LEGEND_DYNCLASS_NAME,
219
                                COLOR_TABLE_LEGEND_DYNCLASS_NAME+" Persistence definition",
220
                                null, 
221
                                null
222
                );
223

    
224
                // Symbols
225
                definition.addDynFieldList(FIELD_SYMBOLS).setMandatory(true);
226
                // Descriptions
227
                definition.addDynFieldList(FIELD_DESCRIPTIONS).setMandatory(true);
228

    
229
        }
230

    
231
}