View Javadoc

1   // $Id: SameNameDifferentTypesView.java 169 2010-08-26 18:59:04Z 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;
19  
20  import java.beans.PropertyChangeEvent;
21  import java.beans.PropertyChangeListener;
22  import java.util.Collections;
23  import java.util.Map;
24  
25  import javax.swing.JComponent;
26  import javax.swing.JList;
27  import javax.swing.JPopupMenu;
28  import javax.swing.JScrollPane;
29  import javax.swing.JSplitPane;
30  import javax.swing.ListModel;
31  import javax.xml.namespace.QName;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.apache.xerces.xs.XSObjectList;
36  import org.springframework.binding.value.ValueModel;
37  import org.springframework.binding.value.support.ValueHolder;
38  import org.springframework.context.ApplicationEvent;
39  import org.springframework.context.ApplicationListener;
40  import org.springframework.richclient.application.PageComponentContext;
41  import org.springframework.richclient.application.support.AbstractView;
42  import org.springframework.richclient.command.CommandGroup;
43  import org.springframework.richclient.command.support.GlobalCommandIds;
44  import org.springframework.richclient.factory.ComponentFactory;
45  import org.springframework.richclient.layout.GridBagLayoutBuilder;
46  import org.springframework.richclient.list.ListSelectionValueModelAdapter;
47  import org.springframework.richclient.util.PopupMenuMouseListener;
48  
49  import de.mindcrimeilab.xsanalyzer.actions.AbstractXSObjectCommand;
50  import de.mindcrimeilab.xsanalyzer.actions.TypeHierarchyCommand;
51  import de.mindcrimeilab.xsanalyzer.actions.TypeInspectionCommand;
52  import de.mindcrimeilab.xsanalyzer.actions.XsAnalyzerApplicationEvent;
53  import de.mindcrimeilab.xsanalyzer.actions.XsElementPropertiesExecutor;
54  import de.mindcrimeilab.xsanalyzer.model.JListSelectionValueModelAdapter;
55  import de.mindcrimeilab.xsanalyzer.model.SameNameDetailListModelFactory;
56  import de.mindcrimeilab.xsanalyzer.model.SameNameListModel;
57  import de.mindcrimeilab.xsanalyzer.model.XsAnalyzerApplicationModel;
58  import de.mindcrimeilab.xsanalyzer.rules.constraints.SchemaTypeConstraint;
59  import de.mindcrimeilab.xsanalyzer.ui.MasterListDetailList;
60  import de.mindcrimeilab.xsanalyzer.ui.filter.FilterToolbarFactory;
61  import de.mindcrimeilab.xsanalyzer.ui.renderer.QNameRenderer;
62  import de.mindcrimeilab.xsanalyzer.ui.renderer.SchemaElementsRenderer;
63  
64  /**
65   * 
66   * @author Michael Engelhardt<me@mindcrime-ilab.de>
67   * @author $Author: agony $
68   * @version $Revision: 169 $
69   * 
70   */
71  public class SameNameDifferentTypesView extends AbstractView implements ApplicationListener {
72  
73      private static final Log logger = LogFactory.getLog("xsAnalyzerApplicationLogger");
74  
75      private XsAnalyzerApplicationModel model;
76  
77      private final MasterListDetailList masterDetailList;
78  
79      private final AbstractXSObjectCommand typeHierarchyCommand = new TypeHierarchyCommand();
80  
81      private final AbstractXSObjectCommand typeInspectionCommand = new TypeInspectionCommand();
82  
83      private final XsElementPropertiesExecutor xsElementPropertiesExecutor = new XsElementPropertiesExecutor();
84  
85      private final ValueModel numNames = new ValueHolder(0);
86  
87      private final ValueModel numTypes = new ValueHolder(0);
88  
89      private final SchemaTypeConstraint elementTypeConstraint = new SchemaTypeConstraint();
90  
91      /**
92       * 
93       */
94      public SameNameDifferentTypesView() {
95          masterDetailList = new MasterListDetailList();
96          masterDetailList.setListModelFactory(new SameNameDetailListModelFactory(elementTypeConstraint));
97  
98          masterDetailList.addPropertyChangeListener(MasterListDetailList.PC_DETAILLIST_LISTMODEL, new PropertyChangeListener() {
99  
100             @Override
101             public void propertyChange(PropertyChangeEvent evt) {
102                 logger.debug("Adjusting commands to new model");
103                 final ListModel listModel = (ListModel) evt.getNewValue();
104                 final ValueModel selectionModel = new ListSelectionValueModelAdapter(masterDetailList.getDetailList().getSelectionModel());
105                 final ValueModel vModel = new JListSelectionValueModelAdapter(listModel, (ListSelectionValueModelAdapter) selectionModel);
106                 typeHierarchyCommand.setValueModel(vModel);
107                 typeInspectionCommand.setValueModel(vModel);
108                 numTypes.setValue(listModel.getSize());
109             }
110         });
111     }
112 
113     @Override
114     protected void registerLocalCommandExecutors(PageComponentContext context) {
115         context.register(GlobalCommandIds.PROPERTIES, xsElementPropertiesExecutor);
116     }
117 
118     /*
119      * (non-Javadoc)
120      * 
121      * @see org.springframework.richclient.application.support.AbstractView#createControl()
122      */
123     @Override
124     protected JComponent createControl() {
125 
126         final JList masterList = masterDetailList.getMasterList();
127         final JList detailList = masterDetailList.getDetailList();
128         detailList.setCellRenderer(new SchemaElementsRenderer());
129         masterList.setCellRenderer(new QNameRenderer());
130 
131         final CommandGroup group = getWindowCommandManager().createCommandGroup("typeListCommandGroup", new Object[] { typeHierarchyCommand, typeInspectionCommand, "separator", GlobalCommandIds.PROPERTIES});
132         final JPopupMenu popup = group.createPopupMenu();
133         detailList.addMouseListener(new PopupMenuMouseListener(popup));
134 
135         // build master view
136         final GridBagLayoutBuilder masterPanelBuilder = new GridBagLayoutBuilder();
137         final ComponentFactory cf = getComponentFactory();
138         masterPanelBuilder.append(cf.createLabel("sameNameDifferentTypesView.nameList.label", new ValueModel[] { numNames})).nextLine();
139         masterPanelBuilder.append(cf.createScrollPane(masterList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), 1, 1, true, true);
140 
141         // build detail view
142         final GridBagLayoutBuilder detailPanelBuilder = new GridBagLayoutBuilder();
143         detailPanelBuilder.append(FilterToolbarFactory.createFilterToolBar(elementTypeConstraint), 1, 1, true, false).nextLine();
144         detailPanelBuilder.append(cf.createLabel("sameNameDifferentTypesView.typeList.label", new ValueModel[] { numTypes})).nextLine();
145         detailPanelBuilder.append(cf.createScrollPane(detailList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), 1, 1, true, true);
146 
147         final JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
148         jsp.setDividerLocation(0.5f);
149         jsp.setTopComponent(masterPanelBuilder.getPanel());
150         jsp.setBottomComponent(detailPanelBuilder.getPanel());
151         return jsp;
152     }
153 
154     /*
155      * (non-Javadoc)
156      * 
157      * @see
158      * org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
159      */
160     @Override
161     public void onApplicationEvent(ApplicationEvent event) {
162         if (event instanceof XsAnalyzerApplicationEvent) {
163             switch (((XsAnalyzerApplicationEvent) event).getEventType()) {
164                 case SAME_NAMES:
165                     XsAnalyzerApplicationModel appmodel = (XsAnalyzerApplicationModel) getApplicationContext().getBean("applicationModel");
166                     setModel(appmodel);
167                     break;
168                 case OPEN:
169                     invalidate();
170                     break;
171             }
172         }
173     }
174 
175     /**
176      * @param model
177      *            the model to set
178      */
179     public void setModel(XsAnalyzerApplicationModel model) {
180         this.model = model;
181         updateModel();
182     }
183 
184     private void updateModel() {
185         if (null != model) {
186             final Map<QName, XSObjectList> sameNamesDifferentTypes = model.getSameNamesDifferentTypes();
187             update(sameNamesDifferentTypes);
188         }
189     }
190 
191     /**
192      * @param sameNamesDifferentTypes
193      */
194     private void update(final Map<QName, XSObjectList> sameNamesDifferentTypes) {
195         ListModel listModel = new SameNameListModel(sameNamesDifferentTypes);
196         masterDetailList.setMasterListModel(listModel);
197         numNames.setValue(listModel.getSize());
198     }
199 
200     private void invalidate() {
201         this.update(Collections.<QName, XSObjectList> emptyMap());
202     }
203 }