Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toc / gui / TOCRenderer.java @ 29598

History | View | Annotate | Download (7.48 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 org.gvsig.app.project.documents.view.toc.gui;
48

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

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

    
70
import org.gvsig.app.project.documents.view.toc.ITocItem;
71
import org.gvsig.app.project.documents.view.toc.TocItemBranch;
72
import org.gvsig.fmap.mapcontext.layers.FLyrDefault;
73

    
74

    
75

    
76

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

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

    
94
        private static final Font BIGGER_FONT = 
95
                                                                          new Font("SansSerif", Font.BOLD, 12);
96

    
97
        GridBagLayout gridbag = new GridBagLayout();
98
        GridBagConstraints c = new GridBagConstraints();
99
    /**
100
     * Creates a new TOCRenderer object.
101
     */
102
    public TOCRenderer() {
103
        this.setBackground(Color.lightGray);
104
        // this.setLayout(new BorderLayout());
105
        this.setLayout(new FlowLayout(FlowLayout.LEADING, 0,0));
106
                check = new JCheckBox();
107
                label = new JLabel();
108
                
109

    
110

    
111
                /* this.setLayout(gridbag);
112

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

    
128

    
129

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

    
140
                SpringLayout theLayout = new SpringLayout();                 
141
                this.setLayout(theLayout);
142

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

    
167
                theLayout.putConstraint(SpringLayout.WEST, label,
168
                                                   5,
169
                                                   SpringLayout.EAST, check); 
170

    
171
                this.setBorder(bordeSeleccionado);
172

    
173
        check.setBackground(UIManager.getColor("Button.background"));
174
        label.setForeground(UIManager.getColor("Tree.textForeground"));
175
        
176
        
177
    }
178

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

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

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

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

    
269
        return this;
270
    }
271

    
272
        public Rectangle getCheckBoxBounds() {
273
                return check.getBounds();
274
        }
275

    
276
}