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