gvsig-raster / org.gvsig.raster.wms / trunk / org.gvsig.raster.wms / org.gvsig.raster.wms.app.wmsclient / src / main / java / org / gvsig / raster / wms / app / wmsclient / gui / panel / LayerTree.java @ 2484
History | View | Annotate | Download (5.13 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
package org.gvsig.raster.wms.app.wmsclient.gui.panel; |
24 |
|
25 |
import java.awt.Component; |
26 |
|
27 |
import javax.swing.JToolTip; |
28 |
import javax.swing.JTree; |
29 |
import javax.swing.ToolTipManager; |
30 |
import javax.swing.tree.DefaultTreeCellRenderer; |
31 |
|
32 |
import org.gvsig.andami.PluginServices; |
33 |
import org.gvsig.gui.beans.controls.MultiLineToolTip; |
34 |
import org.gvsig.raster.wms.io.WMSLayerNode; |
35 |
|
36 |
|
37 |
/**
|
38 |
* LayerTree extends the standard JTree class to allow use custom tooltips.
|
39 |
* It is just JTree containing RemoteLayerNode at its nodes.
|
40 |
*
|
41 |
* @author jaume
|
42 |
*
|
43 |
*/
|
44 |
public class LayerTree extends JTree { |
45 |
public boolean showLayerNames = false; |
46 |
private int count = 0; |
47 |
public LayerTree(){
|
48 |
super();
|
49 |
ToolTipManager.sharedInstance().registerComponent(this); |
50 |
ToolTipManager.sharedInstance().setDismissDelay(60*1000); |
51 |
setCellRenderer(new MyRenderer());
|
52 |
} |
53 |
|
54 |
/**
|
55 |
* Layer tree specific renderer allowing to show multiple line
|
56 |
* tooltips
|
57 |
* @author jaume
|
58 |
*
|
59 |
*/
|
60 |
private class MyRenderer extends DefaultTreeCellRenderer { |
61 |
|
62 |
|
63 |
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { |
64 |
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
|
65 |
if (value instanceof WMSLayerNode){ |
66 |
WMSLayerNode layer = (WMSLayerNode) value; |
67 |
|
68 |
String myLatLonTxt = layer.getLatLonBox();
|
69 |
if (myLatLonTxt == null) |
70 |
myLatLonTxt = "-";
|
71 |
|
72 |
String myAbstract = layer.getAbstract();
|
73 |
|
74 |
if (myAbstract == null) |
75 |
myAbstract = "-";
|
76 |
else
|
77 |
myAbstract = format(myAbstract.trim(), 100);
|
78 |
|
79 |
String text =
|
80 |
PluginServices.getText(this, "abstract") + ":\n" + myAbstract + |
81 |
"\n\n" +
|
82 |
PluginServices.getText(this, "covered_extension") + ":\n" + myLatLonTxt; |
83 |
|
84 |
setToolTipText(text); |
85 |
|
86 |
if (!showLayerNames) {
|
87 |
if (layer.getName() != null || layer.getName() == "") { |
88 |
text = layer.toString(); |
89 |
text = text.substring(text.indexOf(']')+2, text.length()); |
90 |
setText(text); |
91 |
} |
92 |
} |
93 |
|
94 |
// Dimension sz = getPreferredSize();
|
95 |
// sz.setSize((sz.getWidth()+50) - (3*count), sz.getHeight());
|
96 |
// setPreferredSize(sz);
|
97 |
// count++;
|
98 |
|
99 |
} else {
|
100 |
setToolTipText(null);
|
101 |
} |
102 |
return this; |
103 |
} |
104 |
} |
105 |
|
106 |
|
107 |
/**
|
108 |
* Cuts the message text to force its lines to be shorter or equal to
|
109 |
* lineLength.
|
110 |
* @param message, the message.
|
111 |
* @param lineLength, the max line length in number of characters.
|
112 |
* @return the formated message.
|
113 |
*/
|
114 |
public static String format(String message, int lineLength){ |
115 |
if (message.length() <= lineLength) return message; |
116 |
String[] lines = message.split("\n"); |
117 |
String theMessage = ""; |
118 |
for (int i = 0; i < lines.length; i++) { |
119 |
String line = lines[i].trim();
|
120 |
if (line.length()<lineLength)
|
121 |
theMessage += line+"\n";
|
122 |
else {
|
123 |
String[] chunks = line.split(" "); |
124 |
String newLine = ""; |
125 |
for (int j = 0; j < chunks.length; j++) { |
126 |
int currentLength = newLine.length();
|
127 |
chunks[j] = chunks[j].trim(); |
128 |
if (chunks[j].length()==0) |
129 |
continue;
|
130 |
if ((currentLength + chunks[j].length() + " ".length()) <= lineLength) |
131 |
newLine += chunks[j] + " ";
|
132 |
else {
|
133 |
newLine += "\n"+chunks[j]+" "; |
134 |
theMessage += newLine; |
135 |
newLine = "";
|
136 |
} |
137 |
} |
138 |
} |
139 |
} |
140 |
return theMessage;
|
141 |
} |
142 |
|
143 |
public JToolTip createToolTip() { |
144 |
MultiLineToolTip tip = new MultiLineToolTip();
|
145 |
tip.setComponent(this);
|
146 |
return tip;
|
147 |
} |
148 |
} |