001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.contrib.poibrowser;
019:
020: import java.awt.event.WindowAdapter;
021: import java.awt.event.WindowEvent;
022: import java.io.FileInputStream;
023: import java.io.IOException;
024:
025: import javax.swing.JFrame;
026: import javax.swing.JScrollPane;
027: import javax.swing.JTree;
028: import javax.swing.tree.DefaultMutableTreeNode;
029: import javax.swing.tree.DefaultTreeModel;
030: import javax.swing.tree.MutableTreeNode;
031:
032: import org.apache.poi.poifs.eventfilesystem.POIFSReader;
033:
034: /**
035: * <p>The main class of the POI Browser. It shows the structure of POI
036: * filesystems (Microsoft Office documents) in a {@link
037: * JTree}. Specify their filenames on the command line!</p>
038: *
039: * @see POIFSReader
040: *
041: * @author Rainer Klute <a
042: * href="mailto:klute@rainer-klute.de"><klute@rainer-klute.de></a>
043: * @version $Id: POIBrowser.java 489730 2006-12-22 19:18:16Z bayard $
044: * @since 2002-01-19
045: */
046: public class POIBrowser extends JFrame {
047:
048: /**
049: * <p>The tree's root node must be visible to all methods.</p>
050: */
051: protected MutableTreeNode rootNode;
052:
053: /**
054: * <p>Takes a bunch of file names as command line parameters,
055: * opens each of them as a POI filesystem and displays their
056: * internal structures in a {@link JTree}.</p>
057: */
058: public static void main(String[] args) {
059: new POIBrowser().run(args);
060: }
061:
062: protected void run(String[] args) {
063: addWindowListener(new WindowAdapter() {
064: public void windowClosing(WindowEvent e) {
065: System.exit(0);
066: }
067: });
068:
069: /* Create the tree model with a root node. The latter is
070: * invisible but it must be present because a tree model
071: * always needs a root. */
072: rootNode = new DefaultMutableTreeNode("POI Filesystems");
073: DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
074:
075: /* Create the tree UI element. */
076: final JTree treeUI = new JTree(treeModel);
077: getContentPane().add(new JScrollPane(treeUI));
078:
079: /* Add the POI filesystems to the tree. */
080: int displayedFiles = 0;
081: for (int i = 0; i < args.length; i++) {
082: final String filename = args[i];
083: try {
084: POIFSReader r = new POIFSReader();
085: r.registerListener(new TreeReaderListener(filename,
086: rootNode));
087: r.read(new FileInputStream(filename));
088: displayedFiles++;
089: } catch (IOException ex) {
090: System.err.println(filename + ": " + ex);
091: } catch (Throwable t) {
092: System.err
093: .println("Unexpected exception while reading \""
094: + filename + "\":");
095: t.printStackTrace(System.err);
096: }
097: }
098:
099: /* Exit if there is no file to display (none specified or only
100: * files with problems). */
101: if (displayedFiles == 0) {
102: System.out.println("No POI filesystem(s) to display.");
103: System.exit(0);
104: }
105:
106: /* Make the tree UI element visible. */
107: treeUI.setRootVisible(true);
108: treeUI.setShowsRootHandles(true);
109: ExtendableTreeCellRenderer etcr = new ExtendableTreeCellRenderer();
110: etcr.register(DocumentDescriptor.class,
111: new DocumentDescriptorRenderer());
112: etcr.register(PropertySetDescriptor.class,
113: new PropertySetDescriptorRenderer());
114: treeUI.setCellRenderer(etcr);
115: setSize(600, 450);
116: setTitle("POI Browser 0.09");
117: setVisible(true);
118: }
119:
120: }
|