Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / toc / TOCRenderer.java @ 2531

History | View | Annotate | Download (6.96 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
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package com.iver.cit.gvsig.gui.toc;
48

    
49
import java.awt.Color;
50
import java.awt.Component;
51
import java.awt.Dimension;
52
import java.awt.Font;
53
import java.awt.GridBagConstraints;
54
import java.awt.GridBagLayout;
55
import java.awt.Rectangle;
56

    
57
import javax.swing.BorderFactory;
58
import javax.swing.JCheckBox;
59
import javax.swing.JLabel;
60
import javax.swing.JPanel;
61
import javax.swing.JTree;
62
import javax.swing.SpringLayout;
63
import javax.swing.UIManager;
64
import javax.swing.border.Border;
65
import javax.swing.tree.DefaultMutableTreeNode;
66
import javax.swing.tree.TreeCellRenderer;
67

    
68
import com.iver.cit.gvsig.fmap.layers.FLayer;
69

    
70

    
71

    
72
/**
73
 * Renderer que actua sobre el TOC.
74
 *
75
 * @author vcn
76
 */
77
public class TOCRenderer extends JPanel implements TreeCellRenderer {
78
    public Border bordeSeleccionado = BorderFactory.createEtchedBorder();
79

    
80
    /*public Border bordeSeleccionado = BorderFactory.createBevelBorder(BevelBorder.RAISED,
81
            SystemColor.black, SystemColor.lightGray, SystemColor.gray,
82
            SystemColor.lightGray);
83
    */
84
    private Border bordeNormal = BorderFactory.createEmptyBorder();
85
    
86
        private JCheckBox check;
87
        private JLabel label;
88

    
89
        private static final Font BIGGER_FONT = 
90
                                                                          new Font("SansSerif", Font.BOLD, 12);
91

    
92
        GridBagLayout gridbag = new GridBagLayout();
93
        GridBagConstraints c = new GridBagConstraints();
94
    /**
95
     * Creates a new TOCRenderer object.
96
     */
97
    public TOCRenderer() {
98
        this.setBackground(Color.lightGray);
99
        // this.setLayout(new BorderLayout());
100
        
101
                check = new JCheckBox();
102
                label = new JLabel();
103
                
104

    
105

    
106
                /* this.setLayout(gridbag);
107

108
                c.fill = GridBagConstraints.NONE;
109
                c.weightx = 1.0;
110
                c.anchor = GridBagConstraints.WEST;
111
                // c.weightx = check.getWidth();
112
                gridbag.setConstraints(check,c);
113
                this.add(check);
114
                c.anchor = GridBagConstraints.WEST;
115
                gridbag.setConstraints(colorPanel,c);
116
                // c.weightx = colorPanel.getWidth();
117
                this.add(colorPanel);
118
                c.gridwidth = GridBagConstraints.REMAINDER; //end row
119
                c.fill = GridBagConstraints.HORIZONTAL;                
120
                gridbag.setConstraints(label,c);
121
                this.add(label); */ 
122

    
123

    
124

    
125
                /* this.setLayout(new FlowLayout(FlowLayout.LEFT));
126
                this.add(check);
127
                this.add(colorPanel);
128
                this.add(label); */
129
                
130
                /* this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
131
                this.add(check);
132
                this.add(colorPanel);
133
                this.add(label); */  
134

    
135
                SpringLayout theLayout = new SpringLayout();                 
136
                this.setLayout(theLayout);
137

    
138
                //int rows = 1;
139
                //int cols = 2;
140
                                
141
                /* this.setLayout(new BorderLayout());
142
                
143
                this.add(check, BorderLayout.WEST);
144
                this.add(label, BorderLayout.CENTER);
145
                */ 
146
                /*
147
                GridBagLayout gridbag = new GridBagLayout();
148
        GridBagConstraints c = new GridBagConstraints();
149
        
150
        this.setLayout(gridbag);
151
        this.add(check);
152
        c.gridwidth = GridBagConstraints.REMAINDER;
153
        c.fill = GridBagConstraints.HORIZONTAL;
154
        this.add(label, c);
155
        */
156
                
157
//                Adjust constraints for the text field so it's at
158
//                (<label's right edge> + 5, 5).
159
                this.add(check);
160
                this.add(label);  
161

    
162
                theLayout.putConstraint(SpringLayout.WEST, label,
163
                                                   5,
164
                                                   SpringLayout.EAST, check); 
165

    
166
                this.setBorder(bordeSeleccionado);
167

    
168
        check.setBackground(UIManager.getColor("Button.background"));
169
        label.setForeground(UIManager.getColor("Tree.textForeground"));
170
        
171
        
172
    }
173

    
174
    /**
175
     * M?todo llamado una vez por cada nodo, y todas las veces que se redibuja
176
     * en pantalla el TOC.
177
     *
178
     * @param tree
179
     * @param value
180
     * @param isSelected
181
     * @param expanded
182
     * @param leaf
183
     * @param row
184
     * @param hasFocus
185
     *
186
     * @return
187
     */
188
    public Component getTreeCellRendererComponent(JTree tree, Object value,
189
        boolean isSelected, boolean expanded, boolean leaf, int row,
190
        boolean hasFocus) {
191
                
192
        DefaultMutableTreeNode n = (DefaultMutableTreeNode) value;
193
                String stringValue = "";
194
                this.setBackground(UIManager.getColor("Button.background"));
195
                this.label.setFont(tree.getFont());
196
                // System.out.println("ancho tree=" + tree.getWidth());
197

    
198
                if (n.getUserObject() instanceof ITocItem)
199
                {
200
                        
201
                       ITocItem item = (ITocItem) n.getUserObject();                       
202
                        stringValue = item.getLabel();
203
                        
204
                Dimension sizeNode = item.getSize(); // Se fija en el resize del TOC
205
                
206
                        this.setPreferredSize(sizeNode);
207
                        
208
                        if (item instanceof TocItemBranch)
209
                        {
210
                                TocItemBranch branch = (TocItemBranch) item;
211
                                FLayer lyr = branch.getLayer();
212
                                check.setVisible(true);
213
                                check.setSelected(lyr.isVisible());
214
                                check.setEnabled(true);
215
                            if (!lyr.isWithinScale(lyr.getFMap().getScaleView()))
216
                                check.setEnabled(false);
217
                                
218
                                if (lyr.isActive())
219
                                {                                
220
                                        this.setBorder(bordeSeleccionado);
221
                                        this.label.setFont(BIGGER_FONT);
222
                                }
223
                                else
224
                                {                                
225
                                        this.setBorder(bordeNormal);
226
                                }                                
227
                }
228
                        else
229
                        {
230
                                check.setVisible(false);
231
                                this.setBorder(bordeNormal);                        
232
                                
233
                        }
234
                label.setText(stringValue);
235
                if (item.getIcon() != null)
236
                {
237
                        label.setIcon(item.getIcon());
238
                        // System.out.println("Pongo etiqueta " + stringValue + " con icono " + item.getIcon().toString());
239
                }
240
                this.setPreferredSize(sizeNode);
241
                }                
242
                // this.setPreferredSize(new Dimension(tree.getWidth()-60,24)); // sizeNode);
243

    
244
        if (leaf) {
245
            // label.setIcon(UIManager.getIcon("Tree.leafIcon"));
246
            
247
        } else if (expanded) {
248
            //label.setIcon(UIManager.getIcon("Tree.openIcon"));
249
        } else {
250
            //label.setIcon(UIManager.getIcon("Tree.closedIcon"));
251
        }
252

    
253
        return this;
254
    }
255

    
256
        public Rectangle getCheckBoxBounds() {
257
                return check.getBounds();
258
        }
259

    
260
}