KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > activation > viewers > ImageViewer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)ImageViewer.java 1.8 05/11/16
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.activation.viewers;
29
30 import java.awt.*;
31 import java.io.*;
32 import java.beans.*;
33 import javax.activation.*;
34
35 public class ImageViewer extends Panel implements CommandObject {
36     // UI Vars...
37
private ImageViewerCanvas canvas = null;
38     
39     // File Vars
40
// private InputStream data_ins = null;
41
private Image image = null;
42     private DataHandler _dh = null;
43     
44     private boolean DEBUG = false;
45     /**
46      * Constructor
47      */

48     public ImageViewer(){
49     
50     // create the ImageViewerCanvas
51
canvas = new ImageViewerCanvas();
52     add(canvas);
53     }
54     /**
55      * Set the DataHandler for this CommandObject
56      * @param DataHandler the DataHandler
57      */

58     public void setCommandContext(String JavaDoc verb, DataHandler dh) throws IOException{
59     _dh = dh;
60     this.setInputStream( _dh.getInputStream() );
61     }
62     //--------------------------------------------------------------------
63

64     /**
65      * Set the data stream, component to assume it is ready to
66      * be read.
67      */

68     private void setInputStream(InputStream ins) throws IOException {
69     MediaTracker mt = new MediaTracker(this);
70     int bytes_read = 0;
71     byte data[] = new byte[1024];
72     ByteArrayOutputStream baos = new ByteArrayOutputStream();
73     
74     while((bytes_read = ins.read(data)) >0)
75         baos.write(data, 0, bytes_read);
76     ins.close();
77     
78     // convert the buffer into an image
79
image = getToolkit().createImage(baos.toByteArray());
80     
81     mt.addImage(image, 0);
82     
83     try {
84         mt.waitForID(0);
85         mt.waitForAll();
86         if(mt.statusID(0, true ) != MediaTracker.COMPLETE){
87         System.out.println("Error occured in image loading = " +
88                    mt.getErrorsID(0));
89         
90         }
91         
92     }
93     catch(InterruptedException JavaDoc e) {
94         throw new IOException("Error reading image data");
95     }
96     
97     canvas.setImage(image);
98     if(DEBUG)
99         System.out.println("calling invalidate");
100     
101     }
102     //--------------------------------------------------------------------
103
public void addNotify(){
104     super.addNotify(); // call the real one first...
105
this.invalidate();
106     this.validate();
107     this.doLayout();
108     }
109     //--------------------------------------------------------------------
110
public Dimension getPreferredSize(){
111     return canvas.getPreferredSize();
112     }
113
114 }
115
116
117
118
119
120
121
122
123
124
125
126
Popular Tags