Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wmts / trunk / org.gvsig.raster.wmts / org.gvsig.raster.wmts.swing / org.gvsig.raster.wmts.swing.impl / src / main / java / org / gvsig / raster / wmts / swing / impl / panel / layer / LayerTree.java @ 2613

History | View | Annotate | Download (4.66 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.wmts.swing.impl.panel.layer;
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.gui.beans.controls.MultiLineToolTip;
33
import org.gvsig.i18n.Messages;
34
import org.gvsig.raster.wmts.ogc.struct.WMTSLayer;
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
@SuppressWarnings("serial")
45
public class LayerTree extends JTree {
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
       
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 WMTSLayer) {
67
                    WMTSLayer layer = (WMTSLayer) value;
68
                
69
                String myAbstract = layer.getAbstract();
70
                
71
                if (myAbstract == null)
72
                    myAbstract = "-";
73
                else 
74
                    myAbstract = format(myAbstract.trim(), 100);
75
                
76
                String text =
77
                    Messages.getText("abstract") + ":\n" + myAbstract + "\n\n";
78
                
79
                setToolTipText(text);
80
                
81
                if (!showLayerNames) {
82
                        if (layer.getTitle() != null || layer.getTitle() == "") {
83
                                text = layer.toString();
84
                                text = text.substring(text.indexOf(']') + 2, text.length());
85
                                setText(text);
86
                        }
87
                }
88
                
89
            } else {
90
                setToolTipText(null);
91
            }
92
            return this;
93
        }
94
    }
95
    
96
    
97
    /**
98
     * Cuts the message text to force its lines to be shorter or equal to 
99
     * lineLength.
100
     * @param message, the message.
101
     * @param lineLength, the max line length in number of characters.
102
     * @return the formated message.
103
     */
104
    public static String format(String message, int lineLength){
105
        if (message.length() <= lineLength) return message;
106
        String[] lines = message.split("\n");
107
        String theMessage = "";
108
        
109
        for (int i = 0; i < lines.length; i++) {
110
            String line = lines[i].trim();
111
            if (line.length() < lineLength)
112
                theMessage += line + "\n";
113
            else {
114
                String[] chunks = line.split(" ");
115
                String newLine  = "";
116
                for (int j = 0; j < chunks.length; j++) {
117
                    int currentLength = newLine.length();
118
                    chunks[j] = chunks[j].trim();
119
                    if (chunks[j].length() == 0)
120
                        continue;
121
                    if ((currentLength + chunks[j].length() + " ".length()) <= lineLength)
122
                        newLine += chunks[j] + " ";
123
                    else {
124
                        newLine += "\n"+chunks[j]+" ";
125
                        theMessage += newLine;
126
                        newLine = "";
127
                    }
128
                }
129
            }
130
        }
131
        return theMessage;
132
    }
133
    
134
    public JToolTip createToolTip() {
135
            MultiLineToolTip tip = new MultiLineToolTip();
136
            tip.setComponent(this);
137
            return tip;
138
    }
139
}