KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > PrintPreview


1 /*
2  * SSHTools - Java SSH2 API
3  *
4  * Copyright (C) 2002 Lee David Painter.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * You may also distribute it and/or modify it under the terms of the
12  * Apache style J2SSH Software License. A copy of which should have
13  * been provided with the distribution.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * License document supplied with your distribution for more details.
19  *
20  */

21
22 package com.sshtools.ui.swing;
23
24 import java.awt.BorderLayout JavaDoc;
25 import java.awt.Color JavaDoc;
26 import java.awt.Component JavaDoc;
27 import java.awt.Dimension JavaDoc;
28 import java.awt.FlowLayout JavaDoc;
29 import java.awt.Graphics JavaDoc;
30 import java.awt.Image JavaDoc;
31 import java.awt.Insets JavaDoc;
32 import java.awt.event.ActionEvent JavaDoc;
33 import java.awt.event.ActionListener JavaDoc;
34 import java.awt.image.BufferedImage JavaDoc;
35 import java.awt.print.PageFormat JavaDoc;
36 import java.awt.print.Printable JavaDoc;
37 import java.awt.print.PrinterException JavaDoc;
38 import java.awt.print.PrinterJob JavaDoc;
39 import javax.swing.JButton JavaDoc;
40 import javax.swing.JComboBox JavaDoc;
41 import javax.swing.JLabel JavaDoc;
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.JScrollPane JavaDoc;
44 import javax.swing.border.MatteBorder JavaDoc;
45
46 /**
47  *
48  *
49  * @author $author$
50  */

51 public class PrintPreview extends JPanel JavaDoc implements ActionListener JavaDoc, Runnable JavaDoc {
52     /** */
53     protected PrinterJob JavaDoc job;
54
55     /** */
56     protected JButton JavaDoc setup;
57
58     /** */
59     protected JComboBox JavaDoc scale;
60
61     /** */
62     protected int pageWidth;
63
64     /** */
65     protected Printable JavaDoc printable;
66
67     /** */
68     protected PreviewContainer previewer;
69
70     /** */
71     protected PageFormat JavaDoc pageFormat;
72
73     /** */
74     protected int pageHeight;
75
76     /** */
77     protected JScrollPane JavaDoc scrollpane;
78
79     /**
80      * Creates a new PrintPreview object.
81      *
82      * @param printable
83      * @param pageFormat
84      */

85     public PrintPreview(Printable JavaDoc printable, PageFormat JavaDoc pageFormat) {
86         super(new BorderLayout JavaDoc());
87         this.printable = printable;
88         job = PrinterJob.getPrinterJob();
89         this.pageFormat = pageFormat;
90
91         JPanel JavaDoc jpanel = new JPanel JavaDoc(new FlowLayout JavaDoc(0, 2, 2));
92         setup = new JButton JavaDoc("Setup");
93         setup.addActionListener(this);
94         setup.setMnemonic('p');
95         jpanel.add(setup);
96
97         String JavaDoc[] as = { "10 %", "25 %", "50 %", "75 %", "100 %", "200 %" };
98         scale = new JComboBox JavaDoc(as);
99         scale.setSelectedItem(as[2]);
100         scale.addActionListener(this);
101         scale.setEditable(true);
102         jpanel.add(scale);
103         add(jpanel, BorderLayout.NORTH);
104         previewer = new PreviewContainer();
105
106         if ((pageFormat.getHeight() == 0.0D) || (pageFormat.getWidth() == 0.0D)) {
107             return;
108         }
109
110         pageWidth = (int) pageFormat.getWidth();
111         pageHeight = (int) pageFormat.getHeight();
112
113         byte byte0 = 50;
114         int i = (pageWidth * byte0) / 100;
115         int j = (pageHeight * byte0) / 100;
116         int k = 0;
117
118         try {
119             do {
120                 BufferedImage JavaDoc bufferedimage = new BufferedImage JavaDoc(pageWidth, pageHeight, 1);
121                 Graphics JavaDoc g = bufferedimage.getGraphics();
122                 g.setColor(Color.white);
123                 g.fillRect(0, 0, pageWidth, pageHeight);
124
125                 if (printable.print(g, pageFormat, k) != 0) {
126                     break;
127                 }
128
129                 PagePreview pagepreview = new PagePreview(i, j, bufferedimage);
130                 previewer.add(pagepreview);
131                 k++;
132             } while (true);
133         } catch (PrinterException JavaDoc printerexception) {
134         }
135
136         scrollpane = new JScrollPane JavaDoc(previewer);
137         add(scrollpane, BorderLayout.CENTER);
138     }
139
140     /**
141      *
142      *
143      * @return
144      */

145     public PageFormat JavaDoc getPageFormat() {
146         return pageFormat;
147     }
148
149     private void setup() {
150         pageFormat = job.pageDialog(pageFormat);
151         pageWidth = (int) pageFormat.getWidth();
152         pageHeight = (int) pageFormat.getHeight();
153
154         Thread JavaDoc thread = new Thread JavaDoc(this);
155         thread.start();
156     }
157
158     /**
159      *
160      */

161     public void run() {
162         String JavaDoc s = scale.getSelectedItem().toString();
163         scrollpane.setViewportView(new JLabel JavaDoc("Resizing"));
164
165         if (s.endsWith("%")) {
166             s = s.substring(0, s.length() - 1);
167         }
168
169         s = s.trim();
170
171         int i = 0;
172
173         try {
174             i = Integer.parseInt(s);
175         } catch (NumberFormatException JavaDoc numberformatexception) {
176             i = 100;
177         }
178
179         int j = (pageWidth * i) / 100;
180         int k = (pageHeight * i) / 100;
181         Component JavaDoc[] acomponent = previewer.getComponents();
182
183         for (int l = 0; l < acomponent.length; l++) {
184             if (acomponent[l] instanceof PagePreview) {
185                 PagePreview pagepreview = (PagePreview) acomponent[l];
186                 pagepreview.setScaledSize(j, k);
187             }
188         }
189
190         scrollpane.setViewportView(previewer);
191     }
192
193     /**
194      *
195      *
196      * @param actionevent
197      */

198     public void actionPerformed(ActionEvent JavaDoc actionevent) {
199         if (actionevent.getSource() == setup) {
200             setup();
201         } else if (actionevent.getSource() == scale) {
202             Thread JavaDoc thread = new Thread JavaDoc(this);
203             thread.start();
204         }
205     }
206
207     class PagePreview extends JPanel JavaDoc {
208         protected int m_h;
209         protected int m_w;
210         protected Image JavaDoc m_img;
211         protected Image JavaDoc m_source;
212
213         public PagePreview(int i, int j, Image JavaDoc image) {
214             m_w = i;
215             m_h = j;
216             m_source = image;
217             m_img = m_source.getScaledInstance(m_w, m_h, 4);
218             m_img.flush();
219             setBackground(Color.white);
220             setBorder(new MatteBorder JavaDoc(1, 1, 2, 2, Color.black));
221         }
222
223         public Dimension JavaDoc getMaximumSize() {
224             return getPreferredSize();
225         }
226
227         public Dimension JavaDoc getPreferredSize() {
228             Insets JavaDoc insets = getInsets();
229
230             return new Dimension JavaDoc(m_w + insets.left + insets.right, m_h + insets.top + insets.bottom);
231         }
232
233         public void paint(Graphics JavaDoc g) {
234             g.setColor(getBackground());
235             g.fillRect(0, 0, getWidth(), getHeight());
236             g.drawImage(m_img, 0, 0, this);
237             paintBorder(g);
238         }
239
240         public Dimension JavaDoc getMinimumSize() {
241             return getPreferredSize();
242         }
243
244         public void setScaledSize(int i, int j) {
245             m_w = i;
246             m_h = j;
247             m_img = m_source.getScaledInstance(m_w, m_h, 4);
248             repaint();
249         }
250     }
251
252     class PreviewContainer extends JPanel JavaDoc {
253         protected int V_GAP;
254         protected int H_GAP;
255
256         PreviewContainer() {
257             V_GAP = 10;
258             H_GAP = 16;
259         }
260
261         public Dimension JavaDoc getMaximumSize() {
262             return getPreferredSize();
263         }
264
265         public void doLayout() {
266             Insets JavaDoc insets = getInsets();
267             int i = insets.left + H_GAP;
268             int j = insets.top + V_GAP;
269             int k = getComponentCount();
270
271             if (k == 0) {
272                 return;
273             }
274
275             Component JavaDoc component = getComponent(0);
276             Dimension JavaDoc dimension = component.getPreferredSize();
277             int l = dimension.width;
278             int i1 = dimension.height;
279             Dimension JavaDoc dimension1 = getParent().getSize();
280             int j1 = Math.max((dimension1.width - H_GAP) / (l + H_GAP), 1);
281             int k1 = k / j1;
282
283             if ((k1 * j1) < k) {
284                 k1++;
285             }
286
287             int l1 = 0;
288
289             for (int i2 = 0; i2 < k1; i2++) {
290                 for (int j2 = 0; j2 < j1; j2++) {
291                     if (l1 >= k) {
292                         return;
293                     }
294
295                     Component JavaDoc component1 = getComponent(l1++);
296                     component1.setBounds(i, j, l, i1);
297                     i += (l + H_GAP);
298                 }
299
300                 j += (i1 + V_GAP);
301                 i = insets.left + H_GAP;
302             }
303         }
304
305         public Dimension JavaDoc getMinimumSize() {
306             return getPreferredSize();
307         }
308
309         public Dimension JavaDoc getPreferredSize() {
310             int i = getComponentCount();
311
312             if (i == 0) {
313                 return new Dimension JavaDoc(H_GAP, V_GAP);
314             }
315
316             Component JavaDoc component = getComponent(0);
317             Dimension JavaDoc dimension = component.getPreferredSize();
318             int j = dimension.width;
319             int k = dimension.height;
320             Dimension JavaDoc dimension1 = getParent().getSize();
321             int l = Math.max((dimension1.width - H_GAP) / (j + H_GAP), 1);
322             int i1 = i / l;
323
324             if ((i1 * l) < i) {
325                 i1++;
326             }
327
328             int j1 = (l * (j + H_GAP)) + H_GAP;
329             int k1 = (i1 * (k + V_GAP)) + V_GAP;
330             Insets JavaDoc insets = getInsets();
331
332             return new Dimension JavaDoc(j1 + insets.left + insets.right, k1 + insets.top + insets.bottom);
333         }
334     }
335 }
336
Popular Tags