KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.awt.Font JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.swing.JPanel JavaDoc;
28 import javax.swing.JTextArea JavaDoc;
29 import javax.swing.JTree JavaDoc;
30 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
31
32 import org.apache.poi.hpsf.Property;
33 import org.apache.poi.hpsf.PropertySet;
34 import org.apache.poi.hpsf.Section;
35 import org.apache.poi.hpsf.SummaryInformation;
36
37 /**
38  * <p>Renders a {@link PropertySetDescriptor} by more or less dumping
39  * the stuff into a {@link JTextArea}.</p>
40  *
41  * @author Rainer Klute <a
42  * HREF="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
43  * @version $Id: PropertySetDescriptorRenderer.java,v 1.7 2004/04/09 13:05:08 glens Exp $
44  * @since 2002-02-05
45  */

46 public class PropertySetDescriptorRenderer extends DocumentDescriptorRenderer
47 {
48
49     public Component JavaDoc getTreeCellRendererComponent(final JTree JavaDoc tree,
50                                                   final Object JavaDoc value,
51                                                   final boolean selected,
52                                                   final boolean expanded,
53                                                   final boolean leaf,
54                                                   final int row,
55                                                   final boolean hasFocus)
56     {
57         final PropertySetDescriptor d = (PropertySetDescriptor)
58             ((DefaultMutableTreeNode JavaDoc) value).getUserObject();
59         final PropertySet ps = d.getPropertySet();
60         final JPanel JavaDoc p = new JPanel JavaDoc();
61         final JTextArea JavaDoc text = new JTextArea JavaDoc();
62         text.setBackground(new Color JavaDoc(200, 255, 200));
63         text.setFont(new Font JavaDoc("Monospaced", Font.PLAIN, 10));
64         text.append(renderAsString(d));
65         text.append("\nByte order: " +
66                     Codec.hexEncode((short) ps.getByteOrder()));
67         text.append("\nFormat: " +
68                     Codec.hexEncode((short) ps.getFormat()));
69         text.append("\nOS version: " +
70                     Codec.hexEncode(ps.getOSVersion()));
71         text.append("\nClass ID: " +
72                     Codec.hexEncode(ps.getClassID()));
73         text.append("\nSection count: " + ps.getSectionCount());
74         text.append(sectionsToString(ps.getSections()));
75         p.add(text);
76
77         if (ps instanceof SummaryInformation)
78         {
79             /* Use the convenience methods. */
80             final SummaryInformation si = (SummaryInformation) ps;
81             text.append("\n");
82             text.append("\nTitle: " + si.getTitle());
83             text.append("\nSubject: " + si.getSubject());
84             text.append("\nAuthor: " + si.getAuthor());
85             text.append("\nKeywords: " + si.getKeywords());
86             text.append("\nComments: " + si.getComments());
87             text.append("\nTemplate: " + si.getTemplate());
88             text.append("\nLast Author: " + si.getLastAuthor());
89             text.append("\nRev. Number: " + si.getRevNumber());
90             text.append("\nEdit Time: " + si.getEditTime());
91             text.append("\nLast Printed: " + si.getLastPrinted());
92             text.append("\nCreate Date/Time: " + si.getCreateDateTime());
93             text.append("\nLast Save Date/Time: " + si.getLastSaveDateTime());
94             text.append("\nPage Count: " + si.getPageCount());
95             text.append("\nWord Count: " + si.getWordCount());
96             text.append("\nChar Count: " + si.getCharCount());
97             // text.append("\nThumbnail: " + si.getThumbnail());
98
text.append("\nApplication Name: " + si.getApplicationName());
99             text.append("\nSecurity: " + si.getSecurity());
100         }
101
102         if (selected)
103             Util.invert(text);
104         return p;
105     }
106
107
108
109     /**
110      * <p>Returns a string representation of a list of {@link
111      * Section}s.</p>
112      */

113     protected String JavaDoc sectionsToString(final List JavaDoc sections)
114     {
115         final StringBuffer JavaDoc b = new StringBuffer JavaDoc();
116         int count = 1;
117         for (Iterator JavaDoc i = sections.iterator(); i.hasNext();)
118         {
119             Section s = (Section) i.next();
120             String JavaDoc d = toString(s, "Section " + count++);
121             b.append(d);
122         }
123         return b.toString();
124     }
125
126
127
128     /**
129      * <p>Returns a string representation of a {@link Section}.</p>
130      */

131     protected String JavaDoc toString(final Section s, final String JavaDoc name)
132     {
133         final StringBuffer JavaDoc b = new StringBuffer JavaDoc();
134         b.append("\n" + name + " Format ID: ");
135         b.append(Codec.hexEncode(s.getFormatID()));
136         b.append("\n" + name + " Offset: " + s.getOffset());
137         b.append("\n" + name + " Section size: " + s.getSize());
138         b.append("\n" + name + " Property count: " + s.getPropertyCount());
139
140         final Property[] properties = s.getProperties();
141         for (int i = 0; i < properties.length; i++)
142         {
143             final Property p = properties[i];
144             final Object JavaDoc value = p.getValue();
145             b.append("\n" + name + " ");
146             b.append("PID_");
147             b.append(p.getID());
148             b.append(' ');
149             b.append(s.getPIDString(p.getID()) + ": ");
150             if (value instanceof byte[])
151             {
152                 byte[] b2 = (byte[]) value;
153                 b.append("0x" + Codec.hexEncode(b2, 0, 4));
154                 b.append(' ');
155                 b.append("0x" + Codec.hexEncode(b2, 4, b2.length - 4));
156             }
157             else if (value != null)
158                 b.append(value.toString());
159             else
160                 b.append("null");
161         }
162         return b.toString();
163     }
164
165 }
166
Popular Tags