Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / toc / TOCRenderer.java @ 423

History | View | Annotate | Download (5.67 KB)

1
/*
2
 * Created on 23-feb-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
package com.iver.cit.gvsig.gui.toc;
8

    
9
import java.awt.BorderLayout;
10
import java.awt.Color;
11
import java.awt.Component;
12
import java.awt.Dimension;
13
import java.awt.Font;
14
import java.awt.GridBagConstraints;
15
import java.awt.GridBagLayout;
16
import java.awt.Rectangle;
17

    
18
import javax.swing.BorderFactory;
19
import javax.swing.JCheckBox;
20
import javax.swing.JLabel;
21
import javax.swing.JPanel;
22
import javax.swing.JTree;
23
import javax.swing.SpringLayout;
24
import javax.swing.UIManager;
25
import javax.swing.border.Border;
26
import javax.swing.tree.DefaultMutableTreeNode;
27
import javax.swing.tree.TreeCellRenderer;
28

    
29
import com.iver.cit.gvsig.fmap.layers.FLayer;
30

    
31

    
32

    
33
/**
34
 * Renderer que actua sobre el TOC.
35
 *
36
 * @author vcn
37
 */
38
public class TOCRenderer extends JPanel implements TreeCellRenderer {
39
    public Border bordeSeleccionado = BorderFactory.createEtchedBorder();
40

    
41
    /*public Border bordeSeleccionado = BorderFactory.createBevelBorder(BevelBorder.RAISED,
42
            SystemColor.black, SystemColor.lightGray, SystemColor.gray,
43
            SystemColor.lightGray);
44
    */
45
    private Border bordeNormal = BorderFactory.createEmptyBorder();
46
    
47
        private JCheckBox check;
48
        private JLabel label;
49

    
50
        private static final Font BIGGER_FONT = 
51
                                                                          new Font("SansSerif", Font.BOLD, 12);
52

    
53
        GridBagLayout gridbag = new GridBagLayout();
54
        GridBagConstraints c = new GridBagConstraints();
55
    /**
56
     * Creates a new TOCRenderer object.
57
     */
58
    public TOCRenderer() {
59
        this.setBackground(Color.lightGray);
60
        // this.setLayout(new BorderLayout());
61
        
62
                check = new JCheckBox();
63
                label = new JLabel();
64
                
65

    
66

    
67
                /* this.setLayout(gridbag);
68

69
                c.fill = GridBagConstraints.NONE;
70
                c.weightx = 1.0;
71
                c.anchor = GridBagConstraints.WEST;
72
                // c.weightx = check.getWidth();
73
                gridbag.setConstraints(check,c);
74
                this.add(check);
75
                c.anchor = GridBagConstraints.WEST;
76
                gridbag.setConstraints(colorPanel,c);
77
                // c.weightx = colorPanel.getWidth();
78
                this.add(colorPanel);
79
                c.gridwidth = GridBagConstraints.REMAINDER; //end row
80
                c.fill = GridBagConstraints.HORIZONTAL;                
81
                gridbag.setConstraints(label,c);
82
                this.add(label); */ 
83

    
84

    
85

    
86
                /* this.setLayout(new FlowLayout(FlowLayout.LEFT));
87
                this.add(check);
88
                this.add(colorPanel);
89
                this.add(label); */
90
                
91
                /* this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
92
                this.add(check);
93
                this.add(colorPanel);
94
                this.add(label); */  
95

    
96
                SpringLayout theLayout = new SpringLayout();                 
97
                this.setLayout(theLayout);
98

    
99
                int rows = 1;
100
                int cols = 2;
101
                                
102
                /* this.setLayout(new BorderLayout());
103
                
104
                this.add(check, BorderLayout.WEST);
105
                this.add(label, BorderLayout.CENTER);
106
                */ 
107
                /*
108
                GridBagLayout gridbag = new GridBagLayout();
109
        GridBagConstraints c = new GridBagConstraints();
110
        
111
        this.setLayout(gridbag);
112
        this.add(check);
113
        c.gridwidth = GridBagConstraints.REMAINDER;
114
        c.fill = GridBagConstraints.HORIZONTAL;
115
        this.add(label, c);
116
        */
117
                
118
//                Adjust constraints for the text field so it's at
119
//                (<label's right edge> + 5, 5).
120
                this.add(check);
121
                this.add(label);  
122

    
123
                theLayout.putConstraint(SpringLayout.WEST, label,
124
                                                   5,
125
                                                   SpringLayout.EAST, check); 
126

    
127
                this.setBorder(bordeSeleccionado);
128

    
129
        check.setBackground(UIManager.getColor("Button.background"));
130
        label.setForeground(UIManager.getColor("Tree.textForeground"));
131
        
132
        
133
    }
134

    
135
    /**
136
     * M?todo llamado una vez por cada nodo, y todas las veces que se redibuja
137
     * en pantalla el TOC.
138
     *
139
     * @param tree
140
     * @param value
141
     * @param isSelected
142
     * @param expanded
143
     * @param leaf
144
     * @param row
145
     * @param hasFocus
146
     *
147
     * @return
148
     */
149
    public Component getTreeCellRendererComponent(JTree tree, Object value,
150
        boolean isSelected, boolean expanded, boolean leaf, int row,
151
        boolean hasFocus) {
152
                
153
        DefaultMutableTreeNode n = (DefaultMutableTreeNode) value;
154
                String stringValue = "";
155
                this.setBackground(UIManager.getColor("Button.background"));
156
                this.label.setFont(tree.getFont());
157
                // System.out.println("ancho tree=" + tree.getWidth());
158

    
159
                if (n.getUserObject() instanceof ITocItem)
160
                {
161
                        
162
                       ITocItem item = (ITocItem) n.getUserObject();                       
163
                        stringValue = item.getLabel();
164
                        
165
                Dimension sizeNode = item.getSize(); // Se fija en el resize del TOC
166
                
167
                        this.setPreferredSize(sizeNode);
168
                        
169
                        if (item instanceof TocItemBranch)
170
                        {
171
                                TocItemBranch branch = (TocItemBranch) item;
172
                                FLayer lyr = branch.getLayer();
173
                                check.setVisible(true);
174
                                check.setSelected(lyr.isVisible());                
175
                                if (lyr.isActive())
176
                                {                                
177
                                        this.setBorder(bordeSeleccionado);
178
                                        this.label.setFont(BIGGER_FONT);
179
                                }
180
                                else
181
                                {                                
182
                                        this.setBorder(bordeNormal);
183
                                }                                
184
                }
185
                        else
186
                        {
187
                                check.setVisible(false);
188
                                this.setBorder(bordeNormal);                        
189
                                
190
                        }
191
                label.setText(stringValue);
192
                if (item.getIcon() != null)
193
                {
194
                        label.setIcon(item.getIcon());
195
                        // System.out.println("Pongo etiqueta " + stringValue + " con icono " + item.getIcon().toString());
196
                }
197
                this.setPreferredSize(sizeNode);
198
                }                
199
                // this.setPreferredSize(new Dimension(tree.getWidth()-60,24)); // sizeNode);
200

    
201
        if (leaf) {
202
            // label.setIcon(UIManager.getIcon("Tree.leafIcon"));
203
            
204
        } else if (expanded) {
205
            //label.setIcon(UIManager.getIcon("Tree.openIcon"));
206
        } else {
207
            //label.setIcon(UIManager.getIcon("Tree.closedIcon"));
208
        }
209

    
210
        return this;
211
    }
212

    
213
        public Rectangle getCheckBoxBounds() {
214
                return check.getBounds();
215
        }
216

    
217
}