Revision 13927 trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/rendering/ZSort.java

View differences:

ZSort.java
57 57
 * @author jaume dominguez faus - jaume.dominguez@iver.es
58 58
 */
59 59
public class ZSort implements IPersistence {
60
    private final ISymbol[] symbols;
61
    private boolean usingSymbolLevels;
62
    private Hashtable table = new Hashtable();
63
    /**
64
     * Constructor Method
65
     * @param legend
66
     */
67
    public ZSort(ILegend legend) {
68
        if (legend instanceof IClassifiedLegend) {
69
            ISymbol[] ss = ((IClassifiedLegend) legend).getSymbols();
70
            symbols = new ISymbol[ss.length];
71
            System.arraycopy(
72
                    ss, 0,
73
                    symbols, 0, ss.length);
74
        } else {
75
            symbols = new ISymbol[] {legend.getDefaultSymbol()};
76
        }
77
    }
78
    /**
79
     * Ok if it is being used symbol levels, false on the contrary.
80
     * @return usingSymbolLevels, boolean;
81
     */
82
    public boolean isUsingZSort() {
83
        return usingSymbolLevels;
84
    }
85
    /**
86
     * Stablishes if the Symbol Levels is going to be used or not.
87
     * @param b
88
     */
60
	private int[][] matrix;
61
	private boolean usingZSort;
62
	private Hashtable<ISymbol, Integer> symbols = new Hashtable<ISymbol, Integer>(); 
89 63

  
90
    public void setUsingZSort(boolean b) {
91
        usingSymbolLevels = b;
92
    }
93
    /**
94
     * Returns the number of layers that has the symbol which is an instance of
95
     * IMultiLayerSymbol and has the maximum number of layers.
96
     * @return
97
     */
98
    public int getMaxSymbolLayerCount() {
99
        int count = 0;
100
        for (int i = 0; i < symbols.length; i++) {
101
            int levels = (symbols[i] instanceof IMultiLayerSymbol) ?
102
                    ((IMultiLayerSymbol) symbols[i]).getLayerCount() : 1;
103
                    count = Math.max(count, levels);
104
        }
105
        return count;
64
	public ZSort(ILegend legend) {
65
		ISymbol[] symbols;
66
		if (legend instanceof IClassifiedLegend) {
67
			symbols = ((IClassifiedLegend) legend).getSymbols();
68
			
69
		} else {
70
			symbols = new ISymbol[] {legend.getDefaultSymbol()};
71
		}
72
		matrix = new int[symbols.length][];
73
		
74
		for (int i = 0; i < symbols.length; i++) {
75
			this.symbols.put(symbols[i], i);
76
			matrix[i] = new int[ 
77
			                     symbols[i] instanceof IMultiLayerSymbol ? 
78
			                   	 ((IMultiLayerSymbol) symbols[i]).getLayerCount() : 
79
			                   	 1
80
			                   ];
81
		}
82
		System.err.println(this);
83
	}
106 84

  
107
    }
108 85

  
109
    public ISymbol getSymbolByFeatureAndLevel(IFeature feat, int level) {
110
        // TODO Implement it
111
        throw new Error("Not yet implemented!");
86
	public String getClassName() {
87
		return getClass().getName();
88
	}
112 89

  
113
    }
114
    /**
115
     * Returns an array composed by all the symbols.
116
     * @return symbols, ISymbol[]
117
     */
118
    public ISymbol[] getSymbols() {
119
        return symbols;
120
    }
121
    /**
122
     * Returns an array composed by all the descriptions of the symbols.
123
     *
124
     * @return desc, String[]
125
     */
126
    public String[] getDescriptions() {
127
        String[] desc = new String[symbols.length];
128
        for (int i = 0; i < desc.length; i++) {
129
            desc[i] = symbols[i].getDescription();
130
        }
131
        return desc;
132
    }
90
	public XMLEntity getXMLEntity() {
91
		// TODO Auto-generated method stub
92
		throw new Error("Not yet implemented!");
93
	}
94
	
95
	public void setXMLEntity(XMLEntity xml) {
96
		// TODO Auto-generated method stub
97
		throw new Error("Not yet implemented!");
98
	}
133 99

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

  
146
            ISymbol sym = (ISymbol) it.next();
147
            int[] levels = (int[]) table.get(sym);
101
	public int getLevelCount() {
102
		int count = -1;
103
		for (int i = 0; i < matrix.length; i++) {
104
			for (int j = 0; j < matrix[i].length; j++) {
105
				count = Math.max(count, matrix[i][j]);
106
			}
107
		}
108
		return count + 1;
109
	}
148 110

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

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

  
171
            ISymbol sym = (ISymbol) it.next();
172
            int[] levels = (int[]) table.get(sym);
173
            for (int i = 0; i < levels.length; i++) {
174
                level = Math.max(level, levels[i]);
175
            }
116
	public void setLevels(ISymbol sym, int[] values) {
117
		setLevels(symbols.get(sym), values);
118
	}
176 119

  
177 120

  
178
        }
179
        return level;
180
    }
121
	public void setLevels(int row, int[] values) {
122
		matrix[row] = values;
123
	}
181 124

  
182 125

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

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

  
205
        ISymbol[] syms = (ISymbol[]) table.keySet().toArray(new ISymbol[0]);
206
        int[][] aux = new int[syms.length][];
207
        Iterator it = table.keySet().iterator();
208
        int i = 0;
209
        while (it.hasNext()) {
210
            aux[i] = (int[]) it.next();
211
            i++;
212
        }
131
	public ISymbol[] getSymbols() {
132
		return symbols.keySet().toArray(new ISymbol[0]);
133
	}
213 134

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

  
222
        for (i = 0; i < isUsedLevel.length; i++) {
223
            if (!isUsedLevel[i]) {
224
                for (int j = 0; j < aux.length; j++) {
225
                    int[] thisLevels = aux[i];
226
                    for (int k = 0; j < thisLevels.length; k++) {
227
                        if (thisLevels[j]>i)
228
                            thisLevels[j]--;
229
                    }
136
	public String[] getDescriptions() {
137
		ISymbol[] symbols = getSymbols();
138
		String[] descs = new String[symbols.length];
139
		for (int i = 0; i < descs.length; i++) {
140
			descs[i] = symbols[i].getDescription();
141
		}
142
		return descs;
143
	}
230 144

  
231
                }
232
            }
233
        }
234 145

  
235
    }
146
	public int getSymbolLevel(ISymbol layer) {
147
		ISymbol[] theSymbols = getSymbols();
148
		
149
		for (int i = 0; i < theSymbols.length; i++) {
150
			ISymbol mySymbol = theSymbols[i];
151
			
152
			if (mySymbol instanceof IMultiLayerSymbol) {
153
				IMultiLayerSymbol multiSym = (IMultiLayerSymbol) mySymbol;
154
				for (int j = 0; j < multiSym.getLayerCount(); j++) {
155
					ISymbol myInnerSymbol = multiSym.getLayer(j);
156
					if (myInnerSymbol.equals(layer)) {
157
						int row = symbols.get(multiSym);
158
						return matrix[row][j];
159
					}
160
				}
161
			} else {
162
				if (mySymbol.equals(layer)) {
163
					return matrix[symbols.get(layer)][i];
164
				}
165
			}
166
		}
167
		
168
		System.err.println("did not find symbol: '"+layer.getDescription()+"' in zSort");
169
		return 0;
170
	}
171
	
172
	public int getTopLevelIndexAllowed() {
173
		ISymbol[] symbols = getSymbols();
174
		int count=0;
175
		for (int i = 0; i < symbols.length; i++) {
176
			if (symbols[i] instanceof IMultiLayerSymbol) {
177
				IMultiLayerSymbol mSymbol = (IMultiLayerSymbol) symbols[i];
178
				count = Math.max(count, mSymbol.getLayerCount());
179
			} else
180
				count = Math.max(count, 1);
181
		}
182
		return count;
183
	}
184
	
185
	@Override
186
	public String toString() {
187
		String out = "Symbols:\n---------\n\n";
188
		ISymbol[] syms = getSymbols();
189
		for (int i = 0; i < syms.length; i++) {
190
			out += syms.getClass()+":\t"+syms[i].getDescription();
191
		}
192
		out += "\nMatrix:\n--------\n\n";
193
		out += "    \t";
194
		for (int i = 0; i < getTopLevelIndexAllowed(); i++) {
195
			out += "column"+i+"\t\t";
196
		}
197
		out += "\n";
198
		for (int i = 0; i < matrix.length; i++) {
199
			out += "row "+i+":\t";
200
			for (int j = 0; j < matrix[i].length; j++) {
201
				out += matrix[i][j]+"\t\t";
202
			}
203
			out += "\n";
204
		}
205
		return out;
206
	}
207
	
208
//    private final ISymbol[] symbols;
209
//    private boolean usingSymbolLevels;
210
//    private Hashtable<ISymbol, int[]> table = new Hashtable<ISymbol, int[]>();
211
//    /**
212
//     * Constructor Method
213
//     * @param legend
214
//     */
215
//    public ZSort(ILegend legend) {
216
//        if (legend instanceof IClassifiedLegend) {
217
//            ISymbol[] ss = ((IClassifiedLegend) legend).getSymbols();
218
//            symbols = new ISymbol[ss.length];
219
//            System.arraycopy(
220
//                    ss, 0,
221
//                    symbols, 0, ss.length);
222
//        } else {
223
//            symbols = new ISymbol[] {legend.getDefaultSymbol()};
224
//        }
225
//    }
226
//    /**
227
//     * Ok if it is being used symbol levels, false on the contrary.
228
//     * @return usingSymbolLevels, boolean;
229
//     */
230
//    public boolean isUsingZSort() {
231
//        return usingSymbolLevels;
232
//    }
233
//    /**
234
//     * Stablishes if the Symbol Levels is going to be used or not.
235
//     * @param b
236
//     */
237
//
238
//    public void setUsingZSort(boolean b) {
239
//        usingSymbolLevels = b;
240
//    }
241
//    /**
242
//     * Returns the number of layers that has the symbol which is an instance of
243
//     * IMultiLayerSymbol and has the maximum number of layers.
244
//     * @return
245
//     */
246
//    public int getMaxSymbolLayerCount() {
247
//        int count = 0;
248
//        for (int i = 0; i < symbols.length; i++) {
249
//            int levels = (symbols[i] instanceof IMultiLayerSymbol) ?
250
//                    ((IMultiLayerSymbol) symbols[i]).getLayerCount() : 1;
251
//                    count = Math.max(count, levels);
252
//        }
253
//        return count;
254
//
255
//    }
256
//
257
//    public ISymbol getSymbolByFeatureAndLevel(IFeature feat, int level) {
258
//        // TODO Implement it
259
//        throw new Error("Not yet implemented!");
260
//
261
//    }
262
//    /**
263
//     * Returns an array composed by all the symbols.
264
//     * @return symbols, ISymbol[]
265
//     */
266
//    public ISymbol[] getSymbols() {
267
//        return symbols;
268
//    }
269
//    /**
270
//     * Returns an array composed by all the descriptions of the symbols.
271
//     *
272
//     * @return desc, String[]
273
//     */
274
//    public String[] getDescriptions() {
275
//        String[] desc = new String[symbols.length];
276
//        for (int i = 0; i < desc.length; i++) {
277
//            desc[i] = symbols[i].getDescription();
278
//        }
279
//        return desc;
280
//    }
281
//
282
//    /**
283
//     * Obtains the level of a symbol.The method to obtain this value will be different
284
//     * depending on the type of the symbol (if the symbol is multilayer we have
285
//     * to check all the layers).
286
//     *
287
//     * @param layer
288
//     * @return int
289
//     */
290
//    public int getSymbolLevel(ISymbol layer) {
291
//        Iterator<ISymbol> it = table.keySet().iterator();
292
//        while (it.hasNext()) {
293
//
294
//            ISymbol sym = it.next();
295
//            int[] levels = table.get(sym);
296
//
297
//            if (sym instanceof IMultiLayerSymbol) {
298
//                IMultiLayerSymbol mlSym = (IMultiLayerSymbol) sym;
299
//                for (int i = 0; i < mlSym.getLayerCount(); i++) {
300
//                    if (mlSym.getLayer(i).equals(layer))
301
//                        return levels[i];
302
//                }
303
//            } else {
304
//                if (sym.equals(layer)) return levels[0];
305
//            }
306
//
307
//        }
308
//        return 0; // symbol does not exist yet in the ZSort
309
//    }
310
//    /**
311
//     * Returns the maximum level of all the symbols contained in the hash table
312
//     * @return int
313
//     */
314
//    public int getLevelCount() {
315
//        int level = 0;
316
//        Iterator<ISymbol> it = table.keySet().iterator();
317
//        while (it.hasNext()) {
318
//
319
//            ISymbol sym = it.next();
320
//            int[] levels = table.get(sym);
321
//            for (int i = 0; i < levels.length; i++) {
322
//                level = Math.max(level, levels[i]);
323
//            }
324
//
325
//
326
//        }
327
//        return level + 1;
328
//    }
329
//
330
//
331
//    /**
332
//     * Sets the levels associated with the symbols. The length of the
333
//     * argument levels must match the number of layers of the symbol in
334
//     * case the symbol is an {@link IMultiLayerSymbol}, or 1 otherwise
335
//     * @param sym
336
//     * @param levels
337
//     */
338
//    public void setLevelsToSymbol(ISymbol sym, int[] levels) {
339
//        table.put(sym, levels);
340
//        removeEmptyGaps();
341
//        
342
//    }
343
//
344
//    /**
345
//     * Ensures that the table does not contain map levels with no symbols by
346
//     * updating the map level index to the next immediate map level to the
347
//     * previous non-empty one.
348
//     */
349
//    private void removeEmptyGaps() {
350
//        if (table.isEmpty()) return;
351
//
352
//        ISymbol[] syms = table.keySet().toArray(new ISymbol[0]);
353
//        int[][] aux = new int[syms.length][];
354
//        Iterator<ISymbol> it = table.keySet().iterator();
355
//        int i = 0;
356
//        while (it.hasNext()) {
357
//            aux[i] = table.get(it.next());
358
//            i++;
359
//        }
360
//
361
//        boolean[] isUsedLevel = new boolean[getLevelCount()];
362
//        for (i = 0; i < aux.length; i++) {
363
//            int[] thisLevels = aux[i];
364
//            for (int j = 0; j < thisLevels.length; j++) {
365
//                isUsedLevel[thisLevels[j]] = true;
366
//            }
367
//        }
368
//
369
//        for (i = 0; i < isUsedLevel.length; i++) {
370
//            if (!isUsedLevel[i]) {
371
//                for (int j = 0; j < aux.length; j++) {
372
//                    int[] thisLevels = aux[i];
373
//                    for (int k = 0; j < thisLevels.length; k++) {
374
//                        if (thisLevels[j]>i)
375
//                            thisLevels[j]--;
376
//                    }
377
//
378
//                }
379
//            }
380
//        }
381
//
382
//    }
383
//
384
//    public String toString() {
385
//        String out = super.toString();
386
//        Iterator<ISymbol> it = table.keySet().iterator();
387
//        while (it.hasNext()) {
388
//            ISymbol sym = it.next();
389
//            int[] levels = table.get(sym);
390
//            out += "\n"+sym.getDescription();
391
//            for (int i = 0; i < levels.length; i++) {
392
//                out += "\t"+levels[i];
393
//            }
394
//        }
395
//        return out;
396
//    }
397
//
398
//    public String getClassName() {
399
//        return getClass().getName();
400
//    }
401
//
402
//    public XMLEntity getXMLEntity() {
403
//        // TODO Auto-generated method stub
404
//        throw new Error("Not yet implemented!");
405
//    }
406
//
407
//    public void setXMLEntity(XMLEntity xml) {
408
//        // TODO Auto-generated method stub
409
//        throw new Error("Not yet implemented!");
410
//    }
236 411

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

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

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

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

  
265 412
}

Also available in: Unified diff