Revision 2589

View differences:

org.gvsig.legend.quantitybycategory.app.mainplugin/tags/org.gvsig.legend.quantitybycategory.app.mainplugin-1.0.75/pom.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2

  
3
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4

  
5
	<modelVersion>4.0.0</modelVersion>
6
	<artifactId>org.gvsig.legend.quantitybycategory.app.mainplugin</artifactId>
7
	<name>Vector legend: Quantity by category</name>
8
	<version>1.0.75</version>
9
	
10
  <parent>
11
      <groupId>org.gvsig</groupId>
12
      <artifactId>org.gvsig.desktop</artifactId>
13
      <version>2.0.241</version>
14
  </parent>
15
	
16
	<url>https://devel.gvsig.org/redmine/projects/gvsig-base-legends</url>
17
	
18
    <scm>
19
        <connection>scm:svn:https://devel.gvsig.org/svn/gvsig-base-legends/org.gvsig.legend.quantitybycategory.app.mainplugin/tags/org.gvsig.legend.quantitybycategory.app.mainplugin-1.0.75</connection>
20
        <developerConnection>scm:svn:https://devel.gvsig.org/svn/gvsig-base-legends/org.gvsig.legend.quantitybycategory.app.mainplugin/tags/org.gvsig.legend.quantitybycategory.app.mainplugin-1.0.75</developerConnection>
21
        <url>scm:svn:https://devel.gvsig.org/svn/gvsig-base-legends/org.gvsig.legend.quantitybycategory.app.mainplugin/tags/org.gvsig.legend.quantitybycategory.app.mainplugin-1.0.75</url>
22
    </scm>
23
    <repositories>
24
      <repository>
25
        <id>gvsig-public-http-repository</id>
26
        <name>gvSIG maven public HTTP repository</name>
27
        <url>http://devel.gvsig.org/m2repo/j2se</url>
28
        <releases>
29
          <enabled>true</enabled>
30
          <updatePolicy>daily</updatePolicy>
31
          <checksumPolicy>warn</checksumPolicy>
32
        </releases>
33
        <snapshots>
34
          <enabled>true</enabled>
35
          <updatePolicy>daily</updatePolicy>
36
          <checksumPolicy>warn</checksumPolicy>
37
        </snapshots>
38
      </repository>
39
    </repositories>
40

  
41
	<description>Vector legend: Quantity by category. This legend draws quantities for each category.</description>
42
	<build>
43

  
44
        <plugins>
45

  
46
		    <plugin>
47
		      <artifactId>maven-release-plugin</artifactId>
48
		      <version>2.0</version>
49
		      <configuration>
50
		        <tagBase>https://devel.gvsig.org/svn/gvsig-base-legends/org.gvsig.legend.quantitybycategory.app.mainplugin/tags</tagBase>
51
		      </configuration>
52
		    </plugin>			
53
     
54
        </plugins>        
55
	</build>
56
	<dependencies>
57
		<dependency>
58
			<groupId>org.gvsig</groupId>
59
			<artifactId>org.gvsig.app.mainplugin</artifactId>
60
		</dependency>
61
        <dependency>
62
            <groupId>org.gvsig</groupId>
63
            <artifactId>org.gvsig.andami</artifactId>
64
        </dependency>
65
        <dependency>
66
            <groupId>org.gvsig</groupId>
67
            <artifactId>org.gvsig.symbology.lib.api</artifactId>
68
        </dependency>
69
        <dependency>
70
            <groupId>org.gvsig</groupId>
71
            <artifactId>org.gvsig.symbology.lib.impl</artifactId>
72
        </dependency>
73
        <dependency>
74
            <groupId>org.gvsig</groupId>
75
            <artifactId>org.gvsig.symbology.swing.api</artifactId>
76
        </dependency>
77
        <dependency>
78
            <groupId>org.gvsig</groupId>
79
            <artifactId>org.gvsig.fmap.mapcontext.api</artifactId>
80
        </dependency>
81
        <dependency>
82
            <groupId>org.gvsig</groupId>
83
            <artifactId>org.gvsig.utils</artifactId>
84
        </dependency>
85
        <dependency>
86
            <groupId>org.gvsig</groupId>
87
            <artifactId>org.gvsig.fmap.dal.api</artifactId>
88
        </dependency>
89

  
90
        <dependency>
91
            <groupId>org.gvsig</groupId>
92
            <artifactId>org.gvsig.i18n</artifactId>
93
        </dependency>
94
        
95
		<dependency>
96
            <groupId>org.gvsig</groupId>
97
            <artifactId>org.gvsig.fmap.geometry.api</artifactId>
98
        </dependency>
99
        
100
        <dependency>
101
            <groupId>org.gvsig</groupId>
102
            <artifactId>org.gvsig.tools.lib</artifactId>
103
        </dependency>
104
        <dependency>
105
            <groupId>org.gvsig</groupId>
106
            <artifactId>org.gvsig.ui</artifactId>
107
        </dependency>        
108
        <dependency>
109
            <groupId>org.gvsig</groupId>
110
            <artifactId>org.gvsig.projection.api</artifactId>
111
        </dependency>        
112
        <dependency>
113
            <groupId>org.gvsig</groupId>
114
            <artifactId>org.gvsig.metadata.lib.basic.api</artifactId>
115
        </dependency>        
116
        
117
        <dependency>
118
            <groupId>org.gvsig</groupId>
119
            <artifactId>org.gvsig.compat.api</artifactId>
120
        </dependency>
121

  
122
        <dependency>
123
            <groupId>org.gvsig</groupId>
124
            <artifactId>org.gvsig.legend.graduatedsymbols.app.mainplugin</artifactId>
125
            <version>1.0.1</version>
126
        </dependency>
127

  
128
	</dependencies>
129

  
130
	<properties>
131
	    <gvsig.package.info.categories>View,Vector,Symbology</gvsig.package.info.categories>
132
        <gvsig.package.info.official>true</gvsig.package.info.official>
133
        <gvsig.package.info.dependencies>required: org.gvsig.app.mainplugin -ge 2,required: org.gvsig.legend.graduatedsymbols.app.mainplugin -ge 1</gvsig.package.info.dependencies>
134
        <gvsig.package.info.state>testing</gvsig.package.info.state>
135
		<gvsig.package.info.poolURL>https://devel.gvsig.org/download/projects/gvsig-base-legends/pool</gvsig.package.info.poolURL>
136
	</properties>
137

  
138

  
139
</project>
org.gvsig.legend.quantitybycategory.app.mainplugin/tags/org.gvsig.legend.quantitybycategory.app.mainplugin-1.0.75/buildNumber.properties
1
#Sun Mar 03 13:16:06 CET 2019
2
buildNumber=78
org.gvsig.legend.quantitybycategory.app.mainplugin/tags/org.gvsig.legend.quantitybycategory.app.mainplugin-1.0.75/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.symbology.library.QuantityByCategoryLibrary
org.gvsig.legend.quantitybycategory.app.mainplugin/tags/org.gvsig.legend.quantitybycategory.app.mainplugin-1.0.75/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.quantitybycategory.app.mainplugin/tags/org.gvsig.legend.quantitybycategory.app.mainplugin-1.0.75/src/main/java/org/gvsig/symbology/fmap/rendering/QuantityByCategoryLegend.java
1
package org.gvsig.symbology.fmap.rendering;
2

  
3
import java.util.ArrayList;
4
import java.util.List;
5

  
6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8

  
9
import org.gvsig.fmap.dal.feature.Feature;
10
import org.gvsig.fmap.geom.Geometry;
11
import org.gvsig.fmap.geom.GeometryLocator;
12
import org.gvsig.fmap.geom.GeometryManager;
13
import org.gvsig.fmap.mapcontext.MapContextException;
14
import org.gvsig.fmap.mapcontext.MapContextLocator;
15
import org.gvsig.fmap.mapcontext.MapContextManager;
16
import org.gvsig.fmap.mapcontext.rendering.legend.IInterval;
17
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
18
import org.gvsig.fmap.mapcontext.rendering.legend.events.SymbolLegendEvent;
19
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
20
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
21
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.AbstractClassifiedVectorLegend;
22
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.VectorialIntervalLegend;
23
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
24
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
25
import org.gvsig.tools.ToolsLocator;
26
import org.gvsig.tools.dynobject.DynStruct;
27
import org.gvsig.tools.persistence.PersistenceManager;
28
import org.gvsig.tools.persistence.PersistentState;
29
import org.gvsig.tools.persistence.exception.PersistenceException;
30
import org.gvsig.tools.util.Callable;
31

  
32
/**
33
 * Implements a legend where the user can compare two different characteristics
34
 * of a region in the map. These two "fields" will be compared, on one side,
35
 * using a color for the region and , on the other side, using a graduated symbol.
36
 * Both methods will change (the color or the size of the symbol) depending on
37
 * the value of the fields.
38
 *
39
 * @author jaume dominguez faus - jaume.dominguez@iver.es
40
 */
41
public class QuantityByCategoryLegend extends AbstractClassifiedVectorLegend {
42

  
43
    private static final Logger logger =
44
        LoggerFactory.getLogger(QuantityByCategoryLegend.class);
45

  
46
    public static final String
47
    QUANTITY_BY_CATEGORY_LEGEND_PERSISTENCE_DEFINITION_NAME =
48
    "QUANTITY_BY_CATEGORY_LEGEND_PERSISTENCE_DEFINITION_NAME";
49

  
50
    public static final String
51
    QUANTITY_BY_CATEGORY_LEGEND_NAME = "QUANTITY_BY_CATEGORY_LEGEND_NAME";
52

  
53
	private GraduatedSymbolsLegend graduatedSymbol;
54
	private VectorialIntervalLegend colorRamp;
55

  
56
	private ISymbol defaultSymbol = null;
57
	private int shapeType = Geometry.TYPES.SURFACE;
58

  
59
	private boolean isUseDefaultSymbol;
60
	// private DataSource ds;
61

  
62
	public QuantityByCategoryLegend() {
63
		graduatedSymbol = new GraduatedSymbolsLegend();
64
		colorRamp = new VectorialIntervalLegend(getShapeType());
65
		this.setShapeType(Geometry.TYPES.SURFACE);
66

  
67
		ISymbol def = MapContextLocator.getSymbolManager().
68
            createSymbol(getShapeType());
69
		this.setDefaultSymbol(def);
70
	}
71

  
72

  
73
	public void clear() {
74
		colorRamp.clear();
75
		graduatedSymbol.clear();
76
	}
77

  
78
	public String[] getClassifyingFieldNames() {
79
		ArrayList<String> l = new ArrayList<String>();
80
		for (int i = 0; i < graduatedSymbol.getClassifyingFieldNames().length; i++) {
81
			l.add(graduatedSymbol.getClassifyingFieldNames()[i]);
82
		}
83

  
84
		for (int i = 0; i < colorRamp.getClassifyingFieldNames().length; i++) {
85
			l.add(colorRamp.getClassifyingFieldNames()[i]);
86
		}
87
		return l.toArray(new String[l.size()]);
88
	}
89

  
90

  
91
	@Override
92
	public int[] getClassifyingFieldTypes() {
93
        ArrayList<Integer> l = new ArrayList<Integer>();
94
        for (int i = 0; i < graduatedSymbol.getClassifyingFieldTypes().length; i++) {
95
            l.add(graduatedSymbol.getClassifyingFieldTypes()[i]);
96
        }
97

  
98
        for (int i = 0; i < colorRamp.getClassifyingFieldTypes().length; i++) {
99
            l.add(colorRamp.getClassifyingFieldTypes()[i]);
100
        }
101

  
102
        int len = l.size();
103
        int[] resp = new int[len];
104
        for (int i=0; i<len; i++) {
105
            resp[i] = l.get(i).intValue();
106
        }
107
        return resp;
108
	}
109

  
110
	public void setClassifyingFieldTypes(int[] fieldTypes) {
111
		if (fieldTypes.length == 2) {
112
	        colorRamp.setClassifyingFieldTypes(new int[] {fieldTypes[1]});
113
	        graduatedSymbol.setClassifyingFieldTypes(new int[] {fieldTypes[0]});
114
		} else {
115
		    logger.info("Error: Unexpected array size (should be 2)");
116
		}
117
		super.setClassifyingFieldTypes(fieldTypes);
118
	}
119

  
120

  
121
	/**
122
	 * Sets the field names required to build this legend. In this case
123
	 * fieldNames is an array of length 2 where the first element is
124
	 * the field name for the embedded GraduatedSymbolLegend, and the
125
	 * second is the field name for the embedded colorRamp (VectorialIntervalLegend)
126
	 * legend.
127
	 */
128
	public void setClassifyingFieldNames(String[] fieldNames) {
129

  
130
		if (fieldNames.length == 2) {
131
	        colorRamp.setClassifyingFieldNames(new String[] {fieldNames[1]});
132
	        graduatedSymbol.setClassifyingFieldNames(new String[] {fieldNames[0]});
133
		} else {
134
            logger.info("Error: Unexpected array size (should be 2)");
135
		}
136
		super.setClassifyingFieldNames(fieldNames);
137
	}
138

  
139
	public void addSymbol(Object key, ISymbol symbol) {
140
//		System.out.println("adding "+key+"["+symbol+"]");
141

  
142
		if(symbol instanceof IFillSymbol)
143
			colorRamp.addSymbol(key, symbol);
144
		else if(symbol instanceof IMarkerSymbol)
145
			graduatedSymbol.addSymbol(key, symbol);
146

  
147
		fireClassifiedSymbolChangeEvent(
148
		    new SymbolLegendEvent(null, symbol));
149
	}
150

  
151
	public void delSymbol(Object key) {
152
		colorRamp.delSymbol(key);
153
		graduatedSymbol.delSymbol(key);
154
		fireClassifiedSymbolChangeEvent(
155
				new SymbolLegendEvent(
156
						null,
157
						null));
158
	}
159

  
160
	public String[] getDescriptions() {
161
		String[] desc1 = colorRamp.getDescriptions();
162
		String[] desc2 = graduatedSymbol.getDescriptions();
163
        Object[] objects1 = colorRamp.getValues();
164
        Object[] objects2 = graduatedSymbol.getValues();
165

  
166
        List descriptionsList = new ArrayList<String>();
167
        for (int i = 0; i < objects1.length; i++) {
168
            //Pretend?a saltar el valor por defecto pero no es necesario si en getValues se pone como clave null
169
//            if(objects1[i] instanceof IInterval){
170
                descriptionsList.add(desc1[i]);
171
//            }
172
        }
173
        for (int i = 0; i < objects2.length; i++) {
174
            //Pretend?a saltar el valor por defecto pero no es necesario si en getValues se pone como clave null
175
//            if(objects2[i] instanceof IInterval){
176
                descriptionsList.add(desc2[i]);
177
//            }
178
        }
179

  
180
        String[] descriptions = new String[descriptionsList.size()];
181
        descriptions = (String[]) descriptionsList.toArray(descriptions);
182
        return descriptions;
183

  
184
	}
185

  
186
	public ISymbol[] getSymbols() {
187
		ISymbol[] symbols1 = colorRamp.getSymbols();
188
		ISymbol[] symbols2 = graduatedSymbol.getSymbols();
189
	    Object[] objects1 = colorRamp.getValues();
190
	    Object[] objects2 = graduatedSymbol.getValues();
191

  
192

  
193
        List symbolsList = new ArrayList<ISymbol>();
194
        for (int i = 0; i < objects1.length; i++) {
195
            //Pretend?a saltar el valor por defecto pero no es necesario si en getValues se pone como clave null
196
//            if(objects1[i] instanceof IInterval){
197
                symbolsList.add(symbols1[i]);
198
//            }
199
        }
200
        for (int i = 0; i < objects2.length; i++) {
201
            //Pretend?a saltar el valor por defecto pero no es necesario si en getValues se pone como clave null
202
//            if(objects2[i] instanceof IInterval){
203
                symbolsList.add(symbols2[i]);
204
//            }
205
        }
206

  
207
        ISymbol[] symbols = new ISymbol[symbolsList.size()];
208
        symbols = (ISymbol[]) symbolsList.toArray(symbols);
209
        return symbols;
210
	}
211

  
212
	public Object[] getValues() {
213
		Object[] objects1 = colorRamp.getValues();
214
		Object[] objects2 = graduatedSymbol.getValues();
215

  
216
		List objectsList = new ArrayList<IInterval>();
217
		for (int i = 0; i < objects1.length; i++) {
218
            if(objects1[i] instanceof IInterval){
219
                objectsList.add(objects1[i]);
220
            } else {
221
                objectsList.add(null); //Si no es un IInterval es el valor por defecto
222
            }
223
        }
224
        for (int i = 0; i < objects2.length; i++) {
225
            if(objects2[i] instanceof IInterval){
226
                objectsList.add(objects2[i]);
227
            } else {
228
                objectsList.add(null); //Si no es un IInterval es el valor por defecto
229
            }
230
        }
231
        Object[] objects = new IInterval[objectsList.size()];
232
        objects = objectsList.toArray(objects);
233
		return objects;
234

  
235
	}
236

  
237

  
238
	public Object clone() throws CloneNotSupportedException {
239

  
240
	    QuantityByCategoryLegend resp =
241
	        (QuantityByCategoryLegend) super.clone();
242

  
243
        resp.setShapeType(this.getShapeType());
244

  
245
	    resp.useDefaultSymbol(this.isUseDefaultSymbol());
246
	    ISymbol defsym = this.getDefaultSymbol();
247
	    defsym = (ISymbol) defsym.clone();
248
	    resp.setDefaultSymbol(defsym);
249

  
250
	    GraduatedSymbolsLegend gleg = this.getGraduatedSymbolsLegend();
251
	    gleg = (GraduatedSymbolsLegend) gleg.cloneLegend();
252
	    resp.setGraduateSymbolLegend(gleg);
253

  
254
	    VectorialIntervalLegend interleg = this.getColorRampLegend();
255
	    interleg = (VectorialIntervalLegend) interleg.cloneLegend();
256
	    resp.setColorRampLegend(interleg);
257
		return resp;
258
	}
259
	/**
260
	 * Obtains the GraduatedSymbolLegend
261
	 *
262
	 * @return GraduatedSymbolLegend
263
	 */
264
	public GraduatedSymbolsLegend getGraduatedSymbolsLegend() {
265
		return graduatedSymbol;
266
	}
267
	/**
268
	 * Obtains the VectorialIntervalLegend
269
	 *
270
	 * @return VectorialIntervalLegend
271
	 */
272
	public VectorialIntervalLegend getColorRampLegend() {
273
		return colorRamp;
274
	}
275

  
276
	/*
277
	public void setDataSource(DataSource ds) throws FieldNotFoundException, ReadDriverException {
278
		// TODO remove it when FLyrVect.forTestOnlyVariableUseIterators_REMOVE_THIS_FIELD is removed
279
//		if (FLyrVect.forTestOnlyVariableUseIterators_REMOVE_THIS_FIELD) {
280
			this.ds = ds;
281
//		}
282
		graduatedSymbol.setDataSource(ds);
283
		colorRamp.setDataSource(ds);
284
	}
285
	*/
286

  
287
	/*
288
	public ISymbol getSymbol(int i) {
289

  
290
		IMarkerSymbol sym1 = (IMarkerSymbol) graduatedSymbol.getSymbol(i);
291
		ISymbol sym2 =  colorRamp.getSymbol(i);
292

  
293
		IMultiLayerSymbol multiSym = null;
294
		int shpt = this.getShapeType();
295

  
296
		if (isPolygonal(shpt)) {
297
            // symbol from the GraduatedSymbolLegend is a marker, but
298
            // what we need is a fill symbol. Will use a MarkerFillSymbol
299
            // to enable support for Polygons
300
            MarkerFillSymbol aux = new MarkerFillSymbol();
301
            // tell the fill style to draw the IMarkerSymbol
302
            // as a IFillSymbol centering it in the shape polygon
303
            // centroid and applying offset (if any).
304
            aux.setMarker(sym1);
305
            SimpleMarkerFillPropertiesStyle p =
306
                new SimpleMarkerFillPropertiesStyle();
307
            p.setFillStyle(SimpleMarkerFillPropertiesStyle.SINGLE_CENTERED_SYMBOL);
308
            aux.setMarkerFillProperties(p);
309

  
310
            multiSym = MapContextLocator.getSymbolManager().
311
                createMultiLayerSymbol(Geometry.TYPES.SURFACE);
312

  
313
            multiSym.addLayer(sym2);
314
            multiSym.addLayer(aux);
315
            break;
316

  
317
		} else {
318
		    logger.info("Error: unexpected shape type (should be surface): " + shpt);
319
		}
320

  
321
		return multiSym;
322
	}
323
	*/
324

  
325
	public ISymbol getSymbolByFeature(Feature feat) throws MapContextException {
326

  
327
		ISymbol sym1 = null, sym2 = null;
328
		sym1 = graduatedSymbol.getSymbolByFeature(feat);
329
		// Ensure fill symbol
330
		sym1 = GraduatedSymbolsLegend.toFillSymbol(sym1);
331

  
332
		sym2 = colorRamp.getSymbolByFeature(feat);
333

  
334
		IMultiLayerSymbol multiSym = null;
335
		int shpt = this.getShapeType();
336

  
337
		if (isPolygonal(shpt)) {
338

  
339
            multiSym = MapContextLocator.getSymbolManager().
340
                createMultiLayerSymbol(Geometry.TYPES.SURFACE);
341

  
342
            if (sym2 != null) multiSym.addLayer(sym2);
343
            if (sym1 != null) multiSym.addLayer(sym1);
344
		} else {
345
		    logger.info("Error: unexpected shape type (should be surface): " + shpt);
346
		}
347

  
348
		return multiSym;
349
	}
350

  
351
	public void setShapeType(int shpt) {
352

  
353
	    this.shapeType = shpt;
354
	    ISymbol sym = null;
355

  
356
		graduatedSymbol.setShapeType(Geometry.TYPES.POINT);
357
		sym = MapContextLocator.getSymbolManager().createSymbol(
358
		    Geometry.TYPES.POINT);
359
		graduatedSymbol.setDefaultSymbol(sym);
360

  
361
		colorRamp.setShapeType(shpt);
362
		sym = MapContextLocator.getSymbolManager().createSymbol(shpt);
363
		colorRamp.setDefaultSymbol(sym);
364
	}
365

  
366

  
367

  
368
	public boolean isUseDefaultSymbol() {
369
		return isUseDefaultSymbol;
370
	}
371

  
372
	public void useDefaultSymbol(boolean b) {
373
		this.isUseDefaultSymbol = b;
374
	}
375

  
376
	public void setGraduateSymbolLegend(GraduatedSymbolsLegend legend) {
377
		this.graduatedSymbol = legend;
378
	}
379

  
380
	public void setColorRampLegend(VectorialIntervalLegend legend) {
381
		this.colorRamp = legend;
382
	}
383

  
384
	public String getClassName() {
385
		return getClass().getName();
386
	}
387

  
388
	public void replace(ISymbol oldSymbol, ISymbol newSymbol) {
389
		ISymbol[] symbols;
390
		// look first in the graduated symbol legend
391
		symbols = graduatedSymbol.getSymbols();
392

  
393
		for (int i = 0; i < symbols.length; i++) {
394
			if (symbols[i].equals(oldSymbol)) {
395
				graduatedSymbol.replace(oldSymbol, newSymbol);
396
				return;
397
			}
398
		}
399

  
400
		// if the symbol wasn't found yet, proceed with color ramp
401
		symbols = colorRamp.getSymbols();
402

  
403
		for (int i = 0; i < symbols.length; i++) {
404
			if (symbols[i].equals(oldSymbol)) {
405
				colorRamp.replace(oldSymbol, newSymbol);
406
				return;
407
			}
408
		}
409

  
410
	}
411

  
412
    public static boolean isPolygonal(int ty) {
413
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
414
        return geomManager.isSubtype(Geometry.TYPES.MULTISURFACE, ty) || 
415
            geomManager.isSubtype(Geometry.TYPES.SURFACE, ty);
416
    }
417

  
418
    public ISymbol getDefaultSymbol() {
419
        return this.defaultSymbol;
420
    }
421

  
422

  
423
    public int getShapeType() {
424
        return this.shapeType;
425
    }
426

  
427

  
428
    public void setDefaultSymbol(ISymbol s) {
429
        this.defaultSymbol = s;
430
    }
431

  
432

  
433
    // =============================
434

  
435
    public static class RegisterPersistence implements Callable {
436

  
437
        public Object call() throws Exception {
438

  
439
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
440
            if (manager.getDefinition(
441
                QUANTITY_BY_CATEGORY_LEGEND_PERSISTENCE_DEFINITION_NAME) == null) {
442
                DynStruct definition = manager
443
                    .addDefinition(QuantityByCategoryLegend.class,
444
                        QUANTITY_BY_CATEGORY_LEGEND_PERSISTENCE_DEFINITION_NAME,
445
                        QUANTITY_BY_CATEGORY_LEGEND_PERSISTENCE_DEFINITION_NAME
446
                        + " Persistence definition", null, null);
447

  
448
                definition.extend(manager.getDefinition(
449
                    AbstractClassifiedVectorLegend
450
                    .CLASSIFIED_VECTOR_LEGEND_PERSISTENCE_DEFINITION_NAME));
451

  
452
                definition.addDynFieldBoolean("useDefaultSymbol")
453
                .setMandatory(true);
454

  
455
                definition.addDynFieldObject("graduatedLegend")
456
                .setClassOfValue(ILegend.class).setMandatory(true);
457
                definition.addDynFieldObject("rampLegend")
458
                .setClassOfValue(ILegend.class).setMandatory(true);
459
            }
460
            return Boolean.TRUE;
461
        }
462

  
463
    }
464

  
465
    public static class RegisterLegend implements Callable {
466

  
467
        public Object call() throws Exception {
468
            MapContextManager manager =
469
                MapContextLocator.getMapContextManager();
470

  
471
            manager.registerLegend(
472
                QUANTITY_BY_CATEGORY_LEGEND_NAME,
473
                QuantityByCategoryLegend.class);
474

  
475
            return Boolean.TRUE;
476
        }
477

  
478
    }
479

  
480
    public void saveToState(PersistentState state) throws PersistenceException {
481

  
482
        super.saveToState(state);
483
        state.set("useDefaultSymbol", this.isUseDefaultSymbol());
484
        state.set("graduatedLegend", this.getGraduatedSymbolsLegend());
485
        state.set("rampLegend", this.getColorRampLegend());
486
    }
487

  
488
    public void loadFromState(PersistentState state)
489
        throws PersistenceException {
490

  
491
        super.loadFromState(state);
492

  
493
        Boolean b = state.getBoolean("useDefaultSymbol");
494
        this.useDefaultSymbol(b);
495

  
496
        ILegend leg = (ILegend) state.get("graduatedLegend");
497
        if (leg instanceof GraduatedSymbolsLegend) {
498
            this.setGraduateSymbolLegend((GraduatedSymbolsLegend) leg);
499
        } else {
500
            throw new PersistenceException(new ClassCastException(
501
                "Unexpected legend type (" + leg + "). Should be GraduatedSymbolsLegend"));
502
        }
503

  
504
        leg = (ILegend) state.get("rampLegend");
505
        if (leg instanceof VectorialIntervalLegend) {
506
            this.setColorRampLegend((VectorialIntervalLegend) leg);
507
        } else {
508
            throw new PersistenceException(new ClassCastException(
509
                "Unexpected legend type (" + leg + "). Should be VectorialIntervalLegend"));
510
        }
511
    }
512

  
513

  
514

  
515
}
org.gvsig.legend.quantitybycategory.app.mainplugin/tags/org.gvsig.legend.quantitybycategory.app.mainplugin-1.0.75/src/main/java/org/gvsig/symbology/gui/layerproperties/QuantityByCategoryPanel.java
1
package org.gvsig.symbology.gui.layerproperties;
2

  
3
import java.awt.BorderLayout;
4
import java.awt.GridLayout;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
import java.util.ArrayList;
8
import java.util.List;
9

  
10
import javax.swing.BorderFactory;
11
import javax.swing.DefaultComboBoxModel;
12
import javax.swing.ImageIcon;
13
import javax.swing.JButton;
14
import javax.swing.JComboBox;
15
import javax.swing.JComponent;
16
import javax.swing.JOptionPane;
17
import javax.swing.JPanel;
18

  
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21

  
22
import org.gvsig.andami.IconThemeHelper;
23
import org.gvsig.andami.ui.mdiManager.IWindow;
24
import org.gvsig.andami.ui.mdiManager.WindowInfo;
25
import org.gvsig.app.ApplicationLocator;
26
import org.gvsig.app.project.documents.view.legend.gui.ILegendPanel;
27
import org.gvsig.app.project.documents.view.legend.gui.MultipleAttributes;
28
import org.gvsig.app.project.documents.view.legend.gui.SymbolTable;
29
import org.gvsig.app.project.documents.view.legend.gui.VectorialInterval;
30
import org.gvsig.fmap.dal.exception.DataException;
31
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
32
import org.gvsig.fmap.dal.feature.FeatureStore;
33
import org.gvsig.fmap.dal.feature.FeatureType;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.mapcontext.MapContextLocator;
36
import org.gvsig.fmap.mapcontext.layers.FLayer;
37
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
38
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedLegend;
39
import org.gvsig.fmap.mapcontext.rendering.legend.IInterval;
40
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
41
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialIntervalLegend;
42
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
43
import org.gvsig.gui.beans.AcceptCancelPanel;
44
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
45
import org.gvsig.i18n.Messages;
46
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.VectorialIntervalLegend;
47
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
48
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
49
import org.gvsig.symbology.fmap.rendering.GraduatedSymbolsLegend;
50
import org.gvsig.symbology.fmap.rendering.QuantityByCategoryLegend;
51

  
52

  
53
/**
54
 * Implements the panel of a legend where the user can compare two different characteristics
55
 * of a region in the map. These two "fields" will be compared, on one side,
56
 * using a color for the region and , on the other side, using a graduated symbol.
57
 * Both methods will change (the color or the size of the symbol) depending on
58
 * the value of the fields.
59
 *
60
 * @author jaume dominguez faus - jaume.dominguez@iver.es
61
 */
62
public class QuantityByCategoryPanel extends JPanel
63
implements ILegendPanel, ActionListener {
64

  
65
	private static final long serialVersionUID = 5098346573350040756L;
66
	private static final Logger logger =
67
	    LoggerFactory.getLogger(QuantityByCategoryPanel.class);
68

  
69
	private JPanel pnlNorth;
70
	private JPanel pnlSouth;
71
	private JPanel pnlButtons;
72
	private GridBagLayoutPanel pnlFields;
73
	private JPanel pnlColorAndSymbol;
74
	private JComboBox cmbColorField;
75
	private JComboBox cmbGraduatedSymbolField;
76
	private JButton btnColor;
77
	private JButton btnSymbol;
78
//	private JButton btnDelete;
79
//	private JButton btnDeleteAll;
80
	private QuantityByCategoryLegend legend;
81
	private QuantityByCategoryLegend oldLegend;
82
	private FLayer layer;
83
	private SymbolTable symbolTable;
84
	/**
85
	 * Constructor method
86
	 */
87
	public QuantityByCategoryPanel() {
88
		super();
89
		initialize();
90
	}
91
	/**
92
	 * This method initializes this
93
	 */
94
	private void initialize() {
95
		this.setLayout(new BorderLayout());
96
		this.setSize(490, 300);
97
		this.add(getPnlNorth(), BorderLayout.NORTH);
98
		this.add(getPnlSouth(),BorderLayout.CENTER);
99
//		this.add(getPnlButtons(),BorderLayout.SOUTH);
100
	}
101
	/**
102
	 * Obtains the south panel
103
	 *
104
	 * @return JPanel
105
	 */
106
	private JPanel getPnlSouth() {
107
		if (pnlSouth == null) {
108
			pnlSouth = new JPanel();
109
			pnlSouth.setLayout(new BorderLayout());
110
		}
111
		return pnlSouth;
112
	}
113
	/**
114
	 * Obtains the panel where the buttons will be placed
115
	 *
116
	 * @return JPanel
117
	 */
118
//	private JPanel getPnlButtons() {
119
//		if(pnlButtons == null) {
120
//			pnlButtons = new JPanel();
121
//			pnlButtons.add(getButDel());
122
//			pnlButtons.add(getButDelAll());
123
//		}
124
//		return pnlButtons;
125
//	}
126
	/**
127
	 * Obtains the button that is used to delete all the rows of the symbolTable
128
	 *
129
	 * @return JButton
130
	 */
131
//	private JButton getButDelAll() {
132
//		if (btnDeleteAll == null) {
133
//			btnDeleteAll = new JButton();
134
//			btnDeleteAll.addActionListener(this);
135
//			btnDeleteAll.setText(PluginServices.getText(this, "Quitar_todos"));
136
//		}
137
//		return btnDeleteAll;
138
//	}
139
	/**
140
	 * Obtains the button that is used to delete a row of the symbolTable
141
	 *
142
	 * @return JButton
143
	 */
144
//	private JButton getButDel() {
145
//		if (btnDelete == null) {
146
//			btnDelete = new JButton();
147
//			btnDelete.addActionListener(this);
148
//			btnDelete.setText(PluginServices.getText(this, "Quitar"));
149
//		}
150
//
151
//		return btnDelete;
152
//	}
153
	/**
154
	 * Obtains the north panel
155
	 *
156
	 * @return JPanel
157
	 */
158
	private JPanel getPnlNorth() {
159
		if (pnlNorth == null) {
160
			pnlNorth = new JPanel(new GridLayout(1, 2));
161
			pnlNorth.add(getPnlFields());
162
			pnlNorth.add(getPnlColorAndSymbol());
163
		}
164

  
165
		return pnlNorth;
166
	}
167
	/**
168
	 * Obtains the panel where the user has the options to select the variation to be applied
169
	 *
170
	 * @return JPanel
171
	 */
172
	private JPanel getPnlColorAndSymbol() {
173
		if (pnlColorAndSymbol == null) {
174
			pnlColorAndSymbol = new JPanel();
175
			pnlColorAndSymbol.setBorder(
176
					BorderFactory.createTitledBorder(
177
							null, Messages.getText("variation_by")));
178
			pnlColorAndSymbol.add(getBtnColor());
179
			pnlColorAndSymbol.add(getBtnSymbol());
180
		}
181

  
182
		return pnlColorAndSymbol;
183
	}
184
	/**
185
	 * Creates the button which is used to selecte the variation by symbol
186
	 *
187
	 * @return JButton
188
	 */
189
	private JButton getBtnSymbol() {
190
		if (btnSymbol == null) {
191
			btnSymbol = new JButton(Messages.getText("symbol"));
192
			btnSymbol.addActionListener(this);
193
		}
194
		return btnSymbol;
195
	}
196
	/**
197
	 * Creates the button which is used to selecte the variation by color ramp
198
	 *
199
	 * @return JButton
200
	 */
201
	private JButton getBtnColor() {
202
		if (btnColor == null) {
203
			btnColor = new JButton(Messages.getText("color_ramp"));
204
			btnColor.addActionListener(this);
205
		}
206

  
207
		return btnColor;
208
	}
209
	/**
210
	 * Creates the panel where the JComboBoxes to select the fields for the variation will be placed
211
	 *
212
	 * @return GridBagLayoutPanel
213
	 */
214
	private GridBagLayoutPanel getPnlFields() {
215
		if (pnlFields == null) {
216
			pnlFields = new GridBagLayoutPanel();
217
			pnlFields.setBorder(
218
					BorderFactory.createTitledBorder(
219
							null, Messages.getText("value_fields")));
220
			pnlFields.addComponent(
221
			    Messages.getText("color_field"), getCmbColorField());
222
			pnlFields.addComponent(
223
			    Messages.getText("symbol_field") ,getCmbGraduatedField());
224
		}
225

  
226
		return pnlFields;
227
	}
228
	/**
229
	 * Creates a JComboBox where the user will select the field for the symbol variation
230
	 *
231
	 * @return JComboBox
232
	 */
233
	private JComboBox getCmbGraduatedField() {
234
		if (cmbGraduatedSymbolField == null) {
235
			cmbGraduatedSymbolField = new JComboBox();
236
			cmbGraduatedSymbolField.addActionListener(this);
237
		}
238
		return cmbGraduatedSymbolField;
239
	}
240
	/**
241
	 * Creates a JComboBox where the user will select the field for the color ramp variation
242
	 *
243
	 * @return JComboBox
244
	 */
245
	private JComboBox getCmbColorField() {
246
		if (cmbColorField == null) {
247
			cmbColorField = new JComboBox();
248
			cmbColorField.addActionListener(this);
249
		}
250
		return cmbColorField;
251
	}
252

  
253
	public void setData(FLayer lyr, ILegend legend) {
254
		this.layer = lyr;
255
		this.oldLegend = null;
256

  
257
		if (symbolTable != null)
258
			pnlSouth.remove(symbolTable);
259

  
260

  
261
		symbolTable = new SymbolTable(
262
		    this, SymbolTable.INTERVALS_TYPE, Geometry.TYPES.GEOMETRY);
263

  
264
		pnlSouth.add(symbolTable,BorderLayout.CENTER);
265
		fillFieldNames();
266

  
267

  
268
		if (legend instanceof QuantityByCategoryLegend) {
269
            this.oldLegend = (QuantityByCategoryLegend) legend.cloneLegend();
270
			cmbColorField.setSelectedItem(this.oldLegend.getColorRampLegend().getClassifyingFieldNames()[0]);
271
			cmbGraduatedSymbolField.setSelectedItem(
272
			    this.oldLegend.getGraduatedSymbolsLegend()
273
			    .getClassifyingFieldNames()[0]);
274
			symbolTable.removeAllItems();
275

  
276
			this.fillTableSkipDefault(this.oldLegend);
277

  
278
		} else {
279
			this.oldLegend = new QuantityByCategoryLegend();
280
			String[] fnn = {
281
                (String) getCmbColorField().getSelectedItem(),
282
                (String) getCmbGraduatedField().getSelectedItem() };
283

  
284
			this.oldLegend.setClassifyingFieldNames(fnn);
285

  
286
            try {
287
                FLyrVect lv = (FLyrVect) lyr;
288
                this.setFieldTypesFromFieldNames(lv, this.oldLegend, fnn);
289
                this.oldLegend.setShapeType(lv.getShapeType());
290
            } catch (Exception exc) {
291
                logger.info("Error while setting data in legend panel", exc);
292
                ApplicationLocator.getManager().messageDialog(
293
                    Messages.getText("error_trying_to_access_to_the_layer"),
294
                    Messages.getText("error"),
295
                    JOptionPane.ERROR_MESSAGE);
296
            }
297
		}
298
	}
299

  
300
	private void setFieldTypesFromFieldNames(
301
	    FLyrVect vect,
302
	    QuantityByCategoryLegend leg,
303
	    String[] nn) throws DataException {
304

  
305
        FeatureStore fsto = (FeatureStore) vect.getDataStore();
306
        FeatureType fty = fsto.getDefaultFeatureType();
307
        int[] fieldTypes  = new int[nn.length];
308
        FeatureAttributeDescriptor attdesc = null;
309

  
310
        for (int i = 0; i < nn.length; i++) {
311
            attdesc = fty.getAttributeDescriptor(nn[i]);
312
            fieldTypes[i]= attdesc.getDataType().getType();
313
        }
314
        leg.setClassifyingFieldTypes(fieldTypes);
315
	}
316

  
317

  
318

  
319
	private void fillSymbolListFromTable() {
320

  
321
		ISymbol theSymbol;
322
		IInterval theInterval = null;
323
		// Borramos las anteriores listas:
324
		this.oldLegend.clear();
325

  
326
		FLyrVect vect = (FLyrVect) layer;
327
		try {
328

  
329
		    String[] fNames = this.oldLegend.getClassifyingFieldNames();
330
			if (fNames != null) {
331
			    setFieldTypesFromFieldNames(vect, this.oldLegend, fNames);
332
			}
333
		} catch (DataException e) {
334

  
335
            logger.info("Error while fillSymbolListFromTablel", e);
336
            ApplicationLocator.getManager().messageDialog(
337
                Messages.getText("could_not_setup_legend"),
338
                Messages.getText("error"),
339
                JOptionPane.ERROR_MESSAGE);
340
            return;
341
		}
342

  
343
		for (int row = 0; row < symbolTable.getRowCount(); row++) {
344

  
345
			theInterval = (IInterval) symbolTable.getFieldValue(row, 1);
346
			theSymbol = (ISymbol) symbolTable.getFieldValue(row, 0);
347
			theSymbol.setDescription((String) symbolTable.getFieldValue(
348
					row, 2));
349

  
350
			if (theSymbol instanceof IFillSymbol) {
351
				this.oldLegend.getColorRampLegend().addSymbol(theInterval, theSymbol);
352
			} else if (theSymbol instanceof IMarkerSymbol){
353
				this.oldLegend.getGraduatedSymbolsLegend().addSymbol(theInterval, theSymbol);
354
			}
355

  
356
		}
357

  
358
		if(oldLegend.getColorRampLegend().isUseDefaultSymbol())
359
			this.oldLegend.getColorRampLegend().addSymbol(
360
			    null, oldLegend.getColorRampLegend().getDefaultSymbol());
361
		if(oldLegend.getGraduatedSymbolsLegend().isUseDefaultSymbol())
362
			this.oldLegend.getGraduatedSymbolsLegend().addSymbol(
363
			    null, oldLegend.getGraduatedSymbolsLegend().getDefaultSymbol());
364
	}
365

  
366
	public ILegend getLegend() {
367
		fillSymbolListFromTable();
368
        this.legend = (QuantityByCategoryLegend) this.oldLegend.cloneLegend();
369
		return this.legend;
370
	}
371

  
372
	public String getDescription() {
373
		return Messages.getText("draw_quantities_for_each_category");
374
	}
375

  
376
	public ImageIcon getIcon() {
377
	    ImageIcon ii = IconThemeHelper.getImageIcon(
378
	        "legend-overview-quantity-by-category");
379
	    return ii;
380
	}
381

  
382
	public Class getParentClass() {
383
		return MultipleAttributes.class;
384
	}
385

  
386
	public String getTitle() {
387
		return Messages.getText("quantity_by_category");
388
	}
389

  
390
	public JPanel getPanel() {
391
		return this;
392
	}
393

  
394
	public Class getLegendClass() {
395
		return QuantityByCategoryLegend.class;
396
	}
397

  
398
	public void actionPerformed(ActionEvent e) {
399
		JComponent c = (JComponent) e.getSource();
400

  
401
		if (c.equals(getBtnColor())) {
402
			VectorialIntervalLegend colorRamp = this.oldLegend.getColorRampLegend();
403
			String fieldName = (String) getCmbColorField().getSelectedItem();
404
			if (!fieldName.equals(colorRamp.getClassifyingFieldNames()[0])) {
405
				// if classification field has changed, clear the legend
406
				colorRamp.setClassifyingFieldNames(new String[] {fieldName});
407
				colorRamp.clear();
408
			}
409
			// create a new modal window to edit the color ramp legend
410
			VectorialInterval legPanel = new VectorialInterval();
411
			legPanel.setData(layer, colorRamp);
412
			InnerWindow window = new InnerWindow(legPanel);
413

  
414
			ApplicationLocator.getManager().getUIManager().addWindow(window);
415

  
416
			cmbColorField.setSelectedItem(colorRamp.getClassifyingFieldNames()[0].toString());
417
			ILegend newLegend = window.getLegend();
418
			if (newLegend != null) {
419

  
420
				if (newLegend instanceof VectorialIntervalLegend) {
421
	                this.oldLegend.getColorRampLegend().clear();
422
	                this.oldLegend.setColorRampLegend(
423
	                    (VectorialIntervalLegend) newLegend);
424
				} else {
425
		            ApplicationLocator.getManager().messageDialog(
426
		                Messages.getText("could_not_setup_legend"),
427
		                Messages.getText("error"),
428
		                JOptionPane.ERROR_MESSAGE);
429
				}
430
			}
431
			symbolTable.removeAllItems();
432
			this.fillTableSkipDefault(this.oldLegend);
433

  
434
		} else if (c.equals(getBtnSymbol())) {
435

  
436
			GraduatedSymbolsLegend graduatedSymbol =
437
			    this.oldLegend.getGraduatedSymbolsLegend();
438
			String fieldName = (String) getCmbGraduatedField().getSelectedItem();
439
			if (!fieldName.equals(graduatedSymbol.getClassifyingFieldNames()[0])) {
440
				// if classification field has changed, clear the legend
441
				graduatedSymbol.setClassifyingFieldNames(new String[] {fieldName});
442

  
443
		        ISymbol def = MapContextLocator.getSymbolManager().
444
		            createSymbol(Geometry.TYPES.POINT);
445
				graduatedSymbol.setDefaultSymbol(def);
446

  
447
				graduatedSymbol.setMinSymbolSize(1);
448
				graduatedSymbol.setMaxSymbolSize(14);
449
				graduatedSymbol.clear();
450
			}
451
			// create a new modal window to edit the graduated symbol legend
452
			GraduatedSymbolsPanel legPanel = new GraduatedSymbolsPanel(false);
453
			legPanel.setData(layer, graduatedSymbol);
454
			InnerWindow window = new InnerWindow(legPanel);
455

  
456
			ApplicationLocator.getManager().getUIManager().addWindow(window);
457

  
458
			cmbGraduatedSymbolField.setSelectedItem(graduatedSymbol.getClassifyingFieldNames()[0].toString());
459
			ILegend newLegend = window.getLegend();
460
			if(newLegend != null) {
461

  
462
			    if (newLegend instanceof GraduatedSymbolsLegend) {
463
	                this.oldLegend.getGraduatedSymbolsLegend().clear();
464
	                this.oldLegend.setGraduateSymbolLegend(
465
	                    (GraduatedSymbolsLegend) newLegend);
466
			    } else {
467
                    ApplicationLocator.getManager().messageDialog(
468
                        Messages.getText("could_not_setup_legend"),
469
                        Messages.getText("error"),
470
                        JOptionPane.ERROR_MESSAGE);
471
			    }
472
			}
473
			symbolTable.removeAllItems();
474
			this.fillTableSkipDefault(this.oldLegend);
475
		}
476

  
477
		else if (c.equals(getCmbColorField())) {
478
			symbolTable.removeAllItems();
479

  
480
			this.fillTableSkipDefault(
481
			    this.oldLegend.getGraduatedSymbolsLegend().getSymbols(),
482
			    this.oldLegend.getGraduatedSymbolsLegend().getValues(),
483
			    this.oldLegend.getDescriptions());
484
		}
485

  
486
		else if (c.equals(getCmbGraduatedField())) {
487
			symbolTable.removeAllItems();
488

  
489
			this.fillTableSkipDefault(
490
			    this.oldLegend.getColorRampLegend().getSymbols(),
491
			    this.oldLegend.getColorRampLegend().getValues(),
492
			    this.oldLegend.getDescriptions());
493
		}
494

  
495
//		if (c.equals(getButDel())) {
496
//			symbolTable.removeSelectedRows();
497
//		}
498
//
499
//		else if(c.equals(getButDelAll())) {
500
//			symbolTable.removeAllItems();
501
//		}
502
	}
503
	private class InnerWindow extends JPanel implements IWindow {
504

  
505
		private ActionListener okAction = new ActionListener() {
506
			public void actionPerformed(ActionEvent e) {
507
				legend = panel.getLegend();
508
				ApplicationLocator.getManager().getUIManager()
509
				.closeWindow(InnerWindow.this);
510
			}
511

  
512
		}, cancelAction = new ActionListener() {
513
			public void actionPerformed(ActionEvent e) {
514
                ApplicationLocator.getManager().getUIManager()
515
                .closeWindow(InnerWindow.this);
516
			}
517
		};
518
		private ILegend legend;
519
		private ILegendPanel panel;
520
		private WindowInfo wi;
521
		public InnerWindow(ILegendPanel panel) {
522
			this.panel = panel;
523
			this.setLayout(new BorderLayout());
524
			add((JComponent) panel, BorderLayout.NORTH);
525
			add(new AcceptCancelPanel(okAction, cancelAction), BorderLayout.SOUTH);
526
		}
527

  
528
		public ILegend getLegend() {
529
			return legend;
530
		}
531

  
532
		public WindowInfo getWindowInfo() {
533
			if (wi == null) {
534
				wi = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
535
				JComponent c = (JComponent) panel;
536
				wi.setWidth(c.getWidth());
537
				wi.setHeight(c.getHeight());
538
				wi.setTitle(panel.getTitle());
539
			}
540
			return wi;
541
		}
542

  
543
		public Object getWindowProfile() {
544
			return WindowInfo.DIALOG_PROFILE;
545
		}
546

  
547

  
548
	}
549
	/**
550
	 * Fills the comboboxes that are placed in the panel with the
551
	 * classifying field names of the layer
552
	 *
553
	 */
554
	protected void fillFieldNames() {
555

  
556
		ArrayList<String> nomFields = null;
557
		FLyrVect vect = null;
558

  
559
		try {
560

  
561
            FeatureStore fsto = null;
562
            FeatureType fty = null;
563
            FeatureAttributeDescriptor[] atts = null;
564
			if (layer instanceof FLyrVect) {
565
			    vect = (FLyrVect) layer;
566
	            fsto = (FeatureStore) vect.getDataStore();
567
	            fty = fsto.getDefaultFeatureType();
568
	            atts = fty.getAttributeDescriptors();
569
			} else {
570
	            ApplicationLocator.getManager().messageDialog(
571
	                Messages.getText("error_trying_to_access_to_the_layer"),
572
	                Messages.getText("error"),
573
	                JOptionPane.ERROR_MESSAGE);
574
	            logger.info("Error while fillFieldNames, Unexpected layer class: "
575
	                + layer);
576
	            return;
577
			}
578
			nomFields = new ArrayList<String>();
579
			for (int i = 0; i < atts.length; i++) {
580
			    if (atts[i].getDataType().isNumeric()) {
581
                    nomFields.add(atts[i].getName());
582
			    }
583
			}
584
		} catch (DataException e) {
585
            ApplicationLocator.getManager().messageDialog(
586
                Messages.getText("error_trying_to_access_to_the_layer"),
587
                Messages.getText("error"),
588
                JOptionPane.ERROR_MESSAGE);
589
            logger.info("Error while fillFieldNames", e);
590
            nomFields = new ArrayList<String>();
591
		}
592

  
593
		DefaultComboBoxModel cM = new DefaultComboBoxModel(nomFields.toArray());
594
		cmbColorField.setModel(cM);
595
		cM = new DefaultComboBoxModel(nomFields.toArray());
596
		cmbGraduatedSymbolField.setModel(cM);
597

  
598
		symbolTable.removeAllItems();
599
	}
600

  
601

  
602
	public boolean isSuitableFor(FLayer layer) {
603
		if (layer instanceof FLyrVect) {
604
			try {
605
				FLyrVect vect = (FLyrVect) layer;
606
				int shpt = vect.getGeometryType().getType();
607

  
608
				if (!QuantityByCategoryLegend.isPolygonal(shpt)) {
609
				    return false;
610
				}
611

  
612
                FeatureStore fsto = (FeatureStore) vect.getDataStore();
613
                FeatureType fty = fsto.getDefaultFeatureType();
614
                FeatureAttributeDescriptor[] atts = fty.getAttributeDescriptors();
615

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

  
632

  
633
	// reemplazar esto por arriba
634
    private void fillTableSkipDefault(
635
        IClassifiedLegend leg) {
636

  
637
        Object[] src_expr = leg.getValues();
638
        ISymbol[] src_syms = leg.getSymbols();
639
        String[] src_descs = leg.getDescriptions();
640

  
641
        List syms = new ArrayList();
642
        List vals = new ArrayList();
643
        List descs = new ArrayList();
644
        for (int i=0; i<src_expr.length; i++) {
645
            if (src_expr[i] instanceof IInterval) {
646
                syms.add(src_syms[i]);
647
                vals.add(src_expr[i]);
648
                descs.add(src_descs[i]);
649
            }
650
        }
651
        symbolTable.fillTableFromSymbolList(
652
            (ISymbol[]) syms.toArray(new ISymbol[0]),
653
            (Object[]) vals.toArray(new Object[0]),
654
            (String[]) descs.toArray(new String[0]));
655
    }
656

  
657
    private void fillTableSkipDefault(
658
        ISymbol[] src_syms,
659
        Object[] src_expr,
660
        String[] src_descs) {
661

  
662
        List syms = new ArrayList();
663
        List vals = new ArrayList();
664
        List descs = new ArrayList();
665
        for (int i=0; i<src_expr.length; i++) {
666
            if (src_expr[i] instanceof IInterval) {
667
                syms.add(src_syms[i]);
668
                vals.add(src_expr[i]);
669
                descs.add(src_descs[i]);
670
            }
671
        }
672
        symbolTable.fillTableFromSymbolList(
673
            (ISymbol[]) syms.toArray(new ISymbol[0]),
674
            (Object[]) vals.toArray(new Object[0]),
675
            (String[]) descs.toArray(new String[0]));
676
    }
677

  
678

  
679
}
org.gvsig.legend.quantitybycategory.app.mainplugin/tags/org.gvsig.legend.quantitybycategory.app.mainplugin-1.0.75/src/main/java/org/gvsig/symbology/library/QuantityByCategoryLibrary.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.QuantityByCategoryLegend;
29
import org.gvsig.symbology.gui.layerproperties.QuantityByCategoryPanel;
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 Quantity by category legend
39
 * 
40
 * @author jldominguez
41
 *
42
 */
43
public class QuantityByCategoryLibrary extends AbstractLibrary {
44

  
45

  
46
    public void doRegistration() {
47
        registerAsImplementationOf(QuantityByCategoryLibrary.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("legend",
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff