Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdfviewer » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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   package org.apache.pdfbox.pdfviewer;
   18   
   19   import java.awt.Component;
   20   
   21   import javax.swing.JTree;
   22   
   23   import javax.swing.tree.DefaultTreeCellRenderer;
   24   
   25   import org.apache.pdfbox.cos.COSArray;
   26   import org.apache.pdfbox.cos.COSBase;
   27   import org.apache.pdfbox.cos.COSDictionary;
   28   import org.apache.pdfbox.cos.COSName;
   29   import org.apache.pdfbox.cos.COSNull;
   30   import org.apache.pdfbox.cos.COSFloat;
   31   import org.apache.pdfbox.cos.COSInteger;
   32   import org.apache.pdfbox.cos.COSStream;
   33   import org.apache.pdfbox.cos.COSString;
   34   
   35   /**
   36    * A class to render tree cells for the pdfviewer.
   37    *
   38    * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
   39    * @version $Revision: 1.6 $
   40    */
   41   public class PDFTreeCellRenderer extends DefaultTreeCellRenderer
   42   {
   43       /**
   44        * {@inheritDoc}
   45        */
   46       public Component getTreeCellRendererComponent(
   47           JTree tree,
   48           Object nodeValue,
   49           boolean isSelected,
   50           boolean expanded,
   51           boolean leaf,
   52           int row,
   53           boolean componentHasFocus)
   54       {
   55           nodeValue = convertToTreeObject( nodeValue );
   56           return super.getTreeCellRendererComponent( tree, nodeValue, isSelected, expanded, leaf,
   57                   row, componentHasFocus );
   58       }
   59   
   60       private Object convertToTreeObject( Object nodeValue )
   61       {
   62           if( nodeValue instanceof MapEntry )
   63           {
   64               MapEntry entry = (MapEntry)nodeValue;
   65               COSName key = (COSName)entry.getKey();
   66               COSBase value = (COSBase)entry.getValue();
   67               nodeValue = key.getName() + ":" + convertToTreeObject( value );
   68           }
   69           else if( nodeValue instanceof COSFloat )
   70           {
   71               nodeValue = "" + ((COSFloat)nodeValue).floatValue();
   72           }
   73           else if( nodeValue instanceof COSInteger )
   74           {
   75               nodeValue = "" + ((COSInteger)nodeValue).intValue();
   76           }
   77           else if( nodeValue instanceof COSString )
   78           {
   79               nodeValue = ((COSString)nodeValue).getString();
   80           }
   81           else if( nodeValue instanceof COSName )
   82           {
   83               nodeValue = ((COSName)nodeValue).getName();
   84           }
   85           else if( nodeValue instanceof ArrayEntry )
   86           {
   87               ArrayEntry entry = (ArrayEntry)nodeValue;
   88               nodeValue = "[" + entry.getIndex() + "]" + convertToTreeObject( entry.getValue() );
   89           }
   90           else if( nodeValue instanceof COSNull )
   91           {
   92               nodeValue = "null";
   93           }
   94           else if( nodeValue instanceof COSDictionary )
   95           {
   96               COSDictionary dict = (COSDictionary)nodeValue;
   97               if( nodeValue instanceof COSStream )
   98               {
   99                   nodeValue = "Stream";
  100               }
  101               else
  102               {
  103                   nodeValue = "Dictionary";
  104               }
  105   
  106               COSName type = (COSName)dict.getDictionaryObject( COSName.TYPE );
  107               if( type != null )
  108               {
  109                   nodeValue = nodeValue + "(" + type.getName();
  110                   COSName subType = (COSName)dict.getDictionaryObject( COSName.SUBTYPE );
  111                   if( subType != null )
  112                   {
  113                       nodeValue = nodeValue + ":" + subType.getName();
  114                   }
  115   
  116                   nodeValue = nodeValue + ")";
  117               }
  118           }
  119           else if( nodeValue instanceof COSArray )
  120           {
  121               nodeValue="Array";
  122           }
  123           else if( nodeValue instanceof COSString )
  124           {
  125               nodeValue = ((COSString)nodeValue).getString();
  126           }
  127           return nodeValue;
  128   
  129       }
  130   }

Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdfviewer » [javadoc | source]