001: /**
002: * Copyright (c) 2003-2006, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdfviewer;
031:
032: import java.awt.Component;
033:
034: import javax.swing.JTree;
035:
036: import javax.swing.tree.DefaultTreeCellRenderer;
037:
038: import org.pdfbox.cos.COSArray;
039: import org.pdfbox.cos.COSBase;
040: import org.pdfbox.cos.COSDictionary;
041: import org.pdfbox.cos.COSName;
042: import org.pdfbox.cos.COSNull;
043: import org.pdfbox.cos.COSFloat;
044: import org.pdfbox.cos.COSInteger;
045: import org.pdfbox.cos.COSStream;
046: import org.pdfbox.cos.COSString;
047:
048: /**
049: * A class to render tree cells for the pdfviewer.
050: *
051: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
052: * @version $Revision: 1.6 $
053: */
054: public class PDFTreeCellRenderer extends DefaultTreeCellRenderer {
055: /**
056: * {@inheritDoc}
057: */
058: public Component getTreeCellRendererComponent(JTree tree,
059: Object nodeValue, boolean isSelected, boolean expanded,
060: boolean leaf, int row, boolean componentHasFocus) {
061: nodeValue = convertToTreeObject(nodeValue);
062: return super .getTreeCellRendererComponent(tree, nodeValue,
063: isSelected, expanded, leaf, row, componentHasFocus);
064: }
065:
066: private Object convertToTreeObject(Object nodeValue) {
067: if (nodeValue instanceof MapEntry) {
068: MapEntry entry = (MapEntry) nodeValue;
069: COSName key = (COSName) entry.getKey();
070: COSBase value = (COSBase) entry.getValue();
071: nodeValue = key.getName() + ":"
072: + convertToTreeObject(value);
073: } else if (nodeValue instanceof COSFloat) {
074: nodeValue = "" + ((COSFloat) nodeValue).floatValue();
075: } else if (nodeValue instanceof COSInteger) {
076: nodeValue = "" + ((COSInteger) nodeValue).intValue();
077: } else if (nodeValue instanceof COSString) {
078: nodeValue = ((COSString) nodeValue).getString();
079: } else if (nodeValue instanceof COSName) {
080: nodeValue = ((COSName) nodeValue).getName();
081: } else if (nodeValue instanceof ArrayEntry) {
082: ArrayEntry entry = (ArrayEntry) nodeValue;
083: nodeValue = "[" + entry.getIndex() + "]"
084: + convertToTreeObject(entry.getValue());
085: } else if (nodeValue instanceof COSNull) {
086: nodeValue = "null";
087: } else if (nodeValue instanceof COSDictionary) {
088: COSDictionary dict = (COSDictionary) nodeValue;
089: if (nodeValue instanceof COSStream) {
090: nodeValue = "Stream";
091: } else {
092: nodeValue = "Dictionary";
093: }
094:
095: COSName type = (COSName) dict
096: .getDictionaryObject(COSName.TYPE);
097: if (type != null) {
098: nodeValue = nodeValue + "(" + type.getName();
099: COSName subType = (COSName) dict
100: .getDictionaryObject(COSName.SUBTYPE);
101: if (subType != null) {
102: nodeValue = nodeValue + ":" + subType.getName();
103: }
104:
105: nodeValue = nodeValue + ")";
106: }
107: } else if (nodeValue instanceof COSArray) {
108: nodeValue = "Array";
109: } else if (nodeValue instanceof COSString) {
110: nodeValue = ((COSString) nodeValue).getString();
111: }
112: return nodeValue;
113:
114: }
115: }
|