Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG_1.11.0_Mejoras_gvSIG-EIEL / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toc / gui / TOCRenderer.java @ 35646

History | View | Annotate | Download (8.18 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.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
import java.util.Random;
58

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

    
71
import com.iver.cit.gvsig.fmap.layers.FLyrDefault;
72
import com.iver.cit.gvsig.project.documents.view.toc.ITocItem;
73
import com.iver.cit.gvsig.project.documents.view.toc.TocItemBranch;
74
import com.iver.cit.gvsig.project.documents.view.toc.util.DropTargetInfo;
75

    
76

    
77

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

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

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

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

    
113

    
114
                /* this.setLayout(gridbag);
115

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

    
131

    
132

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

    
143
                SpringLayout theLayout = new SpringLayout();                 
144
                this.setLayout(theLayout);
145

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

    
170
                theLayout.putConstraint(SpringLayout.WEST, label,
171
                                                   5,
172
                                                   SpringLayout.EAST, check); 
173

    
174
                this.setBorder(bordeSeleccionado);
175

    
176
        check.setBackground(UIManager.getColor("Button.background"));
177
        label.setForeground(UIManager.getColor("Tree.textForeground"));
178
        
179
        
180
    }
181
    
182
    public void setGraphicIndicatorInfo(DropTargetInfo dti) {
183
            graphicIndicatorInfo = dti;
184
    }
185

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

    
210
                if (n.getUserObject() instanceof ITocItem)
211
                {
212
                        
213
                       ITocItem item = (ITocItem) n.getUserObject();          
214
                       
215
                       if (graphicIndicatorInfo != null) {
216
                               if (item == graphicIndicatorInfo.getTocItem()) {
217
                                this.setBackground( getRndColor() );
218
                           }
219
                       }
220
                       
221
                        stringValue = item.getLabel();
222
                        
223
                Dimension sizeNode = item.getSize();
224
                // Se fija en el resize del TOC
225
                
226
                        this.setPreferredSize(sizeNode);
227
                        
228
                        if (item instanceof TocItemBranch)
229
                        {
230
                                TocItemBranch branch = (TocItemBranch) item;
231
                                FLyrDefault lyr = (FLyrDefault)branch.getLayer();
232
                                check.setVisible(true);
233
                                check.setSelected(lyr.visibleRequired());
234
                                if (!lyr.isAvailable()) {                                                                                
235
                                        check.setEnabled(false);
236
                                } else {                                        
237
                                        check.setEnabled(true);
238
                                        if (!lyr.isWithinScale(lyr.getMapContext().getScaleView()))
239
                                                check.setEnabled(false);
240
                                        
241

    
242
                                        if (lyr.isEditing())
243
                                        {
244
                                                this.label.setForeground(Color.RED);
245
                                        }
246
                                        else this.label.setForeground(Color.BLACK);
247
                                }
248
                                if (lyr.isActive())
249
                                {                                
250
                                        this.setBorder(bordeSeleccionado);
251
                                        this.label.setFont(BIGGER_FONT);
252
                                }
253
                                else
254
                                {                                
255
                                        this.setBorder(bordeNormal);
256
                                }                                
257
                }
258
                        else
259
                        {
260
                                check.setVisible(false);
261
                                this.setBorder(bordeNormal);                        
262
                                
263
                        }
264
                label.setText(stringValue);
265
                Icon icono = item.getIcon();
266
                if (icono != null)
267
                {
268
                        label.setIcon(icono);
269
                        //System.out.println(">>>>>Pongo etiqueta " + stringValue + " con icono " + item.getIcon().toString());
270
                }
271
                this.setPreferredSize(sizeNode);
272
                }                
273
                // this.setPreferredSize(new Dimension(tree.getWidth()-60,24)); // sizeNode);
274

    
275
        if (leaf) {
276
            // label.setIcon(UIManager.getIcon("Tree.leafIcon"));
277
            
278
        } else if (expanded) {
279
            //label.setIcon(UIManager.getIcon("Tree.openIcon"));
280
        } else {
281
            //label.setIcon(UIManager.getIcon("Tree.closedIcon"));
282
        }
283

    
284
        return this;
285
    }
286

    
287
        public Rectangle getCheckBoxBounds() {
288
                return check.getBounds();
289
        }
290
        
291
        private static Random rnd = new Random(System.currentTimeMillis());
292
        
293
        private Color getRndColor() {
294
                int r = 128 + rnd.nextInt(128);
295
                int g = 128 + rnd.nextInt(128);
296
                int b = 128 + rnd.nextInt(128);
297
                return new Color(r,g,b);
298
        }
299

    
300
}