svn-gvsig-desktop / tags / v2_0_0_Build_2056 / extensions / org.gvsig.hyperlink.app / org.gvsig.hyperlink.app.extension / src / main / java / org / gvsig / hyperlink / app / extension / actions / PdfHyperlinkPanel.java @ 39035
History | View | Annotate | Download (17.9 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
|
23 |
package org.gvsig.hyperlink.app.extension.actions; |
24 |
|
25 |
import java.awt.BorderLayout; |
26 |
import java.awt.Component; |
27 |
import java.awt.Container; |
28 |
import java.awt.Dimension; |
29 |
import java.awt.FlowLayout; |
30 |
import java.awt.Toolkit; |
31 |
import java.awt.event.ActionEvent; |
32 |
import java.awt.event.ActionListener; |
33 |
import java.awt.print.PageFormat; |
34 |
import java.awt.print.Paper; |
35 |
import java.awt.print.PrinterException; |
36 |
import java.awt.print.PrinterJob; |
37 |
import java.beans.PropertyChangeEvent; |
38 |
import java.beans.PropertyChangeListener; |
39 |
import java.net.URI; |
40 |
|
41 |
import javax.print.attribute.HashPrintRequestAttributeSet; |
42 |
import javax.print.attribute.PrintRequestAttributeSet; |
43 |
import javax.print.attribute.SetOfIntegerSyntax; |
44 |
import javax.print.attribute.standard.PageRanges; |
45 |
import javax.swing.JButton; |
46 |
import javax.swing.JLabel; |
47 |
import javax.swing.JOptionPane; |
48 |
import javax.swing.JPanel; |
49 |
import javax.swing.JScrollPane; |
50 |
import javax.swing.JTextField; |
51 |
|
52 |
import org.gvsig.andami.IconThemeHelper; |
53 |
import org.gvsig.andami.PluginServices; |
54 |
import org.gvsig.andami.messages.NotificationManager; |
55 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
56 |
import org.gvsig.andami.ui.mdiManager.WindowInfo; |
57 |
import org.gvsig.hyperlink.app.extension.AbstractHyperLinkPanel; |
58 |
import org.jpedal.Display; |
59 |
import org.jpedal.PdfDecoder; |
60 |
import org.jpedal.exception.PdfException; |
61 |
import org.jpedal.objects.PrinterOptions; |
62 |
|
63 |
public class PdfHyperlinkPanel extends AbstractHyperLinkPanel implements |
64 |
PropertyChangeListener, IWindow {
|
65 |
|
66 |
private static final long serialVersionUID = 1L; |
67 |
private WindowInfo m_viewInfo;
|
68 |
private PdfDecoder pf = null; |
69 |
|
70 |
private int currentPage = 1; |
71 |
private final JLabel pageCounter1 = new JLabel(" " |
72 |
+ PluginServices.getText(this, "Pagina") + " "); |
73 |
private JTextField pageCounter2 = new JTextField(4); |
74 |
private JLabel pageCounter3 = |
75 |
new JLabel(PluginServices.getText(this, "de")); |
76 |
|
77 |
public static void initializeIcons() { |
78 |
IconThemeHelper.registerIcon("toolbar-go", "go-next", PdfHyperlinkPanel.class); |
79 |
IconThemeHelper.registerIcon("toolbar-go", "go-previous", PdfHyperlinkPanel.class); |
80 |
IconThemeHelper.registerIcon("toolbar-go", "go-next-fast", PdfHyperlinkPanel.class); |
81 |
IconThemeHelper.registerIcon("toolbar-go", "go-previous-fast", PdfHyperlinkPanel.class); |
82 |
IconThemeHelper.registerIcon("toolbar-go", "go-first", PdfHyperlinkPanel.class); |
83 |
IconThemeHelper.registerIcon("toolbar-go", "go-last", PdfHyperlinkPanel.class); |
84 |
IconThemeHelper.registerIcon("action", "document-print", PdfHyperlinkPanel.class); |
85 |
} |
86 |
|
87 |
public PdfHyperlinkPanel(URI doc) { |
88 |
super(doc);
|
89 |
initialize(); |
90 |
} |
91 |
|
92 |
private void initialize() { |
93 |
|
94 |
pf = new PdfDecoder();
|
95 |
|
96 |
try {
|
97 |
if (document != null && document.isAbsolute()) { |
98 |
String urlString = document.toURL().toString();
|
99 |
// avoid the openPdfFileFromURL method in first term, to avoid
|
100 |
// the download dialog
|
101 |
if (urlString.startsWith("file:")) { |
102 |
urlString = urlString.replaceFirst("file:/*", "/"); // keep |
103 |
// just
|
104 |
// one
|
105 |
// slash
|
106 |
// at
|
107 |
// the
|
108 |
// beginning
|
109 |
urlString = urlString.replaceAll("%20", " "); // PdfDecoder |
110 |
// doesn't
|
111 |
// correctly
|
112 |
// digest %20
|
113 |
// spaces
|
114 |
pf.openPdfFile(urlString); |
115 |
} else {
|
116 |
pf.openPdfFileFromURL(urlString, true);
|
117 |
} |
118 |
} else {
|
119 |
PluginServices.getLogger().warn(PluginServices.getText(this,
|
120 |
"Hyperlink_linked_field_doesnot_exist"));
|
121 |
return;
|
122 |
} |
123 |
|
124 |
// these 2 lines opens page 1 at 100% scaling
|
125 |
pf.decodePage(currentPage); |
126 |
float scaling = (float) 1.5; |
127 |
pf.setPageParameters(scaling, 1); // values scaling (1=100%). page |
128 |
// number
|
129 |
pf.setDisplayView(Display.SINGLE_PAGE, Display.DISPLAY_CENTERED); |
130 |
} catch (Exception e) { |
131 |
NotificationManager.addWarning(PluginServices.getText(this,
|
132 |
"Hyperlink_linked_field_doesnot_exist"), e);
|
133 |
return;
|
134 |
} |
135 |
|
136 |
// setup our GUI display
|
137 |
initializeViewer(); |
138 |
|
139 |
// set page number display
|
140 |
pageCounter2.setText(currentPage + "");
|
141 |
pageCounter3.setText(PluginServices.getText(this, "de") + " " |
142 |
+ pf.getPageCount() + " ");
|
143 |
} |
144 |
|
145 |
public void setCurrentURL(URI currentURL) { |
146 |
document = currentURL; |
147 |
} |
148 |
|
149 |
private void initializeViewer() { |
150 |
|
151 |
Container cPane = this; |
152 |
cPane.setLayout(new BorderLayout()); |
153 |
|
154 |
// JButton open = initOpenBut();//setup open button
|
155 |
Component[] itemsToAdd = initChangerPanel();// setup page display and |
156 |
// changer
|
157 |
|
158 |
JPanel topBar = new JPanel(); |
159 |
topBar.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0)); |
160 |
// topBar.add(pageChanger);
|
161 |
for (int i = 0; i < itemsToAdd.length; i++) { |
162 |
topBar.add(itemsToAdd[i]); |
163 |
} |
164 |
|
165 |
cPane.add(topBar, BorderLayout.NORTH);
|
166 |
|
167 |
JScrollPane display = getJPaneViewer();// setup scrollpane with pdf |
168 |
// display inside
|
169 |
cPane.add(display, BorderLayout.CENTER);
|
170 |
|
171 |
// pack();
|
172 |
|
173 |
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); |
174 |
setSize(screen.width / 2, screen.height / 2); |
175 |
// <start-13>
|
176 |
// setLocationRelativeTo(null);//centre on screen
|
177 |
// <end-13>
|
178 |
setVisible(true);
|
179 |
} |
180 |
|
181 |
private Component[] initChangerPanel() { |
182 |
|
183 |
Component[] list = new Component[12]; |
184 |
|
185 |
/** back to page 1 */
|
186 |
JButton start = new JButton(); |
187 |
start.setBorderPainted(false);
|
188 |
start.setIcon( IconThemeHelper.getImageIcon("go-first"));
|
189 |
start.setToolTipText(PluginServices.getText(this, "primera_pagina")); |
190 |
// currentBar1.add(start);
|
191 |
list[0] = start;
|
192 |
start.addActionListener(new ActionListener() { |
193 |
|
194 |
public void actionPerformed(ActionEvent e) { |
195 |
if (pf != null && currentPage != 1) { |
196 |
currentPage = 1;
|
197 |
try {
|
198 |
pf.decodePage(currentPage); |
199 |
pf.invalidate(); |
200 |
repaint(); |
201 |
} catch (Exception e1) { |
202 |
System.err.println("back to page 1"); |
203 |
e1.printStackTrace(); |
204 |
} |
205 |
|
206 |
// set page number display
|
207 |
pageCounter2.setText(currentPage + "");
|
208 |
} |
209 |
} |
210 |
}); |
211 |
|
212 |
/** back 10 icon */
|
213 |
JButton fback = new JButton(); |
214 |
fback.setBorderPainted(false);
|
215 |
fback.setIcon( IconThemeHelper.getImageIcon("go-previous-fast"));
|
216 |
fback.setToolTipText(PluginServices.getText(this, "diez_paginas_atras")); |
217 |
// currentBar1.add(fback);
|
218 |
list[1] = fback;
|
219 |
fback.addActionListener(new ActionListener() { |
220 |
|
221 |
public void actionPerformed(ActionEvent e) { |
222 |
if (pf != null && currentPage > 10) { |
223 |
currentPage -= 10;
|
224 |
try {
|
225 |
pf.decodePage(currentPage); |
226 |
pf.invalidate(); |
227 |
repaint(); |
228 |
} catch (Exception e1) { |
229 |
System.err.println("back 10 pages"); |
230 |
e1.printStackTrace(); |
231 |
} |
232 |
|
233 |
// set page number display
|
234 |
pageCounter2.setText(currentPage + "");
|
235 |
} |
236 |
} |
237 |
}); |
238 |
|
239 |
/** back icon */
|
240 |
JButton back = new JButton(); |
241 |
back.setBorderPainted(false);
|
242 |
back.setIcon( IconThemeHelper.getImageIcon("go-previous"));
|
243 |
|
244 |
back.setToolTipText(PluginServices.getText(this, "pagina_atras")); |
245 |
// currentBar1.add(back);
|
246 |
list[2] = back;
|
247 |
back.addActionListener(new ActionListener() { |
248 |
|
249 |
public void actionPerformed(ActionEvent e) { |
250 |
if (pf != null && currentPage > 1) { |
251 |
currentPage -= 1;
|
252 |
try {
|
253 |
pf.decodePage(currentPage); |
254 |
pf.invalidate(); |
255 |
repaint(); |
256 |
} catch (Exception e1) { |
257 |
System.err.println("back 1 page"); |
258 |
e1.printStackTrace(); |
259 |
} |
260 |
|
261 |
// set page number display
|
262 |
pageCounter2.setText(currentPage + "");
|
263 |
} |
264 |
} |
265 |
}); |
266 |
|
267 |
pageCounter2.setEditable(true);
|
268 |
pageCounter2.addActionListener(new ActionListener() { |
269 |
|
270 |
public void actionPerformed(ActionEvent a) { |
271 |
|
272 |
String value = pageCounter2.getText().trim();
|
273 |
int newPage;
|
274 |
|
275 |
// allow for bum values
|
276 |
try {
|
277 |
newPage = Integer.parseInt(value);
|
278 |
|
279 |
if ((newPage > pf.getPageCount()) | (newPage < 1)) { |
280 |
return;
|
281 |
} |
282 |
|
283 |
currentPage = newPage; |
284 |
try {
|
285 |
pf.decodePage(currentPage); |
286 |
pf.invalidate(); |
287 |
repaint(); |
288 |
} catch (Exception e) { |
289 |
System.err.println("page number entered"); |
290 |
e.printStackTrace(); |
291 |
} |
292 |
|
293 |
} catch (Exception e) { |
294 |
JOptionPane.showMessageDialog(null, |
295 |
">" + value + "< " |
296 |
+ PluginServices.getText(this, "valor_incorrecto") |
297 |
+ pf.getPageCount()); |
298 |
} |
299 |
|
300 |
} |
301 |
|
302 |
}); |
303 |
|
304 |
/** put page count in middle of forward and back */
|
305 |
// currentBar1.add(pageCounter1);
|
306 |
// currentBar1.add(new JPanel());//add gap
|
307 |
// currentBar1.add(pageCounter2);
|
308 |
// currentBar1.add(new JPanel());//add gap
|
309 |
// currentBar1.add(pageCounter3);
|
310 |
list[3] = pageCounter1;
|
311 |
list[4] = new JPanel(); |
312 |
list[5] = pageCounter2;
|
313 |
list[6] = new JPanel(); |
314 |
list[7] = pageCounter3;
|
315 |
|
316 |
/** forward icon */
|
317 |
JButton forward = new JButton(); |
318 |
forward.setBorderPainted(false);
|
319 |
forward.setIcon( IconThemeHelper.getImageIcon("go-next"));
|
320 |
forward.setToolTipText(PluginServices.getText(this, "pagina_delante")); |
321 |
// currentBar1.add(forward);
|
322 |
list[8] = forward;
|
323 |
forward.addActionListener(new ActionListener() { |
324 |
|
325 |
public void actionPerformed(ActionEvent e) { |
326 |
if (pf != null && currentPage < pf.getPageCount()) { |
327 |
currentPage += 1;
|
328 |
try {
|
329 |
pf.decodePage(currentPage); |
330 |
pf.invalidate(); |
331 |
repaint(); |
332 |
} catch (Exception e1) { |
333 |
System.err.println("forward 1 page"); |
334 |
e1.printStackTrace(); |
335 |
} |
336 |
|
337 |
// set page number display
|
338 |
pageCounter2.setText(currentPage + "");
|
339 |
} |
340 |
} |
341 |
}); |
342 |
|
343 |
/** fast forward icon */
|
344 |
JButton fforward = new JButton(); |
345 |
fforward.setBorderPainted(false);
|
346 |
fforward.setIcon( IconThemeHelper.getImageIcon("go-next-fast"));
|
347 |
fforward.setToolTipText(PluginServices.getText(this,
|
348 |
"10_paginas_delante"));
|
349 |
// currentBar1.add(fforward);
|
350 |
list[9] = fforward;
|
351 |
fforward.addActionListener(new ActionListener() { |
352 |
|
353 |
public void actionPerformed(ActionEvent e) { |
354 |
if (pf != null && currentPage < pf.getPageCount() - 9) { |
355 |
currentPage += 10;
|
356 |
try {
|
357 |
pf.decodePage(currentPage); |
358 |
pf.invalidate(); |
359 |
repaint(); |
360 |
} catch (Exception e1) { |
361 |
System.err.println("forward 10 pages"); |
362 |
e1.printStackTrace(); |
363 |
} |
364 |
|
365 |
// set page number display
|
366 |
pageCounter2.setText(currentPage + "");
|
367 |
} |
368 |
} |
369 |
}); |
370 |
|
371 |
/** goto last page */
|
372 |
JButton end = new JButton(); |
373 |
end.setBorderPainted(false);
|
374 |
end.setIcon( IconThemeHelper.getImageIcon("go-last"));
|
375 |
end.setToolTipText(PluginServices.getText(this, "ultima_pagina")); |
376 |
// currentBar1.add(end);
|
377 |
list[10] = end;
|
378 |
end.addActionListener(new ActionListener() { |
379 |
|
380 |
public void actionPerformed(ActionEvent e) { |
381 |
if (pf != null && currentPage < pf.getPageCount()) { |
382 |
currentPage = pf.getPageCount(); |
383 |
try {
|
384 |
pf.decodePage(currentPage); |
385 |
pf.invalidate(); |
386 |
repaint(); |
387 |
} catch (Exception e1) { |
388 |
System.err.println("forward to last page"); |
389 |
e1.printStackTrace(); |
390 |
} |
391 |
|
392 |
// set page number display
|
393 |
pageCounter2.setText(currentPage + "");
|
394 |
} |
395 |
} |
396 |
}); |
397 |
|
398 |
/** Print */
|
399 |
JButton print = new JButton(); |
400 |
print.setBorderPainted(false);
|
401 |
print.setIcon( IconThemeHelper.getImageIcon("document-print"));
|
402 |
print.setToolTipText(PluginServices.getText(this, "imprimir")); |
403 |
// currentBar1.add(end);
|
404 |
list[11] = print;
|
405 |
print.addActionListener(new ActionListener() { |
406 |
|
407 |
public void actionPerformed(ActionEvent e) { |
408 |
printPDF(); |
409 |
} |
410 |
}); |
411 |
return list;
|
412 |
} |
413 |
|
414 |
public void printPDF() { |
415 |
PrinterJob printJob = PrinterJob.getPrinterJob(); |
416 |
|
417 |
printJob.setPageable(pf); |
418 |
// decode_pdf.setPageFormat(pf);
|
419 |
|
420 |
/**
|
421 |
* this additional functionality is available under printable interface
|
422 |
*/
|
423 |
// setup default values to add into JPS
|
424 |
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); |
425 |
aset.add(new PageRanges(1, pf.getPageCount())); |
426 |
|
427 |
boolean printFile = printJob.printDialog(aset);
|
428 |
|
429 |
// set page range
|
430 |
PageRanges r = (PageRanges) aset.get(PageRanges.class); |
431 |
if (r != null) |
432 |
try {
|
433 |
PageFormat pformat = printJob.defaultPage();
|
434 |
|
435 |
Paper paper = new Paper(); |
436 |
paper.setSize(595, 842); // default A4 |
437 |
paper.setImageableArea(43, 43, 545, 792); // actual print 'zone' |
438 |
|
439 |
pformat.setPaper(paper); |
440 |
|
441 |
// pageable provides a method getPageFormat(int p) - this method
|
442 |
// allows it to be set by JPedal
|
443 |
pf.setPageFormat(pformat); |
444 |
pf.setPagePrintRange((SetOfIntegerSyntax) r);
|
445 |
} catch (PdfException e) {
|
446 |
// TODO Auto-generated catch block
|
447 |
e.printStackTrace(); |
448 |
} |
449 |
|
450 |
/**
|
451 |
* generic call to both Pageable and printable
|
452 |
*/
|
453 |
if (printFile)
|
454 |
try {
|
455 |
pf.setPrintPageScalingMode(PrinterOptions.PAGE_SCALING_FIT_TO_PRINTER_MARGINS); |
456 |
printJob.print(); |
457 |
} catch (PrinterException e) { |
458 |
// TODO Auto-generated catch block
|
459 |
e.printStackTrace(); |
460 |
} |
461 |
} |
462 |
|
463 |
private JScrollPane getJPaneViewer() { |
464 |
JScrollPane currentScroll = new JScrollPane(); |
465 |
currentScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
466 |
currentScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
|
467 |
|
468 |
currentScroll.setViewportView(pf); |
469 |
return currentScroll;
|
470 |
} |
471 |
|
472 |
/**
|
473 |
* This is not used by hyperlink, but it's kept as it may be
|
474 |
* useful to get a quick PDF window in gvSIG.
|
475 |
*/
|
476 |
public WindowInfo getWindowInfo() {
|
477 |
if (m_viewInfo == null) { |
478 |
m_viewInfo = new WindowInfo(WindowInfo.PALETTE);
|
479 |
m_viewInfo.setMaximizable(true);
|
480 |
m_viewInfo.setWidth(this.getWidth());
|
481 |
m_viewInfo.setHeight(this.getHeight());
|
482 |
m_viewInfo.setTitle(PluginServices.getText(this, "pdf_viewer")); |
483 |
} |
484 |
return m_viewInfo;
|
485 |
} |
486 |
|
487 |
public void propertyChange(PropertyChangeEvent arg0) { |
488 |
// TODO Auto-generated method stub
|
489 |
|
490 |
} |
491 |
|
492 |
public Object getWindowProfile() { |
493 |
return WindowInfo.EDITOR_PROFILE;
|
494 |
} |
495 |
} |