KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > tree > view > BasicTreeCellRenderer


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui.tree.view;
25
26 import org.objectweb.fractal.gui.Constants;
27 import org.objectweb.fractal.gui.model.Component;
28
29 import java.awt.Color JavaDoc;
30 import java.awt.Font JavaDoc;
31 import java.net.URL JavaDoc;
32
33 import javax.swing.JTree JavaDoc;
34 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
35 import javax.swing.ImageIcon JavaDoc;
36
37 /**
38  * A {@link javax.swing.tree.TreeCellRenderer} to draw component names with a
39  * color indicating their type and status. More precisely, the name of a slave
40  * component is drawn in italic, the name of invalid component is drawn in
41  * {@link Constants#ERROR_COLOR ERROR_COLOR}, and missing components names are
42  * drawn as "<missing&gt".
43  */

44
45 public class BasicTreeCellRenderer extends DefaultTreeCellRenderer JavaDoc {
46
47   /**
48    * The font used by the super class to tree cells.
49    */

50
51   private Font JavaDoc normalFont;
52
53   public java.awt.Component JavaDoc getTreeCellRendererComponent (
54     final JTree JavaDoc tree,
55     final Object JavaDoc value,
56     final boolean sel,
57     final boolean expanded,
58     final boolean leaf,
59     final int row,
60     final boolean hasFocus)
61   {
62     super.getTreeCellRendererComponent(
63       tree, value, sel, expanded, leaf, row, hasFocus);
64     if (!(value instanceof Component)) {
65       return this;
66     }
67     if (normalFont == null) {
68       normalFont = getFont();
69     }
70     Component c = (Component)value;
71     long status = c.getStatus();
72     setForeground (status == 0 ? Color.black : Constants.ERROR_COLOR);
73     if ((status & Component.NAME_MISSING) != 0) {
74       setText("<missing>");
75     } else {
76       setText(c.getName());
77     }
78
79     Color JavaDoc col = new Color JavaDoc (255, 255, 204);
80     tree.setBackground (col);
81     setBackgroundNonSelectionColor(col);
82     setBackgroundSelectionColor(new Color JavaDoc(255, 204, 102));
83
84     URL JavaDoc url_item = getClass().getResource
85       ("/org/objectweb/fractal/gui/resources/tr_item.gif");
86         this.setLeafIcon (new ImageIcon JavaDoc(url_item));
87     URL JavaDoc url_open = getClass().getResource
88       ("/org/objectweb/fractal/gui/resources/tr_open.gif");
89     this.setOpenIcon (new ImageIcon JavaDoc (url_open));
90     URL JavaDoc url_closed = getClass().getResource
91       ("/org/objectweb/fractal/gui/resources/tr_closed.gif");
92     this.setClosedIcon (new ImageIcon JavaDoc (url_closed));
93
94     if (c.getMasterComponent() != null) {
95       setFont(normalFont.deriveFont(Font.ITALIC));
96     } else {
97       setFont(normalFont);
98     }
99     return this;
100   }
101 }
102
Popular Tags