Statistics
| Revision:

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

History | View | Annotate | Download (6.25 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 4433 2006-03-15 11:01:17Z jaume $
45
* $Log$
46
* Revision 1.2.2.6  2006-03-15 10:59:25  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.2.2.5  2006/03/03 08:29:22  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.4  2006/02/28 15:25:14  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.2.2.4  2006/02/17 12:57:34  jaume
56
* oculta/eXconde los nombres de las capas y adem?s corrige el error de selecci?n de varios styles si hay alguna capa seleccionada repetida
57
*
58
* Revision 1.2.2.3  2006/01/31 16:25:24  jaume
59
* correcciones de bugs
60
*
61
* Revision 1.3  2006/01/26 16:07:14  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.2.2.1  2006/01/26 12:59:32  jaume
65
* 0.5
66
*
67
* Revision 1.2  2006/01/24 14:36:33  jaume
68
* This is the new version
69
*
70
* Revision 1.1.2.1  2006/01/17 12:56:03  jaume
71
* *** empty log message ***
72
*
73
*
74
*/
75
/**
76
 * 
77
 */
78
package com.iver.cit.gvsig.gui.panels;
79

    
80
import java.awt.Component;
81

    
82
import javax.swing.JToolTip;
83
import javax.swing.JTree;
84
import javax.swing.ToolTipManager;
85
import javax.swing.tree.DefaultTreeCellRenderer;
86

    
87
import com.iver.andami.PluginServices;
88
import com.iver.cit.gvsig.fmap.layers.WMSLayerNode;
89
import com.iver.cit.gvsig.gui.beans.controls.MultiLineToolTip;
90

    
91
/**
92
 * LayerTree extends the standard JTree class to allow use custom tooltips.
93
 * It is just JTree containing WMSLayerNode at its nodes.
94
 * 
95
 * @author jaume
96
 *
97
 */
98
public class LayerTree extends JTree {
99
        public boolean showLayerNames = false;
100
        private int count = 0;
101
        public LayerTree(){
102
        super();
103
        ToolTipManager.sharedInstance().registerComponent(this);
104
        ToolTipManager.sharedInstance().setDismissDelay(60*1000);
105
        setCellRenderer(new MyRenderer());
106
    }
107

    
108
    /**
109
     * Layer tree specific renderer allowing to show multiple line
110
     * tooltips 
111
     * @author jaume
112
     *
113
     */
114
    private class MyRenderer extends DefaultTreeCellRenderer {
115
       
116
        
117
       public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
118
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
119
            if (value instanceof WMSLayerNode){
120
                WMSLayerNode layer = (WMSLayerNode) value;
121
                
122
                String myLatLonTxt = layer.getLatLonBox();
123
                if (myLatLonTxt == null)
124
                    myLatLonTxt = "-";
125
                
126
                String myAbstract = layer.getAbstract();
127
                
128
                if (myAbstract == null)
129
                    myAbstract = "-";
130
                else 
131
                    myAbstract = format(myAbstract.trim(), 100);
132
                
133
                String text =
134
                    PluginServices.getText(this, "abstract") + ":\n" + myAbstract +
135
                    "\n\n" +
136
                    PluginServices.getText(this, "covered_extension") + ":\n" + myLatLonTxt;
137
                
138
                setToolTipText(text);
139
                
140
                if (!showLayerNames) {
141
                        if (layer.getName() != null || layer.getName() == "") {
142
                                text = layer.toString();
143
                                text = text.substring(text.indexOf(']')+2, text.length());
144
                                setText(text);
145
                        }
146
                }
147
                
148
//                Dimension sz  = getPreferredSize();
149
//                sz.setSize((sz.getWidth()+50) - (3*count), sz.getHeight());
150
//                setPreferredSize(sz);
151
//                count++;
152
                
153
            } else {
154
                setToolTipText(null);
155
            }
156
            return this;
157
        }
158
    }
159
    
160
    
161
    /**
162
     * Cuts the message text to force its lines to be shorter or equal to 
163
     * lineLength.
164
     * @param message, the message.
165
     * @param lineLength, the max line length in number of characters.
166
     * @return the formated message.
167
     */
168
    public static String format(String message, int lineLength){
169
        if (message.length() <= lineLength) return message;
170
        String[] lines = message.split("\n");
171
        String theMessage = "";
172
        for (int i = 0; i < lines.length; i++) {
173
            String line = lines[i].trim();
174
            if (line.length()<lineLength)
175
                theMessage += line+"\n";
176
            else {
177
                String[] chunks = line.split(" ");
178
                String newLine = "";
179
                for (int j = 0; j < chunks.length; j++) {
180
                    int currentLength = newLine.length();
181
                    chunks[j] = chunks[j].trim();
182
                    if (chunks[j].length()==0)
183
                        continue;
184
                    if ((currentLength + chunks[j].length() + " ".length()) <= lineLength)
185
                        newLine += chunks[j] + " ";
186
                    else {
187
                        newLine += "\n"+chunks[j]+" ";
188
                        theMessage += newLine;
189
                        newLine = "";
190
                    }
191
                }
192
            }
193
        }
194
        return theMessage;
195
    }
196
    
197
    public JToolTip createToolTip() {
198
        MultiLineToolTip tip = new MultiLineToolTip();
199
        tip.setComponent(this);
200
        return tip;
201
      }
202
}