svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / gui / PDFViewerWindow.java @ 40558
History | View | Annotate | Download (16.6 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.app.project.documents.gui; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.Component; |
28 |
import java.awt.Container; |
29 |
import java.awt.Dimension; |
30 |
import java.awt.FlowLayout; |
31 |
import java.awt.Toolkit; |
32 |
import java.awt.event.ActionEvent; |
33 |
import java.awt.event.ActionListener; |
34 |
import java.awt.print.PageFormat; |
35 |
import java.awt.print.Paper; |
36 |
import java.awt.print.PrinterException; |
37 |
import java.awt.print.PrinterJob; |
38 |
import java.beans.PropertyChangeEvent; |
39 |
import java.beans.PropertyChangeListener; |
40 |
import java.net.URL; |
41 |
|
42 |
import javax.print.attribute.HashPrintRequestAttributeSet; |
43 |
import javax.print.attribute.PrintRequestAttributeSet; |
44 |
import javax.print.attribute.standard.PageRanges; |
45 |
import javax.swing.ImageIcon; |
46 |
import javax.swing.JButton; |
47 |
import javax.swing.JLabel; |
48 |
import javax.swing.JOptionPane; |
49 |
import javax.swing.JPanel; |
50 |
import javax.swing.JScrollPane; |
51 |
import javax.swing.JTextField; |
52 |
|
53 |
import org.jpedal.Display; |
54 |
import org.jpedal.PdfDecoder; |
55 |
import org.jpedal.exception.PdfException; |
56 |
import org.jpedal.objects.PrinterOptions; |
57 |
|
58 |
import org.gvsig.andami.PluginServices; |
59 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
60 |
import org.gvsig.andami.ui.mdiManager.WindowInfo; |
61 |
|
62 |
public class PDFViewerWindow extends JPanel implements IWindow, |
63 |
PropertyChangeListener {
|
64 |
|
65 |
private static final long serialVersionUID = 4022263653761949704L; |
66 |
private WindowInfo m_viewInfo;
|
67 |
private PdfDecoder pf;
|
68 |
|
69 |
// private String viewerTitle="Visor PDF";
|
70 |
private String currentFile = null; |
71 |
private int currentPage = 1; |
72 |
private final JLabel pageCounter1 = new JLabel(" " |
73 |
+ PluginServices.getText(this, "Pagina") + " "); |
74 |
private JTextField pageCounter2 = new JTextField(4); |
75 |
private JLabel pageCounter3 = |
76 |
new JLabel(PluginServices.getText(this, "de")); |
77 |
|
78 |
public PDFViewerWindow(String currentFile) { |
79 |
super();
|
80 |
initialize(currentFile); |
81 |
} |
82 |
|
83 |
private void initialize(String currentFile) { |
84 |
|
85 |
pf = new PdfDecoder();
|
86 |
this.currentFile = currentFile;
|
87 |
// currentFile = "C:\\Cartografia\\prueba.pdf";
|
88 |
|
89 |
try {
|
90 |
// this opens the PDF and reads its internal details
|
91 |
pf.openPdfFile(currentFile); |
92 |
|
93 |
// these 2 lines opens page 1 at 100% scaling
|
94 |
pf.decodePage(currentPage); |
95 |
float scaling = (float) 1.5; |
96 |
pf.setPageParameters(scaling, 1); // values scaling (1=100%). page |
97 |
// number
|
98 |
pf.setDisplayView(Display.SINGLE_PAGE, Display.DISPLAY_CENTERED); |
99 |
} catch (Exception e) { |
100 |
e.printStackTrace(); |
101 |
} |
102 |
|
103 |
// setup our GUI display
|
104 |
initializeViewer(); |
105 |
|
106 |
// set page number display
|
107 |
pageCounter2.setText(currentPage + "");
|
108 |
pageCounter3.setText(PluginServices.getText(this, "de") + " " |
109 |
+ pf.getPageCount() + " ");
|
110 |
} |
111 |
|
112 |
public void setCurrentFile(String currentFile) { |
113 |
this.currentFile = currentFile;
|
114 |
} |
115 |
|
116 |
private void initializeViewer() { |
117 |
|
118 |
Container cPane = this; |
119 |
cPane.setLayout(new BorderLayout()); |
120 |
|
121 |
// JButton open = initOpenBut();//setup open button
|
122 |
Component[] itemsToAdd = initChangerPanel();// setup page display and |
123 |
// changer
|
124 |
|
125 |
JPanel topBar = new JPanel(); |
126 |
topBar.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0)); |
127 |
// topBar.add(pageChanger);
|
128 |
for (int i = 0; i < itemsToAdd.length; i++) { |
129 |
topBar.add(itemsToAdd[i]); |
130 |
} |
131 |
|
132 |
cPane.add(topBar, BorderLayout.NORTH);
|
133 |
|
134 |
JScrollPane display = getJPaneViewer();// setup scrollpane with pdf |
135 |
// display inside
|
136 |
cPane.add(display, BorderLayout.CENTER);
|
137 |
|
138 |
// pack();
|
139 |
|
140 |
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); |
141 |
setSize(screen.width / 2, screen.height / 2); |
142 |
// <start-13>
|
143 |
// setLocationRelativeTo(null);//centre on screen
|
144 |
// <end-13>
|
145 |
setVisible(true);
|
146 |
} |
147 |
|
148 |
private Component[] initChangerPanel() { |
149 |
|
150 |
Component[] list = new Component[12]; |
151 |
|
152 |
/** back to page 1 */
|
153 |
JButton start = new JButton(); |
154 |
start.setBorderPainted(false);
|
155 |
URL startImage =
|
156 |
getClass().getResource( |
157 |
"/org/jpedal/examples/simpleviewer/res/start.gif");
|
158 |
start.setIcon(new ImageIcon(startImage)); |
159 |
start.setToolTipText(PluginServices.getText(this, "primera_pagina")); |
160 |
// currentBar1.add(start);
|
161 |
list[0] = start;
|
162 |
start.addActionListener(new ActionListener() { |
163 |
|
164 |
public void actionPerformed(ActionEvent e) { |
165 |
if (currentFile != null && currentPage != 1) { |
166 |
currentPage = 1;
|
167 |
try {
|
168 |
pf.decodePage(currentPage); |
169 |
pf.invalidate(); |
170 |
repaint(); |
171 |
} catch (Exception e1) { |
172 |
System.err.println("back to page 1"); |
173 |
e1.printStackTrace(); |
174 |
} |
175 |
|
176 |
// set page number display
|
177 |
pageCounter2.setText(currentPage + "");
|
178 |
} |
179 |
} |
180 |
}); |
181 |
|
182 |
/** back 10 icon */
|
183 |
JButton fback = new JButton(); |
184 |
fback.setBorderPainted(false);
|
185 |
URL fbackImage =
|
186 |
getClass().getResource( |
187 |
"/org/jpedal/examples/simpleviewer/res/fback.gif");
|
188 |
fback.setIcon(new ImageIcon(fbackImage)); |
189 |
fback |
190 |
.setToolTipText(PluginServices.getText(this, "diez_paginas_atras")); |
191 |
// currentBar1.add(fback);
|
192 |
list[1] = fback;
|
193 |
fback.addActionListener(new ActionListener() { |
194 |
|
195 |
public void actionPerformed(ActionEvent e) { |
196 |
if (currentFile != null && currentPage > 10) { |
197 |
currentPage -= 10;
|
198 |
try {
|
199 |
pf.decodePage(currentPage); |
200 |
pf.invalidate(); |
201 |
repaint(); |
202 |
} catch (Exception e1) { |
203 |
System.err.println("back 10 pages"); |
204 |
e1.printStackTrace(); |
205 |
} |
206 |
|
207 |
// set page number display
|
208 |
pageCounter2.setText(currentPage + "");
|
209 |
} |
210 |
} |
211 |
}); |
212 |
|
213 |
/** back icon */
|
214 |
JButton back = new JButton(); |
215 |
back.setBorderPainted(false);
|
216 |
URL backImage =
|
217 |
getClass().getResource( |
218 |
"/org/jpedal/examples/simpleviewer/res/back.gif");
|
219 |
back.setIcon(new ImageIcon(backImage)); |
220 |
back.setToolTipText(PluginServices.getText(this, "pagina_atras")); |
221 |
// currentBar1.add(back);
|
222 |
list[2] = back;
|
223 |
back.addActionListener(new ActionListener() { |
224 |
|
225 |
public void actionPerformed(ActionEvent e) { |
226 |
if (currentFile != null && currentPage > 1) { |
227 |
currentPage -= 1;
|
228 |
try {
|
229 |
pf.decodePage(currentPage); |
230 |
pf.invalidate(); |
231 |
repaint(); |
232 |
} catch (Exception e1) { |
233 |
System.err.println("back 1 page"); |
234 |
e1.printStackTrace(); |
235 |
} |
236 |
|
237 |
// set page number display
|
238 |
pageCounter2.setText(currentPage + "");
|
239 |
} |
240 |
} |
241 |
}); |
242 |
|
243 |
pageCounter2.setEditable(true);
|
244 |
pageCounter2.addActionListener(new ActionListener() { |
245 |
|
246 |
public void actionPerformed(ActionEvent a) { |
247 |
|
248 |
String value = pageCounter2.getText().trim();
|
249 |
int newPage;
|
250 |
|
251 |
// allow for bum values
|
252 |
try {
|
253 |
newPage = Integer.parseInt(value);
|
254 |
|
255 |
if ((newPage > pf.getPageCount()) | (newPage < 1)) { |
256 |
return;
|
257 |
} |
258 |
|
259 |
currentPage = newPage; |
260 |
try {
|
261 |
pf.decodePage(currentPage); |
262 |
pf.invalidate(); |
263 |
repaint(); |
264 |
} catch (Exception e) { |
265 |
System.err.println("page number entered"); |
266 |
e.printStackTrace(); |
267 |
} |
268 |
|
269 |
} catch (Exception e) { |
270 |
JOptionPane.showMessageDialog(
|
271 |
null,
|
272 |
">" + value + "< " |
273 |
+ PluginServices.getText(this, "valor_incorrecto") |
274 |
+ pf.getPageCount()); |
275 |
} |
276 |
|
277 |
} |
278 |
|
279 |
}); |
280 |
|
281 |
/** put page count in middle of forward and back */
|
282 |
// currentBar1.add(pageCounter1);
|
283 |
// currentBar1.add(new JPanel());//add gap
|
284 |
// currentBar1.add(pageCounter2);
|
285 |
// currentBar1.add(new JPanel());//add gap
|
286 |
// currentBar1.add(pageCounter3);
|
287 |
list[3] = pageCounter1;
|
288 |
list[4] = new JPanel(); |
289 |
list[5] = pageCounter2;
|
290 |
list[6] = new JPanel(); |
291 |
list[7] = pageCounter3;
|
292 |
|
293 |
/** forward icon */
|
294 |
JButton forward = new JButton(); |
295 |
forward.setBorderPainted(false);
|
296 |
URL fowardImage =
|
297 |
getClass().getResource( |
298 |
"/org/jpedal/examples/simpleviewer/res/forward.gif");
|
299 |
forward.setIcon(new ImageIcon(fowardImage)); |
300 |
forward.setToolTipText(PluginServices.getText(this, "pagina_delante")); |
301 |
// currentBar1.add(forward);
|
302 |
list[8] = forward;
|
303 |
forward.addActionListener(new ActionListener() { |
304 |
|
305 |
public void actionPerformed(ActionEvent e) { |
306 |
if (currentFile != null && currentPage < pf.getPageCount()) { |
307 |
currentPage += 1;
|
308 |
try {
|
309 |
pf.decodePage(currentPage); |
310 |
pf.invalidate(); |
311 |
repaint(); |
312 |
} catch (Exception e1) { |
313 |
System.err.println("forward 1 page"); |
314 |
e1.printStackTrace(); |
315 |
} |
316 |
|
317 |
// set page number display
|
318 |
pageCounter2.setText(currentPage + "");
|
319 |
} |
320 |
} |
321 |
}); |
322 |
|
323 |
/** fast forward icon */
|
324 |
JButton fforward = new JButton(); |
325 |
fforward.setBorderPainted(false);
|
326 |
URL ffowardImage =
|
327 |
getClass().getResource( |
328 |
"/org/jpedal/examples/simpleviewer/res/fforward.gif");
|
329 |
fforward.setIcon(new ImageIcon(ffowardImage)); |
330 |
fforward.setToolTipText(PluginServices.getText(this,
|
331 |
"10_paginas_delante"));
|
332 |
// currentBar1.add(fforward);
|
333 |
list[9] = fforward;
|
334 |
fforward.addActionListener(new ActionListener() { |
335 |
|
336 |
public void actionPerformed(ActionEvent e) { |
337 |
if (currentFile != null && currentPage < pf.getPageCount() - 9) { |
338 |
currentPage += 10;
|
339 |
try {
|
340 |
pf.decodePage(currentPage); |
341 |
pf.invalidate(); |
342 |
repaint(); |
343 |
} catch (Exception e1) { |
344 |
System.err.println("forward 10 pages"); |
345 |
e1.printStackTrace(); |
346 |
} |
347 |
|
348 |
// set page number display
|
349 |
pageCounter2.setText(currentPage + "");
|
350 |
} |
351 |
} |
352 |
}); |
353 |
|
354 |
/** goto last page */
|
355 |
JButton end = new JButton(); |
356 |
end.setBorderPainted(false);
|
357 |
URL endImage =
|
358 |
getClass().getResource( |
359 |
"/org/jpedal/examples/simpleviewer/res/end.gif");
|
360 |
end.setIcon(new ImageIcon(endImage)); |
361 |
end.setToolTipText(PluginServices.getText(this, "ultima_pagina")); |
362 |
// currentBar1.add(end);
|
363 |
list[10] = end;
|
364 |
end.addActionListener(new ActionListener() { |
365 |
|
366 |
public void actionPerformed(ActionEvent e) { |
367 |
if (currentFile != null && currentPage < pf.getPageCount()) { |
368 |
currentPage = pf.getPageCount(); |
369 |
try {
|
370 |
pf.decodePage(currentPage); |
371 |
pf.invalidate(); |
372 |
repaint(); |
373 |
} catch (Exception e1) { |
374 |
System.err.println("forward to last page"); |
375 |
e1.printStackTrace(); |
376 |
} |
377 |
|
378 |
// set page number display
|
379 |
pageCounter2.setText(currentPage + "");
|
380 |
} |
381 |
} |
382 |
}); |
383 |
|
384 |
/** Print */
|
385 |
JButton print = new JButton(); |
386 |
print.setBorderPainted(false);
|
387 |
URL printImage =
|
388 |
getClass().getResource( |
389 |
"/org/jpedal/examples/simpleviewer/res/print.gif");
|
390 |
print.setIcon(new ImageIcon(printImage)); |
391 |
print.setToolTipText(PluginServices.getText(this, "imprimir")); |
392 |
// currentBar1.add(end);
|
393 |
list[11] = print;
|
394 |
print.addActionListener(new ActionListener() { |
395 |
|
396 |
public void actionPerformed(ActionEvent e) { |
397 |
printPDF(); |
398 |
} |
399 |
}); |
400 |
return list;
|
401 |
} |
402 |
|
403 |
public void printPDF() { |
404 |
PrinterJob printJob = PrinterJob.getPrinterJob(); |
405 |
|
406 |
printJob.setPageable(pf); |
407 |
// decode_pdf.setPageFormat(pf);
|
408 |
|
409 |
/**
|
410 |
* this additional functionality is available under printable interface
|
411 |
*/
|
412 |
// setup default values to add into JPS
|
413 |
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); |
414 |
aset.add(new PageRanges(1, pf.getPageCount())); |
415 |
|
416 |
boolean printFile = printJob.printDialog(aset);
|
417 |
|
418 |
// set page range
|
419 |
PageRanges r = (PageRanges) aset.get(PageRanges.class); |
420 |
if (r != null) { |
421 |
try {
|
422 |
PageFormat pformat = printJob.defaultPage();
|
423 |
|
424 |
Paper paper = new Paper(); |
425 |
paper.setSize(595, 842); // default A4 |
426 |
paper.setImageableArea(43, 43, 545, 792); // actual print 'zone' |
427 |
|
428 |
pformat.setPaper(paper); |
429 |
|
430 |
// pageable provides a method getPageFormat(int p) - this method
|
431 |
// allows it to be set by JPedal
|
432 |
pf.setPageFormat(pformat); |
433 |
pf.setPagePrintRange(r); |
434 |
} catch (PdfException e) {
|
435 |
// TODO Auto-generated catch block
|
436 |
e.printStackTrace(); |
437 |
} |
438 |
} |
439 |
|
440 |
/**
|
441 |
* generic call to both Pageable and printable
|
442 |
*/
|
443 |
if (printFile) {
|
444 |
try {
|
445 |
pf.setPrintPageScalingMode(PrinterOptions.PAGE_SCALING_FIT_TO_PRINTER_MARGINS); |
446 |
printJob.print(); |
447 |
} catch (PrinterException e) { |
448 |
// TODO Auto-generated catch block
|
449 |
e.printStackTrace(); |
450 |
} |
451 |
} |
452 |
} |
453 |
|
454 |
private JScrollPane getJPaneViewer() { |
455 |
JScrollPane currentScroll = new JScrollPane(); |
456 |
currentScroll |
457 |
.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
458 |
currentScroll |
459 |
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
|
460 |
|
461 |
currentScroll.setViewportView(pf); |
462 |
return currentScroll;
|
463 |
} |
464 |
|
465 |
public WindowInfo getWindowInfo() {
|
466 |
if (m_viewInfo == null) { |
467 |
m_viewInfo = new WindowInfo(WindowInfo.PALETTE);
|
468 |
m_viewInfo.setMaximizable(true);
|
469 |
m_viewInfo.setWidth(this.getWidth());
|
470 |
m_viewInfo.setHeight(this.getHeight());
|
471 |
m_viewInfo.setTitle(PluginServices.getText(this, "pdf_viewer")); |
472 |
} |
473 |
return m_viewInfo;
|
474 |
} |
475 |
|
476 |
public void propertyChange(PropertyChangeEvent arg0) { |
477 |
// TODO Auto-generated method stub
|
478 |
|
479 |
} |
480 |
|
481 |
public Object getWindowProfile() { |
482 |
// TODO Auto-generated method stub
|
483 |
return WindowInfo.TOOL_PROFILE;
|
484 |
} |
485 |
|
486 |
} |