001: /*
002: Copyright © 2006,2007 Stefano Chizzolini. http://clown.stefanochizzolini.it
003:
004: Contributors:
005: * Stefano Chizzolini (original code developer, http://www.stefanochizzolini.it):
006: contributed code is Copyright © 2006,2007 by Stefano Chizzolini.
007:
008: This file should be part of the source code distribution of "PDF Clown library"
009: (the Program): see the accompanying README files for more info.
010:
011: This Program is free software; you can redistribute it and/or modify it under
012: the terms of the GNU General Public License as published by the Free Software
013: Foundation; either version 2 of the License, or (at your option) any later version.
014:
015: This Program is distributed in the hope that it will be useful, but WITHOUT ANY
016: WARRANTY, either expressed or implied; without even the implied warranty of
017: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
018:
019: You should have received a copy of the GNU General Public License along with this
020: Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
021:
022: Redistribution and use, with or without modification, are permitted provided that such
023: redistributions retain the above copyright notice, license and disclaimer, along with
024: this list of conditions.
025: */
026:
027: package it.stefanochizzolini.clown.documents.interaction.navigation.document;
028:
029: import it.stefanochizzolini.clown.documents.Document;
030: import it.stefanochizzolini.clown.files.File;
031: import it.stefanochizzolini.clown.documents.interaction.Destination;
032: import it.stefanochizzolini.clown.objects.PdfDictionary;
033: import it.stefanochizzolini.clown.objects.PdfDirectObject;
034: import it.stefanochizzolini.clown.objects.PdfInteger;
035: import it.stefanochizzolini.clown.objects.PdfLiteral;
036: import it.stefanochizzolini.clown.objects.PdfName;
037: import it.stefanochizzolini.clown.objects.PdfObjectWrapper;
038: import it.stefanochizzolini.clown.objects.PdfReference;
039: import it.stefanochizzolini.clown.util.NotImplementedException;
040:
041: /**
042: Outline item.
043: @version 0.0.4
044: */
045: public class Bookmark extends PdfObjectWrapper<PdfDictionary> {
046: // <class>
047: // <dynamic>
048: // <constructors>
049: public Bookmark(Document context, String title,
050: Destination destination) {
051: super (context.getFile(), new PdfDictionary(new PdfName[] {
052: PdfName.Title, PdfName.Dest, PdfName.Count },
053: new PdfDirectObject[] {
054: new PdfLiteral(title),
055: (destination == null) ? null : destination
056: .getBaseObject(), new PdfInteger(0) }));
057: }
058:
059: Bookmark(PdfDirectObject baseObject) {
060: super (baseObject, null // NO container (bookmark MUST be an indirect object [PDF:1.6:8.2.2]).
061: );
062: }
063:
064: // </constructors>
065:
066: // <interface>
067: // <public>
068: public Bookmarks getBookmarks() {
069: return new Bookmarks(getBaseObject());
070: }
071:
072: @Override
073: public Bookmark clone(Document context) {
074: throw new NotImplementedException();
075: }
076:
077: public boolean getExpanded() {
078: PdfInteger countObject = (PdfInteger) getBaseDataObject().get(
079: PdfName.Count);
080:
081: return (countObject == null || countObject.getValue() >= 0);
082: }
083:
084: public void setExpanded(boolean value) {
085: PdfInteger countObject = (PdfInteger) getBaseDataObject().get(
086: PdfName.Count);
087: if (countObject == null)
088: return;
089:
090: /*
091: NOTE: Non-negative Count entry means open, negative Count entry means closed [PDF:1.6:8.2.2].
092: */
093: if (value) {
094: countObject.setValue(Math.abs(countObject.getValue()));
095: } else {
096: countObject.setValue(-Math.abs(countObject.getValue()));
097: }
098: }
099:
100: /**
101: @version 0.0.4
102: */
103: public Destination getDestination() {
104: PdfDirectObject destinationObject = getBaseDataObject().get(
105: PdfName.Dest);
106: // Is the destination indirectly specified?
107: if (destinationObject instanceof PdfLiteral) // Indirect specification.
108: return getDocument().getNames().getDestinations().get(
109: destinationObject);
110: else
111: // Direct specification.
112: return new Destination(destinationObject,
113: ((PdfReference) getBaseObject())
114: .getIndirectObject());
115: }
116:
117: public void setDestination(Destination value) {
118: getBaseDataObject().put(PdfName.Dest, value.getBaseObject());
119: }
120:
121: public Bookmark getNextSibling() {
122: PdfReference reference = (PdfReference) getBaseDataObject()
123: .get(PdfName.Next);
124: if (reference == null)
125: return null; // This bookmark is the last one, no next sibling available.
126:
127: return new Bookmark(reference);
128: }
129:
130: public void setNextSibling(Bookmark value) {
131: throw new NotImplementedException();
132: }
133:
134: public Bookmark getParent() {
135: PdfReference reference = (PdfReference) getBaseDataObject()
136: .get(PdfName.Parent);
137: // Is its parent a bookmark?
138: /*
139: NOTE: the Title entry can be used as a flag to distinguish bookmark
140: (outline item) dictionaries from outline (root) dictionaries.
141: */
142: if (((PdfDictionary) File.resolve(reference))
143: .containsKey(PdfName.Title)) // Bookmark.
144: return new Bookmark(reference);
145: else
146: // Outline root.
147: return null; // NO parent bookmark.
148: }
149:
150: public void setParent(Bookmark value) {
151: throw new NotImplementedException();
152: }
153:
154: public Bookmark getPreviousSibling() {
155: PdfReference reference = (PdfReference) getBaseDataObject()
156: .get(PdfName.Prev);
157: if (reference == null)
158: return null; // This bookmark is the first one, no previous sibling available.
159:
160: return new Bookmark(reference);
161: }
162:
163: public void setPreviousSibling(Bookmark value) {
164: throw new NotImplementedException();
165: }
166:
167: public String getTitle() {
168: return ((PdfLiteral) getBaseDataObject().get(PdfName.Title))
169: .getValue();
170: }
171:
172: public void setTitle(String value) {
173: ((PdfLiteral) getBaseDataObject().get(PdfName.Title))
174: .setValue(value);
175: }
176: // </public>
177: // </interface>
178: // </dynamic>
179: // </class>
180: }
|