Statistics
| Revision:

root / branches / gvSIG_WMSv2 / extensions / extWMS / src / com / iver / cit / gvsig / gui / panels / LayerTree.java @ 3655

History | View | Annotate | Download (4.94 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 3655 2006-01-17 12:59:53Z jaume $
45
* $Log$
46
* Revision 1.1.2.1  2006-01-17 12:56:03  jaume
47
* *** empty log message ***
48
*
49
*
50
*/
51
/**
52
 * 
53
 */
54
package com.iver.cit.gvsig.gui.panels;
55

    
56
import java.awt.Component;
57

    
58
import javax.swing.JToolTip;
59
import javax.swing.JTree;
60
import javax.swing.ToolTipManager;
61
import javax.swing.tree.DefaultTreeCellRenderer;
62

    
63
import com.iver.andami.PluginServices;
64
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode;
65
import com.iver.cit.gvsig.gui.beans.controls.MultiLineToolTip;
66

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

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

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