Debian Codegraph @

src/dr/app/bfe/XMLViewer.java

raw symbols SCIP
1 /*
2 * XMLViewer.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.bfe;
27
28 import javax.swing.*;
29 import javax.swing.event.ListSelectionEvent;
30 import javax.swing.event.ListSelectionListener;
31 import javax.swing.event.TreeSelectionEvent;
32 import javax.swing.event.TreeSelectionListener;
33 import javax.swing.tree.TreePath;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.io.File;
37 import java.io.IOException;
38
39 /**
40 * Package: XMLViewer
41 * Description:
42 * <p/>
43 * <p/>
44 * Created by
45 *
46 * @author Alexander V. Alekseyenko ([email protected])
47 * Date: Apr 14, 2009
48 * Time: 5:59:52 PM
49 */
50 public class XMLViewer implements ActionListener, ListSelectionListener, TreeSelectionListener {
51 private JPanel panel;
52 private JTextField filenameField;
53 private JButton browseButton;
54 private JButton viewButton;
55 private JTree xmlTree;
56 private JButton expandButton;
57 private JButton contractButton;
58 private JList idElementList;
59
60 public JFrame getFrame() {
61 return frame;
62 }
63
64 private JFrame frame;
65 JFileChooser fc;
66
67 public XMLViewer() {
68 fc = new JFileChooser();
69 fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
70 browseButton.addActionListener(this);
71 viewButton.addActionListener(this);
72 expandButton.addActionListener(this);
73 contractButton.addActionListener(this);
74 xmlTree.setModel(new XMLTreeModel(""));
75 idElementList.addListSelectionListener(this);
76 xmlTree.addTreeSelectionListener(this);
77
78 }
79
80 public XMLViewer(boolean standalone) {
81 /* setup file browser */
82 //super();
83 fc = new JFileChooser();
84 fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
85 browseButton.addActionListener(this);
86 viewButton.addActionListener(this);
87 expandButton.addActionListener(this);
88 contractButton.addActionListener(this);
89 xmlTree.setModel(new XMLTreeModel(""));
90 idElementList.addListSelectionListener(this);
91 xmlTree.addTreeSelectionListener(this);
92
93 if (standalone) {
94 frame = new JFrame("XML Structure Viewer");
95 frame.setContentPane(panel);
96 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
97 frame.pack();
98 frame.setVisible(true);
99 }
100 }
101
102 protected void view(String filename) {
103 xmlTree.setModel(new XMLTreeModel(filename));
104 idElementList.setModel(((XMLTreeModel) xmlTree.getModel()).getXmlModel());
105 }
106
107 public void actionPerformed(ActionEvent e) {
108 if (e.getSource() == browseButton) {
109 int returnVal = fc.showOpenDialog(this.panel);
110
111 if (returnVal == JFileChooser.APPROVE_OPTION) {
112 File file = fc.getSelectedFile();
113 String z = null;
114 try {
115 z = file.getCanonicalPath();
116 filenameField.setText(z);
117 view(filenameField.getText());
118 } catch (IOException fe) {
119 System.err.println("something went wrong with: " + z);
120 }
121 }
122 } else if (e.getSource() == viewButton) {
123 view(filenameField.getText());
124 } else if (e.getSource() == expandButton) {
125 for (int i = 0; i < xmlTree.getRowCount(); i++) {
126 xmlTree.expandRow(i);
127 }
128 } else if (e.getSource() == contractButton) {
129 for (int i = xmlTree.getRowCount(); i > 0; i--) {
130 xmlTree.collapseRow(i);
131 }
132 }
133 }
134
135 public void valueChanged(ListSelectionEvent e) {
136 if (e.getSource() == idElementList && !e.getValueIsAdjusting()) {
137 int index = idElementList.getSelectedIndex();
138 if (index >= 0) {
139 String selectedID = (String) idElementList.getModel().getElementAt(index);
140 System.err.println("Element with id: " + selectedID + " selected");
141 //TODO: add code to highlight the elements refering to this id in the XMLTree
142 highlightID(selectedID);
143
144 }
145 }
146 }
147
148 public void highlightID(String id) {
149 xmlTree.clearSelection();
150 if (id == null || id.equals("")) {
151 return;
152 }
153 for (int i = xmlTree.getRowCount(); i > 0; i--) {
154 xmlTree.collapseRow(i);
155 }
156 TreePath rootPath = xmlTree.getPathForRow(0);
157 highlightID(id, rootPath);
158 }
159
160 public void highlightID(String id, TreePath node) {
161 if (((XMLTreeModel.ElementObject) node.getLastPathComponent()).getId().equals(id)) {
162 xmlTree.expandPath(node);
163 xmlTree.addSelectionPath(node);
164 } else {
165 XMLTreeModel.ElementObject parent = (XMLTreeModel.ElementObject) node.getLastPathComponent();
166 for (XMLTreeModel.ElementObject child : parent.getChildren()) {
167 highlightID(id, node.pathByAddingChild(child));
168 }
169 }
170 }
171
172 public void valueChanged(TreeSelectionEvent e) {
173 idElementList.clearSelection();
174 }
175
176 public JList getIdElementList() {
177 return idElementList;
178 }
179
180 public static void main(String[] args) {
181 XMLViewer z = new XMLViewer(true);
182 }
183 }