View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  Fredrik Vraalsen (fredrik dot vraalsen at sintef dot no)
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
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 GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
20   */
21  package coras.client.ui;
22  
23  import java.awt.Component;
24  import java.awt.Font;
25  
26  import javax.swing.Icon;
27  import javax.swing.ImageIcon;
28  import javax.swing.JLabel;
29  import javax.swing.JTree;
30  import javax.swing.tree.DefaultMutableTreeNode;
31  import javax.swing.tree.DefaultTreeCellRenderer;
32  
33  import coras.common.CorasAsset;
34  import coras.reuse.ExperiencePackage;
35  import coras.riskanalysis.RiskAnalysisProject;
36  import coras.structure.SubProcessEnum;
37  import coras.types.IDocument;
38  import coras.types.IFaultTree;
39  import coras.types.ITable;
40  import coras.types.IUMLModel;
41  
42  /***
43   * @author fvr
44   *
45   * TODO To change the template for this generated type comment go to
46   * Window - Preferences - Java - Code Style - Code Templates
47   */
48  public class CorasTreeCellRenderer extends DefaultTreeCellRenderer {
49      
50      private static Icon FOLDER_OPEN_ICON = null;
51      private static Icon FOLDER_CLOSED_ICON = null;
52      public static Icon UML_ICON = null;
53      public static Icon TABLE_ICON = null;
54      public static Icon DOCUMENT_ICON = null;
55      public static Icon FAULT_TREE_ICON = null;
56      
57      static {
58      	ClassLoader cl = CorasTreeCellRenderer.class.getClassLoader();
59      	FOLDER_OPEN_ICON = new ImageIcon(cl.getResource("images/icons/folderOpen.gif"));
60      	FOLDER_CLOSED_ICON = new ImageIcon(cl.getResource("images/icons/folderClosed.gif"));
61      	UML_ICON = new ImageIcon(cl.getResource("images/icons/uml.gif"));
62      	TABLE_ICON = new ImageIcon(cl.getResource("images/icons/table.gif"));
63      	DOCUMENT_ICON = new ImageIcon(cl.getResource("images/icons/document.gif"));
64      	FAULT_TREE_ICON = new ImageIcon(cl.getResource("images/icons/tree.gif"));
65      }
66  
67      public Component getTreeCellRendererComponent(JTree tree, Object value,
68              boolean sel, boolean expanded, boolean leaf, int row,
69              boolean hasFocus) {
70          Component result = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
71          if (result instanceof JLabel) {
72              JLabel label = (JLabel) result;
73              Object userObject = value instanceof DefaultMutableTreeNode ? ((DefaultMutableTreeNode)value).getUserObject() : null;
74              label.setText(value != null ? value.toString() : "null");
75              Icon icon;
76              if (userObject instanceof RiskAnalysisProject || userObject instanceof ExperiencePackage || userObject instanceof SubProcessEnum) {
77                  if (expanded) {
78                      // icon = FOLDER_OPEN_ICON;
79                      icon = super.getDefaultOpenIcon();
80                  } else {
81                      // icon = FOLDER_CLOSED_ICON;
82                      icon = super.getDefaultClosedIcon();
83                  }
84              } else if (userObject instanceof IUMLModel) {
85                  icon = UML_ICON;
86              } else if (userObject instanceof ITable) {
87                  icon = TABLE_ICON;
88              } else if (userObject instanceof IDocument) {
89                  icon = DOCUMENT_ICON;
90              } else if (userObject instanceof IFaultTree) {
91                  icon = FAULT_TREE_ICON;
92              } else {
93                  icon = null;
94              }
95              label.setIcon(icon);
96              Font font = label.getFont();
97              Font plainFont = font.isPlain() ? font : font.deriveFont(Font.PLAIN);
98              Font italicFont = font.isItalic() ? font : font.deriveFont(Font.ITALIC);
99              if (userObject instanceof CorasAsset && ((CorasAsset)userObject).isDeleted()) {
100             	label.setFont(italicFont);
101             } else {
102             	label.setFont(plainFont);
103             }
104         }
105         return result;
106     }
107 }