1 // $Id: TypeHierarchyPanel.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.panels;
19
20 import java.awt.BorderLayout;
21
22 import javax.swing.JPanel;
23 import javax.swing.JScrollPane;
24 import javax.swing.JTree;
25 import javax.swing.ScrollPaneConstants;
26 import javax.swing.tree.TreeModel;
27
28 import org.apache.xerces.xs.XSObject;
29
30 import de.mindcrimeilab.swing.util.JTreeHelper;
31 import de.mindcrimeilab.xsanalyzer.model.TypeHierarchyTreeModelAdapter;
32 import de.mindcrimeilab.xsanalyzer.ui.renderer.SchemaElementsRenderer;
33
34 /**
35 * Type hierarchy panel displays the type hierarchy (extension / restriction) for a given type.
36 *
37 * @author Michael Engelhardt<me@mindcrime-ilab.de>
38 * @author $Author: agony $
39 * @version $Revision: 165 $
40 *
41 */
42 public class TypeHierarchyPanel extends JPanel {
43
44 /**
45 * serial version UID
46 */
47 private static final long serialVersionUID = -131361261198571107L;
48
49 /**
50 * Type hierarchy tree
51 */
52 private JTree jtTypeHierarchy;
53
54 /**
55 * ctor() Construct new type hierarchy panel
56 */
57 public TypeHierarchyPanel() {
58 super(new BorderLayout());
59 initializeGui();
60 }
61
62 /**
63 * Initialize GUI
64 */
65 private final void initializeGui() {
66 jtTypeHierarchy = new JTree();
67 jtTypeHierarchy.setCellRenderer(new SchemaElementsRenderer());
68 add(new JScrollPane(jtTypeHierarchy, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
69 }
70
71 /**
72 * Get type for which the hierarchy was created.
73 *
74 * @return
75 */
76 public XSObject getType() {
77 final TreeModel model = jtTypeHierarchy.getModel();
78 Object node = null;
79 if (null != model) {
80 node = model.getRoot();
81 while (!model.isLeaf(node)) {
82 node = model.getChild(node, model.getChildCount(node) - 1);
83 }
84 }
85 return (XSObject) node;
86 }
87
88 /**
89 * Set type
90 *
91 * @param type
92 */
93 public void setType(XSObject type) {
94 TreeModel model = jtTypeHierarchy.getModel();
95 model = null;
96 jtTypeHierarchy.setModel(new TypeHierarchyTreeModelAdapter(type));
97 JTreeHelper.expandToLast(jtTypeHierarchy);
98 }
99 }