View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.guilayer.swing.models;
17  
18  import java.awt.Component;
19  import java.awt.Dimension;
20  import java.awt.Font;
21  
22  import javax.swing.Icon;
23  import javax.swing.JTree;
24  import javax.swing.tree.DefaultTreeCellRenderer;
25  
26  import org.xulux.api.core.PartRequest;
27  import org.xulux.api.dataprovider.IField;
28  import org.xulux.api.dataprovider.IMapping;
29  import org.xulux.api.gui.IWidget;
30  import org.xulux.api.rules.IRuleEngine;
31  import org.xulux.core.XuluxContext;
32  import org.xulux.guilayer.swing.util.SwingUtils;
33  import org.xulux.guilayer.swing.widgets.Tree;
34  import org.xulux.utils.ClassLoaderUtils;
35  
36  /**
37   * For now extends the defaultreeCellRenderer.
38   *
39   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
40   * @version $Id: NyxTreeCellRenderer.java,v 1.1 2005/12/18 12:58:21 mvdb Exp $
41   */
42  public class NyxTreeCellRenderer extends DefaultTreeCellRenderer {
43  
44      /**
45       * the tree widget
46       */
47      protected Tree widget;
48      
49      /**
50       * The childwidget
51       */
52      protected IWidget childWidget;
53  
54      /**
55       * the request
56       */
57      protected PartRequest request;
58      
59      /**
60       * Store a temp font in case java suddenly doesn't know what font use.
61       */
62      protected Font tmpFont;
63  
64      /**
65       * @param tree the tree
66       */
67      public NyxTreeCellRenderer(Tree tree) {
68          super();
69          this.widget = tree;
70      }
71  
72      /**
73       * Set the childwidget of the tree
74       *
75       * @param widget the widget to use for formatting. Should be a label.
76       */
77      public void setChildWidget(IWidget widget) {
78          this.childWidget = widget;
79          request = new CellPartRequest(widget);
80      }
81  
82      /**
83       * @return the childwidget
84       */    
85      public IWidget getChildWidget() {
86          return this.childWidget;
87      }
88  
89      /**
90       * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree, java.lang.Object,
91       *             boolean, boolean, boolean, int, boolean)
92       */
93      public Component getTreeCellRendererComponent(
94          JTree tree,
95          Object value,
96          boolean sel,
97          boolean expanded,
98          boolean leaf,
99          int row,
100         boolean hasFocus) {
101         String clazz = widget.getProperty("treefield.class");
102         if (value != null && clazz != null) {
103             if (ClassLoaderUtils.getClass(clazz) == value.getClass()) {
104                 IMapping mapping = XuluxContext.getDictionary().getDefaultProvider().getMapping(value);
105                 String use = widget.getProperty("treefield.use");
106                 if (use != null) {
107                     IField field = mapping.getField(use);
108                     if (field != null) {
109                         value = field.getValue(value);
110                     }
111                 }
112             }
113         }
114         if (getChildWidget() != null) {
115             request.setValue(value);
116             XuluxContext.getRuleEngine().fireFieldRequest(getChildWidget(), request, IRuleEngine.PRE_REQUEST);
117         }
118         try {
119             super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
120             if (getChildWidget() != null) {
121                 IWidget w = getChildWidget();
122                 String icon = w.getProperty("icon");
123                 if (icon != null) {
124                     Icon iconImage = SwingUtils.getIcon(icon, getChildWidget());
125                     setIcon(iconImage);
126                     // if we don't expand the preferred size with the iconwidth
127                     // it will result in dots in the node name.
128                     Dimension dim = ui.getPreferredSize(this);
129                     int width = 4;
130                     if (getFont() != null) {
131                       if (this.tmpFont == null) {
132                       	this.tmpFont = getFont();
133                       }
134                       width += getFontMetrics(getFont()).stringWidth(String.valueOf(value));
135                     } else if (tmpFont != null) {
136                     	width += getFontMetrics(tmpFont).stringWidth(String.valueOf(value));
137                     }
138                     if (iconImage != null) {
139                       width+=iconImage.getIconWidth();
140                     }
141                     dim.width=width;
142                     setPreferredSize(dim);
143                 }
144                 setToolTipText(w.getProperty("tooltip"));
145             }
146         } catch (Exception e) {
147             super.getTreeCellRendererComponent(tree, e.toString(), sel, expanded, leaf, row, hasFocus);
148         }
149         return this;
150     }
151 }