KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > view > attachments > ImageViewer


1 package SnowMailClient.view.attachments;
2
3 import SnowMailClient.model.multipart.*;
4 import SnowMailClient.SnowMailClientApp;
5 import snow.utils.storage.*;
6 import snow.utils.gui.*;
7 import snow.Language.Language;
8
9 import javax.swing.*;
10 import javax.imageio.*;
11 import java.awt.*;
12 import java.awt.image.*;
13 import java.awt.event.*;
14 import java.io.*;
15
16 /** extract image from the mime part in a temp file and show it.
17    limit to the frame size.
18 */

19 public class ImageViewer extends JDialog
20 {
21   final ImageViewerPanel imagePanel = new ImageViewerPanel();
22
23   public ImageViewer(SnowMailClientApp ref, MimePart part)
24   {
25     super(ref, "Image viewer (Mime part "+part.getName()+")", true);
26
27
28     getContentPane().setLayout(new BorderLayout());
29     getContentPane().add(imagePanel, BorderLayout.CENTER);
30
31     JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
32     getContentPane().add(controlPanel, BorderLayout.SOUTH);
33
34     JButton okBt = new JButton(Language.translate("Close"));
35     controlPanel.add(okBt);
36     okBt.setBackground(Color.orange);
37     okBt.setMargin(new Insets(1,2,1,2));
38     okBt.addActionListener(new ActionListener()
39     {
40       public void actionPerformed(ActionEvent e)
41       {
42          setVisible(false);
43          imagePanel.setImage(null);
44          dispose();
45       }
46     });
47
48     int sizeX = 300;
49     int sizeY = 200;
50
51     BufferedImage bim = null;
52
53     try
54     {
55       byte[] cont = part.getByteContent();
56       ByteArrayInputStream bin = new ByteArrayInputStream(cont);
57       bim = ImageIO.read(bin);
58
59       sizeX = bim.getWidth();
60       sizeY = bim.getHeight();
61     }
62     catch(Exception JavaDoc e)
63     {
64       e.printStackTrace();
65       //imageLabel.setText(Language.translate("Cannot read image: %", e.getMessage()));
66
}
67     /*
68     if(sizeX<200) sizeX = 200;
69     if(sizeY<200) sizeY = 200;
70
71     Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
72     if(sizeX>screenDim.getWidth()) sizeX = (int)(screenDim.getWidth()*0.8);
73     if(sizeY>screenDim.getHeight()) sizeY = (int)(screenDim.getHeight()*0.8);
74     */

75     //this.setSize(sizeX + 40, sizeY + 80);
76
//this.pack();
77

78     ref.getProperties().setComponentSizeFromINIFile(this, "ImageViewer", 400, 400, 200,200);
79     this.setPreferredSize( this.getPreferredSize() );
80     imagePanel.setPreferredSize( imagePanel.getPreferredSize() );
81     imagePanel.setSize( imagePanel.getPreferredSize() );
82
83     // only set now, after size has been set
84
imagePanel.setImage(bim);
85
86     this.setVisible(true); // MODAL !
87

88     // store at end of the modal dialog...
89
ref.getProperties().saveComponentSizeInINIFile(this, "ImageViewer");
90     imagePanel.terminate();
91
92   } // Constructor
93

94
95
96
97
98 } // ImageViewer
Popular Tags