1
2
3
4
5
6
7
8
9
10
11
12
13
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
45
46
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
71
72
73
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
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
122
123
124 }
125 else if (value instanceof XSSimpleTypeDefinition) {
126 cell = (JLabel) tcrSimpleTypes.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
127
128
129
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 }