View Javadoc

1   // $Id: XSRenderFactory.java 165 2009-05-28 21:46:38Z agony $
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.JLabel;
23  import javax.swing.JTable;
24  import javax.swing.table.DefaultTableCellRenderer;
25  import javax.swing.table.TableCellRenderer;
26  
27  import org.apache.xerces.xs.StringList;
28  import org.apache.xerces.xs.XSComplexTypeDefinition;
29  import org.apache.xerces.xs.XSConstants;
30  import org.apache.xerces.xs.XSTypeDefinition;
31  
32  import com.l2fprod.common.propertysheet.PropertyRendererFactory;
33  import com.l2fprod.common.propertysheet.PropertyRendererRegistry;
34  import com.l2fprod.common.propertysheet.PropertySheetTableModel;
35  import com.l2fprod.common.propertysheet.PropertySheetTableModel.Item;
36  
37  /**
38   * Renderer for xs properties in property sheet view.
39   * 
40   * @author Michael Engelhardt<me@mindcrime-ilab.de>
41   * @author $Author: agony $
42   * @version $Revision: 165 $
43   * 
44   */
45  public class XSRenderFactory extends PropertyRendererRegistry implements PropertyRendererFactory {
46  
47      /**
48       * ctor
49       */
50      public XSRenderFactory() {
51          super();
52          this.registerRenderer(XSTypeDefinition.class, getXSTypeDefinitionRenderer());
53          this.registerRenderer(short.class, getXSShortRenderer());
54          this.registerRenderer(StringList.class, getXSStringListRenderer());
55      }
56  
57      /**
58       * Return renderer for XSTypeDefinition properties.
59       * 
60       * @return
61       */
62      private XSTypeDefinitionRenderer getXSTypeDefinitionRenderer() {
63          return new XSTypeDefinitionRenderer();
64      }
65  
66      /**
67       * Return renderer for short properties.
68       * 
69       * @return
70       */
71      private XSShortRenderer getXSShortRenderer() {
72          return new XSShortRenderer();
73      }
74  
75      /**
76       * Return renderer for StringList properties
77       * 
78       * @return
79       */
80      private XSStringListRenderer getXSStringListRenderer() {
81          return new XSStringListRenderer();
82      }
83  
84      /**
85       * 
86       * @author Michael Engelhardt<me@mindcrime-ilab.de>
87       * @author $Author: agony $
88       * @version $Revision: 165 $
89       * 
90       */
91      private class XSShortRenderer extends DefaultTableCellRenderer {
92  
93          @Override
94          public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
95              // TODO Auto-generated method stub
96              final JLabel cell = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
97  
98              PropertySheetTableModel tableModel = (PropertySheetTableModel) table.getModel();
99  
100             Item item = tableModel.getPropertySheetElement(row);
101 
102             String name = item.getName();
103 
104             if ("contentType".equals(name)) {
105                 short id = (Short) value;
106                 switch (id) {
107                     case XSComplexTypeDefinition.CONTENTTYPE_ELEMENT:
108                         cell.setText("element");
109                         break;
110                     case XSComplexTypeDefinition.CONTENTTYPE_EMPTY:
111                         cell.setText("empty");
112                         break;
113                     case XSComplexTypeDefinition.CONTENTTYPE_MIXED:
114                         cell.setText("mixed");
115                         break;
116                     case XSComplexTypeDefinition.CONTENTTYPE_SIMPLE:
117                         cell.setText("simple");
118                         break;
119 
120                 }
121             }
122             else if ("derivationMethod".equals(name)) {
123                 short id = (Short) value;
124                 switch (id) {
125                     case XSConstants.DERIVATION_EXTENSION:
126                         cell.setText("extension");
127                         break;
128                     case XSConstants.DERIVATION_LIST:
129                         cell.setText("list");
130                         break;
131                     case XSConstants.DERIVATION_NONE:
132                         cell.setText("none");
133                         break;
134                     case XSConstants.DERIVATION_RESTRICTION:
135                         cell.setText("restriction");
136                         break;
137                     case XSConstants.DERIVATION_SUBSTITUTION:
138                         cell.setText("substitution");
139                         break;
140                     case XSConstants.DERIVATION_UNION:
141                         cell.setText("union");
142                         break;
143                 }
144             }
145             return cell;
146         }
147 
148     }
149 
150     /**
151      * 
152      * @author Michael Engelhardt<me@mindcrime-ilab.de>
153      * @author $Author: agony $
154      * @version $Revision: 165 $
155      * 
156      */
157     private class XSTypeDefinitionRenderer extends DefaultTableCellRenderer implements TableCellRenderer {
158 
159         @Override
160         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
161             // TODO Auto-generated method stub
162             final JLabel cell = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
163 
164             if (value instanceof XSTypeDefinition) {
165                 XSTypeDefinition def = (XSTypeDefinition) value;
166                 cell.setText("{" + def.getNamespace() + "}:" + def.getName());
167             }
168 
169             return cell;
170         }
171     }
172 
173     /**
174      * 
175      * @author Michael Engelhardt<me@mindcrime-ilab.de>
176      * @author $Author: agony $
177      * @version $Revision: 165 $
178      * 
179      */
180     private class XSStringListRenderer extends DefaultTableCellRenderer {
181 
182         @Override
183         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
184             final JLabel cell = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
185 
186             if (value instanceof StringList) {
187                 StringList def = (StringList) value;
188                 StringBuilder sb = new StringBuilder();
189                 for (int i = 0; i < def.getLength(); ++i) {
190                     sb.append(def.item(i));
191                     if (i < (def.getLength() - 1)) {
192                         sb.append(", ");
193                     }
194                 }
195                 cell.setText(sb.toString());
196             }
197 
198             return cell;
199         }
200     }
201 }