/*
* $Id: TreeViewInternalFrame.java 49 2007-05-19 19:24:42Z chammer $
* Copyright (c) 2005-2007 Carsten Hammer, Bruno Lowagie
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* This class was originally published under the MPL by Carsten Hammer.
* It was a part of iText, a Java-PDF library. You can now use it under
* the MIT License; for backward compatibility you can also use it under
* the MPL version 1.1: http://www.mozilla.org/MPL/
* A copy of the MPL license is bundled with the source code FYI.
*/
package com.lowagie.tools.swing;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.Icon;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.JTree;
import javax.swing.Timer;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import com.lowagie.tools.swing.interfaces.UpdateNodeView;
import com.lowagie.tools.swing.treenodes.AbstractTreeNode;
public class TreeViewInternalFrame extends JInternalFrame implements
PropertyChangeListener, UpdateNodeView {
class PdfTreeSelectionAdapter implements TreeSelectionListener {
private TreeViewInternalFrame adaptee;
PdfTreeSelectionAdapter(TreeViewInternalFrame adaptee) {
this.adaptee = adaptee;
}
public void valueChanged(TreeSelectionEvent e) {
adaptee.jTreeValueChanged(e);
}
}
class PDFTreeCellRenderer extends DefaultTreeCellRenderer {
private static final long serialVersionUID = 6336151672826111242L;
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected, expanded,
leaf, row, hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
if (node instanceof AbstractTreeNode) {
node.getUserObject();
setFont(getFont().deriveFont(12.1f));
Icon icon = ((AbstractTreeNode)node).getIcon();
setIcon(icon);
}
return this;
}
}
/** a serial version id. */
private static final long serialVersionUID = 519089296166252223L;
/** the object that will analyze the PDF and create all the nodes. */
AnalyzePDF analyzer;
/** The tree that will reveal the internal PDF structure. */
JTree pdfTree;
/** The table that will show info about the PDFs Crossreference table. */
JTable xrefTable = new JTable();
/** The panel that will contain info about a PDF object (card layout). */
JPanel bottom_panel;
/** The layout that will show the info about the PDF object that is being analyzed. */
CardLayout layout = new CardLayout();
/** The text pane with the info about a PDF object in the bottom panel. */
JTextPane value_text = new JTextPane();
/** Label that shows a value. */
JLabel value_label = new JLabel();
/**
* creates the frame
*
* @param title String
* @param resizable boolean
* @param closable boolean
* @param maximizable boolean
*/
public TreeViewInternalFrame(String title, boolean resizable,
boolean closable, boolean maximizable) {
super(title, resizable, closable, maximizable);
try {
initialize();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Does all the actual work when constructing this object.
*
* @throws Exception
*/
private void initialize() throws Exception {
this.getContentPane().setLayout(new BorderLayout());
JSplitPane main_splitpane = new JSplitPane();
main_splitpane.setMinimumSize(new Dimension(150, 100));
main_splitpane.setOrientation(JSplitPane.VERTICAL_SPLIT);
main_splitpane.setDividerLocation(170);
this.getContentPane().add(main_splitpane, java.awt.BorderLayout.CENTER);
pdfTree = new JTree();
pdfTree.addTreeSelectionListener(new PdfTreeSelectionAdapter(this));
pdfTree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode()));
pdfTree.setCellRenderer(new PDFTreeCellRenderer());
bottom_panel = new JPanel();
bottom_panel.setLayout(layout);
JPanel empty_panel = new JPanel();
empty_panel.setLayout(new BorderLayout());
bottom_panel.add(empty_panel, "empty");
JPanel value_panel = new JPanel();
value_panel.setLayout(new BorderLayout());
JScrollPane value_scrollpane = new JScrollPane();
value_scrollpane.setViewportView(value_label);
value_panel.add(value_scrollpane, java.awt.BorderLayout.CENTER);
bottom_panel.add(value_panel, "values");
JScrollPane text_scrollpane = new JScrollPane();
text_scrollpane.setViewportView(value_text);
bottom_panel.add(text_scrollpane, "text");
JPanel top_panel = new JPanel();
top_panel.setLayout(new BorderLayout());
JSplitPane top_scrollpane = new JSplitPane();
top_scrollpane.setDividerSize(3);
top_scrollpane.setDividerLocation(200);
JScrollPane tree_scrollpane = new JScrollPane();
tree_scrollpane.setViewportView(pdfTree);
top_scrollpane.add(tree_scrollpane, JSplitPane.TOP);
JPanel xref_panel = new JPanel();
xref_panel.setLayout(new BorderLayout());
JScrollPane xref_scrollpane = new JScrollPane();
xref_scrollpane.setViewportView(xrefTable);
xref_panel.add(xref_scrollpane, java.awt.BorderLayout.CENTER);
top_scrollpane.add(xref_panel, JSplitPane.BOTTOM);
top_panel.add(top_scrollpane, BorderLayout.CENTER);
main_splitpane.add(top_panel, JSplitPane.TOP);
main_splitpane.add(bottom_panel, JSplitPane.BOTTOM);
}
public void setTreemodel(TreeModel treemodel) {
pdfTree.setModel(treemodel);
}
/**
* This method gets called when a bound property is changed.
*
* @param evt
* A PropertyChangeEvent object describing the event source and
* the property that has changed.
*/
public void propertyChange(PropertyChangeEvent evt) {
String propertyname = evt.getPropertyName();
if (propertyname == null) {
return;
} else if (propertyname.equals("srcfile")) {
final String filename = (String) evt.getNewValue();
final PageProgressDialog progress;
progress = new PageProgressDialog(null, "PDF Analysis Progress", false);
final JInternalFrame jif = this;
EventDispatchingThread task = new EventDispatchingThread() {
public Object construct() {
AnalyzePDF analyzer = new AnalyzePDF(filename, progress);
jif.setCursor(Cursor.getDefaultCursor());
return analyzer;
}
};
jif.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
task.start();
progress.setVisible(true);
analyzer = (AnalyzePDF) task.get();
Timer activitymonitor = new Timer(250, new ActionListener() {
public void actionPerformed(ActionEvent event) {
analyzer.updatecount();
}
});
analyzer.start();
activitymonitor.start();
setTreemodel(analyzer);
this.xrefTable.setModel(analyzer.getXReftable());
}
}
/**
* Triggered when a cell in the tree is selected.
*
* @param e TreeSelectionEvent
*/
public void jTreeValueChanged(TreeSelectionEvent e) {
String event = e.getClass().toString();
if (event.equalsIgnoreCase("class javax.swing.event.TreeSelectionEvent")) {
AbstractTreeNode selectednode = (AbstractTreeNode) pdfTree
.getLastSelectedPathComponent();
System.out.println("Selected node: " + selectednode);
if (selectednode != null) {
selectednode.updateview(this);
}
}
}
/**
* This method fills the bottom window with the given text in a JtextPane
* IUpdatenodeview method
*
* @param text
* String Plain text
*/
public void showtext(String text) {
value_text.setText(text);
layout.show(bottom_panel, "text");
bottom_panel.repaint();
value_text.repaint();
}
/**
* This method clears the bottom window IUpdatenodeview method
*/
public void showempty() {
layout.show(bottom_panel, "empty");
bottom_panel.repaint();
}
/**
* This method fills the bottom window with the given text in a JLabel
* IUpdatenodeview method
*
* @param text
* String HTML formatted text
*/
public void showvalues(String text) {
value_label.setText(text);
layout.show(bottom_panel, "values");
}
}