View Javadoc

1   // $Id:ComplexTypeCellRenderer.java 62 2008-04-20 12:28:56Z me $
2   /*
3    * xsAnalyzer - XML schema analyzing tool. Copyright (C) 2008 Michael Engelhardt
4    * 
5    * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6    * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
7    * version.
8    * 
9    * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
10   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11   * 
12   * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
13   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14   */
15  /**
16   * 
17   */
18  package de.mindcrimeilab.xsanalyzer.ui.renderer;
19  
20  import java.awt.Component;
21  
22  import javax.swing.DefaultListCellRenderer;
23  import javax.swing.Icon;
24  import javax.swing.JLabel;
25  import javax.swing.JList;
26  import javax.swing.JTree;
27  import javax.swing.ListCellRenderer;
28  import javax.swing.tree.DefaultTreeCellRenderer;
29  import javax.swing.tree.TreeCellRenderer;
30  
31  import org.apache.xerces.xs.XSComplexTypeDefinition;
32  import org.springframework.richclient.application.Application;
33  import org.springframework.richclient.image.IconSource;
34  
35  import de.mindcrimeilab.xsanalyzer.xsext.AnonymousTypeFactory;
36  
37  /**
38   * @author Michael Engelhardt<me@mindcrime-ilab.de>
39   * @author $Author:me $
40   * @version $Revision:62 $
41   * 
42   */
43  public class ComplexTypeCellRenderer implements TreeCellRenderer, ListCellRenderer {
44  
45      private final static Icon icon = ((IconSource) Application.services().getService(IconSource.class)).getIcon("complexType.icon");
46  
47      private final TreeCellRenderer treeCellRendererDelegate = new DefaultTreeCellRenderer();
48  
49      private final ListCellRenderer listCellRendererDelegate = new DefaultListCellRenderer();
50  
51      /*
52       * (non-Javadoc)
53       * 
54       * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax. swing.JTree, java.lang.Object,
55       * boolean, boolean, boolean, int, boolean)
56       */
57      @Override
58      public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
59  
60          JLabel cell = (JLabel) treeCellRendererDelegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
61  
62          ComplexTypeCellRenderer.renderComplexType(value, cell);
63  
64          return cell; // TODO Auto-generated method stub
65      }
66  
67      @Override
68      public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
69  
70          JLabel cell = (JLabel) listCellRendererDelegate.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
71  
72          ComplexTypeCellRenderer.renderComplexType(value, cell);
73  
74          return cell;
75      }
76  
77      /**
78       * @param value
79       * @param cell
80       */
81      private static void renderComplexType(final Object value, JLabel cell) {
82          if (value instanceof XSComplexTypeDefinition) {
83              XSComplexTypeDefinition complexType = ((XSComplexTypeDefinition) value).getAnonymous() ? AnonymousTypeFactory.getProxy((XSComplexTypeDefinition) value) : (XSComplexTypeDefinition) value;
84              StringBuilder sb = new StringBuilder();
85              sb.append(complexType.getName());
86              sb.append(", <");
87              sb.append(complexType.getNamespace());
88              sb.append(">");
89              cell.setText(sb.toString());
90              cell.setIcon(ComplexTypeCellRenderer.icon);
91          }
92      }
93  
94  }