Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / ZSort.java @ 13032

History | View | Annotate | Download (7.92 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.rendering;
42

    
43
import java.util.Hashtable;
44
import java.util.Iterator;
45
import java.util.logging.Level;
46
import java.util.logging.Logger;
47

    
48
import com.iver.cit.gvsig.fmap.core.IFeature;
49
import com.iver.cit.gvsig.fmap.core.symbols.IMultiLayerSymbol;
50
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
51
import com.iver.utiles.IPersistance;
52
import com.iver.utiles.XMLEntity;
53

    
54
/**
55
 * Class ZSort is used in order to store information of the symbols which are
56
 * placed in the Z axis (because a level of symbols has been used).
57
 * @author jaume dominguez faus - jaume.dominguez@iver.es
58
 */
59
public class ZSort implements IPersistance{
60
    private final ISymbol[] symbols;
61
    private boolean usingSymbolLevels;
62
//        private Hashtable symbolLevels = new Hashtable();
63
    private Hashtable table = new Hashtable();
64
    /**
65
     * Constructor Method
66
     * @param legend
67
     */
68
    public ZSort(ILegend legend) {
69
        if (legend instanceof IClassifiedLegend) {
70
            ISymbol[] ss = ((IClassifiedLegend) legend).getSymbols();
71
            symbols = new ISymbol[ss.length];
72
            System.arraycopy(
73
                    ss, 0,
74
                    symbols, 0, ss.length);
75
        } else {
76
            symbols = new ISymbol[] {legend.getDefaultSymbol()};
77
        }
78
    }
79
    /**
80
     * Ok if it is being used symbol levels, false on the contrary.
81
     * @return usingSymbolLevels, boolean;
82
     */
83
    public boolean isUsingZSort() {
84
        return usingSymbolLevels;
85
    }
86
    /**
87
     * Stablishes if the Symbol Levels is going to be used or not.
88
     * @param b
89
     */
90

    
91
    public void setUsingZSort(boolean b) {
92
        usingSymbolLevels = b;
93
    }
94
    /**
95
     * Returns the number of layers that has the symbol which is an instance of
96
     * IMultiLayerSymbol and has the maximum number of layers.
97
     * @return
98
     */
99
    public int getMaxSymbolLayerCount() {
100
        int count = 0;
101
        for (int i = 0; i < symbols.length; i++) {
102
            int levels = (symbols[i] instanceof IMultiLayerSymbol) ?
103
                    ((IMultiLayerSymbol) symbols[i]).getLayerCount() : 1;
104
                    count = Math.max(count, levels);
105
        }
106
        return count;
107

    
108
    }
109

    
110
    public ISymbol getSymbolByFeatureAndLevel(IFeature feat, int level) {
111
        // TODO Implement it
112
        throw new Error("Not yet implemented!");
113

    
114
    }
115
    /**
116
     * Returns an array composed by all the symbols.
117
     * @return symbols, ISymbol[]
118
     */
119
    public ISymbol[] getSymbols() {
120
        return symbols;
121
    }
122
    /**
123
     * Returns an array composed by all the descriptions of the symbols.
124
     *
125
     * @return desc, String[]
126
     */
127
    public String[] getDescriptions() {
128
        String[] desc = new String[symbols.length];
129
        for (int i = 0; i < desc.length; i++) {
130
            desc[i] = symbols[i].getDescription();
131
        }
132
        return desc;
133
    }
134

    
135
    /**
136
     * Obtains the level of a symbol.The method to obtain this value will be different
137
     * depending on the type of the symbol (if the symbol is multilayer we have
138
     * to check all the layers).
139
     *
140
     * @param layer,ISymbol
141
     * @return int
142
     */
143
    public int getSymbolLevel(ISymbol layer) {
144
        Iterator it = table.keySet().iterator();
145
        while (it.hasNext()) {
146

    
147
            ISymbol sym = (ISymbol) it.next();
148
            int[] levels = (int[]) table.get(sym);
149

    
150
            if (sym instanceof IMultiLayerSymbol) {
151
                IMultiLayerSymbol mlSym = (IMultiLayerSymbol) sym;
152
                for (int i = 0; i < mlSym.getLayerCount(); i++) {
153
                    if (mlSym.getLayer(i).equals(layer))
154
                        return levels[i];
155
                }
156
            } else {
157
                if (sym.equals(layer)) return levels[0];
158
            }
159

    
160
        }
161
        return 0; // symbol does not exist yet in the ZSort
162
    }
163
    /**
164
     * Returns the maximum level of all the symbols contained in the hash table
165
     * @return int
166
     */
167
    public int getLevelCount() {
168
        int level = 0;
169
        Iterator it = table.keySet().iterator();
170
        while (it.hasNext()) {
171

    
172
            ISymbol sym = (ISymbol) it.next();
173
            int[] levels = (int[]) table.get(sym);
174
            for (int i = 0; i < levels.length; i++) {
175
                level = Math.max(level, levels[i]);
176
            }
177

    
178

    
179
        }
180
        return level;
181
    }
182

    
183

    
184
    /**
185
     * Sets the levels associated with the symbols. The length of the
186
     * argument levels must match the number of layers of the symbol in
187
     * case the symbol is an {@link IMultiLayerSymbol}, or 1 otherwise
188
     * @param sym
189
     * @param levels
190
     */
191
    public void setLevelsToSymbol(ISymbol sym, int[] levels) {
192
        table.put(sym, levels);
193
        System.err.println(this);
194
//                removeEmptyGaps();
195
        System.err.println(this);
196
    }
197

    
198
    /**
199
     * Ensures that the table does not contain map levels with no symbols by
200
     * updating the map level index to the next inmediate map level to the
201
     * previous non-empty one.
202
     */
203
    private void removeEmptyGaps() {
204
        if (table.isEmpty()) return;
205

    
206
        ISymbol[] syms = (ISymbol[]) table.keySet().toArray(new ISymbol[0]);
207
        int[][] aux = new int[syms.length][];
208
        Iterator it = table.keySet().iterator();
209
        int i = 0;
210
        while (it.hasNext()) {
211
            aux[i] = (int[]) it.next();
212
            i++;
213
        }
214

    
215
        boolean[] isUsedLevel = new boolean[getLevelCount()];
216
        for (i = 0; i < aux.length; i++) {
217
            int[] thisLevels = aux[i];
218
            for (int j = 0; j < thisLevels.length; j++) {
219
                isUsedLevel[thisLevels[j]] = true;
220
            }
221
        }
222

    
223
        for (i = 0; i < isUsedLevel.length; i++) {
224
            if (!isUsedLevel[i]) {
225
                for (int j = 0; j < aux.length; j++) {
226
                    int[] thisLevels = aux[i];
227
                    for (int k = 0; j < thisLevels.length; k++) {
228
                        if (thisLevels[j]>i)
229
                            thisLevels[j]--;
230
                    }
231

    
232
                }
233
            }
234
        }
235

    
236
    }
237

    
238
    public String toString() {
239
        String out = super.toString();
240
        Iterator it = table.keySet().iterator();
241
        while (it.hasNext()) {
242
            ISymbol sym = (ISymbol) it.next();
243
            int[] levels = (int[]) table.get(sym);
244
            out += "\n"+sym.getDescription();
245
            for (int i = 0; i < levels.length; i++) {
246
                out += "\t"+levels[i];
247
            }
248
        }
249
        return out;
250
    }
251

    
252
    public String getClassName() {
253
        return getClass().getName();
254
    }
255

    
256
    public XMLEntity getXMLEntity() {
257
        // TODO Auto-generated method stub
258
        throw new Error("Not yet implemented!");
259
    }
260

    
261
    public void setXMLEntity(XMLEntity xml) {
262
        // TODO Auto-generated method stub
263
        throw new Error("Not yet implemented!");
264
    }
265

    
266
}