1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
54
55
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
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
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
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
164
165
166
167
168
169
170
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
180
181
182
183 acrobat.deactivate();
184 }
185
186 System.exit(0);
187 }
188 });
189
190 try {
191
192
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
204
205
206
207 acrobat.activate();
208
209 } catch (Exception x) {
210 f.getContentPane().add(new JLabel(XNap.tr("Unable to create an Acrobat Reader")), "Center");
211 }
212
213
214 f.pack();
215
216
217
218 f.setVisible(true);
219
220
221 }
222 }
223 }