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