View Javadoc

1   /*
2    * Copyright 2002-2006 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5    * the License. You may obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10   * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11   * specific language governing permissions and limitations under the License.
12   */
13  package de.mindcrimeilab.xsanalyzer;
14  
15  import java.util.LinkedList;
16  import java.util.List;
17  import java.util.Set;
18  
19  import javax.swing.JComponent;
20  
21  import org.apache.xerces.impl.xs.XSGroupDecl;
22  import org.apache.xerces.xs.XSElementDeclaration;
23  import org.apache.xerces.xs.XSModel;
24  import org.apache.xerces.xs.XSNamespaceItem;
25  import org.apache.xerces.xs.XSTypeDefinition;
26  import org.jfree.chart.ChartFactory;
27  import org.jfree.chart.ChartPanel;
28  import org.jfree.chart.JFreeChart;
29  import org.springframework.binding.value.ValueModel;
30  import org.springframework.binding.value.support.ValueHolder;
31  import org.springframework.context.ApplicationEvent;
32  import org.springframework.context.ApplicationListener;
33  import org.springframework.richclient.application.support.AbstractView;
34  import org.springframework.richclient.factory.ComponentFactory;
35  import org.springframework.richclient.layout.GridBagLayoutBuilder;
36  
37  import de.mindcrimeilab.xsanalyzer.actions.XsAnalyzerApplicationEvent;
38  import de.mindcrimeilab.xsanalyzer.model.PieDatasetValueModel;
39  import de.mindcrimeilab.xsanalyzer.model.XsAnalyzerApplicationModel;
40  import de.mindcrimeilab.xsanalyzer.util.XSModelHelper;
41  
42  /**
43   * This class defines the initial view to be presented in the archetypeapplication. It is constructed automatically by
44   * the platform and configured according to the bean specification in the application context.
45   * 
46   * @author Larry Streepy
47   * 
48   */
49  public class SchemaInfoView extends AbstractView implements ApplicationListener {
50  
51      private XsAnalyzerApplicationModel model;
52  
53      private final ValueModel numComplexTypes = new ValueHolder(0);
54  
55      private final ValueModel numSimpleTypes = new ValueHolder(0);
56  
57      private final ValueModel numElements = new ValueHolder(0);
58  
59      private final ValueModel numGroups = new ValueHolder(0);
60  
61      private final ValueModel targetNamespace = new ValueHolder(getMessage("No schema loaded."));
62  
63      private JFreeChart schemaElementsChart;
64  
65      private ChartPanel cpSchemaElements;
66  
67      /**
68       * @return the model
69       */
70      public XsAnalyzerApplicationModel getModel() {
71          return model;
72      }
73  
74      /**
75       * @param model
76       *            the model to set
77       */
78      public void setModel(XsAnalyzerApplicationModel model) {
79          this.model = model;
80          if (null != this.model.getSchemaModel()) {
81              update();
82          }
83      }
84  
85      /**
86       * 
87       */
88      private void update() {
89          XSModel xsModel = model.getSchemaModel();
90          if (null != xsModel) {
91              final List<XSNamespaceItem> namespaces = new LinkedList<XSNamespaceItem>();
92              final XSNamespaceItem tns = model.getTargetNamespace();
93              org.springframework.util.Assert.notNull(tns);
94              namespaces.add(tns);
95              targetNamespace.setValue(tns.getSchemaNamespace());
96              Set<? extends XSTypeDefinition> ctypes = XSModelHelper.getComponents(xsModel, namespaces, new short[] { XSTypeDefinition.COMPLEX_TYPE});
97              numComplexTypes.setValue(ctypes.size());
98              Set<? extends XSTypeDefinition> stypes = XSModelHelper.getComponents(xsModel, namespaces, new short[] { XSTypeDefinition.SIMPLE_TYPE});
99              numSimpleTypes.setValue(stypes.size());
100             List<XSElementDeclaration> elements = XSModelHelper.getElements(xsModel, namespaces);
101             numElements.setValue(elements.size());
102             List<? extends XSGroupDecl> groups = XSModelHelper.getGroups(xsModel, namespaces);
103             numGroups.setValue(groups.size());
104         }
105     }
106 
107     /**
108      * Create the actual UI control for this view. It will be placed into the window according to the layout of the page
109      * holding this view.
110      */
111     @Override
112     protected JComponent createControl() {
113         createSchemaElementsChart();
114         GridBagLayoutBuilder builder = new GridBagLayoutBuilder();
115 
116         final ComponentFactory cf = getComponentFactory();
117         builder.append(cf.createTitleLabel("schemaInfoView.titleLabel"), 1, 1, true, false).nextLine();
118         builder.append(cf.createLabel("schemaInfoView.schema.targetNamespace", new ValueModel[] { targetNamespace}), 1, 1, true, false).nextLine();
119 
120         builder.append(cpSchemaElements, 1, 1, true, true);
121         return builder.getPanel();
122     }
123 
124     public void onApplicationEvent(ApplicationEvent event) {
125         if (event instanceof XsAnalyzerApplicationEvent) {
126             switch (((XsAnalyzerApplicationEvent) event).getEventType()) {
127                 case OPEN:
128                     update();
129                     break;
130             }
131         }
132     }
133 
134     private void createSchemaElementsChart() {
135         PieDatasetValueModel<String> pieDataset = new PieDatasetValueModel<String>();
136         pieDataset.setValue(getMessage("schemaInfoView.schema.numberOfComplexTypes"), numComplexTypes);
137         pieDataset.setValue(getMessage("schemaInfoView.schema.numberOfSimpleTypes"), numSimpleTypes);
138         pieDataset.setValue(getMessage("schemaInfoView.schema.numberOfGroups"), numGroups);
139         pieDataset.setValue(getMessage("schemaInfoView.schema.numberOfElements"), numElements);
140 
141         schemaElementsChart = ChartFactory.createPieChart(getMessage("schemaInfoView.sectionLabel"), pieDataset, false, true, false);
142         cpSchemaElements = new ChartPanel(schemaElementsChart, true);
143     }
144 }