001    package echopointng.tree;
002    /* 
003     * This file is part of the Echo Point Project.  This project is a collection
004     * of Components that have extended the Echo Web Application Framework.
005     *
006     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
007     *
008     * The contents of this file are subject to the Mozilla Public License Version
009     * 1.1 (the "License"); you may not use this file except in compliance with
010     * the License. You may obtain a copy of the License at
011     * http://www.mozilla.org/MPL/
012     *
013     * Software distributed under the License is distributed on an "AS IS" basis,
014     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015     * for the specific language governing rights and limitations under the
016     * License.
017     *
018     * Alternatively, the contents of this file may be used under the terms of
019     * either the GNU General Public License Version 2 or later (the "GPL"), or
020     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
021     * in which case the provisions of the GPL or the LGPL are applicable instead
022     * of those above. If you wish to allow use of your version of this file only
023     * under the terms of either the GPL or the LGPL, and not to allow others to
024     * use your version of this file under the terms of the MPL, indicate your
025     * decision by deleting the provisions above and replace them with the notice
026     * and other provisions required by the GPL or the LGPL. If you do not delete
027     * the provisions above, a recipient may use your version of this file under
028     * the terms of any one of the MPL, the GPL or the LGPL.
029     */
030    
031    /*
032    * The design paradigm and class name used within have been taken directly from
033    * the java.swing package has been retro-fitted to work with the NextApp Echo web framework.
034    *
035    * This file was made part of the EchoPoint project on the 25/07/2002.
036    *
037    */
038    import nextapp.echo2.app.Color;
039    import nextapp.echo2.app.Component;
040    import nextapp.echo2.app.Extent;
041    import nextapp.echo2.app.Font;
042    import nextapp.echo2.app.Label;
043    import echopointng.Tree;
044    import echopointng.xhtml.XhtmlFragment;
045    
046    /**
047     * Used to display an entry in a tree.  If the tree cell value is a
048     * DefaultMutableTreeNode and its user object is a Component
049     * then the DefaultTreeCellRenderer will return null on getTreeCellRenderText and
050     * return the Component in getTreeCellRendererComponent.
051     * <p>
052     * Otherwise it will "cast" the cell value to a String via toString() and return a
053     * LabelEx via the getTreeCellRenderText method.  It uses the Tree's TreeIcon object to 
054     * obtain an icon for "parent" and "leaf" nodes.
055     * <p>
056     * You can derived your own <code>TreeCellRenderer</code>class from this one and 
057     * specify your own icons for each node if you wish.
058     * <p>
059     * Note that this class also has Style support so you can set appearance 
060     * attributes via a Style object or the EchoPoint CSS support.
061     * <p>
062     * The standard LabelEx background, foreground and font properties are used for non
063     * selected tree cell nodes.  The selectedBackground, selectedForeground and 
064     * selectedFont properties are used for selected tree cell nodes.
065     * 
066     */
067    public class DefaultTreeCellRenderer extends Label implements TreeCellRenderer {
068    
069      public static final Font.Typeface TYPEFACE =
070        new Font.Typeface( "Helvetica", new Font.Typeface( "Verdana", Font.TIMES_ROMAN ) );
071    
072            /** The default Tree Cell Font */
073            //public static final Font DEFAULT_FONT = new Font(Font.ARIAL, Font.PLAIN, new Extent(8,Extent.PT));
074            public static final Font DEFAULT_FONT = new Font( TYPEFACE, Font.BOLD, new Extent( 8, Extent.PT ) );
075    
076            /** The default Selected Tree Cell Background */
077            public static final Color DEFAULT_SELECTED_BACKGROUND = Color.BLACK;
078            //public static final Color DEFAULT_SELECTED_BACKGROUND = new Color( 0xcd2519 );
079    
080            /** The default Selected Tree Cell FOREGROUND */
081            public static final Color DEFAULT_SELECTED_FOREGROUND = Color.WHITE;
082    
083            /** The default Selected Tree Cell Font */
084            //public static final Font DEFAULT_SELECTED_FONT = new Font(Font.ARIAL, Font.BOLD, new Extent(8,Extent.PT));
085            public static final Font DEFAULT_SELECTED_FONT = new Font( TYPEFACE, Font.BOLD, new Extent( 10, Extent.PT ) );
086            
087            public static final String PROPERTY_SELECTED_BACKGROUND = "selectedBackground";
088            public static final String PROPERTY_SELECTED_FOREGROUND = "selectedForeground";
089            public static final String PROPERTY_SELECTED_FONT = "selectedFont";
090            
091            private Color saveBackground;
092            private Color saveForeground;
093            private Font  saveFont;
094            
095            /**
096              * Creates a new instance of DefaultTreeCellRenderer.  
097              */
098            public DefaultTreeCellRenderer() {
099                    super();
100                    
101                    saveBackground = super.getBackground();
102                    saveForeground = super.getForeground();
103                    saveFont = super.getFont();
104                    
105                    //setHorizontalAlignment(EchoConstants.LEFT);
106                    setSelectedBackground(DEFAULT_SELECTED_BACKGROUND);
107                    setSelectedForeground(DEFAULT_SELECTED_FOREGROUND);
108                    setSelectedFont(DEFAULT_FONT);
109                    
110                    setBackground(null);
111                    setFont(DEFAULT_FONT);
112            }
113    
114            /**
115              * @return the background color to be used for selected tree cell nodes.
116              */
117            public Color getSelectedBackground() {
118                    return (Color) getProperty(PROPERTY_SELECTED_BACKGROUND);
119            }
120            /**
121              * @return the foreground color to be used for selected tree cell nodes.
122              */
123            public Color getSelectedForeground() {
124                    return (Color) getProperty(PROPERTY_SELECTED_FOREGROUND);
125            }
126            /**
127              * @return the font to be used for selected tree cell nodes.
128              */
129            public Font getSelectedFont() {
130                    return (Font) getProperty(PROPERTY_SELECTED_FONT);
131            }
132            
133            /**
134             * Sets the background color to be used for selected tree cell nodes.
135             * 
136             * @param newValue -  The newValue to set.
137             */
138            public void setSelectedBackground(Color newValue) {
139                    setProperty(PROPERTY_SELECTED_BACKGROUND,newValue);
140            }
141            
142            /**
143             * Sets the foreground color to be used for selected tree cell nodes.
144             * 
145             * @param newValue -  The newValue to set.
146             */
147            public void setSelectedForeground(Color newValue) {
148                    setProperty(PROPERTY_SELECTED_FOREGROUND,newValue);
149            }
150            
151            /**
152             * Sets the font to be used for selected tree cell nodes.
153             * 
154             * @param newValue -  The newValue to set.
155             */
156            public void setSelectedFont(Font newValue) {
157                    setProperty(PROPERTY_SELECTED_FONT,newValue);
158            }
159            
160            /**
161             * @see nextapp.echo2.app.Component#setBackground(nextapp.echo2.app.Color)
162             */
163            public void setBackground(Color newValue) {
164                    this.saveBackground = newValue;
165                    super.setBackground(newValue);
166            }
167            
168            /**
169             * @see nextapp.echo2.app.Component#setForeground(nextapp.echo2.app.Color)
170             */
171            public void setForeground(Color newValue) {
172                    this.saveForeground = newValue;
173                    super.setForeground(newValue);
174            }
175            
176            /**
177             * @see nextapp.echo2.app.Component#setFont(nextapp.echo2.app.Font)
178             */
179            public void setFont(Font newValue) {
180                    this.saveFont = newValue;
181                    super.setFont(newValue);
182            }
183            
184            /**
185             * @see echopointng.tree.TreeCellRenderer#getTreeCellRendererComponent(Tree, Object, boolean, boolean, boolean)
186             */
187            public Component getTreeCellRendererComponent(Tree tree, Object node, boolean selected, boolean expanded, boolean leaf) {
188                    if (node instanceof DefaultMutableTreeNode) {
189                            Object value = ((DefaultMutableTreeNode) node).getUserObject();
190                            if (value instanceof Component) {
191                                    Component c = (Component) value;
192                                    c.setEnabled(tree.isRenderEnabled());
193                                    return c;
194                            }
195                    }
196                    return null;
197            }
198            
199            /**
200             * @see echopointng.tree.TreeCellRenderer#getTreeCellRendererXhtml(echopointng.Tree, java.lang.Object, boolean, boolean, boolean)
201             */
202            public XhtmlFragment getTreeCellRendererXhtml(Tree tree, Object node, boolean selected, boolean expanded, boolean leaf) {
203                    if (node instanceof XhtmlFragment) {
204                            return (XhtmlFragment) node;
205                    }
206                    if (node instanceof DefaultMutableTreeNode) {
207                            Object value = ((DefaultMutableTreeNode) node).getUserObject();
208                            if (value instanceof XhtmlFragment) {
209                                    return (XhtmlFragment) value;
210                            }
211                    }
212                    return null;
213            }
214            /**
215              * Configures the renderer based on the passed in parameters.
216              * <p>
217              * The foreground color of the LabelEx is set based on the selection while
218              * the icon is set based on on <code>leaf</code> and <code>expanded</code>.
219              * <p>
220              * The same LabelEx object is returned for each call to this method.  The
221              * Tree rendering code uses only the public properties of the return 
222              * LabelEx.  In fact since DefaultTreeCellRenderer is derived from LabelEx
223              * it simply returns itself.
224              * 
225              * @see echopointng.tree.TreeCellRenderer#getTreeCellRendererText(Tree, Object, boolean, boolean, boolean)
226              */
227            public Label getTreeCellRendererText(Tree tree, Object node, boolean sel, boolean expanded, boolean leaf) {
228    
229                    String stringValue = "";
230                    if (node instanceof DefaultMutableTreeNode) {
231                            Object value = ((DefaultMutableTreeNode) node).getUserObject();
232                            if (value instanceof Component || value instanceof XhtmlFragment || value == null) {
233                                    return null;
234                            }
235                            stringValue = value.toString();
236                    } else {
237                            if (node != null) {
238                                    stringValue = node.toString();
239                            }
240                    }
241    
242                    // text 
243                    setText(stringValue);
244                    
245                    // fonts and colors
246                    if (sel) {
247                            super.setForeground(getSelectedForeground());
248                            super.setBackground(getSelectedBackground());
249                            if (getSelectedFont() == null)
250                                    super.setFont(tree.getFont());
251                            else
252                                    super.setFont(getSelectedFont());
253                    } else {
254                            super.setForeground(saveForeground);
255                            super.setBackground(saveBackground);
256                            if (saveFont == null)
257                                    super.setFont(tree.getFont());
258                            else
259                                    super.setFont(saveFont);
260                    }
261    
262                    // icons
263                    boolean isRoot = false;
264                    TreeModel model = tree.getModel();
265                    if (model != null)
266                            isRoot = (node == model.getRoot());
267    
268                    setIcon(null);
269                    TreeIcons icons = tree.getTreeIcons();
270                    if (icons != null) {
271                            if (isRoot && expanded) {
272                                    setIcon(icons.getIcon(TreeIcons.ICON_ROOTOPEN));
273                            } else if (isRoot) {
274                                    setIcon(icons.getIcon(TreeIcons.ICON_ROOT));
275                            } else if (leaf) {
276                                    setIcon(icons.getIcon(TreeIcons.ICON_LEAF));
277                            } else if (expanded) {
278                                    setIcon(icons.getIcon(TreeIcons.ICON_FOLDEROPEN));
279                            } else {
280                                    setIcon(icons.getIcon(TreeIcons.ICON_FOLDER));
281                            }
282                    }
283    
284                    //enabledness
285                    //setEnabled(tree.isEnabled());
286                    return this;
287            }
288    }