KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > contrib > poibrowser > POIBrowser


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.contrib.poibrowser;
20
21 import java.awt.event.WindowAdapter JavaDoc;
22 import java.awt.event.WindowEvent JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import javax.swing.JFrame JavaDoc;
27 import javax.swing.JScrollPane JavaDoc;
28 import javax.swing.JTree JavaDoc;
29 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
30 import javax.swing.tree.DefaultTreeModel JavaDoc;
31 import javax.swing.tree.MutableTreeNode JavaDoc;
32
33 import org.apache.poi.poifs.eventfilesystem.POIFSReader;
34
35 /**
36  * <p>The main class of the POI Browser. It shows the structure of POI
37  * filesystems (Microsoft Office documents) in a {@link
38  * JTree}. Specify their filenames on the command line!</p>
39  *
40  * @see POIFSReader
41  *
42  * @author Rainer Klute <a
43  * HREF="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
44  * @version $Id: POIBrowser.java,v 1.8 2004/06/22 16:16:33 klute Exp $
45  * @since 2002-01-19
46  */

47 public class POIBrowser extends JFrame JavaDoc
48 {
49
50     /**
51      * <p>The tree's root node must be visible to all methods.</p>
52      */

53     protected MutableTreeNode JavaDoc rootNode;
54
55
56
57     /**
58      * <p>Takes a bunch of file names as command line parameters,
59      * opens each of them as a POI filesystem and displays their
60      * internal structures in a {@link JTree}.</p>
61      */

62     public static void main(String JavaDoc[] args)
63     {
64         new POIBrowser().run(args);
65     }
66
67
68
69     protected void run(String JavaDoc[] args)
70     {
71         addWindowListener(new WindowAdapter JavaDoc()
72             {
73                 public void windowClosing(WindowEvent JavaDoc e)
74                 {
75                         System.exit(0);
76                 }
77             });
78
79         /* Create the tree model with a root node. The latter is
80          * invisible but it must be present because a tree model
81          * always needs a root. */

82         rootNode = new DefaultMutableTreeNode JavaDoc("POI Filesystems");
83         DefaultTreeModel JavaDoc treeModel = new DefaultTreeModel JavaDoc(rootNode);
84
85         /* Create the tree UI element. */
86         final JTree JavaDoc treeUI = new JTree JavaDoc(treeModel);
87         getContentPane().add(new JScrollPane JavaDoc(treeUI));
88
89         /* Add the POI filesystems to the tree. */
90         int displayedFiles = 0;
91         for (int i = 0; i < args.length; i++)
92         {
93             final String JavaDoc filename = args[i];
94             try
95             {
96                 POIFSReader r = new POIFSReader();
97                 r.registerListener(new TreeReaderListener(filename, rootNode));
98                 r.read(new FileInputStream JavaDoc(filename));
99                 displayedFiles++;
100             }
101             catch (IOException JavaDoc ex)
102             {
103                 System.err.println(filename + ": " + ex);
104             }
105             catch (Throwable JavaDoc t)
106             {
107                 System.err.println("Unexpected exception while reading \"" +
108                                    filename + "\":");
109                 t.printStackTrace(System.err);
110             }
111         }
112
113         /* Exit if there is no file to display (none specified or only
114          * files with problems). */

115         if (displayedFiles == 0)
116         {
117             System.out.println("No POI filesystem(s) to display.");
118             System.exit(0);
119         }
120
121         /* Make the tree UI element visible. */
122         treeUI.setRootVisible(true);
123         treeUI.setShowsRootHandles(true);
124         ExtendableTreeCellRenderer etcr = new ExtendableTreeCellRenderer();
125         etcr.register(DocumentDescriptor.class,
126                       new DocumentDescriptorRenderer());
127         etcr.register(PropertySetDescriptor.class,
128                       new PropertySetDescriptorRenderer());
129         treeUI.setCellRenderer(etcr);
130         setSize(600, 450);
131         setTitle("POI Browser 0.08");
132         setVisible(true);
133     }
134
135 }
136
Popular Tags