Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toc / gui / TOCRenderer.java @ 38116

History | View | Annotate | Download (4.94 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2012 gvSIG Association
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., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.app.project.documents.view.toc.gui;
24

    
25
import java.awt.Color;
26
import java.awt.Component;
27
import java.awt.Dimension;
28
import java.awt.Font;
29
import java.awt.Rectangle;
30

    
31
import javax.swing.Icon;
32
import javax.swing.JCheckBox;
33
import javax.swing.JLabel;
34
import javax.swing.JPanel;
35
import javax.swing.JTree;
36
import javax.swing.SpringLayout;
37
import javax.swing.tree.DefaultMutableTreeNode;
38
import javax.swing.tree.TreeCellRenderer;
39

    
40
import org.gvsig.app.project.documents.view.toc.ITocItem;
41
import org.gvsig.app.project.documents.view.toc.TocItemBranch;
42
import org.gvsig.fmap.mapcontext.layers.FLyrDefault;
43

    
44
/**
45
 * Renderer que actua sobre el TOC.
46
 * 
47
 * @author vcn
48
 */
49
public class TOCRenderer extends JPanel implements TreeCellRenderer {
50

    
51
    private static final long serialVersionUID = -6733445768959238193L;
52
    private JCheckBox check;
53
    private JLabel label;
54

    
55
    private final Color editingColor;
56

    
57
    /**
58
     * Creates a new TOCRenderer object.
59
     */
60
    public TOCRenderer() {
61
        this(Color.RED);
62
    }
63

    
64
    /**
65
     * Creates a new TOCRenderer object.
66
     */
67
    public TOCRenderer(Color editingColor) {
68
        this.editingColor = editingColor;
69

    
70
        check = new JCheckBox();
71
        label = new JLabel();
72

    
73
        SpringLayout theLayout = new SpringLayout();
74
        this.setLayout(theLayout);
75

    
76
        // Adjust constraints for the text field so it's at
77
        // (<label's right edge> + 5, 5).
78
        this.add(check);
79
        this.add(label);
80

    
81
        theLayout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.EAST,
82
            check);
83
    }
84

    
85
    /**
86
     * M?todo llamado una vez por cada nodo, y todas las veces que se redibuja
87
     * en pantalla el TOC.
88
     * 
89
     * @param tree
90
     * @param value
91
     * @param isSelected
92
     * @param expanded
93
     * @param leaf
94
     * @param row
95
     * @param hasFocus
96
     * 
97
     * @return
98
     */
99
    public Component getTreeCellRendererComponent(JTree tree, Object value,
100
        boolean isSelected, boolean expanded, boolean leaf, int row,
101
        boolean hasFocus) {
102

    
103
        DefaultMutableTreeNode n = (DefaultMutableTreeNode) value;
104
        Color foreground = tree.getForeground();
105
        this.label.setFont(tree.getFont());
106

    
107
        if (n.getUserObject() instanceof ITocItem) {
108

    
109
            ITocItem item = (ITocItem) n.getUserObject();
110
            label.setText(item.getLabel());
111
            Icon icono = item.getIcon();
112
            if (icono != null) {
113
                label.setIcon(icono);
114
            }
115

    
116
            this.validate();
117
            Dimension sizeNode = item.getSize(); // Se fija en el resize del TOC
118
            this.setPreferredSize(sizeNode);
119

    
120
            if (item instanceof TocItemBranch) {
121
                TocItemBranch branch = (TocItemBranch) item;
122
                FLyrDefault lyr = (FLyrDefault) branch.getLayer();
123
                check.setVisible(true);
124
                check.setSelected(lyr.visibleRequired());
125
                if (!lyr.isAvailable()) {
126
                    check.setEnabled(false);
127
                } else {
128
                    check.setEnabled(true);
129
                    if (!lyr.isWithinScale(lyr.getMapContext().getScaleView())) {
130
                        check.setEnabled(false);
131
                    }
132

    
133
                    if (lyr.isEditing()) {
134
                        this.label.setForeground(editingColor);
135
                    } else {
136
                        this.label.setForeground(foreground);
137
                    }
138
                }
139
                if (lyr.isActive()) {
140
                    this.label.setFont(label.getFont().deriveFont(Font.BOLD));
141
                }
142
            } else {
143
                check.setVisible(false);
144
            }
145
        }
146

    
147
        if (leaf) {
148
            // label.setIcon(UIManager.getIcon("Tree.leafIcon"));
149
        } else if (expanded) {
150
            // label.setIcon(UIManager.getIcon("Tree.openIcon"));
151
        } else {
152
            // label.setIcon(UIManager.getIcon("Tree.closedIcon"));
153
        }
154

    
155
        return this;
156
    }
157

    
158
    public Rectangle getCheckBoxBounds() {
159
        return check.getBounds();
160
    }
161

    
162
}