View Javadoc

1   // $Id:SchemaElementsRenderer.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  import java.util.Locale;
22  
23  import javax.swing.DefaultListCellRenderer;
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.DefaultMutableTreeNode;
29  import javax.swing.tree.DefaultTreeCellRenderer;
30  import javax.swing.tree.TreeCellRenderer;
31  import javax.swing.tree.TreeModel;
32  
33  import org.apache.xerces.xs.XSAttributeDeclaration;
34  import org.apache.xerces.xs.XSComplexTypeDefinition;
35  import org.apache.xerces.xs.XSElementDeclaration;
36  import org.apache.xerces.xs.XSModelGroupDefinition;
37  import org.apache.xerces.xs.XSSimpleTypeDefinition;
38  import org.springframework.context.MessageSource;
39  import org.springframework.richclient.application.Application;
40  
41  import de.mindcrimeilab.xsanalyzer.model.TreeModelAdapter;
42  
43  /**
44   * @author Michael Engelhardt<me@mindcrime-ilab.de>
45   * @author $Author:me $
46   * @version $Revision:62 $
47   * 
48   */
49  public class SchemaElementsRenderer implements TreeCellRenderer, ListCellRenderer {
50  
51      private final ComplexTypeCellRenderer tcrComplexTypes = new ComplexTypeCellRenderer();
52  
53      private final SimpleTypeCellRenderer tcrSimpleTypes = new SimpleTypeCellRenderer();
54  
55      private final ElementRenderer tcrElements = new ElementRenderer();
56  
57      private final AttributeRenderer tcrAttributes = new AttributeRenderer();
58  
59      private final GroupCellRenderer tcrModelGroups = new GroupCellRenderer();
60  
61      private final ListCellRenderer lcrParticle = new XSParticleRenderer();
62  
63      private final TreeCellRenderer tcrDefault = new DefaultTreeCellRenderer();
64  
65      private final ListCellRenderer lcrDefault = new DefaultListCellRenderer();
66  
67      private static final MessageSource MESSAGE_SOURCE = (MessageSource) Application.services().getService(MessageSource.class);
68  
69      /*
70       * (non-Javadoc)
71       * 
72       * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree, java.lang.Object, boolean,
73       * boolean, boolean, int, boolean)
74       */
75      @Override
76      public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
77  
78          JLabel cell = null;
79          if (value instanceof XSComplexTypeDefinition) {
80              cell = (JLabel) tcrComplexTypes.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
81          }
82          else if (value instanceof XSElementDeclaration) {
83              cell = (JLabel) tcrElements.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
84          }
85          else if (value instanceof XSSimpleTypeDefinition) {
86              cell = (JLabel) tcrSimpleTypes.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
87          }
88          else if (value instanceof XSModelGroupDefinition) {
89              cell = (JLabel) tcrModelGroups.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
90          }
91          else if (value instanceof XSAttributeDeclaration) {
92              cell = (JLabel) tcrAttributes.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
93          }
94          else if (value instanceof DefaultMutableTreeNode) {
95              cell = (JLabel) getTreeCellRendererComponent(tree, ((DefaultMutableTreeNode) value).getUserObject(), selected, expanded, leaf, row, hasFocus);
96          }
97          else if (value instanceof String) {
98              // @TODO re-think
99              final TreeModel model = tree.getModel();
100             final String message;
101             if (model instanceof TreeModelAdapter) {
102                 message = SchemaElementsRenderer.getMessage((String) value, new Object[] { model.getChildCount(value)});
103             }
104             else {
105                 message = SchemaElementsRenderer.getMessage((String) value);
106             }
107             cell = (JLabel) tcrDefault.getTreeCellRendererComponent(tree, message, selected, expanded, leaf, row, hasFocus);
108         }
109         else {
110             cell = (JLabel) tcrDefault.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
111         }
112 
113         return cell;
114     }
115 
116     @Override
117     public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
118         JLabel cell = null;
119         if (value instanceof XSComplexTypeDefinition) {
120             cell = (JLabel) tcrComplexTypes.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
121             // } else if (value instanceof XSElementDeclaration) {
122             // cell = (JLabel) tcrElements.getListCellRendererComponent(list,
123             // value, index, isSelected, cellHasFocus);
124         }
125         else if (value instanceof XSSimpleTypeDefinition) {
126             cell = (JLabel) tcrSimpleTypes.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
127             // } else if (value instanceof XSModelGroupDefinition) {
128             // cell = (JLabel) tcrModelGroups.getListCellRendererComponent(list,
129             // value, index, isSelected, cellHasFocus);
130         }
131         else if (value instanceof XSAttributeDeclaration) {
132             cell = (JLabel) tcrAttributes.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
133         }
134         else if (value instanceof XSAttributeDeclaration) {
135             cell = (JLabel) lcrParticle.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
136         }
137         else {
138             cell = (JLabel) lcrDefault.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
139         }
140 
141         return cell;
142     }
143 
144     private static String getMessage(String code) {
145         return SchemaElementsRenderer.MESSAGE_SOURCE.getMessage(code, null, code, Locale.getDefault());
146     }
147 
148     private static String getMessage(String code, Object[] args) {
149         return SchemaElementsRenderer.MESSAGE_SOURCE.getMessage(code, args, code, Locale.getDefault());
150     }
151 }