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 @ 40558
History | View | Annotate | Download (5.29 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.Rectangle; |
31 |
|
32 |
import javax.swing.Icon; |
33 |
import javax.swing.JCheckBox; |
34 |
import javax.swing.JLabel; |
35 |
import javax.swing.JPanel; |
36 |
import javax.swing.JTree; |
37 |
import javax.swing.SpringLayout; |
38 |
import javax.swing.tree.DefaultMutableTreeNode; |
39 |
import javax.swing.tree.TreeCellRenderer; |
40 |
|
41 |
import org.gvsig.app.project.documents.view.toc.ITocItem; |
42 |
import org.gvsig.app.project.documents.view.toc.TocItemBranch; |
43 |
import org.gvsig.fmap.mapcontext.layers.FLyrDefault; |
44 |
|
45 |
/**
|
46 |
* Renderer que actua sobre el TOC.
|
47 |
*
|
48 |
* @author vcn
|
49 |
*/
|
50 |
public class TOCRenderer extends JPanel implements TreeCellRenderer { |
51 |
|
52 |
private static final long serialVersionUID = -6733445768959238193L; |
53 |
private JCheckBox check; |
54 |
private JLabel label; |
55 |
|
56 |
private final Color editingColor; |
57 |
|
58 |
/**
|
59 |
* toc background color
|
60 |
*/
|
61 |
private final Color tocBgColor; |
62 |
|
63 |
/**
|
64 |
* Creates a new TOCRenderer object.
|
65 |
*/
|
66 |
public TOCRenderer(Color tocBackground) { |
67 |
this(Color.RED, tocBackground); |
68 |
} |
69 |
|
70 |
/**
|
71 |
* Creates a new TOCRenderer object.
|
72 |
*/
|
73 |
public TOCRenderer(Color editingColor, Color tocBackground) { |
74 |
|
75 |
this.editingColor = editingColor;
|
76 |
this.tocBgColor = tocBackground;
|
77 |
|
78 |
check = new JCheckBox(); |
79 |
label = new JLabel(); |
80 |
|
81 |
SpringLayout theLayout = new SpringLayout(); |
82 |
this.setLayout(theLayout);
|
83 |
|
84 |
// Adjust constraints for the text field so it's at
|
85 |
// (<label's right edge> + 5, 5).
|
86 |
this.add(check);
|
87 |
this.add(label);
|
88 |
|
89 |
theLayout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.EAST, |
90 |
check); |
91 |
} |
92 |
|
93 |
/**
|
94 |
* M?todo llamado una vez por cada nodo, y todas las veces que se redibuja
|
95 |
* en pantalla el TOC.
|
96 |
*
|
97 |
* @param tree
|
98 |
* @param value
|
99 |
* @param isSelected
|
100 |
* @param expanded
|
101 |
* @param leaf
|
102 |
* @param row
|
103 |
* @param hasFocus
|
104 |
*
|
105 |
* @return
|
106 |
*/
|
107 |
public Component getTreeCellRendererComponent(JTree tree, Object value, |
108 |
boolean isSelected, boolean expanded, boolean leaf, int row, |
109 |
boolean hasFocus) {
|
110 |
|
111 |
DefaultMutableTreeNode n = (DefaultMutableTreeNode) value; |
112 |
Color foreground = tree.getForeground();
|
113 |
this.label.setFont(tree.getFont());
|
114 |
|
115 |
this.label.setBackground(this.tocBgColor); |
116 |
this.setBackground(this.tocBgColor); |
117 |
|
118 |
if (n.getUserObject() instanceof ITocItem) { |
119 |
|
120 |
ITocItem item = (ITocItem) n.getUserObject(); |
121 |
label.setText(item.getLabel()); |
122 |
Icon icono = item.getIcon();
|
123 |
if (icono != null) { |
124 |
label.setIcon(icono); |
125 |
} |
126 |
|
127 |
this.validate();
|
128 |
Dimension sizeNode = item.getSize(); // Se fija en el resize del TOC |
129 |
this.setPreferredSize(sizeNode);
|
130 |
|
131 |
if (item instanceof TocItemBranch) { |
132 |
TocItemBranch branch = (TocItemBranch) item; |
133 |
FLyrDefault lyr = (FLyrDefault) branch.getLayer(); |
134 |
check.setVisible(true);
|
135 |
check.setSelected(lyr.visibleRequired()); |
136 |
check.setBackground(this.tocBgColor);
|
137 |
if (!lyr.isAvailable()) {
|
138 |
check.setEnabled(false);
|
139 |
} else {
|
140 |
check.setEnabled(true);
|
141 |
if (!lyr.isWithinScale(lyr.getMapContext().getScaleView())) {
|
142 |
check.setEnabled(false);
|
143 |
} |
144 |
|
145 |
if (lyr.isEditing()) {
|
146 |
this.label.setForeground(editingColor);
|
147 |
} else {
|
148 |
this.label.setForeground(foreground);
|
149 |
} |
150 |
} |
151 |
if (lyr.isActive()) {
|
152 |
this.label.setFont(label.getFont().deriveFont(Font.BOLD)); |
153 |
} |
154 |
} else {
|
155 |
check.setVisible(false);
|
156 |
} |
157 |
} |
158 |
|
159 |
if (leaf) {
|
160 |
// label.setIcon(UIManager.getIcon("Tree.leafIcon"));
|
161 |
} else if (expanded) { |
162 |
// label.setIcon(UIManager.getIcon("Tree.openIcon"));
|
163 |
} else {
|
164 |
// label.setIcon(UIManager.getIcon("Tree.closedIcon"));
|
165 |
} |
166 |
|
167 |
return this; |
168 |
} |
169 |
|
170 |
public Rectangle getCheckBoxBounds() { |
171 |
return check.getBounds();
|
172 |
} |
173 |
|
174 |
} |