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.Color;
021: import java.awt.Component;
022: import java.awt.Font;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.swing.JPanel;
027: import javax.swing.JTextArea;
028: import javax.swing.JTree;
029: import javax.swing.tree.DefaultMutableTreeNode;
030:
031: import org.apache.poi.hpsf.Property;
032: import org.apache.poi.hpsf.PropertySet;
033: import org.apache.poi.hpsf.Section;
034: import org.apache.poi.hpsf.SummaryInformation;
035:
036: /**
037: * <p>Renders a {@link PropertySetDescriptor} by more or less dumping
038: * the stuff into a {@link JTextArea}.</p>
039: *
040: * @author Rainer Klute <a
041: * href="mailto:klute@rainer-klute.de"><klute@rainer-klute.de></a>
042: * @version $Id: PropertySetDescriptorRenderer.java 489730 2006-12-22 19:18:16Z bayard $
043: * @since 2002-02-05
044: */
045: public class PropertySetDescriptorRenderer extends
046: DocumentDescriptorRenderer {
047:
048: public Component getTreeCellRendererComponent(final JTree tree,
049: final Object value, final boolean selected,
050: final boolean expanded, final boolean leaf, final int row,
051: final boolean hasFocus) {
052: final PropertySetDescriptor d = (PropertySetDescriptor) ((DefaultMutableTreeNode) value)
053: .getUserObject();
054: final PropertySet ps = d.getPropertySet();
055: final JPanel p = new JPanel();
056: final JTextArea text = new JTextArea();
057: text.setBackground(new Color(200, 255, 200));
058: text.setFont(new Font("Monospaced", Font.PLAIN, 10));
059: text.append(renderAsString(d));
060: text.append("\nByte order: "
061: + Codec.hexEncode((short) ps.getByteOrder()));
062: text.append("\nFormat: "
063: + Codec.hexEncode((short) ps.getFormat()));
064: text.append("\nOS version: "
065: + Codec.hexEncode(ps.getOSVersion()));
066: text.append("\nClass ID: " + Codec.hexEncode(ps.getClassID()));
067: text.append("\nSection count: " + ps.getSectionCount());
068: text.append(sectionsToString(ps.getSections()));
069: p.add(text);
070:
071: if (ps instanceof SummaryInformation) {
072: /* Use the convenience methods. */
073: final SummaryInformation si = (SummaryInformation) ps;
074: text.append("\n");
075: text.append("\nTitle: " + si.getTitle());
076: text.append("\nSubject: " + si.getSubject());
077: text.append("\nAuthor: " + si.getAuthor());
078: text.append("\nKeywords: " + si.getKeywords());
079: text.append("\nComments: " + si.getComments());
080: text.append("\nTemplate: " + si.getTemplate());
081: text.append("\nLast Author: " + si.getLastAuthor());
082: text.append("\nRev. Number: " + si.getRevNumber());
083: text.append("\nEdit Time: " + si.getEditTime());
084: text
085: .append("\nLast Printed: "
086: + si.getLastPrinted());
087: text.append("\nCreate Date/Time: "
088: + si.getCreateDateTime());
089: text.append("\nLast Save Date/Time: "
090: + si.getLastSaveDateTime());
091: text.append("\nPage Count: " + si.getPageCount());
092: text.append("\nWord Count: " + si.getWordCount());
093: text.append("\nChar Count: " + si.getCharCount());
094: // text.append("\nThumbnail: " + si.getThumbnail());
095: text.append("\nApplication Name: "
096: + si.getApplicationName());
097: text.append("\nSecurity: " + si.getSecurity());
098: }
099:
100: if (selected)
101: Util.invert(text);
102: return p;
103: }
104:
105: /**
106: * <p>Returns a string representation of a list of {@link
107: * Section}s.</p>
108: */
109: protected String sectionsToString(final List sections) {
110: final StringBuffer b = new StringBuffer();
111: int count = 1;
112: for (Iterator i = sections.iterator(); i.hasNext();) {
113: Section s = (Section) i.next();
114: String d = toString(s, "Section " + count++);
115: b.append(d);
116: }
117: return b.toString();
118: }
119:
120: /**
121: * <p>Returns a string representation of a {@link Section}.</p>
122: * @param s the section
123: * @param name the section's name
124: * @return a string representation of the {@link Section}
125: */
126: protected String toString(final Section s, final String name) {
127: final StringBuffer b = new StringBuffer();
128: b.append("\n" + name + " Format ID: ");
129: b.append(Codec.hexEncode(s.getFormatID()));
130: b.append("\n" + name + " Offset: " + s.getOffset());
131: b.append("\n" + name + " Section size: " + s.getSize());
132: b.append("\n" + name + " Property count: "
133: + s.getPropertyCount());
134:
135: final Property[] properties = s.getProperties();
136: for (int i = 0; i < properties.length; i++) {
137: final Property p = properties[i];
138: final long id = p.getID();
139: final long type = p.getType();
140: final Object value = p.getValue();
141: b.append('\n');
142: b.append(name);
143: b.append(", Name: ");
144: b.append(id);
145: b.append(" (");
146: b.append(s.getPIDString(id));
147: b.append("), Type: ");
148: b.append(type);
149: b.append(", Value: ");
150: if (value instanceof byte[]) {
151: byte[] b2 = (byte[]) value;
152: b.append("0x" + Codec.hexEncode(b2, 0, 4));
153: b.append(' ');
154: b.append("0x" + Codec.hexEncode(b2, 4, b2.length - 4));
155: } else if (value != null)
156: b.append(value.toString());
157: else
158: b.append("null");
159: }
160: return b.toString();
161: }
162:
163: }
|