Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extWMS / src / com / iver / cit / gvsig / gui / panels / LayerTree.java @ 3746

History | View | Annotate | Download (5.01 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
/* CVS MESSAGES:
43
*
44
* $Id: LayerTree.java 3746 2006-01-24 14:40:18Z jaume $
45
* $Log$
46
* Revision 1.2  2006-01-24 14:36:33  jaume
47
* This is the new version
48
*
49
* Revision 1.1.2.1  2006/01/17 12:56:03  jaume
50
* *** empty log message ***
51
*
52
*
53
*/
54
/**
55
 * 
56
 */
57
package com.iver.cit.gvsig.gui.panels;
58

    
59
import java.awt.Component;
60

    
61
import javax.swing.JToolTip;
62
import javax.swing.JTree;
63
import javax.swing.ToolTipManager;
64
import javax.swing.tree.DefaultTreeCellRenderer;
65

    
66
import com.iver.andami.PluginServices;
67
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode;
68
import com.iver.cit.gvsig.gui.beans.controls.MultiLineToolTip;
69

    
70
/**
71
 * LayerTree extends the standard JTree class to allow use custom tooltips.
72
 * It is just JTree containing WMSLayerNode at its nodes.
73
 * 
74
 * @author jaume
75
 *
76
 */
77
public class LayerTree extends JTree {
78

    
79
    public LayerTree(){
80
        super();
81
        ToolTipManager.sharedInstance().registerComponent(this);
82
        ToolTipManager.sharedInstance().setDismissDelay(60*1000);
83
        setCellRenderer(new MyRenderer());
84
    }
85

    
86
    /**
87
     * Layer tree specific renderer allowing to show multiple line
88
     * tooltips 
89
     * @author jaume
90
     *
91
     */
92
    private class MyRenderer extends DefaultTreeCellRenderer {
93
       
94
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
95
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
96
            if (value instanceof WMSLayerNode){
97
                WMSLayerNode layer = (WMSLayerNode) value;
98
                
99
                String myLatLonTxt = layer.getLatLonBox();
100
                if (myLatLonTxt == null)
101
                    myLatLonTxt = "-";
102
                String myAbstract = layer.getAbstract();
103
                if (myAbstract == null)
104
                    myAbstract = "-";
105
                else 
106
                    myAbstract = format(myAbstract.trim(), 100);
107
                String text =
108
                    PluginServices.getText(this, "abstract") + ":\n" + myAbstract +
109
                    "\n\n" +
110
                    PluginServices.getText(this, "covered_extension") + ":\n" + myLatLonTxt;
111
                
112
                setToolTipText(text);
113
            } else {
114
                setToolTipText(null);
115
            }
116
            return this;
117
        }
118
    }
119
    
120
    
121
    /**
122
     * Cuts the message text to force its lines to be shorter or equal to 
123
     * lineLength.
124
     * @param message, the message.
125
     * @param lineLength, the max line length in number of characters.
126
     * @return the formated message.
127
     */
128
    public static String format(String message, int lineLength){
129
        if (message.length() <= lineLength) return message;
130
        String[] lines = message.split("\n");
131
        String theMessage = "";
132
        for (int i = 0; i < lines.length; i++) {
133
            String line = lines[i].trim();
134
            if (line.length()<lineLength)
135
                theMessage += line+"\n";
136
            else {
137
                String[] chunks = line.split(" ");
138
                String newLine = "";
139
                for (int j = 0; j < chunks.length; j++) {
140
                    int currentLength = newLine.length();
141
                    chunks[j] = chunks[j].trim();
142
                    if (chunks[j].length()==0)
143
                        continue;
144
                    if ((currentLength + chunks[j].length() + " ".length()) <= lineLength)
145
                        newLine += chunks[j] + " ";
146
                    else {
147
                        newLine += "\n"+chunks[j]+" ";
148
                        theMessage += newLine;
149
                        newLine = "";
150
                    }
151
                }
152
                
153
            }
154
        }
155
        return theMessage;
156
    }
157
    
158
    public JToolTip createToolTip() {
159
        MultiLineToolTip tip = new MultiLineToolTip();
160
        tip.setComponent(this);
161
        return tip;
162
      }
163
}