View Javadoc

1   // $Id:SimpleTypeCellRenderer.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.XSSimpleTypeDefinition;
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 SimpleTypeCellRenderer implements TreeCellRenderer, ListCellRenderer {
44  
45      private final static Icon icon = ((IconSource) Application.services().getService(IconSource.class)).getIcon("simpleType.icon");
46  
47      private final TreeCellRenderer delegateTreeCellRenderer = new DefaultTreeCellRenderer();
48  
49      private final ListCellRenderer delegateListCellRenderer = 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) delegateTreeCellRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
61  
62          SimpleTypeCellRenderer.renderSimpleType(value, cell);
63          return cell;
64      }
65  
66      /*
67       * (non-Javadoc)
68       * 
69       * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing .JList, java.lang.Object, int,
70       * boolean, boolean)
71       */
72      @Override
73      public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
74          JLabel cell = (JLabel) delegateListCellRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
75          SimpleTypeCellRenderer.renderSimpleType(value, cell);
76          return cell;
77      }
78  
79      /**
80       * @param value
81       * @param cell
82       */
83      private static void renderSimpleType(Object value, JLabel cell) {
84          if (value instanceof XSSimpleTypeDefinition) {
85              XSSimpleTypeDefinition xsSimple = ((XSSimpleTypeDefinition) value).getAnonymous() ? AnonymousTypeFactory.getProxy((XSSimpleTypeDefinition) value) : (XSSimpleTypeDefinition) value;
86              StringBuilder sb = new StringBuilder();
87              sb.append(xsSimple.getName());
88              sb.append(", <");
89              sb.append(xsSimple.getNamespace());
90              sb.append(">");
91              cell.setText(sb.toString());
92              cell.setIcon(SimpleTypeCellRenderer.icon);
93          }
94      }
95  
96  }