| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 82 |
|
| 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 |
|
| 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 |
} |