View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.viewer.acrobatviewer;
21  
22  import java.awt.BorderLayout;
23  import java.awt.GridBagLayout;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.WindowAdapter;
26  import java.awt.event.WindowEvent;
27  import java.io.File;
28  import java.io.FileInputStream;
29  import java.io.FileNotFoundException;
30  import java.text.DateFormat;
31  import java.util.Calendar;
32  
33  import javax.swing.AbstractAction;
34  import javax.swing.Action;
35  import javax.swing.JFrame;
36  import javax.swing.JLabel;
37  import javax.swing.JPanel;
38  
39  import org.apache.log4j.Logger;
40  import org.xnap.XNap;
41  import org.xnap.gui.component.XNapButton;
42  import org.xnap.gui.util.GUIHelper;
43  import org.xnap.gui.util.GridBagHelper;
44  import org.xnap.gui.util.IconHelper;
45  
46  import com.adobe.acrobat.PDFDocument;
47  import com.adobe.acrobat.Viewer;
48  
49  
50  public class AcrobatViewerPanel extends JPanel
51  {
52  
53      //--- Constant(s) ---
54  
55      //--- Data field(s) ---
56  
57      private static Logger logger = Logger.getLogger(AcrobatViewerPanel.class);
58  
59  	private JLabel jlInfo;
60      
61      private File file;
62  
63  	private Action acViewDocument = new ViewDocumentAction();
64  
65      //--- Constructor(s) ---
66  
67      public AcrobatViewerPanel()
68      {
69  		super(new GridBagLayout());
70  
71  		jlInfo = new JLabel();
72  		jlInfo.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Info")));
73  		GridBagHelper.add(this, jlInfo);
74  
75  		GridBagHelper.addVerticalSpacer(this);
76  
77  		GridBagHelper.addComponent(this, new XNapButton(acViewDocument));
78      }
79  
80      //--- Method(s) ---
81  
82      /***
83       * 
84       */
85      public void display(File f)
86      {
87  		this.file = f;
88  
89  		try {
90  			PDFDocument pdf = new PDFDocument(f);
91  			
92  			DateFormat df = DateFormat.getDateTimeInstance();
93  			
94  			Calendar creation
95  				= pdf.getDocumentInfoDate(PDFDocument.CreationDate_K);
96  			String creationDate 
97  				= (creation!=null) ? df.format(creation.getTime()) : null;
98  
99  			Calendar mod = pdf.getDocumentInfoDate(PDFDocument.ModDate_K);
100 			String modDate 
101 				= (mod != null) ? df.format(mod.getTime()) : null;
102 			
103 			StringBuffer sb = new StringBuffer();
104 			sb.append("<html><table>");
105 			sb.append(row(XNap.tr("Title"),
106 						  pdf.getDocumentInfo(PDFDocument.Title_K)));
107 			sb.append(row(XNap.tr("Subject"),
108 						  pdf.getDocumentInfo(PDFDocument.Subject_K)));
109 			sb.append(row(XNap.tr("Author"), 
110 						  pdf.getDocumentInfo(PDFDocument.Author_K)));
111 			sb.append(row(XNap.tr("Creator"), 
112 						  pdf.getDocumentInfo(PDFDocument.Creator_K)));
113 			sb.append(row(XNap.tr("Created"), 
114 						  pdf.getDocumentInfo(creationDate)));
115 			sb.append(row(XNap.tr("Modified"), 
116 						  pdf.getDocumentInfo(modDate)));
117 			sb.append(row(XNap.tr("Producer"), 
118 						  pdf.getDocumentInfo(PDFDocument.Producer_K)));
119 			sb.append(row(XNap.tr("Keywords"), 
120 						  pdf.getDocumentInfo(PDFDocument.Keywords_K)));
121 			sb.append("</table></html>");
122 			jlInfo.setText(sb.toString());
123 
124 			acViewDocument.setEnabled(true);
125 		} 
126 		catch (Exception e) {
127 			logger.debug("could not read file", e);
128 			jlInfo.setText(XNap.tr("Not a valid pdf file.") + " "
129 						   + e.getLocalizedMessage());
130 			acViewDocument.setEnabled(false);
131 		}
132     }
133 
134 	private String row(String key, String text)
135 	{
136 		StringBuffer sb = new StringBuffer();
137 		sb.append("<tr><td><b>");
138 		sb.append(key);
139 		sb.append("</b></td><td> ");
140 		sb.append((text != null) ? text : XNap.tr("Unknown")); 
141 		sb.append("</td></tr>");
142 		return sb.toString();
143 	}
144 
145 	//--- Inner Class(es) ---
146 
147 	public class ViewDocumentAction extends AbstractAction
148 	{
149 		public ViewDocumentAction()
150 		{
151 			putValue(Action.NAME, XNap.tr("View Document"));
152 			putValue(Action.SHORT_DESCRIPTION, XNap.tr("View PDF Document."));
153 			putValue(IconHelper.XNAP_ICON, "acroread.png");
154 		}
155 
156 		public void actionPerformed(ActionEvent event)
157 		{
158 			JFrame f = new JFrame("XNap - Acrobat Reader - " + file.getName());
159 			f.getContentPane().setLayout(new BorderLayout());
160 
161 			try {
162 
163 				// Construct a acrobat object aka Acrobar Reader
164 				// note that you must also call its activate
165 				// method before you show the containing panel,
166 				// in this case the frame object.
167 
168 				// The acrobat object is declared as final
169 				// so that it could be referenced in the
170 				// following windowClosing method.
171 
172 				final Viewer acrobat = new Viewer();
173 
174 				f.addWindowListener(new WindowAdapter() {
175 						public void windowClosing(WindowEvent e) { 
176 		    
177 							if (acrobat != null) {
178 			
179 								// The deactivate method will ensure that the
180 								// acrobat.properties file is saved
181 								// upon exit.
182 			
183 								acrobat.deactivate();
184 							}
185 		    
186 							System.exit(0); 
187 						}
188 					});
189 
190 				try {
191 
192 					// assumes that args[0] is the name of a file
193 
194 					FileInputStream in = new FileInputStream(file);
195 					acrobat.setDocumentInputStream(in);
196 		    
197 				} catch (FileNotFoundException x) {
198 					f.getContentPane().add(new JLabel(XNap.tr("File not found.")), "Center");
199 				}
200 
201 				f.getContentPane().add(acrobat, BorderLayout.CENTER);
202 
203 				// you must call activate to enable the Viewer object
204 				// to layout its sub-components and the further initialization
205 				// needed for it to be displayed.
206 
207 				acrobat.activate(); //WithoutBars();
208 
209 			} catch (Exception x) {
210 				f.getContentPane().add(new JLabel(XNap.tr("Unable to create an Acrobat Reader")), "Center");
211 			}
212 
213 			//f.setSize(400, 400);
214 			f.pack();
215 			// FIX: MAXIMIZED_BOTH not defined in JDK 1.3
216 			//f.setExtendedState(JFrame.MAXIMIZED_BOTH);
217 			
218 			f.setVisible(true);
219 
220 
221 		}
222 	}
223 }