Statistics
| Revision:

svn-gvsig-desktop / branches / v05 / extensions / extWMS / src / com / iver / cit / gvsig / gui / panels / LayerTree.java @ 3855

History | View | Annotate | Download (5.21 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 3855 2006-01-31 16:25:24Z jaume $
45
* $Log$
46
* Revision 1.2.2.3  2006-01-31 16:25:24  jaume
47
* correcciones de bugs
48
*
49
* Revision 1.3  2006/01/26 16:07:14  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.2.2.1  2006/01/26 12:59:32  jaume
53
* 0.5
54
*
55
* Revision 1.2  2006/01/24 14:36:33  jaume
56
* This is the new version
57
*
58
* Revision 1.1.2.1  2006/01/17 12:56:03  jaume
59
* *** empty log message ***
60
*
61
*
62
*/
63
/**
64
 * 
65
 */
66
package com.iver.cit.gvsig.gui.panels;
67

    
68
import java.awt.Component;
69

    
70
import javax.swing.JToolTip;
71
import javax.swing.JTree;
72
import javax.swing.ToolTipManager;
73
import javax.swing.tree.DefaultTreeCellRenderer;
74

    
75
import com.iver.andami.PluginServices;
76
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode;
77
import com.iver.cit.gvsig.gui.beans.controls.MultiLineToolTip;
78

    
79
/**
80
 * LayerTree extends the standard JTree class to allow use custom tooltips.
81
 * It is just JTree containing WMSLayerNode at its nodes.
82
 * 
83
 * @author jaume
84
 *
85
 */
86
public class LayerTree extends JTree {
87

    
88
    public LayerTree(){
89
        super();
90
        ToolTipManager.sharedInstance().registerComponent(this);
91
        ToolTipManager.sharedInstance().setDismissDelay(60*1000);
92
        setCellRenderer(new MyRenderer());
93
    }
94

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