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