Statistics
| Revision:

root / tags / v10_RC2c / applications / appgvSIG / src / com / iver / cit / gvsig / gui / toc / TOCRenderer.java @ 8745

History | View | Annotate | Download (7.31 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.Icon;
59
import javax.swing.JCheckBox;
60
import javax.swing.JLabel;
61
import javax.swing.JPanel;
62
import javax.swing.JTree;
63
import javax.swing.SpringLayout;
64
import javax.swing.UIManager;
65
import javax.swing.border.Border;
66
import javax.swing.tree.DefaultMutableTreeNode;
67
import javax.swing.tree.TreeCellRenderer;
68

    
69
import com.iver.cit.gvsig.fmap.layers.FLayer;
70
import com.iver.cit.gvsig.fmap.layers.FLyrDefault;
71

    
72

    
73

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

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

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

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

    
107

    
108
                /* this.setLayout(gridbag);
109

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

    
125

    
126

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

    
137
                SpringLayout theLayout = new SpringLayout();                 
138
                this.setLayout(theLayout);
139

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

    
164
                theLayout.putConstraint(SpringLayout.WEST, label,
165
                                                   5,
166
                                                   SpringLayout.EAST, check); 
167

    
168
                this.setBorder(bordeSeleccionado);
169

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

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

    
200
                if (n.getUserObject() instanceof ITocItem)
201
                {
202
                        
203
                       ITocItem item = (ITocItem) n.getUserObject();                       
204
                        stringValue = item.getLabel();
205
                        
206
                Dimension sizeNode = item.getSize(); // Se fija en el resize del TOC
207
                
208
                        this.setPreferredSize(sizeNode);
209
                        
210
                        if (item instanceof TocItemBranch)
211
                        {
212
                                TocItemBranch branch = (TocItemBranch) item;
213
                                FLyrDefault lyr = (FLyrDefault)branch.getLayer();
214
                                check.setVisible(true);
215
                                check.setSelected(lyr.visibleRequired());
216
                                if (!lyr.isAvailable()) {                                                                                
217
                                        check.setEnabled(false);
218
                                } else {                                        
219
                                        check.setEnabled(true);
220
                                        if (!lyr.isWithinScale(lyr.getMapContext().getScaleView()))
221
                                                check.setEnabled(false);
222
                                        
223

    
224
                                        if (lyr.isEditing())
225
                                        {
226
                                                this.label.setForeground(Color.RED);
227
                                        }
228
                                        else this.label.setForeground(Color.BLACK);
229
                                }
230
                                if (lyr.isActive())
231
                                {                                
232
                                        this.setBorder(bordeSeleccionado);
233
                                        this.label.setFont(BIGGER_FONT);
234
                                }
235
                                else
236
                                {                                
237
                                        this.setBorder(bordeNormal);
238
                                }                                
239
                }
240
                        else
241
                        {
242
                                check.setVisible(false);
243
                                this.setBorder(bordeNormal);                        
244
                                
245
                        }
246
                label.setText(stringValue);
247
                Icon icono = item.getIcon();
248
                if (icono != null)
249
                {
250
                        label.setIcon(icono);
251
                        //System.out.println(">>>>>Pongo etiqueta " + stringValue + " con icono " + item.getIcon().toString());
252
                }
253
                this.setPreferredSize(sizeNode);
254
                }                
255
                // this.setPreferredSize(new Dimension(tree.getWidth()-60,24)); // sizeNode);
256

    
257
        if (leaf) {
258
            // label.setIcon(UIManager.getIcon("Tree.leafIcon"));
259
            
260
        } else if (expanded) {
261
            //label.setIcon(UIManager.getIcon("Tree.openIcon"));
262
        } else {
263
            //label.setIcon(UIManager.getIcon("Tree.closedIcon"));
264
        }
265

    
266
        return this;
267
    }
268

    
269
        public Rectangle getCheckBoxBounds() {
270
                return check.getBounds();
271
        }
272

    
273
}