Revision 866 org.gvsig.proj/branches/refactor2018/org.gvsig.proj/org.gvsig.proj.swing/org.gvsig.proj.swing.impl/src/main/java/org/gvsig/proj/swing/impl/CrsSelectorController.java

View differences:

CrsSelectorController.java
5 5
import java.beans.PropertyChangeListener;
6 6
import java.text.ParseException;
7 7
import java.util.ArrayList;
8
import java.util.List;
8 9

  
9 10
import javax.swing.Action;
10 11
import javax.swing.Icon;
......
26 27
import org.gvsig.proj.catalog.exception.UnsupportedCoordinateReferenceSystemException;
27 28
import org.gvsig.proj.catalog.extent.GeographicBoundingBox;
28 29
import org.gvsig.proj.swing.CoordinateReferenceSystemSelectorComponent;
29
import org.gvsig.proj.swing.impl.tree.AsyncLoadedGroup;
30 30
import org.gvsig.proj.swing.impl.tree.BranchNode;
31 31
import org.gvsig.proj.swing.impl.tree.CRSSearchFilter;
32 32
import org.gvsig.proj.swing.impl.tree.CrsLoader;
33 33
import org.gvsig.proj.swing.impl.tree.CrsTreeNode;
34
import org.gvsig.proj.swing.impl.tree.DirectGroupLoader;
34 35
import org.gvsig.proj.swing.impl.tree.GroupLoader;
36
import org.gvsig.proj.swing.impl.tree.AsyncGroupLoader;
35 37
import org.gvsig.proj.swing.impl.tree.LeafNode;
38
import org.gvsig.proj.swing.impl.tree.LoadedGroup;
36 39
import org.gvsig.proj.swing.impl.tree.LoadingNode;
37 40
import org.gvsig.proj.swing.impl.tree.TreeContainer;
38 41
import org.gvsig.tools.ToolsLocator;
......
47 50

  
48 51
public class CrsSelectorController extends CrsSelectorView
49 52
	implements CoordinateReferenceSystemSelectorComponent, 
50
												Component, TreeSelectionListener,
53
												Component,
51 54
												TreeContainer {
55
	private static final long serialVersionUID = -5281166703625751190L;
52 56
	private CoordinateReferenceSystem selectedCrs = null;
53 57
	private ActionListenerSupport listenerSupport = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
54 58
	public static final String CRS_SELECTED_ACTION_COMMAND = "CrsSelected";
55 59
	protected ImageIcon RECENT_ICON;
56 60
	protected ImageIcon FAVOURITE_ICON;
57
	public ImageIcon FOLDER_OPEN_ICON;
58
	public ImageIcon FOLDER_CLOSED_ICON;
59
	public ImageIcon CRS_ICON;
60
	public ImageIcon LOADING_ICON;
61
	private ImageIcon CRS_ICON;
62
	private ImageIcon FOLDER_OPEN_ICON;
63
	private ImageIcon FOLDER_CLOSED_ICON;
61 64
	private static Logger logger = LoggerFactory.getLogger(CrsSelectorController.class);
62 65
	protected DefaultTreeModel model;
63 66
	protected BranchNode root;
64
	protected RecentCRSs recent;
67
	protected LoadedGroup recent;
68
	protected LoadedGroup favorite;
69
	protected BranchNode customNode; 
65 70
	protected IconTheme iconTheme = null;
66 71
	protected I18nManager i18nManager = null;
67 72
	final protected DefaultCoordinateReferenceSystemSwingManager manager;
73
	protected String customLabel = "Layer";
74
	protected ArrayList<CRSDefinition> customList = new ArrayList<CRSDefinition>();
68 75

  
69 76
	
70 77
	public CrsSelectorController(DefaultCoordinateReferenceSystemSwingManager manager) {
......
73 80
		initIcons();
74 81
		initComponents();
75 82
		initTree();
83
		initTreeListeners();
76 84
		resetSearchCombo(null);
77 85
		initSearchActions();
78 86
	}
......
98 106
	
99 107
	
100 108
	protected void initComponents() {
109
		IconTheme theme = getIconTheme();
110

  
111
		this.btnSearch.setIcon(theme.get("crs-crsselector-search"));
112
		this.btnSearchRemove.setIcon(theme.get("crs-crsselector-search-remove"));
113
		this.lblFilterAlpha.setIcon(theme.get("crs-crsselector-text-filter"));
114
		this.lblFilterSpatial.setIcon(theme.get("crs-crsselector-spatial-filter"));
115
		this.btnCrsAdd.setIcon(theme.get("crs-crsselector-crs-new"));
116
		this.btnRecentCrsRemove.setIcon(theme.get("crs-crsselector-recent-remove"));
117
		this.btnFavoritesAdd.setIcon(theme.get("crs-crsselector-favorite-add"));
118
		this.btnFavoritesRemove.setIcon(theme.get("crs-crsselector-favorite-remove"));
119
		this.btnFilterDropdown.setIcon(theme.get("crs-crsselector-dropdown"));
120
		
121
		
101 122
		ToolsSwingManager swingManager = ToolsSwingLocator.getToolsSwingManager();
102 123
		// hide spatial filter since it is currently not supported
103 124
		this.btnFilterDropdown.setVisible(false);
104 125
		this.pnl_filterType.setVisible(false);
105 126
		this.cboFilterSpatial.setVisible(false);
127
		// hide recent remove since it is currently not supported
128
		this.btnRecentCrsRemove.setVisible(false);
106 129

  
107 130
		swingManager.translate(this.btnSearch);
108 131
		swingManager.translate(this.btnSearchRemove);
......
117 140
		this.txtDescription.setLineWrap(true);
118 141
		this.txtDescription.setWrapStyleWord(true);
119 142
		this.txtCrsWkt.setEditable(false);
120
		this.txtCrsWkt.setText("");		
143
		this.txtCrsWkt.setText("");
144
		
121 145
		/**
122 146
		 * TODO Update strings in CrsPanel and change the name of some components
123 147
		 *
......
135 159
		this.txtCrsWkt.setEditable(false);
136 160
		this.txtCrsWkt.setText("");
137 161
		*/
162
		
163

  
164
		
138 165
	}
166

  
167
	protected void initTreeListeners() {
168
		this.btnFavoritesAdd.addActionListener(new ActionListener() {
169
			@Override
170
			public void actionPerformed(ActionEvent e) {
171
				CoordinateReferenceSystem selectedCrs = CrsSelectorController.this.getCoordinateReferenceSystem();
172
				if (selectedCrs != null) {
173
					manager.getCoordinateReferenceSystemFavorites().add(selectedCrs.getDefinition());
174
					favorite.reloadChildren();
175
				}
176
			}
177
		});
178
		
179
		this.btnFavoritesRemove.addActionListener(new ActionListener() {
180
			@Override
181
			public void actionPerformed(ActionEvent e) {
182
				CoordinateReferenceSystem selectedCrs = CrsSelectorController.this.getCoordinateReferenceSystem();
183
				if (selectedCrs != null) {
184
					manager.getCoordinateReferenceSystemFavorites().remove(selectedCrs.getDefinition());
185
					favorite.reloadChildren();
186
				}
187
			}
188
		});
189
		
190
		/*
191
		this.btnRecentCrsRemove.addActionListener(new ActionListener() {
192
			@Override
193
			public void actionPerformed(ActionEvent e) {
194
				CoordinateReferenceSystem selectedCrs = CrsSelectorController.this.getCoordinateReferenceSystem();
195
				if (selectedCrs != null) {
196
					manager.getCoordinateReferenceSystemHistory().remove(selectedCrs.getDefinition());
197
				}
198
			}
199
		});*/
200
	}
139 201
	
140 202
	protected void search() {
141 203
		String filter = getAlphanumericFilter();
......
150 212
		}
151 213
		resetSearchCombo(filter);
152 214
		model.reload();
153
	    expandBranches();
215
	    expandBranches(this.root);
154 216
	}
155
	protected void expandBranches() {
156
		expandBranches(this.root);
157
		
158
	}
159 217
	
160 218
	private void expandBranches(TreeNode root) {
161 219
		for (int i=0; i<root.getChildCount(); i++) {
......
250 308
	}
251 309
	
252 310
	protected void initIcons() {
253
		RECENT_ICON = getIconTheme().get("recent");
254
		FAVOURITE_ICON = getIconTheme().get("favourite");
255
		FOLDER_OPEN_ICON = getIconTheme().get("folder_open");
256
		FOLDER_CLOSED_ICON = getIconTheme().get("folder_closed");
257
		CRS_ICON = getIconTheme().get("crs");
258
		LOADING_ICON = getIconTheme().get("spinner");
311
		RECENT_ICON = getIconTheme().get("crs-crsselector-recent");
312
		FAVOURITE_ICON = getIconTheme().get("crs-crsselector-favorite");
313
		CRS_ICON = getIconTheme().get("crs-crsselector-crs");
314
		FOLDER_OPEN_ICON = getIconTheme().get("crs-branchnode-tree-group-expanded");
315
		FOLDER_CLOSED_ICON = getIconTheme().get("crs-branchnode-tree-group-collapsed");
259 316
	}
260 317
	
261 318
	protected void initTree() {
......
266 323
		tree.getSelectionModel().setSelectionMode
267 324
        	(TreeSelectionModel.SINGLE_TREE_SELECTION);
268 325
		listenerSupport = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
269
		tree.addTreeSelectionListener(this);
326
		tree.addTreeSelectionListener(new TreeSelectionListener() {
327
			@Override
328
			public void valueChanged(TreeSelectionEvent e) {
329
				CrsSelectorController.this.treeSelectedValueChanged(e);
330
			}
331
		});
270 332
		tree.setCellRenderer(new CellRenderer());
271 333
		
272
		recent = new RecentCRSs(root, getI18nManager().getTranslation("Recent"));
334
		recent = new LoadedGroup(root, this, getI18nManager().getTranslation("Recent"), RECENT_ICON);
335
		recent.setLoader(new RecentLoader(recent));
273 336
		recent.loadChildren();
274
		
275 337
		root.add(recent);
276
		BranchNode epsg = new BranchNode(root, this, getI18nManager().getTranslation("EPSG"));
277
		root.add(epsg);
278
		CRSSearchFilter projectedCRSs = new CRSSearchFilter();
279
		projectedCRSs.setAuthority("EPSG");
280
		projectedCRSs.setIncludedTypes(new CRSType[]{CRSType.ProjectedCRSType});
281
		epsg.add(new AuthorityLoadedGroup(epsg, projectedCRSs, getI18nManager().getTranslation("Projected")));
282 338
		
283
		CRSSearchFilter geographicCRSs = new CRSSearchFilter();
284
		geographicCRSs.setAuthority("EPSG");
285
		geographicCRSs.setIncludedTypes(new CRSType[]{CRSType.GeographicCRSType});
286
		epsg.add(new AuthorityLoadedGroup(epsg, geographicCRSs, getI18nManager().getTranslation("Geographic")));
339
		favorite = new LoadedGroup(root, this, getI18nManager().getTranslation("Favorites"), FAVOURITE_ICON);
340
		favorite.setLoader(new FavoriteLoader(favorite));
341
		favorite.loadChildren();
342
		root.add(favorite);
343
		
344
		BranchNode epsg = null;
345
		for (String authority: manager.getCatalogManager().getAuthorityNames()) {
346
			BranchNode authorityNode = new BranchNode(root, this, getI18nManager().getTranslation(authority));
347
			if (authority.equals("EPSG")) {
348
				epsg = authorityNode;
349
			}
350
			root.add(authorityNode);
351
			CRSSearchFilter projectedCRSs = new CRSSearchFilter();
352
			projectedCRSs.setAuthority(authority);
353
			projectedCRSs.setIncludedTypes(new CRSType[]{CRSType.ProjectedCRSType});
354
			authorityNode.add(new AuthorityLoadedGroup(authorityNode, projectedCRSs, getI18nManager().getTranslation("Projected")));
355
			
356
			CRSSearchFilter geographicCRSs = new CRSSearchFilter();
357
			geographicCRSs.setAuthority(authority);
358
			geographicCRSs.setIncludedTypes(new CRSType[]{CRSType.GeographicCRSType});
359
			authorityNode.add(new AuthorityLoadedGroup(authorityNode, geographicCRSs, getI18nManager().getTranslation("Geographic")));
287 360

  
288
		CRSSearchFilter otherCRSs = new CRSSearchFilter();
289
		otherCRSs.setAuthority("EPSG");
290
		otherCRSs.setExcludedTypes(new CRSType[]{CRSType.ProjectedCRSType, CRSType.GeographicCRSType});
291
		epsg.add(new AuthorityLoadedGroup(epsg, otherCRSs, getI18nManager().getTranslation("Other")));
361
			CRSSearchFilter otherCRSs = new CRSSearchFilter();
362
			otherCRSs.setAuthority(authority);
363
			otherCRSs.setExcludedTypes(new CRSType[]{CRSType.ProjectedCRSType, CRSType.GeographicCRSType});
364
			authorityNode.add(new AuthorityLoadedGroup(authorityNode, otherCRSs, getI18nManager().getTranslation("Other")));
365
		}
366
		
367
		initCustomBranch();
368
		
292 369
		tree.setModel(model);
293
		
294
		tree.expandPath(new TreePath(model.getPathToRoot(epsg)));
370
		if (epsg!=null) {
371
			tree.expandPath(new TreePath(model.getPathToRoot(epsg)));
372
		}
295 373
	}
296 374
	
297
	@Override
298
	public void valueChanged(TreeSelectionEvent e) {
375
	protected void initCustomBranch() {
376
		if (customList.size()>0) {
377
			if (customNode == null) {
378
				customNode = new BranchNode(root, this, customLabel);
379
				root.add(customNode);
380
			}
381
			customNode.clear();
382
			for (CRSDefinition def: customList) {
383
				CrsTreeNode node = new CrsTreeNode(def, customNode);
384
				customNode.add(node);
385
			}
386
			model.reload();
387
		}
388
	}
389
	
390
	protected void treeSelectedValueChanged(TreeSelectionEvent e) {
299 391
		TreeNode selection = (TreeNode) this.treeResults.getLastSelectedPathComponent();
300 392
		if (selection==null) {
301 393
			return;
......
336 428

  
337 429
	@Override
338 430
	public void setCustomGroupLabel(String label) {
339
		// TODO Auto-generated method stub
340
		
431
		this.customLabel = label;
341 432
	}
342 433

  
343 434
	@Override
344 435
	public void addCustomCRS(CRSDefinition customCRS) {
345
		// TODO Auto-generated method stub
346
		
436
		this.customList.add(customCRS);
437
		initCustomBranch();
347 438
	}
348 439

  
349 440
	@Override
......
353 444

  
354 445
	@Override
355 446
	public void addCRSSelectionListener(ActionListener listener) {
356
		// TODO Auto-generated method stub
447
		listenerSupport.addActionListener(listener);
357 448
		
358 449
	}
359 450

  
......
374 465
	}
375 466
	
376 467
	
377
	public class RecentCRSs extends BranchNode {
378
		public RecentCRSs(TreeNode parent, String message) {
379
			super(parent, CrsSelectorController.this, message);
380
		}		
381
		@Override
382
		public Icon getIcon(boolean expanded) {
383
			return RECENT_ICON;
468
	public class RecentLoader extends DirectGroupLoader {
469
		public RecentLoader(LoadedGroup caller) {
470
			super(caller);
384 471
		}
472
		
385 473
		@Override
386
		public void reloadChildren() {
387
			super.children.clear();
388
			// FIXME: maybe history should also use CRSDefinitions
474
		public List<TreeNode> doExecute() {
389 475
			String filter;
390 476
			if (CrsSelectorController.this.getAlphanumericFilter()!=null) {
391 477
				filter = CrsSelectorController.this.getAlphanumericFilter().toLowerCase();
......
393 479
			else {
394 480
				filter = null;
395 481
			}
396
			for (CoordinateReferenceSystem crs: manager.getCoordinateReferenceSystemHistory()) {
397
				if (filter==null || crs.getDefinition().getName().toLowerCase().contains(filter)) {
398
					super.children.add(new CrsTreeNode(crs.getDefinition(), this));
482
			ArrayList<TreeNode> results = new ArrayList<TreeNode>();
483
			for (CRSDefinition crsDef: manager.getCoordinateReferenceSystemHistory()) {
484
				if (filter==null || crsDef.toString().toLowerCase().contains(filter)) {
485
					results.add(new CrsTreeNode(crsDef, caller));
399 486
				}
400 487
			}
488
			return results;
401 489
		}
402 490
	}
403 491
	
404
	public class AuthorityLoadedGroup extends AsyncLoadedGroup {
492
	public class FavoriteLoader extends DirectGroupLoader {
493
	
494
		public FavoriteLoader(LoadedGroup caller) {
495
			super(caller);
496
		}
497
		
498
		@Override
499
		public List<TreeNode> doExecute() {
500
			String filter;
501
			if (CrsSelectorController.this.getAlphanumericFilter()!=null) {
502
				filter = CrsSelectorController.this.getAlphanumericFilter().toLowerCase();
503
			}
504
			else {
505
				filter = null;
506
			}
507
			ArrayList<TreeNode> results = new ArrayList<TreeNode>();
508
			for (CRSDefinition crsDef: manager.getCoordinateReferenceSystemFavorites()) {
509
				if (filter==null || crsDef.toString().toLowerCase().contains(filter)) {
510
					results.add(new CrsTreeNode(crsDef, caller));
511
				}				
512
			}
513
			return results;
514
		}
515
	}
516
	
517
	public class AuthorityLoadedGroup extends LoadedGroup {
405 518
		protected final CRSSearchFilter filter;
519
		public AuthorityLoadedGroup(TreeNode parent, CRSSearchFilter filter, String message, Icon icon) {
520
			super(parent, CrsSelectorController.this, message, icon);
521
			this.filter = filter;
522
		}
523
		
406 524
		public AuthorityLoadedGroup(TreeNode parent, CRSSearchFilter filter, String message) {
407
			super(parent, CrsSelectorController.this, message);
525
			super(parent, CrsSelectorController.this, message, null);
408 526
			this.filter = filter;
409 527
		}
410 528

  
411
		protected GroupLoader doCreateLoader() {
529
		protected GroupLoader getLoader() {
412 530
			this.filter.setTextFilter(CrsSelectorController.this.getAlphanumericFilter());
413 531
			// FIXME: set also spatial filter when supported by API and UI
414 532
			this.filter.setSpatialFilter(null);
415 533
			return new CrsLoader(filter, this, CrsSelectorController.this.manager.getCatalogManager());
416 534
		}
417
		
418
		@Override
419
		public void loaded(ArrayList<TreeNode> loadedData) {
420
			this.children.clear();
421
			this.children.addAll(loadedData);
422
			CrsSelectorController.this.model.reload(this);
423
		}
424

  
425
		@Override
426
		protected void cancelled() {
427
			// TODO Auto-generated method stub
428
		}
429 535
	}
430 536

  
431 537
	public class CellRenderer extends DefaultTreeCellRenderer {
......
434 540
				boolean leaf, int row, boolean hasFocus) {
435 541
			java.awt.Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
436 542
			if (value instanceof BranchNode) {
437
				this.setIcon(((BranchNode)value).getIcon(expanded));
543
				Icon icon = ((BranchNode)value).getIcon(expanded);
544
				if (icon!=null) {
545
					this.setIcon(icon);
546
				}
547
				else {
548
					if (expanded) {
549
						this.setIcon(FOLDER_OPEN_ICON);
550
					}
551
					else {
552
						this.setIcon(FOLDER_CLOSED_ICON);
553
					}
554
				}
438 555
			}
439 556
			else if (value instanceof CrsTreeNode) {
440 557
				this.setIcon(CRS_ICON);
441 558
			}
442 559
			else if (value instanceof LeafNode) {
443
				ImageIcon icon = ((LeafNode)value).getIcon();
560
				Icon icon = ((LeafNode)value).getIcon();
444 561
				if (icon != null) {
445 562
					this.setIcon(icon);
446 563
				}
447 564
			}
448 565
			return this;
449 566
		}
567

  
450 568
		
451 569
	}
452 570
	

Also available in: Unified diff