Revision 20779 trunk/applications/appgvSIG/src/com/iver/cit/gvsig/project/documents/view/legend/gui/LegendManager.java

View differences:

LegendManager.java
56 56
import java.util.prefs.Preferences;
57 57

  
58 58
import javax.swing.ImageIcon;
59
import javax.swing.JCheckBox;
60
import javax.swing.JComboBox;
61 59
import javax.swing.JComponent;
62 60
import javax.swing.JFileChooser;
63 61
import javax.swing.JLabel;
......
95 93
 * There are options to create, save or load an existing legend.
96 94
 *
97 95
 * @author jaume dominguez faus - jaume.dominguez@iver.es
98
 * @param <FMapSLDDriver>
99 96
 */
100 97
public class LegendManager extends AbstractThemeManagerPage {
101
	@SuppressWarnings("unchecked")
102
	private static ArrayList legendPool = new ArrayList();
103
	@SuppressWarnings("unchecked")
104
	private static ArrayList legendDriverPool = new ArrayList();
98
	private static final long serialVersionUID = 7989057553773181019L;
99
	private static ArrayList<Class<? extends ILegendPanel>> legendPool = new ArrayList<Class<? extends ILegendPanel>>();
100
	private static ArrayList<Class<? extends IFMapLegendDriver>> legendDriverPool = new ArrayList<Class<? extends IFMapLegendDriver>>();
105 101

  
106 102
	private FLayer layer;
107 103
	private ILegend legend; // Le asignaremos la leyenda del primer tema activo.
108
	private Hashtable pages = new Hashtable();
104
	private Hashtable<Class<? extends ILegendPanel>, ILegendPanel> pages = new Hashtable<Class<? extends ILegendPanel>, ILegendPanel>();
109 105
	private JPanel topPanel = null;
110
	private JLabel jLabel = null;
111
	private JComboBox jComboBox = null;
112
	private JCheckBox jCheckBox = null;
113 106
	private JTextArea titleArea = null;
114 107
	private JPanel preview = null;
115 108
	private JScrollPane jTitleScrollPane = null;
......
127 120
	private JButton btnLoadLegend;
128 121
	private Hashtable<FLayer, ILegend> table = null;
129 122
	private boolean empty = true;
123
	private JLabel iconLabel;
124

  
130 125
	public static String defaultLegendFolderPath;
131 126
	{
132 127

  
......
140 135

  
141 136
				JLegendFileChooser jfc = new JLegendFileChooser(getLegendDrivers(),true);
142 137
				jfc.setAcceptAllFileFilterUsed(false);
143
				
144
				
138

  
139

  
145 140
				File basedir = null;
146 141
				Object[] options = {PluginServices.getText(this, "yes"),
147 142
						PluginServices.getText(this, "no"),
......
155 150

  
156 151
					if (file.exists()) {
157 152
						int answer = JOptionPane.showOptionDialog((Component)PluginServices.getMainFrame(),
158
								PluginServices.getText(this, "error_file_exists"),
153
								PluginServices.getText(this, "error_file_exists")+"\n"+
154
								PluginServices.getText(this, "do_you_want_to_overwrite_it")+"?",
159 155
								PluginServices.getText(this, "confirmation_dialog"),
160 156
								JOptionPane.YES_NO_CANCEL_OPTION,
161 157
								JOptionPane.QUESTION_MESSAGE,
......
173 169

  
174 170
					} catch (LegendDriverException e1) {
175 171
						int type = e1.getType();
176
						String message="Imposible guardar la leyenda:\n";
172
						String message=PluginServices.getText(this, "could_not_save_legend")+":\n";
177 173

  
178 174
						if ((type & LegendDriverException.SAVE_LEGEND_ERROR) != 0){
179 175
							type = type & ~LegendDriverException.SAVE_LEGEND_ERROR;
180
							message += PluginServices.getText(this, "Error guardando la leyenda.\n");
176
							message += PluginServices.getText(this, "error_writing_file")+".\n";
181 177
						}
182 178
						if ((type & LegendDriverException.UNSUPPORTED_LEGEND_CREATION) != 0){
183 179
							type = type & ~LegendDriverException.UNSUPPORTED_LEGEND_CREATION;
184
							message += PluginServices.getText(this, "-Soporte para creaci?n de este tipo de formato no implementado todavia.\n");
180
							message += "-"+ PluginServices.getText(this, "legend_format_not_yet_supported")+"\n";
185 181
						}
186 182
						if (type != 0) {
187
							message = PluginServices.getText(this, "-Error no especificado.\n");
183
							message = "-"+PluginServices.getText(this, "unknown_error")+"\n";
188 184
						}
189 185
						JOptionPane.showMessageDialog(null, message);
190 186
					}
......
202 198

  
203 199
					try {
204 200
						table = driver.read(layer.getMapContext().getLayers(),layer, file);
205
//						ILegend my_legend = driver.read(layer.getMapContext().getLayers(),layer, file);
206
//						if(my_legend != null)
207
//						applyLegend(my_legend);
208 201

  
209 202
						if(table.containsKey(layer))
210 203
							applyLegend((ILegend) table.get(layer));
211
//						if(table.size() > 1)
212
//							applyRestOfLegends(table,layer.getMapContext().getLayers());
213 204

  
214 205

  
215 206
					} catch (LegendDriverException e1) {
216 207
						int type = e1.getType();
217 208
						String message="La leyenda no se aplicar? a la capa:\n";
209
						boolean hasReason = false;
218 210

  
219 211
						if ((type & LegendDriverException.CLASSIFICATION_FIELDS_NOT_FOUND) != 0){
220 212
							type = type & ~LegendDriverException.CLASSIFICATION_FIELDS_NOT_FOUND;
221
							message += PluginServices.getText(this, "-El campo de clasificaci?n no existe.\n");
213
							message +="-"+ PluginServices.getText(this, "classification_field_does_not_exists")+"\n";
214
							hasReason = true;
222 215
						}
223 216
						if ((type & LegendDriverException.LEGEND_TYPE_NOT_YET_SUPPORTED) != 0){
224 217
							type = type & ~LegendDriverException.LEGEND_TYPE_NOT_YET_SUPPORTED;
225
							message += PluginServices.getText(this, "-El tipo de leyenda no es soportado por gvSIG.\n");
218
							message += "-"+ PluginServices.getText(this, "legend_type_not_yet_supported")+"\n";
219
							hasReason = true;
226 220
						}
227 221
						if ((type & LegendDriverException.SYMBOL_TYPE_NOT_YET_SUPPORTED) != 0){
228 222
							type = type & ~LegendDriverException.SYMBOL_TYPE_NOT_YET_SUPPORTED;
229
							message += PluginServices.getText(this, "-El tipo de s?mbolo no es soportado por gvSIG.\n");
223
							message += "-"+ PluginServices.getText(this, "unsupported_symbol_type")+"\n";
224
							hasReason = true;
230 225
						}
231 226
						if ((type & LegendDriverException.LAYER_SHAPETYPE_MISMATCH) != 0){
232 227
							type = type & ~LegendDriverException.LAYER_SHAPETYPE_MISMATCH;
233
							message += PluginServices.getText(this, "-El tipo de capa y de leyenda no coinciden.\n");
228
							message += "-"+ PluginServices.getText(this, "layer_geometry_and_legend_types_are_incompatible")+"\n";
229
							hasReason = true;
234 230
						}
235 231
						if ((type & LegendDriverException.PARSE_LEGEND_FILE_ERROR) != 0){
236 232
							type = type & ~LegendDriverException.PARSE_LEGEND_FILE_ERROR;
237
							message += PluginServices.getText(this, "-Error en el parse de la leyenda.\n");
233
							message += "-"+ PluginServices.getText(this, "failed_reading_file")+"\n";
234
							hasReason = true;
238 235
						}
239 236
						if ((type & LegendDriverException.UNSUPPORTED_LEGEND_FILE_VERSION) != 0){
240 237
							type = type & ~LegendDriverException.UNSUPPORTED_LEGEND_FILE_VERSION;
241
							message += PluginServices.getText(this, "-Versi?n de leyenda no soportado por gvSIG.\n");
238
							message += "-"+ PluginServices.getText(this, "unsupported_legend_file_version")+"\n";
239
							hasReason = true;
242 240
						}
243 241
						if ((type & LegendDriverException.UNSUPPORTED_LEGEND_READING) != 0){
244 242
							type = type & ~LegendDriverException.UNSUPPORTED_LEGEND_READING;
245
							message += PluginServices.getText(this, "-Soporte para lectura de este tipo de formato no implementado todavia.\n");
243
							message +="-"+ PluginServices.getText(this, "unsupported_legend_file_format")+"\n"; 
244
							hasReason = true;
246 245
						}
247
						if (type != 0) {
248
							message = PluginServices.getText(this, "-Error no especificado.\n");
246
						if ((type & LegendDriverException.LAYER_NAME_NOT_FOUND) != 0){
247
							type = type & ~LegendDriverException.UNSUPPORTED_LEGEND_READING;
248
							message +="-"+ PluginServices.getText(this, "could_not_find_layer")+"\n"; 
249
							hasReason = true;
249 250
						}
251
						if (!hasReason) {
252
							message = "-"+ PluginServices.getText(this, "unknown_error")+"\n";
253
						}
250 254
						JOptionPane.showMessageDialog(null, message);
251 255
					}
252 256
				}
......
254 258
		}
255 259

  
256 260

  
257

  
258

  
259

  
260

  
261

  
262 261
		private IFMapLegendDriver[] getLegendDrivers() {
263
			Class[] legendDriverClasses = (Class[]) legendDriverPool.toArray(new Class[0]);
262
			Class<?>[] legendDriverClasses = legendDriverPool.toArray(new Class[0]);
264 263
			ArrayList<IFMapLegendDriver> drivers = new ArrayList<IFMapLegendDriver>();
265 264
			for (int i = 0; i < legendDriverClasses.length; i++) {
265
				String message = PluginServices.getText(this, "adding_legend_file_format_support")+": ";
266 266
				try {
267
					drivers.add((IFMapLegendDriver) legendDriverClasses[i].newInstance());
268
				} catch (InstantiationException e) {
269
					// TODO Auto-generated catch block
270
					e.printStackTrace();
271
				} catch (IllegalAccessException e) {
272
					// TODO Auto-generated catch block
273
					e.printStackTrace();
267
					Class<?> c = legendDriverClasses[i];
268
					drivers.add((IFMapLegendDriver) c.newInstance());
269
				} catch (Exception e) {
270
					NotificationManager.addError(message+PluginServices.getText(this, "failed"), e);
274 271
				}
272
				NotificationManager.addInfo(message+PluginServices.getText(this, "OK"));
273

  
275 274
			}
276 275
			return (IFMapLegendDriver[]) drivers.toArray(new IFMapLegendDriver[0]);
277 276
		};
278 277
	};
279
	private JLabel iconLabel;
280 278

  
279

  
281 280
	public LegendManager() {
282 281
		initialize();
283 282
	}
......
324 323
	 *
325 324
	 * @return javax.swing.JPanel
326 325
	 */
327
	 private JPanel getTopPanel() {
326
	private JPanel getTopPanel() {
328 327
		if (topPanel == null) {
329 328
			topPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
330 329
			topPanel.setPreferredSize(new Dimension(638, 31));
......
333 332
			topPanel.add(getBtnLoadLegend(), null);
334 333
		}
335 334
		return topPanel;
336
	 }
335
	}
337 336

  
338
	 private JButton getBtnSaveLegend() {
339
		 if (btnSaveLegend == null) {
340
			 btnSaveLegend = new JButton(PluginServices.getText(this,"Guardar_leyenda")+"...");
341
			 btnSaveLegend.addActionListener(loadSaveLegendAction);
342
		 }
343
		 return btnSaveLegend;
344
	 }
337
	private JButton getBtnSaveLegend() {
338
		if (btnSaveLegend == null) {
339
			btnSaveLegend = new JButton(PluginServices.getText(this,"Guardar_leyenda")+"...");
340
			btnSaveLegend.addActionListener(loadSaveLegendAction);
341
		}
342
		return btnSaveLegend;
343
	}
345 344

  
346
	 private JButton getBtnLoadLegend() {
347
		 if (btnLoadLegend == null) {
348
			 btnLoadLegend = new JButton(PluginServices.getText(this,"Recuperar_leyenda")+"...");
349
			 btnLoadLegend.addActionListener(loadSaveLegendAction);
350
		 }
351
		 return btnLoadLegend;
352
	 }
345
	private JButton getBtnLoadLegend() {
346
		if (btnLoadLegend == null) {
347
			btnLoadLegend = new JButton(PluginServices.getText(this,"Recuperar_leyenda")+"...");
348
			btnLoadLegend.addActionListener(loadSaveLegendAction);
349
		}
350
		return btnLoadLegend;
351
	}
353 352

  
354
	 /**
355
	  * This method initializes jTextArea
356
	  *
357
	  * @return javax.swing.JTextArea
358
	  */
359
	 private JTextArea getTitleArea() {
360
		 if (titleArea == null) {
361
			 titleArea = new JTextArea();
362
			 titleArea.setBackground(java.awt.SystemColor.control);
363
			 titleArea.setLineWrap(true);
364
			 titleArea.setRows(0);
365
			 titleArea.setWrapStyleWord(false);
366
			 titleArea.setEditable(false);
367
			 titleArea.setPreferredSize(new java.awt.Dimension(495,40));
368
		 }
369
		 return titleArea;
370
	 }
371
	 /**
372
	  * This method initializes jPanel1
373
	  *
374
	  * @return javax.swing.JPanel
375
	  */
376
	 private JPanel getPreviewPanel() {
377
		 if (preview == null) {
378
			 preview = new JPanel();
379
			 preview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
380
			 preview.setBackground(java.awt.SystemColor.text);
381
			 preview.setLayout(new BorderLayout(5,5));
382
			 preview.add(getIconLabel());
383
			 preview.setPreferredSize(new Dimension(
384
					 getSplitPane().getDividerLocation(),
385
					 130));
386
			 preview.setBackground(Color.white);
387
		 }
388
		 return preview;
389
	 }
353
	/**
354
	 * This method initializes jTextArea
355
	 *
356
	 * @return javax.swing.JTextArea
357
	 */
358
	private JTextArea getTitleArea() {
359
		if (titleArea == null) {
360
			titleArea = new JTextArea();
361
			titleArea.setBackground(java.awt.SystemColor.control);
362
			titleArea.setLineWrap(true);
363
			titleArea.setRows(0);
364
			titleArea.setWrapStyleWord(false);
365
			titleArea.setEditable(false);
366
			titleArea.setPreferredSize(new java.awt.Dimension(495,40));
367
		}
368
		return titleArea;
369
	}
370
	/**
371
	 * This method initializes jPanel1
372
	 *
373
	 * @return javax.swing.JPanel
374
	 */
375
	private JPanel getPreviewPanel() {
376
		if (preview == null) {
377
			preview = new JPanel();
378
			preview.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
379
			preview.setBackground(java.awt.SystemColor.text);
380
			preview.setLayout(new BorderLayout(5,5));
381
			preview.add(getIconLabel());
382
			preview.setPreferredSize(new Dimension(
383
					getSplitPane().getDividerLocation(),
384
					130));
385
			preview.setBackground(Color.white);
386
		}
387
		return preview;
388
	}
390 389

  
391
	 private JLabel getIconLabel() {
392
		 if (iconLabel == null) {
390
	private JLabel getIconLabel() {
391
		if (iconLabel == null) {
393 392
			iconLabel = new JLabel();
394 393
			iconLabel.setVerticalAlignment(JLabel.CENTER);
395 394
			iconLabel.setHorizontalAlignment(JLabel.CENTER);
......
399 398
	}
400 399

  
401 400
	/**
402
	  * This method initializes jScrollPane
403
	  *
404
	  * @return javax.swing.JScrollPane
405
	  */
406
	 private JScrollPane getLegendTreeScrollPane() {
407
		 if (legendTreeScrollPane == null) {
408
			 legendTreeScrollPane = new JScrollPane();
409
			 legendTreeScrollPane.setViewportView(getJTreeLegends());
410
		 }
411
		 return legendTreeScrollPane;
412
	 }
401
	 * This method initializes jScrollPane
402
	 *
403
	 * @return javax.swing.JScrollPane
404
	 */
405
	private JScrollPane getLegendTreeScrollPane() {
406
		if (legendTreeScrollPane == null) {
407
			legendTreeScrollPane = new JScrollPane();
408
			legendTreeScrollPane.setViewportView(getJTreeLegends());
409
		}
410
		return legendTreeScrollPane;
411
	}
413 412

  
414
	 /**
415
	  * <p>Adds a new fully-featured legend panel to the LegendManager.<br></p>
416
	  *
417
	  * <p><b>CAUTION:</b> Trying to add a child page whose parent hasn't been added yet
418
	  * causes the application to fall in an infinite loop. This is a known
419
	  * bug, sorry. Just avoid this case or try to fix it (lol).<br></p>
420
	  *
421
	  * <p><b>Notice</b> that there is no type check so if you add a non-ILegendPanel class,
422
	  * you'll have a runtime error later.</p>
423
	  * @param page, Class of type ILegendPanel
424
	  */
425
	 public static void addLegendPage(Class iLegendPanelClass) {
426
		 if (!legendPool.contains(iLegendPanelClass)) {
427
			 legendPool.add(iLegendPanelClass);
428
		 }
429
	 }
413
	/**
414
	 * <p>Adds a new fully-featured legend panel to the LegendManager.<br></p>
415
	 *
416
	 * <p><b>CAUTION:</b> Trying to add a child page whose parent hasn't been added yet
417
	 * causes the application to fall in an infinite loop. This is a known
418
	 * bug, sorry. Just avoid this case or try to fix it (lol).<br></p>
419
	 *
420
	 * <p><b>Notice</b> that there is no type check so if you add a non-ILegendPanel class,
421
	 * you'll have a runtime error later.</p>
422
	 * @param page, Class of type ILegendPanel
423
	 */
424
	public static void addLegendPage(Class<? extends ILegendPanel> iLegendPanelClass) {
425
		if (!legendPool.contains(iLegendPanelClass)) {
426
			legendPool.add(iLegendPanelClass);
427
		}
428
	}
430 429

  
431
	 /**
432
	  * Causes the component to be autofilled with the legend pages that
433
	  * were added through the static method addLegendPage(ILegendPanel page)
434
	  */
435
	 private void fillDialog() {
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);
430
	/**
431
	 * Causes the component to be autofilled with the legend pages that
432
	 * were added through the static method addLegendPage(ILegendPanel page)
433
	 */
434
	private void fillDialog() {
435
		if (empty) {
436
			for (int i = 0; i < legendPool.size(); i++) {
437
				Class<?> pageClass = (Class<?>) legendPool.get(i);
438
				ILegendPanel page;
439
				try {
440
					page = (ILegendPanel) pageClass.newInstance();
441
					if (page.isSuitableFor(layer)) {
442
						// this legend can be applied to this layer
443
						pages.put(page.getClass(), page);
445 444

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

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

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

  
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);
479
				 }
471
				} catch (InstantiationException e) {
472
					NotificationManager.addError("Trying to instantiate an interface" +
473
							" or abstract class + "+pageClass.getName(), e);
474
				} catch (IllegalAccessException e) {
475
					NotificationManager.addError("IllegalAccessException: does " +
476
							pageClass.getName()	+ " class have an anonymous" +
477
							" constructor?", e);
478
				}
480 479

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

  
487
	 private DefaultMutableTreeNode findNode(Class searchID) {
488
		 String title;
489
		 try {
490
			 title = ((ILegendPanel) Class.forName(searchID.getName()).newInstance()).getTitle();
491
		 } catch (Exception e) {
492
			 // this should be impossible, but anyway this case will be treat as the node does not
493
			 // exist.
494
			 return null;
495
		 }
486
	@SuppressWarnings("unchecked")
487
	private DefaultMutableTreeNode findNode(Class searchID) {
488
		String title;
489
		try {
490
			title = ((ILegendPanel) Class.forName(searchID.getName()).newInstance()).getTitle();
491
		} catch (Exception e) {
492
			// this should be impossible, but anyway this case will be treat as the node does not
493
			// exist.
494
			return null;
495
		}
496 496

  
497
		 Enumeration e = root.breadthFirstEnumeration();
498
		 while (e.hasMoreElements()) {
499
			 DefaultMutableTreeNode nodeAux = (DefaultMutableTreeNode) e.nextElement();
500
			 if (nodeAux != null) {
501
				 ILegendPanel legend = (ILegendPanel) nodeAux.getUserObject();
502
				 if (legend == null) continue; // Root node
503
				 if (legend.getTitle().equals(title)) {
504
					 return nodeAux;
505
				 }
506
			 }
507
		 }
508
		 return null;
509
	 }
497
		Enumeration e = root.breadthFirstEnumeration();
498
		while (e.hasMoreElements()) {
499
			DefaultMutableTreeNode nodeAux = (DefaultMutableTreeNode) e.nextElement();
500
			if (nodeAux != null) {
501
				ILegendPanel legend = (ILegendPanel) nodeAux.getUserObject();
502
				if (legend == null) continue; // Root node
503
				if (legend.getTitle().equals(title)) {
504
					return nodeAux;
505
				}
506
			}
507
		}
508
		return null;
509
	}
510 510

  
511
	 private void doInsertNode(DefaultTreeModel treeModel, ILegendPanel page) {
512
		 dirtyTree = ((page.getParentClass() != null) && (findNode(page.getParentClass())==null));
513
		 if (findNode(page.getClass()) != null) // It is already added
514
			 return;
515
		 if (page.getParentClass() != null) {
516
			 if (pages.containsKey(page.getParentClass())) {
517
				 ILegendPanel parent = (ILegendPanel) pages.get(page.getParentClass());
518
				 DefaultMutableTreeNode nodeParent = findNode(parent.getClass());
519
				 if (nodeParent == null) {
520
					 // the parent is empty
521
					 // Recursively add it
522
					 doInsertNode(treeModel, parent);
523
				 } else {
524
					 DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page);
525
					 int children = nodeParent.getChildCount();
526
					 int pos=0;
527
					 for (int i = 0; i < children; i++) {
528
						 DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeModel.getChild(nodeParent, i);
529
						 if (node.getUserObject() instanceof ILegendPanel) {
530
							 String pageTitle = ((ILegendPanel) node.getUserObject()).getTitle();
531
							 if (pageTitle.compareTo(page.getTitle()) < 0) ++pos;
532
						 }
533
					 }
534
					 treeModel.insertNodeInto(nodeValue, nodeParent, pos);
535
				 }
536
			 }
537
		 } else {
538
			 // First level node
539
			 DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page);
540
			 int children = root.getChildCount();
541
			 int pos=0;
542
			 for (int i = 0; i < children; i++) {
543
				 DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeModel.getChild(root, i);
544
				 if (node.getUserObject() instanceof ILegendPanel) {
545
					 String pageTitle = ((ILegendPanel) node.getUserObject()).getTitle();
546
					 if (pageTitle.compareTo(page.getTitle()) < 0) ++pos;
547
				 }
548
			 }
549
			 treeModel.insertNodeInto(nodeValue, root, pos);
550
		 }
551
	 }
511
	private void doInsertNode(DefaultTreeModel treeModel, ILegendPanel page) {
512
		dirtyTree = ((page.getParentClass() != null) && (findNode(page.getParentClass())==null));
513
		if (findNode(page.getClass()) != null) // It is already added
514
			return;
515
		if (page.getParentClass() != null) {
516
			if (pages.containsKey(page.getParentClass())) {
517
				ILegendPanel parent = (ILegendPanel) pages.get(page.getParentClass());
518
				DefaultMutableTreeNode nodeParent = findNode(parent.getClass());
519
				if (nodeParent == null) {
520
					// the parent is empty
521
					// Recursively add it
522
					doInsertNode(treeModel, parent);
523
				} else {
524
					DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page);
525
					int children = nodeParent.getChildCount();
526
					int pos=0;
527
					for (int i = 0; i < children; i++) {
528
						DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeModel.getChild(nodeParent, i);
529
						if (node.getUserObject() instanceof ILegendPanel) {
530
							String pageTitle = ((ILegendPanel) node.getUserObject()).getTitle();
531
							if (pageTitle.compareTo(page.getTitle()) < 0) ++pos;
532
						}
533
					}
534
					treeModel.insertNodeInto(nodeValue, nodeParent, pos);
535
				}
536
			}
537
		} else {
538
			// First level node
539
			DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(page);
540
			int children = root.getChildCount();
541
			int pos=0;
542
			for (int i = 0; i < children; i++) {
543
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeModel.getChild(root, i);
544
				if (node.getUserObject() instanceof ILegendPanel) {
545
					String pageTitle = ((ILegendPanel) node.getUserObject()).getTitle();
546
					if (pageTitle.compareTo(page.getTitle()) < 0) ++pos;
547
				}
548
			}
549
			treeModel.insertNodeInto(nodeValue, root, pos);
550
		}
551
	}
552 552

  
553
	 private JScrollPane getTitleScroll() {
554
		 if (jTitleScrollPane == null) {
555
			 jTitleScrollPane = new JScrollPane();
556
			 jTitleScrollPane.setBounds(2, 2, 498, 42);
557
			 jTitleScrollPane.setViewportView(getTitleArea());
558
		 }
559
		 return jTitleScrollPane;
560
	 }
553
	private JScrollPane getTitleScroll() {
554
		if (jTitleScrollPane == null) {
555
			jTitleScrollPane = new JScrollPane();
556
			jTitleScrollPane.setBounds(2, 2, 498, 42);
557
			jTitleScrollPane.setViewportView(getTitleArea());
558
		}
559
		return jTitleScrollPane;
560
	}
561 561

  
562
	 private JTree getJTreeLegends() {
563
		 if (jTreeLegends == null) {
564
			 jTreeLegends = new JTree();
565
			 jTreeLegends.setRootVisible(false);
566
			 MyTreeCellRenderer treeCellRenderer = new MyTreeCellRenderer();
567
			 treeCellRenderer.setOpenIcon(null);
568
			 treeCellRenderer.setClosedIcon(null);
569
			 treeCellRenderer.setLeafIcon(null);
562
	private JTree getJTreeLegends() {
563
		if (jTreeLegends == null) {
564
			jTreeLegends = new JTree();
565
			jTreeLegends.setRootVisible(false);
566
			MyTreeCellRenderer treeCellRenderer = new MyTreeCellRenderer();
567
			treeCellRenderer.setOpenIcon(null);
568
			treeCellRenderer.setClosedIcon(null);
569
			treeCellRenderer.setLeafIcon(null);
570 570

  
571
			 jTreeLegends.setCellRenderer(treeCellRenderer);
572
			 jTreeLegends.setShowsRootHandles(true);
573
			 jTreeLegends.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
574
				 public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
575
					 if (isTreeListenerDisabled) return;
576
					 DefaultMutableTreeNode node = (DefaultMutableTreeNode)
577
					 jTreeLegends.getLastSelectedPathComponent();
571
			jTreeLegends.setCellRenderer(treeCellRenderer);
572
			jTreeLegends.setShowsRootHandles(true);
573
			jTreeLegends.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
574
				public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
575
					if (isTreeListenerDisabled) return;
576
					DefaultMutableTreeNode node = (DefaultMutableTreeNode)
577
					jTreeLegends.getLastSelectedPathComponent();
578 578

  
579
					 if (node == null) return;
580
					 setActivePage((ILegendPanel) node.getUserObject());
581
				 }
582
			 });
583
			 jTreeLegends.putClientProperty("JTree.linestyle", "Angled");
584
			 jTreeLegends.getSelectionModel().
585
			 setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
586
		 }
587
		 return jTreeLegends;
588
	 }
579
					if (node == null) return;
580
					setActivePage((ILegendPanel) node.getUserObject());
581
				}
582
			});
583
			jTreeLegends.putClientProperty("JTree.linestyle", "Angled");
584
			jTreeLegends.getSelectionModel().
585
			setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
586
		}
587
		return jTreeLegends;
588
	}
589 589

  
590
	 private void setActivePage(ILegendPanel page) {
591
		 if (page.getPanel() == null) {
592
			 // this is what happens when the user clicked in a parent node
593
			 // which only acts as a folder, and does not manage any legend
594
			 // then it expands and selects the first child
595
			 DefaultMutableTreeNode node = findNode(page.getClass());
596
			 if (treeModel.getChildCount(node)>0) {
597
				 DefaultMutableTreeNode dmn = (DefaultMutableTreeNode) treeModel.getChild(node, 0);
598
				 page = (ILegendPanel) dmn.getUserObject();
599
				 setActivePage(page);
600
				 expandAndSelect(page);
601
			 }
602
		 } else {
603
			 // show the page
604
			 activePanel = page;
605
			 setIcon(activePanel.getIcon());
590
	private void setActivePage(ILegendPanel page) {
591
		if (page.getPanel() == null) {
592
			// this is what happens when the user clicked in a parent node
593
			// which only acts as a folder, and does not manage any legend
594
			// then it expands and selects the first child
595
			DefaultMutableTreeNode node = findNode(page.getClass());
596
			if (treeModel.getChildCount(node)>0) {
597
				DefaultMutableTreeNode dmn = (DefaultMutableTreeNode) treeModel.getChild(node, 0);
598
				page = (ILegendPanel) dmn.getUserObject();
599
				setActivePage(page);
600
				expandAndSelect(page);
601
			}
602
		} else {
603
			// show the page
604
			activePanel = page;
605
			setIcon(activePanel.getIcon());
606 606

  
607
			 activePanel.setData(layer, legend);
608
			 getTitleArea().setText(activePanel.getDescription());
609
			 jPanelContainer.setViewportView(activePanel.getPanel());
610
		 }
611
	 }
607
			activePanel.setData(layer, legend);
608
			getTitleArea().setText(activePanel.getDescription());
609
			jPanelContainer.setViewportView(activePanel.getPanel());
610
		}
611
	}
612 612

  
613 613

  
614
	 private void setIcon(ImageIcon icon) {
615
		 getIconLabel().setIcon(icon);
614
	private void setIcon(ImageIcon icon) {
615
		getIconLabel().setIcon(icon);
616 616
	}
617 617

  
618 618

  
619 619
	private class MyTreeCellRenderer extends DefaultTreeCellRenderer {
620
		 private static final long serialVersionUID = -6013698992263578041L;
620
		private static final long serialVersionUID = -6013698992263578041L;
621 621

  
622
		 public MyTreeCellRenderer() {}
622
		public MyTreeCellRenderer() {}
623 623

  
624
		 public Component getTreeCellRendererComponent(
625
				 JTree tree,
626
				 Object value,
627
				 boolean sel,
628
				 boolean expanded,
629
				 boolean leaf,
630
				 int row,
631
				 boolean hasFocus) {
624
		public Component getTreeCellRendererComponent(
625
				JTree tree,
626
				Object value,
627
				boolean sel,
628
				boolean expanded,
629
				boolean leaf,
630
				int row,
631
				boolean hasFocus) {
632 632

  
633
			 super.getTreeCellRendererComponent(
634
					 tree, value, sel,
635
					 expanded, leaf, row,
636
					 hasFocus);
637
			 if (value instanceof DefaultMutableTreeNode)
638
			 {
639
				 DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
640
				 if (node.getUserObject() instanceof ILegendPanel)
641
				 {
642
					 ILegendPanel legend = (ILegendPanel) node.getUserObject();
643
					 this.setText(
644
							 legend.getPanel() == null ? "<html><b>"+legend.getTitle()+"</b></html>":legend.getTitle());
645
				 }
646
			 }
647
			 return this;
648
		 }
633
			super.getTreeCellRendererComponent(
634
					tree, value, sel,
635
					expanded, leaf, row,
636
					hasFocus);
637
			if (value instanceof DefaultMutableTreeNode)
638
			{
639
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
640
				if (node.getUserObject() instanceof ILegendPanel)
641
				{
642
					ILegendPanel legend = (ILegendPanel) node.getUserObject();
643
					this.setText(
644
							legend.getPanel() == null ? "<html><b>"+legend.getTitle()+"</b></html>":legend.getTitle());
645
				}
646
			}
647
			return this;
648
		}
649 649

  
650
	 }
650
	}
651 651

  
652
	 private void expandAndSelect(Object node) {
653
		 isTreeListenerDisabled = true;
654
		 // will expand the tree and select the node
655
		 int i = 0;
656
		 boolean exit = false;
652
	private void expandAndSelect(Object node) {
653
		isTreeListenerDisabled = true;
654
		// will expand the tree and select the node
655
		int i = 0;
656
		boolean exit = false;
657 657

  
658
		 TreePath tp = null;
659
		 // find the page in the tree
660
		 while (i<jTreeLegends.getRowCount() && !exit) {
661
			 //see if this row is the node that we are looking for
658
		TreePath tp = null;
659
		// find the page in the tree
660
		while (i<jTreeLegends.getRowCount() && !exit) {
661
			//see if this row is the node that we are looking for
662 662

  
663
			 tp = jTreeLegends.getPathForRow(i);
664
			 Object[] obj = tp.getPath();
665
			 for (int j = 0; j < obj.length && !exit ; j++) {
666
				 Object o = ((DefaultMutableTreeNode) obj[j]).getUserObject();
663
			tp = jTreeLegends.getPathForRow(i);
664
			Object[] obj = tp.getPath();
665
			for (int j = 0; j < obj.length && !exit ; j++) {
666
				Object o = ((DefaultMutableTreeNode) obj[j]).getUserObject();
667 667

  
668
				 if (o!=null	&& o.getClass().equals(node.getClass())	&& o.equals(node)) {
669
					 // found it! collapse the tree
670
					 while (i>=0) {
671
						 jTreeLegends.collapseRow(i);
672
						 i--;
673
					 }
674
					 exit = true;
675
				 }
676
			 }
677
			 jTreeLegends.expandRow(i);
678
			 i++;
679
		 }
668
				if (o!=null	&& o.getClass().equals(node.getClass())	&& o.equals(node)) {
669
					// found it! collapse the tree
670
					while (i>=0) {
671
						jTreeLegends.collapseRow(i);
672
						i--;
673
					}
674
					exit = true;
675
				}
676
			}
677
			jTreeLegends.expandRow(i);
678
			i++;
679
		}
680 680

  
681
		 // expand the tree and set the selection
682
		 if (tp != null) {
683
			 jTreeLegends.expandPath(tp);
684
			 jTreeLegends.setSelectionPath(tp);
685
		 }
686
		 isTreeListenerDisabled = false;
687
	 }
681
		// expand the tree and set the selection
682
		if (tp != null) {
683
			jTreeLegends.expandPath(tp);
684
			jTreeLegends.setSelectionPath(tp);
685
		}
686
		isTreeListenerDisabled = false;
687
	}
688 688

  
689
	 public String getName() {
690
		 return PluginServices.getText(this,"Simbologia");
691
	 }
689
	public String getName() {
690
		return PluginServices.getText(this,"Simbologia");
691
	}
692 692

  
693
	 public void acceptAction() {
694
		 // automatically handled by the ThemeManagerWindow
695
	 }
693
	public void acceptAction() {
694
		// automatically handled by the ThemeManagerWindow
695
	}
696 696

  
697
	 public void cancelAction() {
698
		 // does nothing
699
	 }
697
	public void cancelAction() {
698
		// does nothing
699
	}
700 700

  
701
	 public void applyAction() {
702
		 legend = activePanel.getLegend();
701
	public void applyAction() {
702
		legend = activePanel.getLegend();
703 703

  
704
		 
705
		 if (table != null && table.size() > 1) 
706
			 applyRestOfLegends(table,layer.getMapContext().getLayers());
707
		 
708
		 /* try to apply the legend to all the active layers that
704

  
705
		if (table != null && table.size() > 1) 
706
			applyRestOfLegends(table,layer.getMapContext().getLayers());
707

  
708
		/* try to apply the legend to all the active layers that
709 709
		 can accept it */
710
		 FLayer[] activeLyrs = layer.getMapContext().getLayers().getActives();
711
		 for (int i=0; i < activeLyrs.length; i++) {
712
			 FLayer laux=activeLyrs[i];
710
		FLayer[] activeLyrs = layer.getMapContext().getLayers().getActives();
711
		for (int i=0; i < activeLyrs.length; i++) {
712
			FLayer laux=activeLyrs[i];
713 713

  
714
			 if (activeLyrs[i] instanceof FLayers){
715
				 laux=getFirstActiveLayerVect((FLayers)activeLyrs[i]);
716
			 }
714
			if (activeLyrs[i] instanceof FLayers){
715
				laux=getFirstActiveLayerVect((FLayers)activeLyrs[i]);
716
			}
717 717

  
718
			 if (laux instanceof ClassifiableVectorial) {
719
				 ClassifiableVectorial aux2 = (ClassifiableVectorial) laux;
720
				 try {
721
					 if (legend instanceof IClassifiedVectorLegend) {
722
						 // Es una leyenda que necesita un recordset con un
723
						 // nombre de campo. Comprobamos que ese recordset
724
						 // tiene ese nombre de campo y es del tipo esperado
725
						 IClassifiedVectorLegend cl = (IClassifiedVectorLegend) legend;
718
			if (laux instanceof ClassifiableVectorial) {
719
				ClassifiableVectorial aux2 = (ClassifiableVectorial) laux;
720
				try {
721
					if (legend instanceof IClassifiedVectorLegend) {
722
						// Es una leyenda que necesita un recordset con un
723
						// nombre de campo. Comprobamos que ese recordset
724
						// tiene ese nombre de campo y es del tipo esperado
725
						IClassifiedVectorLegend cl = (IClassifiedVectorLegend) legend;
726 726

  
727
						 if (aux2 instanceof AlphanumericData) {
727
						if (aux2 instanceof AlphanumericData) {
728 728

  
729
							 if (cl.getValues().length==0) {
730
								 JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"no_es_posible_aplicar_leyenda_vacia"));
731
								 return;
732
							 }
729
							if (cl.getValues().length==0) {
730
								JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"no_es_posible_aplicar_leyenda_vacia"));
731
								return;
732
							}
733 733

  
734
							 aux2.setLegend((IVectorLegend) legend);
735
						 }
736
					 }
737
					 else if (legend instanceof SingleSymbolLegend)
738
						 aux2.setLegend((IVectorLegend) legend);
739
				 } catch (LegendLayerException e) {
740
					 NotificationManager.addError(PluginServices.getText(this, "legend_exception"), e);
741
				 }
742
			 }
743
		 }
744
	 }
734
							aux2.setLegend((IVectorLegend) legend);
735
						}
736
					}
737
					else if (legend instanceof SingleSymbolLegend)
738
						aux2.setLegend((IVectorLegend) legend);
739
				} catch (LegendLayerException e) {
740
					NotificationManager.addError(PluginServices.getText(this, "legend_exception"), e);
741
				}
742
			}
743
		}
744
	}
745 745

  
746
	 private void applyRestOfLegends(Hashtable<FLayer, ILegend> table2,FLayers layers) {
746
	private void applyRestOfLegends(Hashtable<FLayer, ILegend> table2,FLayers layers) {
747 747

  
748
		 for(int i = 0; i < layers.getLayersCount(); i++) {
749
			 FLayer my_layer= layers.getLayer(i);
748
		for(int i = 0; i < layers.getLayersCount(); i++) {
749
			FLayer my_layer= layers.getLayer(i);
750 750

  
751
			 if(!(my_layer instanceof FLayers)){
752
				 if(my_layer instanceof ClassifiableVectorial){
753
					 try {
754
						 if(table.containsKey(my_layer)){
755
							 ClassifiableVectorial lyr = (ClassifiableVectorial)my_layer;
756
							 lyr.setLegend((IVectorLegend) table.get(my_layer));
757
						 }
751
			if(!(my_layer instanceof FLayers)){
752
				if(my_layer instanceof ClassifiableVectorial){
753
					try {
754
						if(table.containsKey(my_layer)){
755
							ClassifiableVectorial lyr = (ClassifiableVectorial)my_layer;
756
							lyr.setLegend((IVectorLegend) table.get(my_layer));
757
						}
758 758

  
759
					 } catch (LegendLayerException e) {
760
						 // TODO Auto-generated catch block
761
						 e.printStackTrace();
762
					 }
763
				 }
764
			 }
765
			 else 
766
				 applyRestOfLegends(table,(FLayers) my_layer);	
767
		 }
768
	 }
759
					} catch (LegendLayerException e) {
760
						// TODO Auto-generated catch block
761
						e.printStackTrace();
762
					}
763
				}
764
			}
765
			else 
766
				applyRestOfLegends(table,(FLayers) my_layer);	
767
		}
768
	}
769 769

  
770
	 @Override
771
	 public void setModel(FLayer layer) {
772
		 this.layer = layer;
773
		 applyLegend(((Classifiable) layer).getLegend());
774
	 }
770
	@Override
771
	public void setModel(FLayer layer) {
772
		this.layer = layer;
773
		applyLegend(((Classifiable) layer).getLegend());
774
	}
775 775

  
776
	 /**
777
	  * Applies the legend to the layer.
778
	  * 
779
	  * @param aLegend , legend that the user wants to apply
780
	  */
781
	 private void applyLegend(ILegend aLegend) {
782
		 this.legend = aLegend;
783
		 fillDialog();
784
		 Enumeration en = pages.keys();
785
		 while (en.hasMoreElements()) {
786
			 ILegendPanel page = (ILegendPanel) pages.get(en.nextElement());
787
			 if (legend.getClass().equals(page.getLegendClass())) {
788
				 setActivePage(page);
789
				 expandAndSelect(page);
790
			 }
791
		 }
792
	 }
776
	/**
777
	 * Applies the legend to the layer.
778
	 * 
779
	 * @param aLegend , legend that the user wants to apply
780
	 */
781
	private void applyLegend(ILegend aLegend) {
782
		this.legend = aLegend;
783
		fillDialog();
784
		Enumeration<Class<? extends ILegendPanel>> en = pages.keys();
785
		while (en.hasMoreElements()) {
786
			ILegendPanel page = (ILegendPanel) pages.get(en.nextElement());
787
			if (legend.getClass().equals(page.getLegendClass())) {
788
				setActivePage(page);
789
				expandAndSelect(page);
790
				return;
791
			}
792
		}
793
		NotificationManager.addWarning(
794
				PluginServices.getText(this, 
795
						"caution_no_registered_panel_associated_to_" +
796
						"loaded_legend_the_legend_wont_be_applied"));
797
	}
793 798

  
794
	 public static void addLegendDriver(Class legendDriverClass) {
795
		 if (!legendDriverPool.contains(legendDriverClass)) {
796
			 legendDriverPool.add(legendDriverClass);
797
		 }
798
	 }
799
	public static void addLegendDriver(Class<? extends IFMapLegendDriver> legendDriverClass) {
800
		if (!legendDriverPool.contains(legendDriverClass)) {
801
			legendDriverPool.add(legendDriverClass);
802
		}
803
	}
799 804
}

Also available in: Unified diff