Revision 18883

View differences:

trunk/applications/appgvSIG/src/com/iver/cit/gvsig/project/documents/view/legend/gui/FilePage.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

  
42
/* CVS MESSAGES:
43
*
44
* $Id$
45
* $Log$
46
* Revision 1.8  2007-09-04 10:53:10  caballero
47
* show page
48
*
49
* Revision 1.7  2007/07/30 12:56:04  jaume
50
* organize imports, java 5 code downgraded to 1.4 and added PictureFillSymbol
51
*
52
* Revision 1.6  2007/07/26 12:28:29  jaume
53
* maybe finished ArrowMarkerSymbol and ArrowDecoratorStyle
54
*
55
* Revision 1.5  2007/05/10 09:46:45  jaume
56
* Refactored legend interface names
57
*
58
* Revision 1.4  2007/04/05 14:31:31  cesar
59
* Change translation keys
60
*
61
* Revision 1.3  2007/04/03 09:12:28  cesar
62
* When saving legends, ask before overwriting files
63
*
64
* Revision 1.2  2007/03/09 11:25:00  jaume
65
* Advanced symbology (start committing)
66
*
67
* Revision 1.1.2.1  2007/02/01 12:12:41  jaume
68
* theme manager window and all its components are now dynamic
69
*
70
*
71
*/
72
package com.iver.cit.gvsig.project.documents.view.legend.gui;
73

  
74
import java.awt.Component;
75
import java.awt.event.ActionListener;
76
import java.io.File;
77
import java.io.FileNotFoundException;
78
import java.io.FileReader;
79
import java.io.FileWriter;
80

  
81
import javax.swing.JComponent;
82
import javax.swing.JFileChooser;
83
import javax.swing.JOptionPane;
84

  
85
import org.exolab.castor.xml.MarshalException;
86
import org.exolab.castor.xml.Marshaller;
87
import org.exolab.castor.xml.ValidationException;
88
import org.gvsig.gui.beans.swing.JButton;
89

  
90
import com.iver.andami.PluginServices;
91
import com.iver.andami.messages.NotificationManager;
92
import com.iver.cit.gvsig.fmap.layers.FLayer;
93
import com.iver.cit.gvsig.fmap.layers.XMLException;
94
import com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable;
95
import com.iver.cit.gvsig.fmap.rendering.ILegend;
96
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
97
import com.iver.utiles.GenericFileFilter;
98
import com.iver.utiles.XMLEntity;
99
import com.iver.utiles.xmlEntity.generate.XmlTag;
100

  
101
public class FilePage extends AbstractThemeManagerPage {
102

  
103
	private ILegend legend;
104
	private FLayer layer;
105
	private JButton btnSaveLegend;
106
	private JButton btnLoadLegend;
107
	private ActionListener actionListener = new ActionListener() {
108
		public void actionPerformed(java.awt.event.ActionEvent e) {
109
			JComponent c = (JComponent) e.getSource();
110
			if (c.equals(getBtnSaveLegend())) {
111
				JFileChooser jfc = new JFileChooser();
112
				jfc.addChoosableFileFilter(new GenericFileFilter("sld","*.sld"));
113
				jfc.addChoosableFileFilter(new GenericFileFilter("gvl",
114
						PluginServices.getText(this, "tipo_de_leyenda")));
115
				File basedir = null;
116
				Object[] options = {PluginServices.getText(this, "yes"),
117
	                    PluginServices.getText(this, "no"),
118
	                    PluginServices.getText(this, "Cancel")};
119

  
120
				while (true) {
121
					jfc.setCurrentDirectory(basedir);
122
					if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION)
123
					{
124
						File file=jfc.getSelectedFile();
125
						if (file.exists()) {
126
							int answer = JOptionPane.showOptionDialog((Component)PluginServices.getMainFrame(),
127
									PluginServices.getText(this, "error_file_exists"),
128
									PluginServices.getText(this, "confirmation_dialog"),
129
									JOptionPane.YES_NO_CANCEL_OPTION,
130
									JOptionPane.QUESTION_MESSAGE,
131
									null,
132
									options, options[1]);
133
							if (answer==2 || answer==JOptionPane.CLOSED_OPTION) {
134
								// 'Cancel' pressed or window closed: don't save anythig, exit save dialog
135
								break;
136
							}
137
							else if (answer==1) {
138
								// 'No' pressed, show jfc dialog again
139
								basedir = file.getParentFile();
140
								continue;
141
							}
142
							// "Yes" pressed, overwrite the file...
143
						}
144
						if (jfc.getFileFilter().accept(new File("dummy.sld")))
145
						{
146
							if (!(file.getPath().toLowerCase().endsWith(".sld"))){
147
								file=new File(file.getPath()+".sld");
148
							}
149
							export2SLD(file);
150
						}
151
						else{
152
							if (!(file.getPath().toLowerCase().endsWith(".gvl"))){
153
								file=new File(file.getPath()+".gvl");
154
							}
155
							writeLegend(file);
156
						}
157
						break;
158
					}
159
				}
160
			} else if (c.equals(getBtnLoadLegend())) {
161
				JFileChooser jfc = new JFileChooser();
162
	    		jfc.addChoosableFileFilter(new GenericFileFilter("gvl",
163
	    				PluginServices.getText(this, "tipo_de_leyenda")));
164
	    		//jfc.addChoosableFileFilter(new GenericFileFilter("sld","sld"));
165

  
166
	    		if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
167
	    			File file=jfc.getSelectedFile();
168
	    			if (!(file.getPath().endsWith(".gvl") || file.getPath().endsWith(".GVL")))	{
169
	    				file=new File(file.getPath()+".gvl");
170
	    			}
171
	    			loadLegend(file);
172
	    		}
173
			}
174
		};
175
	};
176
	public FilePage() {
177
		super();
178
		initialize();
179
	}
180

  
181
	private void initialize() {
182

  
183
	}
184

  
185
	private JButton getBtnSaveLegend() {
186
		if (btnSaveLegend == null) {
187
			btnSaveLegend = new JButton(PluginServices.getText(this,"Guardar_leyenda")+"...");
188
			btnSaveLegend.setActionCommand("SAVE_LEGEND");
189
			btnSaveLegend.addActionListener(actionListener);
190
		}
191
		return btnSaveLegend;
192
	}
193

  
194
	private JButton getBtnLoadLegend() {
195
		if (btnLoadLegend == null) {
196
			btnLoadLegend = new JButton(PluginServices.getText(this,"Recuperar_leyenda")+"...");
197
			btnLoadLegend.setActionCommand("LOAD_LEGEND");
198
			btnLoadLegend.addActionListener(actionListener);
199
		}
200
		return btnLoadLegend;
201
	}
202

  
203
	public String getName() {
204
		return PluginServices.getText(this, "file");
205
	}
206

  
207
	public void acceptAction() {
208
	}
209

  
210
	public void cancelAction() {
211
	}
212

  
213
	public void applyAction() {
214
	}
215

  
216
	 /**
217
     * Reads a legend file and loads into the this LegendManager
218
     * @param file
219
     */
220
    private void loadLegend(File file) {
221
    	File xmlFile = new File(file.getAbsolutePath());
222
    	FileReader reader;
223
    	try {
224
    		reader = new FileReader(xmlFile);
225

  
226
    		XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
227

  
228
    		legend = LegendFactory.createFromXML(new XMLEntity(tag));
229
    	} catch (FileNotFoundException e) {
230
    		// should be unreachable code
231
    		NotificationManager.addError(PluginServices.getText(this, "file_not_found"), e);
232
    	} catch (MarshalException e) {
233
    		NotificationManager.addError(PluginServices.getText(this, "file_corrupt"), e);
234
    	} catch (ValidationException e) {
235
    		// should be unreachable code
236
    		NotificationManager.addError("ValidationException", e);
237
    	} catch (XMLException e) {
238
    		NotificationManager.addError(PluginServices.getText(this, "unsupported_legend"), e);
239
    	}
240
    }
241

  
242

  
243
	public void setModel(FLayer layer) {
244
		this.layer = layer;
245
		this.legend = ((Classifiable) layer).getLegend();
246
	}
247

  
248
	private void export2SLD(File file) {
249
		try {
250
			FileWriter writer = new FileWriter(file.getAbsolutePath());
251
			writer.write( legend.getSLDString(layer.getName()));
252
			writer.close();
253

  
254
		} catch (Exception e) {
255
			NotificationManager.addError(PluginServices.getText(this, "Error_exportando_SLD"), e);
256
		}
257
	}
258

  
259
    private void writeLegend(File file) {
260
		try {
261
			FileWriter writer = new FileWriter(file.getAbsolutePath());
262
			Marshaller m = new Marshaller(writer);
263
			m.setEncoding("ISO-8859-1");
264
			m.marshal(legend.getXMLEntity().getXmlTag());
265
		} catch (Exception e) {
266
			NotificationManager.addError(PluginServices.getText(this, "Error_guardando_la_leyenda"), e);
267
		}
268
	}
269
}
trunk/applications/appgvSIG/src/com/iver/cit/gvsig/project/documents/view/legend/gui/LegendManager.java
48 48
import java.awt.Component;
49 49
import java.awt.Dimension;
50 50
import java.awt.FlowLayout;
51
import java.awt.Graphics;
52 51
import java.awt.event.ActionListener;
53 52
import java.io.File;
54 53
import java.util.ArrayList;
......
90 89
import com.iver.cit.gvsig.fmap.rendering.ILegend;
91 90
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend;
92 91
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
93
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
94
import com.iver.cit.gvsig.gui.styling.SymbolPreviewer;
95 92
/**
96 93
 * Implements the panel which allows the user to control all the information about the
97 94
 * legends of a layer in order to improve the information that it offers to the user.
......
129 126
	private JButton btnSaveLegend;
130 127
	private JButton btnLoadLegend;
131 128
	private Hashtable<FLayer, ILegend> table = null;
132

  
129
	private boolean empty = true;
133 130
	public static String defaultLegendFolderPath;
134 131
	{
135 132

  
......
436 433
	  * were added through the static method addLegendPage(ILegendPanel page)
437 434
	  */
438 435
	 private void fillDialog() {
439
		 for (int i = 0; i < legendPool.size(); i++) {
440
			 Class pageClass = (Class) legendPool.get(i);
441
			 ILegendPanel page;
442
			 try {
443
				 page = (ILegendPanel) pageClass.newInstance();
444
				 if (page.isSuitableFor(layer)) {
445
					 // this legend can be applied to this layer
446
					 pages.put(page.getClass(), page);
436
		 if (empty) {
437
			 for (int i = 0; i < legendPool.size(); i++) {
438
				 Class pageClass = (Class) legendPool.get(i);
439
				 ILegendPanel page;
440
				 try {
441
					 page = (ILegendPanel) pageClass.newInstance();
442
					 if (page.isSuitableFor(layer)) {
443
						 // this legend can be applied to this layer
444
						 pages.put(page.getClass(), page);
447 445

  
448
					 if (dirtyTree) {
449
						 // rebuild page tree
450
						 dirtyTree = false;
446
						 if (dirtyTree) {
447
							 // rebuild page tree
448
							 dirtyTree = false;
451 449

  
452
						 ArrayList legList = new ArrayList(pages.values());
453
						 ArrayList alreadyAdded = new ArrayList();
454
						 DefaultTreeModel model = new DefaultTreeModel(root);
455
						 while (legList.size()>0) {
456
							 ILegendPanel legend = (ILegendPanel) legList.get(0);
457
							 Class parent = legend.getParentClass();
458
							 while (parent != null &&
459
									 !alreadyAdded.contains(pages.get(parent))) {
460
								 legend = (ILegendPanel) pages.get(parent);
450
							 ArrayList legList = new ArrayList(pages.values());
451
							 ArrayList alreadyAdded = new ArrayList();
452
							 DefaultTreeModel model = new DefaultTreeModel(root);
453
							 while (legList.size()>0) {
454
								 ILegendPanel legend = (ILegendPanel) legList.get(0);
455
								 Class parent = legend.getParentClass();
456
								 while (parent != null &&
457
										 !alreadyAdded.contains(pages.get(parent))) {
458
									 legend = (ILegendPanel) pages.get(parent);
459
								 }
460
								 doInsertNode(model, legend);
461
								 legList.remove(legend);
462
								 alreadyAdded.add(legend);
461 463
							 }
462
							 doInsertNode(model, legend);
463
							 legList.remove(legend);
464
							 alreadyAdded.add(legend);
464
							 treeModel = model;
465
							 jTreeLegends.setModel(model);
465 466
						 }
466
						 treeModel = model;
467
						 jTreeLegends.setModel(model);
467
						 doInsertNode(treeModel, page);
468

  
468 469
					 }
469
					 doInsertNode(treeModel, page);
470
					 getJTreeLegends().setModel(treeModel);
470 471

  
472
				 } catch (InstantiationException e) {
473
					 NotificationManager.addError("Trying to instantiate an interface" +
474
							 " or abstract class + "+pageClass.getName(), e);
475
				 } catch (IllegalAccessException e) {
476
					 NotificationManager.addError("IllegalAccessException: does " +
477
							 pageClass.getName()	+ " class have an anonymous" +
478
							 " constructor?", e);
471 479
				 }
472
				 getJTreeLegends().setModel(treeModel);
473 480

  
474
			 } catch (InstantiationException e) {
475
				 NotificationManager.addError("Trying to instantiate an interface" +
476
						 " or abstract class + "+pageClass.getName(), e);
477
			 } catch (IllegalAccessException e) {
478
				 NotificationManager.addError("IllegalAccessException: does " +
479
						 pageClass.getName()	+ " class have an anonymous" +
480
						 " constructor?", e);
481 481
			 }
482

  
482
			 getJTreeLegends().repaint();
483
			 empty = false;
483 484
		 }
484
		 getJTreeLegends().repaint();
485

  
486 485
	 }
487 486

  
488 487
	 private DefaultMutableTreeNode findNode(Class searchID) {
trunk/applications/appgvSIG/src/com/iver/cit/gvsig/gui/styling/JComboBoxSimpleMarkeStyles.java
69 69
		new MyItem(SimpleMarkerSymbol.DIAMOND_STYLE),
70 70
		new MyItem(SimpleMarkerSymbol.X_STYLE),
71 71
		new MyItem(SimpleMarkerSymbol.TRIANGLE_STYLE),
72
		new MyItem(SimpleMarkerSymbol.STAR_STYLE),
72 73
	};
73 74

  
74 75
	/**

Also available in: Unified diff