Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wcs / trunk / org.gvsig.raster.wcs / org.gvsig.raster.wcs.app / org.gvsig.raster.wcs.app.wcsclient / src / main / java / org / gvsig / raster / wcs / app / wcsclient / gui / panel / LayerTree.java @ 418

History | View | Annotate | Download (4.79 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.wcs.app.wcsclient.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.fmap.dal.coverage.store.remote.RemoteWMSLayerNode;
34
import org.gvsig.gui.beans.controls.MultiLineToolTip;
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
        private static final long serialVersionUID = 1L;
46
        public boolean showLayerNames = false;
47
        
48
        public LayerTree(){
49
        super();
50
        ToolTipManager.sharedInstance().registerComponent(this);
51
        ToolTipManager.sharedInstance().setDismissDelay(60*1000);
52
        setCellRenderer(new MyRenderer());
53
    }
54

    
55
    /**
56
     * Layer tree specific renderer allowing to show multiple line
57
     * tooltips 
58
     * @author jaume
59
     *
60
     */
61
        private class MyRenderer extends DefaultTreeCellRenderer {
62
                private static final long serialVersionUID = 1L;
63

    
64
                public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
65
                        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
66
                        if (value instanceof RemoteWMSLayerNode){
67
                                RemoteWMSLayerNode layer = (RemoteWMSLayerNode) value;
68

    
69
                                String myLatLonTxt = layer.getLatLonBox();
70
                                if (myLatLonTxt == null)
71
                                        myLatLonTxt = "-";
72

    
73
                                String myAbstract = layer.getAbstract();
74

    
75
                                if (myAbstract == null)
76
                                        myAbstract = "-";
77
                                else 
78
                                        myAbstract = format(myAbstract.trim(), 100);
79

    
80
                                String text =
81
                                        PluginServices.getText(this, "abstract") + ":\n" + myAbstract +
82
                                        "\n\n" +
83
                                        PluginServices.getText(this, "covered_extension") + ":\n" + myLatLonTxt;
84

    
85
                                setToolTipText(text);
86

    
87
                                if (!showLayerNames) {
88
                                        if (layer.getName() != null || layer.getName() == "") {
89
                                                text = layer.toString();
90
                                                text = text.substring(text.indexOf(']')+2, text.length());
91
                                                setText(text);
92
                                        }
93
                                }
94

    
95
                                //                Dimension sz  = getPreferredSize();
96
                                //                sz.setSize((sz.getWidth()+50) - (3*count), sz.getHeight());
97
                                //                setPreferredSize(sz);
98
                                //                count++;
99

    
100
                        } else {
101
                                setToolTipText(null);
102
                        }
103
                        return this;
104
                }
105
        }
106
    
107
    
108
    /**
109
     * Cuts the message text to force its lines to be shorter or equal to 
110
     * lineLength.
111
     * @param message, the message.
112
     * @param lineLength, the max line length in number of characters.
113
     * @return the formated message.
114
     */
115
    public static String format(String message, int lineLength){
116
        if (message.length() <= lineLength) return message;
117
        String[] lines = message.split("\n");
118
        String theMessage = "";
119
        for (int i = 0; i < lines.length; i++) {
120
            String line = lines[i].trim();
121
            if (line.length()<lineLength)
122
                theMessage += line+"\n";
123
            else {
124
                String[] chunks = line.split(" ");
125
                String newLine = "";
126
                for (int j = 0; j < chunks.length; j++) {
127
                    int currentLength = newLine.length();
128
                    chunks[j] = chunks[j].trim();
129
                    if (chunks[j].length()==0)
130
                        continue;
131
                    if ((currentLength + chunks[j].length() + " ".length()) <= lineLength)
132
                        newLine += chunks[j] + " ";
133
                    else {
134
                        newLine += "\n"+chunks[j]+" ";
135
                        theMessage += newLine;
136
                        newLine = "";
137
                    }
138
                }
139
            }
140
        }
141
        return theMessage;
142
    }
143
    
144
    public JToolTip createToolTip() {
145
            MultiLineToolTip tip = new MultiLineToolTip();
146
            tip.setComponent(this);
147
            return tip;
148
    }
149
}