Revision 2473

View differences:

org.gvsig.legend.proportionalsymbols.app.mainplugin/tags/org.gvsig.legend.proportionalsymbols.app.mainplugin-1.0.71/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.symbology.library.ProportionalSymbolsLibrary
org.gvsig.legend.proportionalsymbols.app.mainplugin/tags/org.gvsig.legend.proportionalsymbols.app.mainplugin-1.0.71/src/main/assembly/gvsig-plugin-package.xml
1
<!--
2

  
3
    gvSIG. Desktop Geographic Information System.
4

  
5
    Copyright (C) 2007-2013 gvSIG Association.
6

  
7
    This program is free software; you can redistribute it and/or
8
    modify it under the terms of the GNU General Public License
9
    as published by the Free Software Foundation; either version 3
10
    of the License, or (at your option) any later version.
11

  
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16

  
17
    You should have received a copy of the GNU General Public License
18
    along with this program; if not, write to the Free Software
19
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
    MA  02110-1301, USA.
21

  
22
    For any additional information, do not hesitate to contact us
23
    at info AT gvsig.com, or visit our website www.gvsig.com.
24

  
25
-->
26
<assembly>
27
  <id>gvsig-plugin-package</id>
28
  <formats>
29
    <format>zip</format>
30
  </formats>
31
  <baseDirectory>${project.artifactId}</baseDirectory>
32
  <includeBaseDirectory>true</includeBaseDirectory>
33
  <files>
34
    <file>
35
      <source>target/${project.artifactId}-${project.version}.jar</source>
36
      <outputDirectory>lib</outputDirectory>
37
    </file>
38
    <file>
39
      <source>target/package.info</source>
40
    </file>
41
  </files>
42

  
43
  <fileSets>
44
    <fileSet>
45
      <directory>src/main/resources-plugin</directory>
46
      <outputDirectory>.</outputDirectory>
47
    </fileSet>
48
  </fileSets>
49

  
50
<!-- No dependencies
51
  <dependencySets>
52
    <dependencySet>
53
      <useProjectArtifact>false</useProjectArtifact>
54
      <useTransitiveDependencies>false</useTransitiveDependencies>
55
      <outputDirectory>lib</outputDirectory>
56
      <includes>
57
		<include>...</include>
58
      </includes>
59
    </dependencySet>
60
  </dependencySets>
61
-->
62

  
63
</assembly>
64

  
org.gvsig.legend.proportionalsymbols.app.mainplugin/tags/org.gvsig.legend.proportionalsymbols.app.mainplugin-1.0.71/src/main/java/org/gvsig/symbology/fmap/rendering/ProportionalSymbolsLegend.java
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 org.gvsig.symbology.fmap.rendering;
42

  
43
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
45

  
46
import org.gvsig.fmap.dal.feature.Feature;
47
import org.gvsig.fmap.geom.Geometry;
48
import org.gvsig.fmap.geom.GeometryLocator;
49
import org.gvsig.fmap.geom.GeometryManager;
50
import org.gvsig.fmap.mapcontext.MapContextLocator;
51
import org.gvsig.fmap.mapcontext.MapContextManager;
52
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
53
import org.gvsig.fmap.mapcontext.rendering.legend.ZSort;
54
import org.gvsig.fmap.mapcontext.rendering.legend.events.IntervalLegendEvent;
55
import org.gvsig.fmap.mapcontext.rendering.legend.events.LabelLegendEvent;
56
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendChangedEvent;
57
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendClearEvent;
58
import org.gvsig.fmap.mapcontext.rendering.legend.events.SymbolLegendEvent;
59
import org.gvsig.fmap.mapcontext.rendering.legend.events.ValueLegendEvent;
60
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
61
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
62
import org.gvsig.i18n.Messages;
63
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.VectorialUniqueValueLegend;
64
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.MarkerFillSymbol;
65
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.MultiLayerFillSymbol;
66
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
67
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
68
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.SimpleMarkerFillPropertiesStyle;
69
import org.gvsig.tools.ToolsLocator;
70
import org.gvsig.tools.dynobject.DynStruct;
71
import org.gvsig.tools.persistence.PersistenceManager;
72
import org.gvsig.tools.persistence.PersistentState;
73
import org.gvsig.tools.persistence.exception.PersistenceException;
74
import org.gvsig.tools.util.Callable;
75

  
76

  
77
/**
78
 * Implements a legend which represents the quantitative information
79
 * (numeric values). This representation is possible thanks to a symbol
80
 * whose size is different each time (depending on the numeric values
81
 * and if we want to use normalization or not).
82
 *
83
 * @author jaume dominguez faus - jaume.dominguez@iver.es
84
 * @author jldominguez
85
 */
86
public class ProportionalSymbolsLegend extends VectorialUniqueValueLegend  {
87

  
88
    private static Logger logger = LoggerFactory.getLogger(ProportionalSymbolsLegend.class);
89
    public static final String
90
    PROPORTIONAL_SYMBOL_LEGEND_NAME =
91
        "PROPORTIONAL_SYMBOL_LEGEND";
92
    public static final String
93
    PROPORTIONAL_SYMBOL_LEGEND_PERSISTENCE_DEFINITION_NAME =
94
        "PROPORTIONAL_SYMBOL_LEGEND_PERSISTENCE_DEFINITION";
95

  
96
	public void setClassifyingFieldNames(String[] fNames) {
97
		super.setClassifyingFieldNames(fNames);
98
		valueField = fNames[0];
99
		normalizationField = fNames[1];
100
	}
101

  
102
	private ISymbol backgroundSymbol;
103
	private String valueField;
104
	private String normalizationField;
105
	private double minSize;
106
	private double maxSize;
107
	private int templateShapeType;
108
	private boolean useNormalization;
109
	private double maxFeature, minFeature;
110
	private ZSort zSort = null;
111

  
112

  
113
	public ISymbol getSymbolByValue(Object key) {
114
		return null;
115
	}
116

  
117
	public ISymbol getSymbolByFeature(Feature feat) {
118

  
119
		ISymbol theSymbol = getDefaultSymbol();
120
		ISymbol auxSymbol;
121

  
122
		double value = 0;
123
		double normValue = 0;
124
		double size;
125
		double separation = maxSize-minSize;
126

  
127
		if (separation == 0) {
128
			separation = 1;
129
		}
130

  
131
		try {
132

  
133
			value = feat.getDouble(this.valueField);
134
			if (useNormalization) {
135

  
136
			    normValue = feat.getDouble(this.normalizationField);
137
			    if (normValue == 0) {
138
			        size = (value >= 0) ?
139
			            Double.POSITIVE_INFINITY :
140
			            Double.NEGATIVE_INFINITY;
141
			    } else {
142
	                value = value / normValue;
143
	                size = minSize + (value * separation) ;
144
			    }
145

  
146
			} else {
147
				double difFeat = maxFeature - minFeature;
148
				double step = difFeat/separation;
149
				size = minSize + ((value - minFeature)/step);
150
			}
151

  
152
		} catch (Exception e) {
153
			logger.info("Error while getting value in ProportionalSymbolsLegend.", e);
154
			return null;
155
		}
156

  
157
		if (size == Double.NaN || size == Double.POSITIVE_INFINITY
158
		    || size == Double.NEGATIVE_INFINITY) {
159

  
160
            // logger.info("The symbol size is NaN or INFINITE.");
161
			return null;
162
		}
163

  
164
		Geometry defgeom = feat.getDefaultGeometry();
165
		int def_geom_type = defgeom.getGeometryType().getType();
166

  
167
		if (isPolygonal(def_geom_type) && theSymbol instanceof IMarkerSymbol) {
168

  
169
			MarkerFillSymbol aux = new MarkerFillSymbol();
170
			((IMarkerSymbol) theSymbol).setSize(size);
171
			aux.setMarker((IMarkerSymbol) theSymbol);
172
			SimpleMarkerFillPropertiesStyle p = new SimpleMarkerFillPropertiesStyle();
173
			p.setFillStyle(SimpleMarkerFillPropertiesStyle.SINGLE_CENTERED_SYMBOL);
174
			aux.setMarkerFillProperties(p);
175
			theSymbol = aux;
176
		}
177
		else if (isLinear(def_geom_type)) {
178

  
179
			ILineSymbol line = (ILineSymbol) theSymbol;
180
			line.setLineWidth(size);
181
			theSymbol = line;
182
		}
183
		else if (isPoint(def_geom_type)) {
184
			IMarkerSymbol marker = (IMarkerSymbol) theSymbol;
185
			marker.setSize(size);
186
			theSymbol = marker;
187
		}
188

  
189
		if (backgroundSymbol != null) {
190
			MultiLayerFillSymbol multi = new MultiLayerFillSymbol() ;
191
			multi.addLayer(backgroundSymbol);
192
			multi.addLayer(theSymbol);
193
			return multi;
194
		}
195
		auxSymbol = theSymbol;
196

  
197
		return auxSymbol;
198

  
199
	}
200

  
201
    public static boolean isPolygonal(int ty) {
202
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
203
        return geomManager.isSubtype(Geometry.TYPES.MULTISURFACE, ty) || 
204
            geomManager.isSubtype(Geometry.TYPES.SURFACE, ty);
205
    }
206

  
207
    public static boolean isLinear(int ty) {
208
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
209
        return geomManager.isSubtype(Geometry.TYPES.MULTICURVE, ty) || 
210
            geomManager.isSubtype(Geometry.TYPES.CURVE, ty);
211
    }
212
    
213
    public static boolean isPoint(int ty) {
214
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
215
        return geomManager.isSubtype(Geometry.TYPES.MULTIPOINT, ty) || 
216
            geomManager.isSubtype(Geometry.TYPES.POINT, ty);
217
    }
218

  
219
	/**
220
	 * Gets the background symbol which only can appear when the shapetype of the layer
221
	 * is polygonal
222
	 *
223
	 * @return ISymbol the symbol for the background
224
	 */
225
	public ISymbol getBackgroundSymbol() {return backgroundSymbol;}
226
	/**
227
	 * Sets the background symbol which is used only when the shapetype of the layer is polygonal
228
	 *
229
	 * @param backgroundSymbol the symbol for the background
230
	 */
231
	public void setBackgroundSymbol(ISymbol backgroundSymbol) {this.backgroundSymbol = backgroundSymbol;}
232
	/**
233
	 * Obtains the classifying field name to be used to calculate the size of the symbol
234
	 *
235
	 * @return String  the name of the field
236
	 * @throws ReadDriverException
237
	 */
238
	public String getValueField() {
239
//		try {
240
//			// TODO:
241
//			// Por los alias, al guardar un proyecto no podemos
242
//			// permitir que se guarde con campos que luego
243
//			// no van a existir.
244
////			int id = dataSource.getFieldIndexByName(valueField);
245
////			valueField = dataSource.getFieldName(id);
246
//		} catch (ReadDriverException e) {
247
//			e.printStackTrace();
248
//			throw new RuntimeException(e);
249
//		}
250
		return valueField;
251
	}
252
	/**
253
	 * Sets the classifying field name to be used to calculate the size of the symbol
254
	 *
255
	 * @param String  the name of the field
256
	 */
257
	public void setValueField(String valueField) {this.valueField = valueField;}
258
	/**
259
	 * Obtains the classifying field name to be used to calculate the size of the symbol when the
260
	 * user is doing it with a normalization value.
261
	 *
262
	 * @return String  the name of the field
263
	 */
264
	public String getNormalizationField() {
265
		return normalizationField;
266
	}
267
	/**
268
	 * Sets the classifying field name to be used to calculate the size of the symbol when the
269
	 * user is doing it with a normalization value.
270
	 *
271
	 * @param String  the name of the field
272
	 */
273
	public void setNormalizationField(String normalizationField) {this.normalizationField = normalizationField;}
274
	/**
275
	 * Obtains the minimum size for the symbol
276
	 *
277
	 * @return double  the minimum size for the symbol
278
	 */
279
	public double getMinSize() {return minSize;}
280
	/**
281
	 * Sets the minimum size for the symbol
282
	 *
283
	 * @param minSize  the minimum size for the symbol
284
	 */
285
	public void setMinSize(double minSize) {this.minSize = minSize;}
286
	/**
287
	 * Obtains the maximum size for the symbol
288
	 *
289
	 * @return double  the minimum size for the symbol
290
	 */
291
	public double getMaxSize() {return maxSize;}
292
	/**
293
	 * Sets the maximum size for the symbol
294
	 *
295
	 * @param maxSize  the minimum size for the symbol
296
	 */
297
	public void setMaxSize(double maxSize) {this.maxSize = maxSize;}
298
	/**
299
	 * Obtains the shapetype of the template symbol
300
	 *
301
	 * @return int shape type for the template symbol
302
	 */
303
	public int getTemplateShapeType() {return templateShapeType;}
304
	/**
305
	 * Sets the shapetype of the template symbol
306
	 *
307
	 * @param templateShapeType shape type for the template symbol
308
	 */
309
	public void setTemplateShapeType(int tst) {
310

  
311
		if (isPolygonal(getShapeType())) {
312
			if (isPoint(tst) || isPolygonal(tst)) {
313
			    this.templateShapeType = tst;
314
			}
315
		} else {
316

  
317
		    if ((isPoint(tst) && isPoint(getShapeType()))
318
		        || (isLinear(tst) && isLinear(getShapeType()))) {
319
		        // || (getShapeType() == Geometry.TYPES.NULL)) {
320

  
321
		        this.templateShapeType = tst;
322
		    }
323
		}
324
	}
325

  
326
	/**
327
	 * Obtains the boolean which is true if the user wants to calculate the size of the
328
	 * symbol using a normalization field.
329
	 *
330
	 * @return boolean true if the user wants normalization.Otherwise, false.
331
	 */
332
	public boolean getUseNormalization() {return useNormalization;}
333
	/**
334
	 * Sets the boolean which is true if the user wants to calculate the size of the
335
	 * symbol using a normalization field.
336
	 *
337
	 * @param useNormalization true if the user wants normalization.Otherwise, false.
338
	 */
339
	public void setUseNormalization(boolean useNormalization) {this.useNormalization = useNormalization;}
340
	/**
341
	 * Obtains the variable which represents the maximum value of the classifying field that is used
342
	 * to calculate the size of the symbol
343
	 *
344
	 * @return double  the maximum value of the classifying field
345
	 */
346
	public double getMaxFeature() {return maxFeature;}
347
	/**
348
	 * Sets the variable which represents the maximum value of the classifying field that is used
349
	 * to calculate the size of the symbol
350
	 *
351
	 * @param maxFeature
352
	 */
353
	public void setMaxFeature(double maxFeature) {this.maxFeature = maxFeature;}
354
	/**
355
	 * Obtains the variable which represents the minimum value of the classifying field that is used
356
	 * to calculate the size of the symbol
357
	 *
358
	 * @return double  the minimum value of the classifying field
359
	 */
360
	public double getMinFeature() {return minFeature;}
361
	/**
362
	 * Sets the variable which represents the minimum value of the classifying field that is used
363
	 * to calculate the size of the symbol
364
	 *
365
	 * @param minFeature
366
	 */
367
	public void setMinFeature(double minFeature) {this.minFeature = minFeature;}
368

  
369

  
370
	public ISymbol[] getSymbols() {
371
		ISymbol[] auxSymbols=super.getSymbols();
372
		if(backgroundSymbol != null){
373
			ISymbol[] symbols=new ISymbol[auxSymbols.length+1];
374
			for (int i = 0; i < auxSymbols.length; i++) {
375
				symbols[i]=auxSymbols[i];
376
			}
377
			symbols[symbols.length-1]=backgroundSymbol;
378
			return symbols;
379
		} else {
380
			return auxSymbols;
381
		}
382
	}
383

  
384

  
385

  
386
	public ZSort getZSort() {
387
		if (zSort == null){
388
			zSort = new MyZSort(this);
389
		}
390
		return zSort;
391
	}
392

  
393
	public void setZSort(ZSort zSort){
394
		return;
395
	}
396

  
397
	private class MyZSort extends ZSort {
398

  
399
		public MyZSort(ILegend legend) {
400
			super(legend);
401
		}
402

  
403
		public void legendChanged(LegendChangedEvent e) {
404
		}
405

  
406
		public String getClassName() {
407
			return getClass().getName();
408
		}
409

  
410
		public int getLevelCount() {
411
			int levels = 0;
412
			if (backgroundSymbol!=null){
413
				if(backgroundSymbol instanceof IMultiLayerSymbol){
414
					levels += ((IMultiLayerSymbol)backgroundSymbol).getLayerCount();
415
				} else {
416
					levels += 1;
417
				}
418
			}
419
			ISymbol sym = getDefaultSymbol();
420
			if(getDefaultSymbol() instanceof IMultiLayerSymbol){
421
				levels += ((IMultiLayerSymbol)sym).getLayerCount();
422
			} else {
423
				levels += 1;
424
			}
425
			return levels+1;
426
		}
427

  
428

  
429
		public void setUsingZSort(boolean usingZSort) {
430
		}
431

  
432
		public void setLevels(ISymbol sym, int[] values) {
433
		}
434

  
435

  
436
		public void setLevels(int row, int[] values) {
437
		}
438

  
439
		public int[] getLevels(ISymbol sym) {
440
			return getLevels(0);
441
		}
442

  
443
		public int[] getLevels(int row) {
444
			int levelsCount = getLevelCount();
445
			int[] levels = new int[levelsCount];
446
			int bgLevels = 1;
447
			if (backgroundSymbol!=null){
448
				if(backgroundSymbol instanceof IMultiLayerSymbol){
449
					bgLevels = ((IMultiLayerSymbol)backgroundSymbol).getLayerCount();
450
				}
451
				for (int i=0; i<bgLevels; i++) {
452
					levels[i]=i;
453
				}
454
			}
455

  
456
			ISymbol sym = getDefaultSymbol();
457
			int frLevels = 1;
458
			if(getDefaultSymbol() instanceof IMultiLayerSymbol){
459
				frLevels = ((IMultiLayerSymbol)sym).getLayerCount();
460
			}
461
			for (int i=0; i<frLevels; i++) {
462
				levels[i+bgLevels]=i+bgLevels;
463
			}
464
			levels[frLevels+bgLevels]=frLevels+bgLevels;
465
			return levels;
466
		}
467

  
468

  
469
		public boolean isUsingZSort() {
470
			return backgroundSymbol!=null;
471
		}
472

  
473

  
474
		public ISymbol[] getSymbols() {
475
			return getSymbols();
476
		}
477

  
478
		public String[] getDescriptions() {
479
			return getDescriptions();
480
		}
481

  
482
		public int getTopLevelIndexAllowed() {
483
			return getLevelCount();
484
		}
485

  
486
		@Override
487
		public String toString() {
488
			String out = "ZSort for ProportionalSymbolLegend";
489
			return out;
490
		}
491

  
492
		public boolean symbolChanged(SymbolLegendEvent e) {
493
			return true;
494
		}
495

  
496
		public boolean classifiedSymbolChange(SymbolLegendEvent e) {
497
			return true;
498
		}
499

  
500
		public boolean intervalChange(IntervalLegendEvent e) {
501
			return false;
502
		}
503

  
504
		public boolean valueChange(ValueLegendEvent e) {
505
			return false;
506
		}
507

  
508
		// TODO should not exist here
509
		public boolean labelFieldChange(LabelLegendEvent e) {
510
			return false;
511
		}
512

  
513
		public void legendCleared(LegendClearEvent event) {
514
		}
515
	}
516

  
517

  
518
    public void loadFromState(PersistentState state)
519
        throws PersistenceException {
520

  
521
        int[] ft = state.getIntArray("fieldTypes");
522
        this.setClassifyingFieldTypes(ft);
523

  
524
        this.valueField = state.getString("valueField");
525
        this.normalizationField = state.getString("normalizationField");
526
        this.templateShapeType = state.getInt("templateShapeType");
527
        this.maxSize = state.getDouble("maxSize");
528
        this.minSize = state.getDouble("minSize");
529
        this.maxFeature = state.getDouble("maxFeature");
530
        this.minFeature = state.getDouble("minFeature");
531
        this.useNormalization = state.getBoolean("useNormalization");
532

  
533
        ISymbol sym = (ISymbol) state.get("defaultSymbol");
534
        this.setDefaultSymbol(sym);
535

  
536
        sym = (ISymbol) state.get("backgroundSymbol");
537
        this.setBackgroundSymbol(sym);
538

  
539
        String[] fieldNames = new String[2];
540
        fieldNames[0]= valueField;
541
        if (normalizationField.compareTo(Messages.getText("none")) == 0) {
542
            fieldNames[1]= fieldNames[0];
543
        } else {
544
            fieldNames[1] = normalizationField;
545
        }
546
        setClassifyingFieldNames(fieldNames);
547
    }
548

  
549

  
550
    public void saveToState(PersistentState state) throws PersistenceException {
551

  
552
        int[] ft = this.getClassifyingFieldTypes();
553
        state.set("fieldTypes", ft);
554

  
555
        state.set("valueField", this.valueField);
556
        state.set("normalizationField", this.normalizationField);
557
        state.set("templateShapeType", this.templateShapeType);
558
        state.set("maxSize", this.maxSize);
559
        state.set("minSize", this.minSize);
560
        state.set("maxFeature", this.maxFeature);
561
        state.set("minFeature", this.minFeature);
562
        state.set("useNormalization", this.useNormalization);
563
        state.set("defaultSymbol", this.getDefaultSymbol());
564
        state.set("backgroundSymbol", this.backgroundSymbol);
565
    }
566

  
567

  
568
    public static class RegisterPersistence implements Callable {
569

  
570
        public Object call() throws Exception {
571

  
572
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
573
            if (manager
574
                .getDefinition(PROPORTIONAL_SYMBOL_LEGEND_PERSISTENCE_DEFINITION_NAME) == null) {
575
                DynStruct definition =
576
                    manager
577
                        .addDefinition(
578
                            ProportionalSymbolsLegend.class,
579
                            PROPORTIONAL_SYMBOL_LEGEND_PERSISTENCE_DEFINITION_NAME,
580
                            PROPORTIONAL_SYMBOL_LEGEND_PERSISTENCE_DEFINITION_NAME
581
                                + " Persistence definition", null, null);
582

  
583
                definition.addDynFieldString("valueField").setMandatory(true);
584
                definition.addDynFieldString("normalizationField").setMandatory(true);
585
                definition.addDynFieldInt("templateShapeType").setMandatory(true);
586
                definition.addDynFieldArray("fieldTypes").setClassOfItems(Integer.class).setMandatory(true);
587
                definition.addDynFieldDouble("maxSize").setMandatory(true);
588
                definition.addDynFieldDouble("minSize").setMandatory(true);
589
                definition.addDynFieldDouble("maxFeature").setMandatory(true);
590
                definition.addDynFieldDouble("minFeature").setMandatory(true);
591
                definition.addDynFieldBoolean("useNormalization").setMandatory(true);
592

  
593
                definition.addDynFieldObject("defaultSymbol")
594
                .setClassOfValue(ISymbol.class).setMandatory(true);
595
                definition.addDynFieldObject("backgroundSymbol")
596
                .setClassOfValue(ISymbol.class).setMandatory(true);
597
            }
598
            return Boolean.TRUE;
599
        }
600

  
601
    }
602

  
603
    public static class RegisterLegend implements Callable {
604

  
605
        public Object call() throws Exception {
606
            MapContextManager manager =
607
                MapContextLocator.getMapContextManager();
608

  
609
            manager.registerLegend(
610
                PROPORTIONAL_SYMBOL_LEGEND_NAME,
611
                ProportionalSymbolsLegend.class);
612

  
613
            return Boolean.TRUE;
614
        }
615

  
616
    }
617

  
618
}
org.gvsig.legend.proportionalsymbols.app.mainplugin/tags/org.gvsig.legend.proportionalsymbols.app.mainplugin-1.0.71/src/main/java/org/gvsig/symbology/gui/layerproperties/ProportionalSymbolsPanel.java
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 org.gvsig.symbology.gui.layerproperties;
42

  
43
import java.awt.BorderLayout;
44
import java.awt.Color;
45
import java.awt.Dimension;
46
import java.awt.FlowLayout;
47
import java.awt.event.ActionEvent;
48
import java.awt.event.ActionListener;
49

  
50
import javax.swing.BorderFactory;
51
import javax.swing.DefaultComboBoxModel;
52
import javax.swing.ImageIcon;
53
import javax.swing.JComboBox;
54
import javax.swing.JLabel;
55
import javax.swing.JOptionPane;
56
import javax.swing.JPanel;
57

  
58
import org.slf4j.Logger;
59
import org.slf4j.LoggerFactory;
60

  
61
import org.gvsig.andami.IconThemeHelper;
62
import org.gvsig.app.ApplicationLocator;
63
import org.gvsig.app.project.documents.view.legend.gui.ILegendPanel;
64
import org.gvsig.app.project.documents.view.legend.gui.JSymbolPreviewButton;
65
import org.gvsig.app.project.documents.view.legend.gui.Quantities;
66
import org.gvsig.fmap.dal.exception.DataException;
67
import org.gvsig.fmap.dal.exception.ReadException;
68
import org.gvsig.fmap.dal.feature.Feature;
69
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
70
import org.gvsig.fmap.dal.feature.FeatureSet;
71
import org.gvsig.fmap.dal.feature.FeatureStore;
72
import org.gvsig.fmap.dal.feature.FeatureType;
73
import org.gvsig.fmap.geom.Geometry;
74
import org.gvsig.fmap.geom.GeometryLocator;
75
import org.gvsig.fmap.geom.GeometryManager;
76
import org.gvsig.fmap.mapcontext.MapContextLocator;
77
import org.gvsig.fmap.mapcontext.layers.FLayer;
78
import org.gvsig.fmap.mapcontext.layers.operations.ClassifiableVectorial;
79
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
80
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
81
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
82
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
83
import org.gvsig.gui.beans.swing.JBlank;
84
import org.gvsig.gui.beans.swing.JNumberSpinner;
85
import org.gvsig.i18n.Messages;
86
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.MarkerFillSymbol;
87
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
88
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.impl.SimpleLineSymbol;
89
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
90
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.impl.SimpleMarkerSymbol;
91
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.IMarkerFillPropertiesStyle;
92
import org.gvsig.symbology.fmap.rendering.ProportionalSymbolsLegend;
93
import org.gvsig.tools.dispose.DisposableIterator;
94

  
95
/**
96
 * Implements the panel for the legend of proportional symbols.In the interface will be
97
 * options to select the value field, the normalization field (if the user wants to use it) and
98
 * options to select the symbol an its minimum and maximum size.
99
 *
100
 * Also there will be possible to select a background symbol (only when the shapetype of the layer is
101
 * polygonal).
102
 *
103
 * @author jaume dominguez faus - jaume.dominguez@iver.es
104
 * @author jldominguez
105
 */
106
public class ProportionalSymbolsPanel extends JPanel implements ILegendPanel {
107

  
108
    private static Logger logger = LoggerFactory.getLogger(ProportionalSymbolsPanel.class);
109

  
110
	private static final long serialVersionUID = 7394720230276170902L;
111
	private JPanel symbolPanel;
112
	private JPanel backgroundPanel;
113
	private JSymbolPreviewButton tmplateSymbol;
114
	private JSymbolPreviewButton backSymbol;
115
	private JComboBox cmbValue;
116
	private JComboBox cmbNormalization;
117
	private JNumberSpinner txtMinSize;
118
	private JNumberSpinner txtMaxSize;
119
	private ClassifiableVectorial myLayer;
120
	private ProportionalSymbolsLegend auxLegend;
121
	private String[] fieldNames;
122
	private int templateShapeType = 0;
123

  
124
	private String noNormalization = Messages.getText("none");
125

  
126
	private boolean useNormalization = true;
127

  
128
	/**
129
	 * Default constructor
130
	 */
131
	public ProportionalSymbolsPanel() {
132
		initialize();
133
	}
134

  
135
	/**
136
	 * This method initializes this
137
	 */
138
	private void initialize() {
139

  
140
		setLayout(new BorderLayout());
141
		JPanel aux = new JPanel(new BorderLayout());
142

  
143
		JPanel fieldsPanel = new JPanel(new FlowLayout());
144
		fieldsPanel.setBorder(BorderFactory.
145
				createTitledBorder(null,
146
				    Messages.getText("fields")
147
						));
148
		fieldsPanel.setPreferredSize(new Dimension(300,60));
149

  
150
		cmbValue = new JComboBox();
151
		cmbValue.setActionCommand("VALUE_SELECTED");
152

  
153
		cmbNormalization = new JComboBox();
154
		cmbNormalization.setActionCommand("NORMALIZATION_SELECTED");
155

  
156
		fieldsPanel.add(new JLabel(
157
		    Messages.getText("value")
158
		    +":" ));
159
		fieldsPanel.add(cmbValue);
160

  
161
		fieldsPanel.add(new JLabel(
162
		    Messages.getText("normalization")
163
		    +":" ));
164
		fieldsPanel.add(cmbNormalization);
165

  
166

  
167
		symbolPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 3,2));
168
		symbolPanel.setBorder(BorderFactory.
169
				createTitledBorder(null,
170
				    Messages.getText("symbol")
171
						));
172

  
173
		GridBagLayoutPanel aux2 = new GridBagLayoutPanel();
174
		aux2.addComponent(new JLabel(
175
		    Messages.getText("size")
176
		    ));
177
		aux2.addComponent(new JBlank(10,10));
178
		aux2.addComponent(
179
		    Messages.getText("from")
180
		    +":",
181
				getTxtMinSize());
182
		aux2.addComponent(
183
		    Messages.getText("to")
184
		    +":",
185
				getTxtMaxSize());
186

  
187
		JPanel templatePanel = new JPanel();
188
		templatePanel.setBorder(BorderFactory.createTitledBorder(null,
189
		    Messages.getText("template")
190
				));
191
		templatePanel.add(getTemplSymbol());
192
		symbolPanel.add(new JBlank(10,10));
193
		symbolPanel.add(aux2);
194
		symbolPanel.add(new JBlank(10,10));
195
		symbolPanel.add(templatePanel);
196

  
197
		aux.add(fieldsPanel, BorderLayout.NORTH);
198
		aux.add(symbolPanel, BorderLayout.CENTER);
199

  
200
		this.add(aux, BorderLayout.CENTER);
201

  
202
		cmbValue.addActionListener(action);
203
		cmbNormalization.addActionListener(action);
204
		tmplateSymbol.addActionListener(action);
205
	}
206
	/**
207
	 * Creates the panel where the button for the background symbol will be placed
208
	 *
209
	 * @return JPanel panel
210
	 */
211

  
212
	private JPanel getBackgroundPanel() {
213
		backgroundPanel = new JPanel();
214
		backgroundPanel.setBorder(BorderFactory.createTitledBorder(null,
215
		    Messages.getText("background")
216
				));
217
		backgroundPanel.add(getBtnBackground());
218
		return backgroundPanel;
219
	}
220
	/**
221
	 * Creates a JNumberSpinner which is used to select the maximum size for the symbol
222
	 *
223
	 * @return JNumberSpinner
224
	 */
225
	private JNumberSpinner getTxtMaxSize() {
226
		if (txtMaxSize == null) {
227
			txtMaxSize = new JNumberSpinner(25.0,7,0.0,100.0,1.0,2);
228
			txtMaxSize.addActionListener(action);
229
		}
230
		return txtMaxSize;
231
	}
232
	/**
233
	 * Creates a JNumberSpinner which is used to select the minimum size for the symbol
234
	 *
235
	 * @return JNumberSpinner
236
	 */
237
	private JNumberSpinner getTxtMinSize() {
238
		if (txtMinSize == null) {
239
			txtMinSize = new JNumberSpinner(3.0,7,0.0,100.0,1.0,2);
240
			txtMinSize.addActionListener(action);
241
		}
242
		return txtMinSize;
243
	}
244
	/**
245
	 * Creates a JSymbolPreviewButton which is used to select the template symbol
246
	 *
247
	 * @return JSymbolPreviewButton
248
	 */
249
	private JSymbolPreviewButton getTemplSymbol() {
250

  
251
		if (tmplateSymbol == null) {
252
			int tst = ProportionalSymbolsLegend.isPolygonal(this.templateShapeType) ?
253
			    Geometry.TYPES.POINT
254
			    : this.templateShapeType;
255

  
256
			tmplateSymbol = new JSymbolPreviewButton(tst);
257
		}
258
		tmplateSymbol.setPreferredSize(new Dimension(100, 45));
259
		return tmplateSymbol;
260
	}
261
	/**
262
	 * Creates a JSymbolPreviewButton which is used to select the background symbol
263
	 *
264
	 * @return JSymbolPreviewButton
265
	 */
266
	private JSymbolPreviewButton getBtnBackground() {
267
		if (backSymbol == null) {
268
			backSymbol = new JSymbolPreviewButton(Geometry.TYPES.SURFACE);
269
			backSymbol.setPreferredSize(new Dimension(100, 45));
270

  
271
            ISymbol fillsym = MapContextLocator.getSymbolManager().createSymbol(
272
                Geometry.TYPES.SURFACE, Color.ORANGE);
273
            backSymbol.setSymbol(fillsym);
274
		}
275
		return backSymbol;
276
	}
277
	/**
278
	 * Creates a new symbol of an specific shapetype with a concrete size
279
	 *
280
	 * @param shapeType
281
	 * @param size
282
	 *
283
	 * @return ISymbol symbol created
284
	 */
285
	private ISymbol newSymbol(int shapeType, double size) {
286
		if (getTemplSymbol().getSymbol() == null) {
287
			ISymbol templateSymbol;
288
            GeometryManager geomManager = GeometryLocator.getGeometryManager();
289
            if( geomManager.isSubtype(Geometry.TYPES.POINT, shapeType) ||
290
                geomManager.isSubtype(Geometry.TYPES.MULTIPOINT, shapeType) ||
291
                geomManager.isSubtype(Geometry.TYPES.SURFACE, shapeType) ||
292
                geomManager.isSubtype(Geometry.TYPES.MULTISURFACE, shapeType) ) {
293
				templateSymbol = new SimpleMarkerSymbol();
294
				((SimpleMarkerSymbol) templateSymbol).setSize(size);
295
				((SimpleMarkerSymbol) templateSymbol).setColor(Color.DARK_GRAY);
296

  
297
            } else if( geomManager.isSubtype(Geometry.TYPES.CURVE, shapeType) ||
298
                geomManager.isSubtype(Geometry.TYPES.MULTICURVE, shapeType) ) {
299
				templateSymbol = new SimpleLineSymbol();
300
				((SimpleLineSymbol) templateSymbol).setLineWidth(size);
301
				((SimpleLineSymbol) templateSymbol).setLineColor(Color.DARK_GRAY);
302

  
303
            } else {
304
				throw new RuntimeException("Unknown symbol type");
305

  
306
            }
307

  
308
			getTemplSymbol().setSymbol(templateSymbol);
309
			return newSymbol(shapeType, size);
310
		} else {
311
			// clone symbol
312
			ISymbol mySymbol = getTemplSymbol().getSymbol();
313

  
314
			try {
315
                mySymbol = (ISymbol) mySymbol.clone();
316
            } catch (CloneNotSupportedException e) {
317
                logger.info("Error while cloning symbol", e);
318
                ApplicationLocator.getManager().messageDialog(
319
                    Messages.getText("_Unable_to_clone_symbol"),
320
                    Messages.getText("error"),
321
                    JOptionPane.ERROR_MESSAGE);
322
            }
323

  
324
			if (mySymbol instanceof ILineSymbol) {
325
				ILineSymbol lSym = (ILineSymbol) mySymbol;
326
				lSym.setLineWidth(size);
327

  
328
			}
329
			if (mySymbol instanceof IMarkerSymbol) {
330
				IMarkerSymbol mSym = (IMarkerSymbol) mySymbol;
331
				mSym.setSize(size);
332

  
333
				if (ProportionalSymbolsLegend.isPolygonal(shapeType)) {
334
					// this is to allow using in Polygon layers
335
					MarkerFillSymbol fillSymbol = new MarkerFillSymbol();
336
					fillSymbol.setOutline(null);
337
					fillSymbol.setFillColor(null);
338
					fillSymbol.getMarkerFillProperties().
339
					setFillStyle(IMarkerFillPropertiesStyle.SINGLE_CENTERED_SYMBOL);
340
					fillSymbol.setMarker(mSym);
341
				}
342
			}
343
			return mySymbol;
344
		}
345
	}
346

  
347
	/**
348
	 * Fills the comboboxes that are placed in the panel with the classifying field names of the layer
349
	 * which contain numerical information
350
	 *
351
	 */
352
	private void fillFieldNames() {
353

  
354
		// SelectableDataSource rs;
355

  
356
		try {
357
		    FeatureStore fsto = (FeatureStore) ((FLyrVect) myLayer).getDataStore();
358
		    FeatureType fty = fsto.getDefaultFeatureType();
359

  
360
		    FeatureAttributeDescriptor[] atts = fty.getAttributeDescriptors();
361
			int cont = 0;
362
			for (int i = 0; i < atts.length; i++) {
363
			    if (atts[i].getDataType().isNumeric()) {
364
                    cont++;
365
			    }
366
			}
367
			String[] nomFields = new String[cont];
368

  
369
			cont = 0;
370
            for (int i = 0; i < atts.length; i++) {
371
                if (atts[i].getDataType().isNumeric()) {
372
                    nomFields[cont] = atts[i].getName();
373
                    cont++;
374
                }
375
            }
376
			this.fieldNames = nomFields;
377

  
378

  
379
			DefaultComboBoxModel cMValue = new DefaultComboBoxModel(this.fieldNames);
380
			DefaultComboBoxModel cMNormalization = new DefaultComboBoxModel(this.fieldNames);
381
			cmbValue.setModel(cMValue);
382
			cmbNormalization.setModel(cMNormalization);
383
			cmbNormalization.addItem(noNormalization);
384

  
385
		} catch (DataException e) {
386

  
387
		    logger.info("Error while getting fields", e);
388
		    ApplicationLocator.getManager().messageDialog(
389
		        Messages.getText("_Error_while_getting_fields"),
390
                Messages.getText("error"),
391
		        JOptionPane.ERROR_MESSAGE);
392

  
393
			// NotificationManager.addError(PluginServices.getText(this, "recovering_recordset"), e);
394
		}
395
	}
396

  
397

  
398
	public void setData(FLayer lyr, ILegend legend) {
399

  
400
		this.myLayer = (ClassifiableVectorial) lyr;
401

  
402
		fillFieldNames();
403

  
404
	    int myt = Geometry.TYPES.GEOMETRY;
405

  
406
	    try {
407
	        myt = this.myLayer.getGeometryType().getType();
408
	    } catch (ReadException re) {
409
	        logger.info("Error while getting geometry type", re);
410
	        ApplicationLocator.getManager().message(
411
	            "Error getting geometry type",
412
	            JOptionPane.ERROR_MESSAGE);
413
	    }
414

  
415
		templateShapeType =
416
		    ProportionalSymbolsLegend.isPolygonal(myt) ?
417
		    Geometry.TYPES.POINT : myt;
418

  
419
		getTemplSymbol().setShapeType(templateShapeType);
420

  
421
		if (ProportionalSymbolsLegend.isPolygonal(myt) && backgroundPanel == null) {
422
		    symbolPanel.add(getBackgroundPanel());
423
		}
424

  
425

  
426
		if (ProportionalSymbolsLegend.class.equals(legend.getClass())) {
427

  
428
            auxLegend = (ProportionalSymbolsLegend) legend.cloneLegend();
429
			cmbValue.setSelectedItem(auxLegend.getClassifyingFieldNames()[0]);
430
			ISymbol defSymbol = auxLegend.getDefaultSymbol();
431

  
432
			if(((ProportionalSymbolsLegend) auxLegend).getUseNormalization())
433
				cmbNormalization.setSelectedItem(auxLegend.getClassifyingFieldNames()[1]);
434
			else cmbNormalization.setSelectedItem(noNormalization);
435

  
436
			txtMaxSize.setDouble(auxLegend.getMaxSize());
437
			txtMinSize.setDouble(auxLegend.getMinSize());
438

  
439
			if (ProportionalSymbolsLegend.isLinear(templateShapeType)) {
440
			    ((ILineSymbol)defSymbol).setLineWidth(2);
441
			} else {
442
                ((IMarkerSymbol)defSymbol).setSize(15);
443
			}
444

  
445
			getTemplSymbol().setSymbol(defSymbol);
446

  
447
			if(((ProportionalSymbolsLegend) auxLegend).getBackgroundSymbol() != null)
448
				getBtnBackground().setSymbol(((ProportionalSymbolsLegend) auxLegend).getBackgroundSymbol());
449

  
450
		} else {
451

  
452
			auxLegend = new ProportionalSymbolsLegend();
453
			auxLegend.setTemplateShapeType(templateShapeType);
454

  
455
			if (ProportionalSymbolsLegend.isLinear(templateShapeType)) {
456
	            auxLegend.setDefaultSymbol(newSymbol(templateShapeType, 2));
457
			} else {
458
	            auxLegend.setDefaultSymbol(newSymbol(templateShapeType, 15));
459
			}
460

  
461
			getTemplSymbol().setSymbol(auxLegend.getDefaultSymbol());
462
			if (ProportionalSymbolsLegend.isLinear(templateShapeType)) {
463
                txtMinSize.setDouble(3);
464
                txtMaxSize.setDouble(3);
465
			} else {
466
                txtMinSize.setDouble(10);
467
                txtMaxSize.setDouble(10);
468
			}
469
		}
470

  
471
	}
472

  
473

  
474
	public ILegend getLegend() {
475
		ProportionalSymbolsLegend theLegend = new ProportionalSymbolsLegend();
476

  
477
		String[] fieldNames = new String[2];
478
		fieldNames[0]= cmbValue.getSelectedItem().toString();
479

  
480
		if(!useNormalization)
481
			fieldNames[1]= fieldNames[0];
482
		else fieldNames[1]= cmbNormalization.getSelectedItem().toString();
483

  
484
		auxLegend.setTemplateShapeType(templateShapeType);
485
		auxLegend.setValueField(cmbValue.getSelectedItem().toString());
486
		auxLegend.setNormalizationField(cmbNormalization.getSelectedItem().toString());
487
		auxLegend.setUseNormalization(useNormalization);
488

  
489
		auxLegend.setMinSize(txtMinSize.getDouble());
490
		auxLegend.setMaxSize(txtMaxSize.getDouble());
491

  
492
		ISymbol symbol = getTemplSymbol().getSymbol();
493
		symbol.setDescription(getSymbolDescription());
494
		auxLegend.setDefaultSymbol(symbol);
495

  
496
        auxLegend.clear();
497
		auxLegend.addSymbol("defaultSymbol", symbol);
498

  
499
		if(auxLegend.isPolygonal(this.templateShapeType)){
500
		    auxLegend.setBackgroundSymbol(getBtnBackground().getSymbol());
501
		}
502
		auxLegend.setClassifyingFieldNames(fieldNames);
503

  
504
		// ==================================
505

  
506
		FLyrVect vect = (FLyrVect) myLayer;
507
		FeatureStore fsto = null;
508
		FeatureType fty = null;
509
		FeatureAttributeDescriptor[] atts = null;
510

  
511
		try {
512
		    fsto = (FeatureStore) vect.getDataStore();
513
		    fty = fsto.getDefaultFeatureType();
514

  
515
		    int[] fieldTyp = new int[2];
516
		    fieldTyp[0] = fty.getAttributeDescriptor(fieldNames[0]).getType();
517
            fieldTyp[1] = fty.getAttributeDescriptor(fieldNames[1]).getType();
518
            auxLegend.setClassifyingFieldTypes(fieldTyp);
519

  
520
		    FeatureSet fset = fsto.getFeatureSet();
521
		    DisposableIterator diter = fset.fastIterator();
522
		    Feature fitem = null;
523
		    double[] maxs = new double[fieldNames.length];
524
            double[] mins = new double[fieldNames.length];
525
            for (int i=0; i<fieldNames.length; i++) {
526
                // init to worst values
527
                maxs[i] = -Double.MAX_VALUE;
528
                mins[i] = Double.MAX_VALUE;
529
            }
530
            double aux_dob = 0;
531

  
532
		    while (diter.hasNext()) {
533
		        fitem = (Feature) diter.next();
534
		        for (int i=0; i<fieldNames.length; i++) {
535
		            aux_dob = fitem.getDouble(fieldNames[i]);
536
		            if (aux_dob > maxs[i]) {
537
		                maxs[i] = aux_dob;
538
		            }
539
                    if (aux_dob < mins[i]) {
540
                        mins[i] = aux_dob;
541
                    }
542
		        }
543
		    }
544
            auxLegend.setMinFeature(mins[0]);
545
            auxLegend.setMaxFeature(maxs[0]);
546

  
547
		} catch (DataException e) {
548
		    logger.info("Error while creating ProportionalSymbols legend", e);
549
		    ApplicationLocator.getManager().message(
550
		        Messages.getText("_Error_while_creating_legend"),
551
		        JOptionPane.ERROR_MESSAGE);
552
		}
553

  
554
        theLegend = (ProportionalSymbolsLegend) auxLegend.cloneLegend();
555
        // theLegend.addSymbol("defaultSymbol", symbol);
556

  
557
		return theLegend;
558
	}
559
	/**
560
	 * Creates the String which will be the description of the symbol. If the selected normalization field
561
	 * is not "None", then the String created will have this structure ValueField/NormalizationField. Else,
562
	 * if the user select "None" for the normalization field,the string will show the ValueField only.
563
	 *
564
	 * @return String	description for the symbol
565
	 */
566

  
567
	private String getSymbolDescription() {
568
		String description = "";
569

  
570
		if (cmbValue.getSelectedItem() != null)
571
			description += cmbValue.getSelectedItem().toString();
572
		if (cmbNormalization.getSelectedItem().toString().compareTo(noNormalization) != 0 )
573
			description += " / "+cmbNormalization.getSelectedItem().toString();
574

  
575
		return description;
576
	}
577

  
578
	public String getDescription() {
579
		return Messages.getText(
580
		    "draw_quantities_using_symbol_size_to_show_exact_values");
581
	}
582

  
583
	public ImageIcon getIcon() {
584
	    return IconThemeHelper.getImageIcon(
585
	        "legend-overview-proportional-symbols");
586
	}
587

  
588
	public Class getParentClass() {
589
		return Quantities.class;
590
	}
591

  
592
	public String getTitle() {
593
	    return Messages.getText("proportional_symbols");
594
	}
595

  
596
	public Class getLegendClass() {
597
		return ProportionalSymbolsLegend.class;
598
	}
599

  
600

  
601
	public boolean isSuitableFor(FLayer layer) {
602
		if (layer instanceof FLyrVect) {
603

  
604
			FLyrVect lyr = (FLyrVect) layer;
605
			try {
606
				if (lyr.getShapeType() == Geometry.TYPES.GEOMETRY)
607
					return false;
608

  
609
				FeatureStore fsto = (FeatureStore) lyr.getDataStore();
610
				FeatureType fty = fsto.getDefaultFeatureType();
611
				FeatureAttributeDescriptor[] atts = fty.getAttributeDescriptors();
612

  
613
				for (int i = 0; i < atts.length; i++) {
614
					if (atts[i].getDataType().isNumeric()) {
615
						return true;
616
					}
617
				}
618
			} catch (DataException e) {
619
			    logger.info("Error in isSuitableFor", e);
620
			    ApplicationLocator.getManager().message(
621
			        Messages.getText("_Error_while_checking_suitability"),
622
			        JOptionPane.ERROR_MESSAGE);
623
				return false;
624
			}
625
		}
626
		return false;
627
	}
628

  
629

  
630
	public JPanel getPanel() {
631
		return this;
632
	}
633

  
634

  
635
	private ActionListener action = new ActionListener() {
636

  
637
		public void actionPerformed(ActionEvent e) {
638

  
639
			if (e.getSource().equals(cmbValue)) {
640
				JComboBox cb = (JComboBox) e.getSource();
641
				auxLegend.setValueField(cb.getSelectedItem().toString());
642
			}
643
			if (e.getSource().equals(cmbNormalization)) {
644
				JComboBox cb = (JComboBox) e.getSource();
645
				if(cb.getSelectedItem().toString().compareTo(noNormalization) == 0) {
646
					useNormalization = false;
647
					auxLegend.setNormalizationField(cmbValue.getSelectedItem().toString());
648
				}
649
				else {
650
					useNormalization = true;
651
					auxLegend.setNormalizationField(cb.getSelectedItem().toString());
652
				}
653
				auxLegend.setUseNormalization(useNormalization);
654
			}
655
			if (e.getSource().equals(txtMinSize)) {
656
//				ISymbol sym = tmplateSymbol.getSymbol();
657
//				if(sym != null) {
658
//					if(sym instanceof ILineSymbol) {
659
//						ILineSymbol line = (ILineSymbol)sym;
660
//						line.setLineWidth(txtMinSize.getDouble());
661
//						tmplateSymbol.setSymbol(line);
662
//					}
663
//
664
//					if(sym instanceof IMarkerSymbol) {
665
//						IMarkerSymbol point = (IMarkerSymbol)sym;
666
//						point.setSize(txtMinSize.getDouble());
667
//						tmplateSymbol.setSymbol(point);
668
//					}
669
//					tmplateSymbol.repaint();
670
//				}
671

  
672
				if(txtMaxSize.getDouble() < txtMinSize.getDouble())
673
					txtMaxSize.setDouble(txtMinSize.getDouble());
674
			}
675
			if(e.getSource().equals(txtMaxSize)) {
676
				if(txtMaxSize.getDouble() < txtMinSize.getDouble())
677
					txtMinSize.setDouble(txtMaxSize.getDouble());
678
			}
679
//			if(e.getSource().equals(tmplateSymbol)) {
680
//				ISymbol sym = tmplateSymbol.getSymbol();
681
//				if(sym != null) {
682
//					if(sym instanceof ILineSymbol) {
683
//						ILineSymbol line = (ILineSymbol)sym;
684
//						txtMinSize.setDouble(((int)line.getLineWidth()));
685
//						txtMaxSize.setDouble(((int)line.getLineWidth()));
686
//					}
687
//
688
//					if(sym instanceof IMarkerSymbol) {
689
//						IMarkerSymbol point = (IMarkerSymbol)sym;
690
//						txtMinSize.setDouble(((int)point.getSize()));
691
//						txtMaxSize.setDouble(((int)point.getSize()));
692
//
693
//					}
694
//				}
695
//			}
696
		}
697
	};
698

  
699
}
org.gvsig.legend.proportionalsymbols.app.mainplugin/tags/org.gvsig.legend.proportionalsymbols.app.mainplugin-1.0.71/src/main/java/org/gvsig/symbology/library/ProportionalSymbolsLibrary.java
1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2013 gvSIG Association
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., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.symbology.library;
24

  
25
import org.gvsig.andami.IconThemeHelper;
26
import org.gvsig.app.project.documents.view.legend.gui.LegendManager;
27
import org.gvsig.symbology.SymbologyLibrary;
28
import org.gvsig.symbology.fmap.rendering.ProportionalSymbolsLegend;
29
import org.gvsig.symbology.gui.layerproperties.ProportionalSymbolsPanel;
30
import org.gvsig.tools.library.AbstractLibrary;
31
import org.gvsig.tools.library.LibraryException;
32
import org.gvsig.tools.persistence.xml.XMLPersistenceLibrary;
33
import org.gvsig.tools.util.Caller;
34
import org.gvsig.tools.util.impl.DefaultCaller;
35

  
36

  
37
/**
38
 * Simple library to register proportional symbols legend
39
 * 
40
 * @author jldominguez
41
 *
42
 */
43
public class ProportionalSymbolsLibrary extends AbstractLibrary {
44

  
45

  
46
    public void doRegistration() {
47
        registerAsImplementationOf(ProportionalSymbolsLibrary.class);
48
        /*
49
         * This checks map context library registration
50
         */
51
        require(SymbologyLibrary.class);
52
        /*
53
         * This is because we need to use persistence manager
54
         */
55
        require(XMLPersistenceLibrary.class);
56
    }
57

  
58
    protected void doInitialize() throws LibraryException {
59
        
60
    }
61

  
62

  
63
    protected void doPostInitialize() throws LibraryException {
64
        
65
        IconThemeHelper.registerIcon(
66
            "legend", "legend-overview-proportional-symbols", this);
67

  
68
        Caller caller = new DefaultCaller();
69
        
70
        caller.add(new ProportionalSymbolsLegend.RegisterLegend());
71
        caller.add(new ProportionalSymbolsLegend.RegisterPersistence());
72
        
73
        /*
74
         * Do register of all
75
         */
76
        if( !caller.call() ) {
77
            throw new LibraryException(
78
                ProportionalSymbolsLibrary.class,
79
                caller.getExceptions());
80
        }
81
        
82
        LegendManager.addLegendPage(ProportionalSymbolsPanel.class);
83

  
84

  
85
        
86
    }
87

  
88
}
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff