1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| public class PreviewDialog extends JDialog implements ActionListener {
private final static double DEFAULT_ZOOM_FACTOR_STEP = 0.1;
protected Pageable pageable;
public PreviewDialog (String title, JFrame owner, Pageable pageable, double zoom){
super(owner,title,true);
this.pageable = pageable;
Preview preview = new Preview(pageable, zoom);
JScrollPane scrollpane = new JScrollPane(preview);
getContentPane().add(scrollpane, "Center");
JToolBar toolbar = new JToolBar();
toolbar.add(getButton("Back24.gif", new BrowseAction(preview, -1)));
toolbar.add(getButton("Forward.gif", new BrowseAction(preview, 1)));
toolbar.add(new JToolBar.Separator());
toolbar.add(getButton("ZoomIn24.gif", new ZoomAction(preview, DEFAULT_ZOOM_FACTOR_STEP)));
toolbar.add(getButton("ZoomOut24.gif", new ZoomAction(preview, -DEFAULT_ZOOM_FACTOR_STEP)));
toolbar.add(new JToolBar.Separator());
JPanel dialog = new JPanel();
dialog.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton ok=new JButton("OK");
ok.addActionListener(this);
dialog.add(ok);
getContentPane().add(dialog, "South");
}
public PreviewDialog(String title, JFrame owner, Pageable pageable){
this(title, owner, pageable, 0.0);
}
public PreviewDialog(String title, JFrame owner, Printable printable, PageFormat format, int pages, double zoom){
this(title, owner, new MyPageable(printable, format,pages), zoom);
}
public PreviewDialog(String title, JFrame owner, Printable printable,PageFormat format, int pages){
this(title, owner, printable, format,pages, 0.0);
}
public static class MyPageable implements Pageable {
private Printable printable;
private PageFormat format;
private int pages;
public MyPageable(Printable printable, PageFormat format, int pages){
this.printable = printable;
this.format = format;
this.pages = pages;
}
public int getNumberOfPages() {
return pages;
}
public PageFormat getPageFormat(int index){
if (index >= pages) throw new IndexOutOfBoundsException ();
return format;
}
public Printable getPrintable(int index){
if (index >= pages) throw new IndexOutOfBoundsException ();
return printable;
}
}
private JButton getButton(String iconName){
return getButton(null, iconName, null);
}
private JButton getButton(String iconName, AbstractAction action){
return getButton(null, iconName, action);
}
private JButton getButton(String name, String iconName, AbstractAction action){
JButton result = null;
ImageIcon icon = null;
URL imageURL = getClass().getClassLoader().getResource("images/"+iconName);
if(imageURL != null)
icon = new ImageIcon(imageURL);
if(action != null){
if(icon != null) action.putValue(Action.SMALL_ICON, new ImageIcon(imageURL));
if(name != null) action.putValue(Action.NAME, name);
result = new JButton(action);
} else
result = new JButton(name, icon);
return result;
}
public void actionPerformed(ActionEvent e) {
dispose();
}
} |
Partager