View Javadoc

1   // $Id: OutlineToolWindow.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;
19  
20  import java.awt.BorderLayout;
21  import java.awt.event.MouseAdapter;
22  import java.awt.event.MouseEvent;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import javax.swing.JComponent;
27  import javax.swing.JPanel;
28  import javax.swing.JPopupMenu;
29  import javax.swing.JTree;
30  import javax.swing.ScrollPaneConstants;
31  import javax.swing.event.TreeSelectionEvent;
32  import javax.swing.event.TreeSelectionListener;
33  import javax.swing.tree.DefaultMutableTreeNode;
34  import javax.swing.tree.TreePath;
35  
36  import org.apache.xerces.xs.XSObject;
37  import org.springframework.binding.value.ValueModel;
38  import org.springframework.context.ApplicationEvent;
39  import org.springframework.context.ApplicationListener;
40  import org.springframework.richclient.application.Application;
41  import org.springframework.richclient.application.PageComponentContext;
42  import org.springframework.richclient.application.statusbar.StatusBar;
43  import org.springframework.richclient.application.support.AbstractView;
44  import org.springframework.richclient.command.CommandGroup;
45  import org.springframework.richclient.command.support.GlobalCommandIds;
46  import org.springframework.richclient.util.PopupMenuMouseListener;
47  
48  import de.mindcrimeilab.util.DefaultMutableTreeNodeAccessor;
49  import de.mindcrimeilab.xsanalyzer.actions.TypeInspectionCommand;
50  import de.mindcrimeilab.xsanalyzer.actions.TypeHierarchyCommand;
51  import de.mindcrimeilab.xsanalyzer.actions.TypeHierarchyExecutor;
52  import de.mindcrimeilab.xsanalyzer.actions.XsAnalyzerApplicationEvent;
53  import de.mindcrimeilab.xsanalyzer.actions.XsElementPropertiesExecutor;
54  import de.mindcrimeilab.xsanalyzer.model.JTreeValueModelAdapter;
55  import de.mindcrimeilab.xsanalyzer.model.TreeModelAdapter;
56  import de.mindcrimeilab.xsanalyzer.model.TreeSelectionValueModelAdapter;
57  import de.mindcrimeilab.xsanalyzer.model.TreeSingleSelectionGuard;
58  import de.mindcrimeilab.xsanalyzer.model.XsAnalyzerApplicationModel;
59  import de.mindcrimeilab.xsanalyzer.ui.renderer.SchemaElementsRenderer;
60  
61  /**
62   * @author Michael Engelhardt<me@mindcrime-ilab.de>
63   * @author $Author: agony $
64   * @version $Revision: 165 $
65   * 
66   */
67  public class OutlineToolWindow extends AbstractView implements ApplicationListener {
68  
69      private JTree jtStructure;
70  
71      private XsAnalyzerApplicationModel model;
72  
73      private final XsElementPropertiesExecutor xsElementPropertiesExecutor = new XsElementPropertiesExecutor();
74  
75      private final TypeHierarchyExecutor typeHierarchyExecutor = new TypeHierarchyExecutor();
76  
77      @Override
78      protected void registerLocalCommandExecutors(PageComponentContext context) {
79          context.register(GlobalCommandIds.PROPERTIES, xsElementPropertiesExecutor);
80      }
81  
82      /**
83       * @return the model
84       */
85      public XsAnalyzerApplicationModel getModel() {
86          return model;
87      }
88  
89      /**
90       * @param model
91       *            the model to set
92       */
93      public void setModel(XsAnalyzerApplicationModel model) {
94          this.model = model;
95          if (null != this.model.getSchemaModel() && null != jtStructure) {
96              jtStructure.setModel(new TreeModelAdapter(this.model.getSchemaModel()));
97          }
98      }
99  
100     /*
101      * Create the actual UI control for this view. It will be placed into the window according to the layout of the page
102      * holding this view.
103      * 
104      * (non-Javadoc)
105      * 
106      * @see org.springframework.richclient.application.support.AbstractView#createControl ()
107      */
108     @Override
109     protected JComponent createControl() {
110         createTree();
111         JPanel jpOutline = getComponentFactory().createPanel(new BorderLayout());
112         jpOutline.add(getComponentFactory().createScrollPane(jtStructure, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
113         return jpOutline;
114     }
115 
116     @Override
117     public void onApplicationEvent(ApplicationEvent event) {
118         logger.debug(event);
119         if (event instanceof XsAnalyzerApplicationEvent) {
120             switch (((XsAnalyzerApplicationEvent) event).getEventType()) {
121                 case OPEN:
122                     jtStructure.setModel(new TreeModelAdapter(model.getSchemaModel()));
123                     StatusBar bar = Application.instance().getLifecycleAdvisor().getStatusBar();
124                     bar.setMessage(getMessage("statusBar.label.action.open", new Object[] { model.getSchemaFile().getAbsolutePath()}));
125                     break;
126             }
127         }
128     }
129 
130     private void createTree() {
131         jtStructure = new JTree(new DefaultMutableTreeNode(getMessage("outlineView.structureTree.initialMessage")));
132         jtStructure.setCellRenderer(new SchemaElementsRenderer());
133 
134         final ValueModel selectionHolder = new TreeSelectionValueModelAdapter(jtStructure.getSelectionModel());
135         final ValueModel vModel = new JTreeValueModelAdapter((TreeSelectionValueModelAdapter) selectionHolder);
136 
137         final DefaultMutableTreeNodeAccessor accessor = new DefaultMutableTreeNodeAccessor();
138         final CommandGroup group = getWindowCommandManager().createCommandGroup("schemaElementsCommandGroup", new Object[] { new TypeHierarchyCommand(vModel), new TypeInspectionCommand(vModel), "separator", "propertiesCommand"});
139 
140         jtStructure.addMouseListener(new PopupMenuMouseListener() {
141 
142             @Override
143             protected JPopupMenu getPopupMenu(MouseEvent e) {
144                 return group.createPopupMenu();
145             }
146         });
147         jtStructure.addMouseListener(new MouseAdapter() {
148 
149             @Override
150             public void mouseClicked(MouseEvent evt) {
151                 if (evt.getClickCount() == 2) {
152                     final Object selectedObject = jtStructure.getLastSelectedPathComponent();
153                     if (selectedObject instanceof XSObject) {
154                         Map<String, XSObject> parameters = new HashMap<String, XSObject>();
155                         parameters.put(TypeHierarchyExecutor.TARGET_OBJECT, (XSObject) selectedObject);
156                         typeHierarchyExecutor.execute(parameters);
157                     }
158                 }
159             }
160         });
161 
162         jtStructure.addTreeSelectionListener(new TreeSelectionListener() {
163 
164             @Override
165             public void valueChanged(TreeSelectionEvent e) {
166                 TreePath path = e.getPath();
167                 if (null != path) {
168                     Map<String, XSObject> parameters = new HashMap<String, XSObject>();
169 
170                     final Object lastPathComponent = path.getLastPathComponent();
171                     if (lastPathComponent instanceof XSObject) {
172                         parameters.put(XsElementPropertiesExecutor.OBJECT, (XSObject) lastPathComponent);
173                         xsElementPropertiesExecutor.execute(parameters);
174                     }
175                 }
176 
177             }
178 
179         });
180 
181         new TreeSingleSelectionGuard(selectionHolder, xsElementPropertiesExecutor);
182         new TreeSingleSelectionGuard(selectionHolder, typeHierarchyExecutor);
183         // new XSObjectTreeSingleSelectionGuard(selectionHolder, (Guarded)
184         // getWindowCommandManager().getCommand("testCommand"));
185     }
186 }