001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.ui;
027:
028: import java.awt.*;
029: import java.awt.event.*;
030: import java.awt.image.*;
031: import java.awt.print.*;
032:
033: import javax.swing.*;
034: import javax.swing.border.*;
035:
036: /**
037: *
038: *
039: * @author $author$
040: * @version $Revision: 1.13 $
041: */
042: public class PrintPreview extends JPanel implements ActionListener,
043: Runnable {
044: /** */
045: protected PrinterJob job;
046:
047: /** */
048: protected JButton setup;
049:
050: /** */
051: protected JComboBox scale;
052:
053: /** */
054: protected int pageWidth;
055:
056: /** */
057: protected Printable printable;
058:
059: /** */
060: protected PreviewContainer previewer;
061:
062: /** */
063: protected PageFormat pageFormat;
064:
065: /** */
066: protected int pageHeight;
067:
068: /** */
069: protected JScrollPane scrollpane;
070:
071: /**
072: * Creates a new PrintPreview object.
073: *
074: * @param printable
075: * @param pageFormat
076: */
077: public PrintPreview(Printable printable, PageFormat pageFormat) {
078: super (new BorderLayout());
079: this .printable = printable;
080: job = PrinterJob.getPrinterJob();
081: this .pageFormat = pageFormat;
082:
083: JPanel jpanel = new JPanel(new FlowLayout(0, 2, 2));
084: setup = new JButton("Setup");
085: setup.addActionListener(this );
086: setup.setMnemonic('p');
087: jpanel.add(setup);
088:
089: String[] as = { "10 %", "25 %", "50 %", "75 %", "100 %",
090: "200 %" };
091: scale = new JComboBox(as);
092: scale.setSelectedItem(as[2]);
093: scale.addActionListener(this );
094: scale.setEditable(true);
095: jpanel.add(scale);
096: add(jpanel, BorderLayout.NORTH);
097: previewer = new PreviewContainer();
098:
099: if ((pageFormat.getHeight() == 0.0D)
100: || (pageFormat.getWidth() == 0.0D)) {
101: System.err.println("Unable to determine default page size");
102:
103: return;
104: }
105:
106: pageWidth = (int) pageFormat.getWidth();
107: pageHeight = (int) pageFormat.getHeight();
108:
109: byte byte0 = 50;
110: int i = (pageWidth * byte0) / 100;
111: int j = (pageHeight * byte0) / 100;
112: int k = 0;
113:
114: try {
115: do {
116: BufferedImage bufferedimage = new BufferedImage(
117: pageWidth, pageHeight, 1);
118: Graphics g = bufferedimage.getGraphics();
119: g.setColor(Color.white);
120: g.fillRect(0, 0, pageWidth, pageHeight);
121:
122: if (printable.print(g, pageFormat, k) != 0) {
123: break;
124: }
125:
126: PagePreview pagepreview = new PagePreview(i, j,
127: bufferedimage);
128: previewer.add(pagepreview);
129: k++;
130: } while (true);
131: } catch (PrinterException printerexception) {
132: printerexception.printStackTrace();
133: System.err.println("Printing error: "
134: + printerexception.toString());
135: }
136:
137: scrollpane = new JScrollPane(previewer);
138: add(scrollpane, BorderLayout.CENTER);
139: }
140:
141: /**
142: *
143: *
144: * @return
145: */
146: public PageFormat getPageFormat() {
147: return pageFormat;
148: }
149:
150: private void setup() {
151: pageFormat = job.pageDialog(pageFormat);
152: pageWidth = (int) pageFormat.getWidth();
153: pageHeight = (int) pageFormat.getHeight();
154:
155: Thread thread = new Thread(this );
156: thread.start();
157: }
158:
159: /**
160: *
161: */
162: public void run() {
163: String s = scale.getSelectedItem().toString();
164: scrollpane.setViewportView(new JLabel("Resizing"));
165:
166: if (s.endsWith("%")) {
167: s = s.substring(0, s.length() - 1);
168: }
169:
170: s = s.trim();
171:
172: int i = 0;
173:
174: try {
175: i = Integer.parseInt(s);
176: } catch (NumberFormatException numberformatexception) {
177: i = 100;
178: }
179:
180: int j = (pageWidth * i) / 100;
181: int k = (pageHeight * i) / 100;
182: Component[] acomponent = previewer.getComponents();
183:
184: for (int l = 0; l < acomponent.length; l++) {
185: if (acomponent[l] instanceof PagePreview) {
186: PagePreview pagepreview = (PagePreview) acomponent[l];
187: pagepreview.setScaledSize(j, k);
188: }
189: }
190:
191: scrollpane.setViewportView(previewer);
192: }
193:
194: /**
195: *
196: *
197: * @param actionevent
198: */
199: public void actionPerformed(ActionEvent actionevent) {
200: if (actionevent.getSource() == setup) {
201: setup();
202: } else if (actionevent.getSource() == scale) {
203: Thread thread = new Thread(this );
204: thread.start();
205: }
206: }
207:
208: class PagePreview extends JPanel {
209: protected int m_h;
210: protected int m_w;
211: protected Image m_img;
212: protected Image m_source;
213:
214: public PagePreview(int i, int j, Image image) {
215: m_w = i;
216: m_h = j;
217: m_source = image;
218: m_img = m_source.getScaledInstance(m_w, m_h, 4);
219: m_img.flush();
220: setBackground(Color.white);
221: setBorder(new MatteBorder(1, 1, 2, 2, Color.black));
222: }
223:
224: public Dimension getMaximumSize() {
225: return getPreferredSize();
226: }
227:
228: public Dimension getPreferredSize() {
229: Insets insets = getInsets();
230:
231: return new Dimension(m_w + insets.left + insets.right, m_h
232: + insets.top + insets.bottom);
233: }
234:
235: public void paint(Graphics g) {
236: g.setColor(getBackground());
237: g.fillRect(0, 0, getWidth(), getHeight());
238: g.drawImage(m_img, 0, 0, this );
239: paintBorder(g);
240: }
241:
242: public Dimension getMinimumSize() {
243: return getPreferredSize();
244: }
245:
246: public void setScaledSize(int i, int j) {
247: m_w = i;
248: m_h = j;
249: m_img = m_source.getScaledInstance(m_w, m_h, 4);
250: repaint();
251: }
252: }
253:
254: class PreviewContainer extends JPanel {
255: protected int V_GAP;
256: protected int H_GAP;
257:
258: PreviewContainer() {
259: V_GAP = 10;
260: H_GAP = 16;
261: }
262:
263: public Dimension getMaximumSize() {
264: return getPreferredSize();
265: }
266:
267: public void doLayout() {
268: Insets insets = getInsets();
269: int i = insets.left + H_GAP;
270: int j = insets.top + V_GAP;
271: int k = getComponentCount();
272:
273: if (k == 0) {
274: return;
275: }
276:
277: Component component = getComponent(0);
278: Dimension dimension = component.getPreferredSize();
279: int l = dimension.width;
280: int i1 = dimension.height;
281: Dimension dimension1 = getParent().getSize();
282: int j1 = Math.max((dimension1.width - H_GAP) / (l + H_GAP),
283: 1);
284: int k1 = k / j1;
285:
286: if ((k1 * j1) < k) {
287: k1++;
288: }
289:
290: int l1 = 0;
291:
292: for (int i2 = 0; i2 < k1; i2++) {
293: for (int j2 = 0; j2 < j1; j2++) {
294: if (l1 >= k) {
295: return;
296: }
297:
298: Component component1 = getComponent(l1++);
299: component1.setBounds(i, j, l, i1);
300: i += (l + H_GAP);
301: }
302:
303: j += (i1 + V_GAP);
304: i = insets.left + H_GAP;
305: }
306: }
307:
308: public Dimension getMinimumSize() {
309: return getPreferredSize();
310: }
311:
312: public Dimension getPreferredSize() {
313: int i = getComponentCount();
314:
315: if (i == 0) {
316: return new Dimension(H_GAP, V_GAP);
317: }
318:
319: Component component = getComponent(0);
320: Dimension dimension = component.getPreferredSize();
321: int j = dimension.width;
322: int k = dimension.height;
323: Dimension dimension1 = getParent().getSize();
324: int l = Math.max((dimension1.width - H_GAP) / (j + H_GAP),
325: 1);
326: int i1 = i / l;
327:
328: if ((i1 * l) < i) {
329: i1++;
330: }
331:
332: int j1 = (l * (j + H_GAP)) + H_GAP;
333: int k1 = (i1 * (k + V_GAP)) + V_GAP;
334: Insets insets = getInsets();
335:
336: return new Dimension(j1 + insets.left + insets.right, k1
337: + insets.top + insets.bottom);
338: }
339: }
340: }
|