svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toc / gui / TOCRenderer.java @ 38513
History | View | Annotate | Download (5.28 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2012 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.app.project.documents.view.toc.gui; |
24 |
|
25 |
import java.awt.Color; |
26 |
import java.awt.Component; |
27 |
import java.awt.Dimension; |
28 |
import java.awt.Font; |
29 |
import java.awt.Rectangle; |
30 |
|
31 |
import javax.swing.Icon; |
32 |
import javax.swing.JCheckBox; |
33 |
import javax.swing.JLabel; |
34 |
import javax.swing.JPanel; |
35 |
import javax.swing.JTree; |
36 |
import javax.swing.SpringLayout; |
37 |
import javax.swing.tree.DefaultMutableTreeNode; |
38 |
import javax.swing.tree.TreeCellRenderer; |
39 |
|
40 |
import org.gvsig.app.project.documents.view.toc.ITocItem; |
41 |
import org.gvsig.app.project.documents.view.toc.TocItemBranch; |
42 |
import org.gvsig.fmap.mapcontext.layers.FLyrDefault; |
43 |
|
44 |
/**
|
45 |
* Renderer que actua sobre el TOC.
|
46 |
*
|
47 |
* @author vcn
|
48 |
*/
|
49 |
public class TOCRenderer extends JPanel implements TreeCellRenderer { |
50 |
|
51 |
private static final long serialVersionUID = -6733445768959238193L; |
52 |
private JCheckBox check; |
53 |
private JLabel label; |
54 |
|
55 |
private final Color editingColor; |
56 |
|
57 |
/**
|
58 |
* toc background color
|
59 |
*/
|
60 |
private final Color tocBgColor; |
61 |
|
62 |
/**
|
63 |
* Creates a new TOCRenderer object.
|
64 |
*/
|
65 |
public TOCRenderer(Color tocBackground) { |
66 |
this(Color.RED, tocBackground); |
67 |
} |
68 |
|
69 |
/**
|
70 |
* Creates a new TOCRenderer object.
|
71 |
*/
|
72 |
public TOCRenderer(Color editingColor, Color tocBackground) { |
73 |
|
74 |
this.editingColor = editingColor;
|
75 |
this.tocBgColor = tocBackground;
|
76 |
|
77 |
check = new JCheckBox(); |
78 |
label = new JLabel(); |
79 |
|
80 |
SpringLayout theLayout = new SpringLayout(); |
81 |
this.setLayout(theLayout);
|
82 |
|
83 |
// Adjust constraints for the text field so it's at
|
84 |
// (<label's right edge> + 5, 5).
|
85 |
this.add(check);
|
86 |
this.add(label);
|
87 |
|
88 |
theLayout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.EAST, |
89 |
check); |
90 |
} |
91 |
|
92 |
/**
|
93 |
* M?todo llamado una vez por cada nodo, y todas las veces que se redibuja
|
94 |
* en pantalla el TOC.
|
95 |
*
|
96 |
* @param tree
|
97 |
* @param value
|
98 |
* @param isSelected
|
99 |
* @param expanded
|
100 |
* @param leaf
|
101 |
* @param row
|
102 |
* @param hasFocus
|
103 |
*
|
104 |
* @return
|
105 |
*/
|
106 |
public Component getTreeCellRendererComponent(JTree tree, Object value, |
107 |
boolean isSelected, boolean expanded, boolean leaf, int row, |
108 |
boolean hasFocus) {
|
109 |
|
110 |
DefaultMutableTreeNode n = (DefaultMutableTreeNode) value; |
111 |
Color foreground = tree.getForeground();
|
112 |
this.label.setFont(tree.getFont());
|
113 |
|
114 |
this.label.setBackground(this.tocBgColor); |
115 |
this.setBackground(this.tocBgColor); |
116 |
|
117 |
if (n.getUserObject() instanceof ITocItem) { |
118 |
|
119 |
ITocItem item = (ITocItem) n.getUserObject(); |
120 |
label.setText(item.getLabel()); |
121 |
Icon icono = item.getIcon();
|
122 |
if (icono != null) { |
123 |
label.setIcon(icono); |
124 |
} |
125 |
|
126 |
this.validate();
|
127 |
Dimension sizeNode = item.getSize(); // Se fija en el resize del TOC |
128 |
this.setPreferredSize(sizeNode);
|
129 |
|
130 |
if (item instanceof TocItemBranch) { |
131 |
TocItemBranch branch = (TocItemBranch) item; |
132 |
FLyrDefault lyr = (FLyrDefault) branch.getLayer(); |
133 |
check.setVisible(true);
|
134 |
check.setSelected(lyr.visibleRequired()); |
135 |
check.setBackground(this.tocBgColor);
|
136 |
if (!lyr.isAvailable()) {
|
137 |
check.setEnabled(false);
|
138 |
} else {
|
139 |
check.setEnabled(true);
|
140 |
if (!lyr.isWithinScale(lyr.getMapContext().getScaleView())) {
|
141 |
check.setEnabled(false);
|
142 |
} |
143 |
|
144 |
if (lyr.isEditing()) {
|
145 |
this.label.setForeground(editingColor);
|
146 |
} else {
|
147 |
this.label.setForeground(foreground);
|
148 |
} |
149 |
} |
150 |
if (lyr.isActive()) {
|
151 |
this.label.setFont(label.getFont().deriveFont(Font.BOLD)); |
152 |
} |
153 |
} else {
|
154 |
check.setVisible(false);
|
155 |
} |
156 |
} |
157 |
|
158 |
if (leaf) {
|
159 |
// label.setIcon(UIManager.getIcon("Tree.leafIcon"));
|
160 |
} else if (expanded) { |
161 |
// label.setIcon(UIManager.getIcon("Tree.openIcon"));
|
162 |
} else {
|
163 |
// label.setIcon(UIManager.getIcon("Tree.closedIcon"));
|
164 |
} |
165 |
|
166 |
return this; |
167 |
} |
168 |
|
169 |
public Rectangle getCheckBoxBounds() { |
170 |
return check.getBounds();
|
171 |
} |
172 |
|
173 |
} |