001: /*
002: * Copyright 2008 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.gui.tree;
017:
018: import java.awt.Color;
019: import java.awt.Component;
020: import java.awt.Dimension;
021: import java.awt.Font;
022: import java.awt.Graphics;
023: import java.awt.GridBagConstraints;
024: import java.awt.GridBagLayout;
025: import java.awt.Insets;
026: import java.awt.Rectangle;
027: import java.io.File;
028:
029: import javax.swing.BorderFactory;
030: import javax.swing.JCheckBox;
031: import javax.swing.JLabel;
032: import javax.swing.JPanel;
033: import javax.swing.JTree;
034: import javax.swing.UIManager;
035: import javax.swing.tree.DefaultMutableTreeNode;
036: import javax.swing.tree.DefaultTreeCellRenderer;
037: import javax.swing.tree.TreeCellRenderer;
038:
039: import org.tp23.gui.tree.model.FileSystemModel;
040: import org.tp23.gui.tree.model.ItemModel;
041:
042: /**
043: * This class reuses some swing objects for each line in the tree as it seems the
044: * renderer is happy with that. Thus you can't add selection listeners
045: * it is ony used for rendering.
046: * @author teknopaul
047: * @version 1.0
048: */
049: public class DirectoryTreeCellRenderer extends JPanel implements
050: TreeCellRenderer {
051:
052: private JLabel label = new JLabel();
053: private GridBagLayout gridBagLayout = new GridBagLayout();
054: private DefaultTreeCellRenderer defaultRenderer = new DefaultTreeCellRenderer();
055: private JLabel iconLabel = new JLabel();
056: private Font bold = new Font(getDefaultFont().getFamily(),
057: Font.BOLD, getDefaultFont().getSize());
058: private Font normal = new Font(getDefaultFont().getFamily(),
059: Font.PLAIN, getDefaultFont().getSize());
060:
061: public DirectoryTreeCellRenderer() {
062: try {
063: jbInit();
064: } catch (Exception e) {
065: e.printStackTrace();
066: }
067: }
068:
069: /**
070: * getTreeCellRendererComponent
071: *
072: * @param tree JTree
073: * @param value Object
074: * @param selected boolean
075: * @param expanded boolean
076: * @param leaf boolean
077: * @param row int
078: * @param hasFocus boolean
079: * @return Component
080: */
081: public Component getTreeCellRendererComponent(JTree tree,
082: Object value, boolean selected, boolean expanded,
083: boolean leaf, int row, boolean hasFocus) {
084: DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
085: if (!(node.getUserObject() instanceof ItemModel)) {
086: return defaultRenderer;
087: }
088: ItemModel dir = (ItemModel) node.getUserObject();
089:
090: if (selected) {
091: this .setBorder(BorderFactory.createLineBorder(
092: getDefaultBackground().brighter(), 1));
093: } else {
094: this .setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
095: this .setBackground(getDefaultBackground());
096: }
097:
098: if (dir instanceof FileSystemModel) {
099: FileSystemModel fsm = (FileSystemModel) dir;
100: if (!fsm.exists()) {
101: label.setForeground(getDefaultForeground().brighter());
102: label.setFont(bold);
103: } else {
104: label.setForeground(getDefaultForeground());
105: label.setFont(normal);
106: }
107: }
108:
109: if (value instanceof DirectoryMutableTreeNode) {
110: label.setText(dir.getName());
111:
112: // There needs to be a way to specify disabled icons.
113: if (!tree.isEnabled()) {
114: if (expanded) {
115: iconLabel.setDisabledIcon(defaultRenderer
116: .getOpenIcon());
117: } else {
118: iconLabel.setDisabledIcon(defaultRenderer
119: .getClosedIcon());
120: }
121: } else {
122: if (expanded) {
123: iconLabel.setIcon(defaultRenderer.getOpenIcon());
124: } else {
125: iconLabel.setIcon(defaultRenderer.getClosedIcon());
126: }
127: }
128: return this ;
129: }
130: if (value instanceof FileMutableTreeNode) {
131: ItemModel file = (ItemModel) node.getUserObject();
132: label.setText(file.getName());
133: iconLabel.setIcon(defaultRenderer.getDefaultLeafIcon());
134: return this ;
135: } else {
136: //System.out.println("not supported obejct:" + value.getClass().getName());
137: return new DefaultTreeCellRenderer();
138: }
139: }
140:
141: private void jbInit() throws Exception {
142: this .setLayout(gridBagLayout);
143: this .setMinimumSize(new Dimension(150, 17));
144: label.setRequestFocusEnabled(true);
145: iconLabel.setText("");
146:
147: this .add(iconLabel, new GridBagConstraints(0, 0, 1, 1, 0.0,
148: 0.0, GridBagConstraints.CENTER,
149: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
150: this .add(label, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
151: GridBagConstraints.CENTER,
152: GridBagConstraints.HORIZONTAL, new Insets(0, 2, 0, 12),
153: 0, 0));
154: }
155:
156: public int getIconWidth() {
157: return defaultRenderer.getOpenIcon().getIconWidth();
158: }
159:
160: public Color getDefaultBackground() {
161: return new JLabel().getBackground();
162: }
163:
164: public Color getDefaultForeground() {
165: return new JLabel().getForeground();
166: }
167:
168: public Font getDefaultFont() {
169: return new JLabel().getFont();
170: }
171:
172: /* I think this enables changing background color
173: public boolean isOpaque() {
174: return true;
175: }
176: */
177: }
|