root / 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 / CrsSelectorComponent.java @ 852
History | View | Annotate | Download (13.2 KB)
1 |
package org.gvsig.proj.swing.impl; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.awt.event.ActionListener; |
5 |
import java.beans.PropertyChangeListener; |
6 |
import java.text.ParseException; |
7 |
import java.util.ArrayList; |
8 |
|
9 |
import javax.swing.Action; |
10 |
import javax.swing.Icon; |
11 |
import javax.swing.ImageIcon; |
12 |
import javax.swing.JComponent; |
13 |
import javax.swing.JTree; |
14 |
import javax.swing.event.TreeSelectionEvent; |
15 |
import javax.swing.event.TreeSelectionListener; |
16 |
import javax.swing.tree.DefaultTreeCellRenderer; |
17 |
import javax.swing.tree.DefaultTreeModel; |
18 |
import javax.swing.tree.TreeNode; |
19 |
import javax.swing.tree.TreePath; |
20 |
import javax.swing.tree.TreeSelectionModel; |
21 |
|
22 |
import org.gvsig.proj.CoordinateReferenceSystem; |
23 |
import org.gvsig.proj.catalog.CRSDefinition; |
24 |
import org.gvsig.proj.catalog.CRSType; |
25 |
import org.gvsig.proj.catalog.exception.UnsupportedCoordinateReferenceSystemException; |
26 |
import org.gvsig.proj.swing.CoordinateReferenceSystemSelectorComponent; |
27 |
import org.gvsig.proj.swing.impl.tree.AsyncLoadedGroup; |
28 |
import org.gvsig.proj.swing.impl.tree.BranchNode; |
29 |
import org.gvsig.proj.swing.impl.tree.CRSSearchFilter; |
30 |
import org.gvsig.proj.swing.impl.tree.CrsLoader; |
31 |
import org.gvsig.proj.swing.impl.tree.CrsTreeNode; |
32 |
import org.gvsig.proj.swing.impl.tree.GroupLoader; |
33 |
import org.gvsig.proj.swing.impl.tree.LeafNode; |
34 |
import org.gvsig.proj.swing.impl.tree.LoadingNode; |
35 |
import org.gvsig.proj.swing.impl.tree.TreeContainer; |
36 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
37 |
import org.gvsig.tools.swing.api.Component; |
38 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
39 |
import org.slf4j.Logger; |
40 |
import org.slf4j.LoggerFactory; |
41 |
|
42 |
public class CrsSelectorComponent extends CrsPanel |
43 |
implements CoordinateReferenceSystemSelectorComponent,
|
44 |
Component, TreeSelectionListener, |
45 |
TreeContainer { |
46 |
private CoordinateReferenceSystem selectedCrs = null; |
47 |
private ActionListenerSupport listenerSupport = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport();
|
48 |
public static final String CRS_SELECTED_ACTION_COMMAND = "CrsSelected"; |
49 |
protected ImageIcon RECENT_ICON; |
50 |
protected ImageIcon FAVOURITE_ICON; |
51 |
public ImageIcon FOLDER_OPEN_ICON; |
52 |
public ImageIcon FOLDER_CLOSED_ICON; |
53 |
public ImageIcon CRS_ICON; |
54 |
public ImageIcon LOADING_ICON; |
55 |
private static Logger logger = LoggerFactory.getLogger(CrsSelectorComponent.class); |
56 |
protected DefaultTreeModel model; |
57 |
protected BranchNode root;
|
58 |
protected RecentCRSs recent;
|
59 |
|
60 |
public CrsSelectorComponent(DefaultCoordinateReferenceSystemSwingManager manager) {
|
61 |
super(manager);
|
62 |
initIcons(); |
63 |
initComponents(); |
64 |
initTree(); |
65 |
resetSearchCombo(null);
|
66 |
initSearchActions(); |
67 |
} |
68 |
|
69 |
@Override
|
70 |
public ImageIcon loadImage( String imageName ) { |
71 |
if (imageName.toLowerCase().endsWith(".png") || imageName.toLowerCase().endsWith(".gif")) { |
72 |
return this.manager.getIconTheme().get(imageName.substring(0, (imageName.length()-4))); |
73 |
} |
74 |
return this.manager.getIconTheme().get(imageName); |
75 |
} |
76 |
|
77 |
protected void initComponents() { |
78 |
// hide spatial filter since it is currently not supported
|
79 |
this.btnFilterDropdown.setVisible(false); |
80 |
this.pnl_filterType.setVisible(false); |
81 |
this.cboFilterSpatial.setVisible(false); |
82 |
this.btnSearch.setToolTipText(manager.getTranslation("Search")); |
83 |
this.btnSearchRemove.setToolTipText(manager.getTranslation("Clear search")); |
84 |
this.btnFavoritesAdd.setToolTipText(manager.getTranslation("Add selected CRS to favorites")); |
85 |
this.btnFavoritesAdd1.setToolTipText(manager.getTranslation("Remove selected CRS from favorites")); |
86 |
this.btnCrsAdd.setToolTipText(manager.getTranslation("Create a new CRS")); |
87 |
this.btnFavoritesAdd2.setToolTipText(manager.getTranslation("Remove selected CRS from recents")); |
88 |
this.lbl_currentCrs.setText(manager.getTranslation("Current coordinate system:")); |
89 |
this.jtxt_description.setEditable(false); |
90 |
this.jtxt_description.setText(""); |
91 |
this.jtxt_description.setLineWrap(true); |
92 |
this.jtxt_description.setWrapStyleWord(true); |
93 |
this.txtCrsWkt.setEditable(false); |
94 |
this.txtCrsWkt.setText(""); |
95 |
} |
96 |
|
97 |
protected void search() { |
98 |
String filter = getAlphanumericFilter();
|
99 |
//this.cboFilterAlpha.removei
|
100 |
//this.cboFilterAlpha.insertItemAt(filter, 0);
|
101 |
if (filter != null && !filter.equals("")) { |
102 |
//this.cboFilterAlpha.addItem(filter);
|
103 |
manager.getCoordinateReferenceSystemTextFilterHistory().add(filter); |
104 |
} |
105 |
for (int i=0; i<root.getChildCount(); i++) { |
106 |
TreeNode node = root.getChildAt(i);
|
107 |
if (node instanceof BranchNode) { |
108 |
((BranchNode)node).reloadChildren(); |
109 |
} |
110 |
} |
111 |
resetSearchCombo(filter); |
112 |
model.reload(); |
113 |
expandBranches(); |
114 |
} |
115 |
protected void expandBranches() { |
116 |
expandBranches(this.root);
|
117 |
|
118 |
} |
119 |
|
120 |
private void expandBranches(TreeNode root) { |
121 |
for (int i=0; i<root.getChildCount(); i++) { |
122 |
TreeNode node = root.getChildAt(i);
|
123 |
if (node instanceof BranchNode) { |
124 |
TreeNode[] path = this.model.getPathToRoot(node); |
125 |
if (path != null) { |
126 |
this.treeResults.expandPath(new TreePath(path)); |
127 |
} |
128 |
expandBranches(node); |
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
protected void resetSearchCombo(String selectedItem) { |
134 |
this.cboFilterAlpha.removeAllItems();
|
135 |
if ("".equals(selectedItem)) { |
136 |
this.cboFilterAlpha.insertItemAt(selectedItem, 0); |
137 |
} |
138 |
for (String filter: manager.getCoordinateReferenceSystemTextFilterHistory()) { |
139 |
this.cboFilterAlpha.addItem(filter);
|
140 |
} |
141 |
this.cboFilterAlpha.setSelectedItem(selectedItem);
|
142 |
} |
143 |
|
144 |
protected void initSearchActions() { |
145 |
this.cboFilterAlpha.getActionMap().put("enterPressed", new Action() { |
146 |
|
147 |
@Override
|
148 |
public void actionPerformed(ActionEvent e) { |
149 |
search(); |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public void setEnabled(boolean b) { |
154 |
// TODO Auto-generated method stub
|
155 |
|
156 |
} |
157 |
|
158 |
@Override
|
159 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
160 |
// TODO Auto-generated method stub
|
161 |
|
162 |
} |
163 |
|
164 |
@Override
|
165 |
public void putValue(String key, Object value) { |
166 |
// TODO Auto-generated method stub
|
167 |
|
168 |
} |
169 |
|
170 |
@Override
|
171 |
public boolean isEnabled() { |
172 |
return true; |
173 |
} |
174 |
|
175 |
@Override
|
176 |
public Object getValue(String key) { |
177 |
// TODO Auto-generated method stub
|
178 |
return null; |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
183 |
// TODO Auto-generated method stub
|
184 |
|
185 |
} |
186 |
}); |
187 |
|
188 |
this.btnSearch.addActionListener(new ActionListener() { |
189 |
|
190 |
@Override
|
191 |
public void actionPerformed(ActionEvent e) { |
192 |
search(); |
193 |
|
194 |
} |
195 |
}); |
196 |
|
197 |
this.btnSearchRemove.addActionListener(new ActionListener() { |
198 |
|
199 |
@Override
|
200 |
public void actionPerformed(ActionEvent e) { |
201 |
resetSearchCombo("");
|
202 |
search(); |
203 |
} |
204 |
}); |
205 |
|
206 |
} |
207 |
|
208 |
protected void reloadBranchNodes() { |
209 |
|
210 |
} |
211 |
|
212 |
protected void initIcons() { |
213 |
RECENT_ICON = manager.getIconTheme().get("recent");
|
214 |
FAVOURITE_ICON = manager.getIconTheme().get("favourite");
|
215 |
FOLDER_OPEN_ICON = manager.getIconTheme().get("folder_open");
|
216 |
FOLDER_CLOSED_ICON = manager.getIconTheme().get("folder_closed");
|
217 |
CRS_ICON = manager.getIconTheme().get("crs");
|
218 |
LOADING_ICON = manager.getIconTheme().get("spinner");
|
219 |
} |
220 |
|
221 |
protected void initTree() { |
222 |
root = new BranchNode(null, this, "root"); |
223 |
model = new DefaultTreeModel(root, false); |
224 |
|
225 |
JTree tree = this.treeResults; |
226 |
|
227 |
|
228 |
tree.getSelectionModel().setSelectionMode |
229 |
(TreeSelectionModel.SINGLE_TREE_SELECTION);
|
230 |
listenerSupport = ToolsSwingLocator.getToolsSwingManager().createActionListenerSupport(); |
231 |
tree.addTreeSelectionListener(this);
|
232 |
tree.setCellRenderer(new CellRenderer());
|
233 |
|
234 |
recent = new RecentCRSs(root, manager.getTranslation("Recent")); |
235 |
recent.loadChildren(); |
236 |
|
237 |
root.add(recent); |
238 |
BranchNode epsg = new BranchNode(root, this, manager.getTranslation("EPSG")); |
239 |
root.add(epsg); |
240 |
CRSSearchFilter projectedCRSs = new CRSSearchFilter();
|
241 |
projectedCRSs.setAuthority("EPSG");
|
242 |
projectedCRSs.setIncludedTypes(new CRSType[]{CRSType.ProjectedCRSType}); |
243 |
epsg.add(new AuthorityLoadedGroup(epsg, projectedCRSs, manager.getTranslation("Projected"))); |
244 |
|
245 |
CRSSearchFilter geographicCRSs = new CRSSearchFilter();
|
246 |
geographicCRSs.setAuthority("EPSG");
|
247 |
geographicCRSs.setIncludedTypes(new CRSType[]{CRSType.GeographicCRSType}); |
248 |
epsg.add(new AuthorityLoadedGroup(epsg, geographicCRSs, manager.getTranslation("Geographic"))); |
249 |
|
250 |
CRSSearchFilter otherCRSs = new CRSSearchFilter();
|
251 |
otherCRSs.setAuthority("EPSG");
|
252 |
otherCRSs.setExcludedTypes(new CRSType[]{CRSType.ProjectedCRSType, CRSType.GeographicCRSType}); |
253 |
epsg.add(new AuthorityLoadedGroup(epsg, otherCRSs, manager.getTranslation("Other"))); |
254 |
tree.setModel(model); |
255 |
|
256 |
tree.expandPath(new TreePath(model.getPathToRoot(epsg))); |
257 |
} |
258 |
|
259 |
@Override
|
260 |
public void valueChanged(TreeSelectionEvent e) { |
261 |
TreeNode selection = (TreeNode) this.treeResults.getLastSelectedPathComponent(); |
262 |
if (selection==null) { |
263 |
return;
|
264 |
} |
265 |
if (selection instanceof CrsTreeNode) { |
266 |
CRSDefinition def = ((CrsTreeNode)selection).getCRS(); |
267 |
try {
|
268 |
selectedCrs = manager.getCRSManager().getCoordinateReferenceSystem(def); |
269 |
this.jtxt_description.setText(def.getDescription());
|
270 |
this.txtCrsWkt.setText(def.toWKT());
|
271 |
listenerSupport.fireActionEvent(new ActionEvent(selection, ActionEvent.ACTION_FIRST, CRS_SELECTED_ACTION_COMMAND)); |
272 |
} catch (UnsupportedCoordinateReferenceSystemException | ParseException e1) { |
273 |
logger.debug("CRS not supported", e);
|
274 |
} |
275 |
} |
276 |
} |
277 |
|
278 |
@Override
|
279 |
public JComponent asJComponent() { |
280 |
return this; |
281 |
} |
282 |
|
283 |
@Override
|
284 |
public CoordinateReferenceSystem getCoordinateReferenceSystem() {
|
285 |
return selectedCrs;
|
286 |
} |
287 |
|
288 |
@Override
|
289 |
public void setCoordinateReferenceSystem(CoordinateReferenceSystem crs) { |
290 |
selectedCrs = crs; |
291 |
// add the provided node to recent nodes and select it
|
292 |
CrsTreeNode node = new CrsTreeNode(crs.getDefinition(), recent);
|
293 |
recent.add(node); |
294 |
model.reload(recent); |
295 |
this.treeResults.setSelectionPath(new TreePath(model.getPathToRoot(node))); |
296 |
|
297 |
} |
298 |
|
299 |
@Override
|
300 |
public void setCustomGroupLabel(String label) { |
301 |
// TODO Auto-generated method stub
|
302 |
|
303 |
} |
304 |
|
305 |
@Override
|
306 |
public void addCustomCRS(CRSDefinition customCRS) { |
307 |
// TODO Auto-generated method stub
|
308 |
|
309 |
} |
310 |
|
311 |
@Override
|
312 |
public void addCRSSpatialFilter(String label, double minLon, double maxLon, double minLat, double maxLat) { |
313 |
// TODO Auto-generated method stub
|
314 |
} |
315 |
|
316 |
@Override
|
317 |
public void addCRSSelectionListener(ActionListener listener) { |
318 |
// TODO Auto-generated method stub
|
319 |
|
320 |
} |
321 |
|
322 |
@Override
|
323 |
public void enableSpatialFilter(String label) { |
324 |
// TODO Auto-generated method stub
|
325 |
|
326 |
} |
327 |
|
328 |
@Override
|
329 |
public void enableAlphanumericFilter(String searchString) { |
330 |
cboFilterAlpha.addItem(searchString); |
331 |
cboFilterAlpha.setSelectedItem(searchString); |
332 |
} |
333 |
|
334 |
protected String getAlphanumericFilter() { |
335 |
return (String) cboFilterAlpha.getSelectedItem(); |
336 |
} |
337 |
|
338 |
|
339 |
public class RecentCRSs extends BranchNode { |
340 |
public RecentCRSs(TreeNode parent, String message) { |
341 |
super(parent, CrsSelectorComponent.this, message);
|
342 |
} |
343 |
@Override
|
344 |
public Icon getIcon(boolean expanded) { |
345 |
return RECENT_ICON;
|
346 |
} |
347 |
@Override
|
348 |
public void reloadChildren() { |
349 |
super.children.clear();
|
350 |
// FIXME: maybe history should also use CRSDefinitions
|
351 |
String filter;
|
352 |
if (CrsSelectorComponent.this.getAlphanumericFilter()!=null) { |
353 |
filter = CrsSelectorComponent.this.getAlphanumericFilter().toLowerCase(); |
354 |
} |
355 |
else {
|
356 |
filter = null;
|
357 |
} |
358 |
for (CoordinateReferenceSystem crs: manager.getCoordinateReferenceSystemHistory()) {
|
359 |
if (filter==null || crs.getDefinition().getName().toLowerCase().contains(filter)) { |
360 |
super.children.add(new CrsTreeNode(crs.getDefinition(), this)); |
361 |
} |
362 |
} |
363 |
} |
364 |
} |
365 |
|
366 |
public class AuthorityLoadedGroup extends AsyncLoadedGroup { |
367 |
protected final CRSSearchFilter filter; |
368 |
public AuthorityLoadedGroup(TreeNode parent, CRSSearchFilter filter, String message) { |
369 |
super(parent, CrsSelectorComponent.this, message);
|
370 |
this.filter = filter;
|
371 |
} |
372 |
|
373 |
protected GroupLoader doCreateLoader() {
|
374 |
this.filter.setTextFilter(CrsSelectorComponent.this.getAlphanumericFilter());
|
375 |
// FIXME: set also spatial filter when supported by API and UI
|
376 |
this.filter.setSpatialFilter(null); |
377 |
return new CrsLoader(filter, this, CrsSelectorComponent.this.manager.getCatalogManager()); |
378 |
} |
379 |
|
380 |
@Override
|
381 |
public void loaded(ArrayList<TreeNode> loadedData) { |
382 |
this.children.clear();
|
383 |
this.children.addAll(loadedData);
|
384 |
CrsSelectorComponent.this.model.reload(this);
|
385 |
} |
386 |
|
387 |
@Override
|
388 |
protected void cancelled() { |
389 |
// TODO Auto-generated method stub
|
390 |
} |
391 |
} |
392 |
|
393 |
public class CellRenderer extends DefaultTreeCellRenderer { |
394 |
@Override
|
395 |
public java.awt.Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, |
396 |
boolean leaf, int row, boolean hasFocus) { |
397 |
java.awt.Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
|
398 |
if (value instanceof BranchNode) { |
399 |
this.setIcon(((BranchNode)value).getIcon(expanded));
|
400 |
} |
401 |
else if (value instanceof CrsTreeNode) { |
402 |
this.setIcon(CRS_ICON);
|
403 |
} |
404 |
else if (value instanceof LeafNode) { |
405 |
ImageIcon icon = ((LeafNode)value).getIcon();
|
406 |
if (icon != null) { |
407 |
this.setIcon(icon);
|
408 |
} |
409 |
} |
410 |
return this; |
411 |
} |
412 |
|
413 |
} |
414 |
|
415 |
public DefaultCoordinateReferenceSystemSwingManager getManager() {
|
416 |
return manager;
|
417 |
} |
418 |
|
419 |
public DefaultTreeModel getModel() { |
420 |
return this.model; |
421 |
} |
422 |
|
423 |
public LoadingNode createLoadingNode(BranchNode parent) {
|
424 |
return new LoadingNode(parent, |
425 |
this,
|
426 |
this.manager.getTranslation("Loading...")); |
427 |
} |
428 |
|
429 |
|
430 |
@Override
|
431 |
public JTree getTree() { |
432 |
return this.treeResults; |
433 |
} |
434 |
|
435 |
|
436 |
} |