KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > editor > jpegphotoeditor


1 package com.ca.directory.jxplorer.editor;
2
3 import com.ca.commons.cbutil.*;
4 import com.ca.directory.jxplorer.JXplorer;
5 import com.ca.directory.jxplorer.HelpIDs;
6
7 import javax.swing.*;
8 import java.awt.*;
9 import java.io.*;
10 import javax.swing.ImageIcon JavaDoc;
11 import java.beans.PropertyChangeEvent JavaDoc;
12 import java.beans.PropertyChangeListener JavaDoc;
13 import java.util.logging.Logger JavaDoc;
14 import java.util.logging.Level JavaDoc;
15
16 /**
17  * JPEG photo viewer.
18  * Allows users to view a JPEG photo and save or load to/from a file.
19  * @author Trudi.
20  */

21 public class jpegphotoeditor extends basicbinaryeditor
22 {
23     protected ImageIcon JavaDoc iconBytes;
24     protected JLabel label;
25     protected int imageHeight, imageWidth, screenHeight, screenWidth; //TE: used for the height and width of the image and screen size.
26
protected JScrollPane scrollPaneLabel;
27
28     private final static Logger JavaDoc log = Logger.getLogger(jpegphotoeditor.class.getName());
29
30     /**
31      * Constructor.
32      * @param owner handle to the application frame, used to display dialog boxes.
33      */

34     public jpegphotoeditor(Frame owner)
35     {
36         this(owner, false);
37     }
38
39     /**
40      * Sets up the frame with one panel, one label which displays the JPEG image and five buttons.
41      * @param owner handle to the application frame, used to display dialog boxes.
42      * @param viewable specifies if there is a viewer for the binary data, if true, the "view" button is added to the panel.
43      */

44     public jpegphotoeditor(Frame owner, boolean viewable)
45     {
46         super(owner);
47         setTitle(CBIntText.get("jpegPhoto"));
48         btnEdit.setVisible(false);
49     }
50
51     /**
52      * Adds the label (which is used for displaying the image) to the panel.
53      */

54     public void addMainViewComponent()
55     {
56         label = new JLabel();
57         scrollPaneLabel = new JScrollPane(label);
58         display.makeHeavy();
59         display.addln(scrollPaneLabel);
60         display.makeLight();
61     }
62
63     /**
64      * Sets up the help button (which is used to open the java help at the appropriate location).
65      * @return btnHelp the button to be added.
66      */

67     public CBButton addHelp()
68     {
69         btnHelp = new CBButton(CBIntText.get("Help"), CBIntText.get("Click here for Help.")); //TE: creates a new help button with a listener that will open JX help at appropriate location.
70
CBHelpSystem.useDefaultHelp(btnHelp, HelpIDs.ATTR_JPEGPHOTO);
71
72         return btnHelp;
73     }
74
75     /**
76      * Sets the size of the panel according to the size of the image.
77      * Reduces the size of the image if bigger than screen size.
78      * Increases the size of the panel if too small.
79      */

80     public void preferredSize(int newWidth, int newHeight)
81     {
82
83         newHeight = newHeight + 80; //TE: image height plus button panel height.
84
if (newWidth < 345)
85         {
86             newWidth = 345;
87         } //TE: minimum width to show all buttons.
88

89         Toolkit toolKit = Toolkit.getDefaultToolkit();
90
91         screenWidth = toolKit.getScreenSize().width;
92         screenHeight = toolKit.getScreenSize().height;
93
94         if (newWidth >= screenWidth)
95         {
96             newWidth = screenWidth - 50;
97         } //TE: we don't want the window to be wider than the screen.
98
if (newHeight >= screenHeight)
99         {
100             newHeight = screenHeight - 50;
101         } //TE: we don't want the window to be higher than the screen.
102

103         setBounds((screenWidth - newWidth) / 2, (screenHeight - newHeight) / 2, newWidth, newHeight); //TE: sets the windows position on the screen and its width & height.
104
}
105
106     /**
107      * Sets the image to display in the editor.
108      */

109     public void setValue(editablebinary editMe)
110     {
111         this.editMe = editMe;
112
113         bytes = editMe.getValue();
114
115         oldBytes = bytes;
116
117         if (bytes != null)
118         {
119             setButtons(true); //TE: enables the ok & save buttons (only needed if value is available).
120
iconBytes = new ImageIcon JavaDoc(bytes);
121             imageHeight = iconBytes.getIconHeight();
122             imageWidth = iconBytes.getIconWidth();
123             preferredSize(imageWidth, imageHeight);
124             label.setIcon(iconBytes);
125         }
126         else
127         {
128             preferredSize(400, 300);
129             setButtons(false); //TE: disables the ok & save buttons (only needed if value is available).
130
}
131     }
132
133     /**
134      * Create the gui chooser for the user to pick a photo with, i.e. load binary data from file.
135      */

136     protected void load()
137     {
138         CBCache.cleanCache(currentDN.toString()); //TE: delete any temporary files associates with this entry.
139

140         JFileChooser chooser = new JFileChooser(JXplorer.getProperty("binary.homeDir"));
141         chooser.addChoosableFileFilter(new CBFileFilter(new String JavaDoc[]{"jpeg", "jpg"}, "JPEG Files (*.jpeg, *.jpg)"));
142
143         ImageAccessory ip = new ImageAccessory(); //TE: sets up the 'play' and 'stop' feature in the JFileChooser.
144
chooser.setAccessory(ip);
145         chooser.addPropertyChangeListener(ip);
146
147
148         if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION)
149             return;
150         File file = chooser.getSelectedFile();
151         JXplorer.setProperty("binary.homeDir", chooser.getSelectedFile().getParent());
152
153         try
154         {
155             FileInputStream input = new FileInputStream(file);
156
157             int length = (int) file.length();
158             if (length > 0)
159             {
160                 bytes = new byte[length];
161                 int read = input.read(bytes);
162                 if (read > 0)
163                 {
164                     iconBytes = new ImageIcon JavaDoc(bytes); //TE: set the selected image in the editor.
165
imageHeight = iconBytes.getIconHeight();
166                     imageWidth = iconBytes.getIconWidth();
167                     label.setIcon(iconBytes);
168                     preferredSize(imageWidth, imageHeight);
169                     setButtons(true); //TE: enables the ok & save buttons (only needed if a value is available).
170
}
171             }
172             input.close();
173         }
174         catch (IOException e)
175         {
176             log.log(Level.WARNING,"Error opening the file!",e);
177         }
178         setVisible(false); //TE: just to get the refresh!!!
179
setVisible(true); //TE: needs to go after the field is set to ensure the attribute value is captured.
180
}
181
182     /**
183      * Save binary data to the file.
184      */

185     protected void save()
186     {
187
188         JFileChooser chooser = new JFileChooser(JXplorer.getProperty("binary.homeDir"));
189
190         if (chooser.showSaveDialog(frame) != JFileChooser.APPROVE_OPTION)
191             return;
192
193         File file = chooser.getSelectedFile();
194         try
195         {
196             FileOutputStream output = new FileOutputStream(file);
197             output.write(bytes);
198             output.close();
199         }
200         catch (IOException e)
201         {
202             log.log(Level.WARNING,"Error writing to the file! ", e);
203         }
204     }
205
206     /**
207      * Enables/disables the ok & save buttons in the editor.
208      */

209     public void setButtons(boolean enabled)
210     {
211         btnOK.setEnabled(enabled);
212         btnSave.setEnabled(enabled);
213     }
214
215     /**
216      * Returns a new value.
217      * @return new value.
218      */

219     public byte[] getNewValue()
220     {
221         if (bytes != null && bytes.length != 0)
222             return bytes;
223         else
224             return null;
225     }
226
227     /**
228      * Sets the value in the table editor.
229      */

230     public void setValue()
231     {
232         if (isChanged())
233             editMe.setValue(getNewValue());
234         quit();
235     }
236
237     /**
238      * Creates a thumbnail of the image that is clicked on
239      * in the file chooser.
240      */

241     public class ImageAccessory extends JComponent
242             implements PropertyChangeListener JavaDoc
243     {
244         ImageIcon JavaDoc image = null;
245         File file = null;
246
247         /**
248          * Constructor that sets the image size.
249          */

250         public ImageAccessory()
251         {
252             setPreferredSize(new Dimension(100, 50));
253         }
254
255         /**
256          * Formats the image.
257          */

258         public void loadImage()
259         {
260             if (file == null)
261             {
262                 image = null;
263                 return;
264             }
265
266             ImageIcon JavaDoc tempImage = new ImageIcon JavaDoc(file.getPath());
267             if (tempImage != null)
268             {
269                 if (tempImage.getIconWidth() > 90)
270                     image = new ImageIcon JavaDoc(tempImage.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT));
271                 else
272                     image = tempImage;
273             }
274         }
275
276         /**
277          * Listens for file selection changes.
278          * @param e
279          */

280         public void propertyChange(PropertyChangeEvent JavaDoc e)
281         {
282             boolean update = false;
283             String JavaDoc prop = e.getPropertyName();
284
285             if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop))
286             {
287                 file = null;
288                 update = true;
289             }
290             else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop))
291             {
292                 file = (File) e.getNewValue();
293                 update = true;
294             }
295
296             if (update)
297             {
298                 image = null;
299                 if (isShowing())
300                 {
301                     loadImage();
302                     repaint();
303                 }
304             }
305         }
306
307         /**
308          * Paints the image.
309          * @param g
310          */

311         public void paintComponent(Graphics g)
312         {
313             if (image == null)
314             {
315                 loadImage();
316             }
317             if (image != null)
318             {
319                 int x = getWidth() / 2 - image.getIconWidth() / 2;
320                 int y = getHeight() / 2 - image.getIconHeight() / 2;
321
322                 if (y < 0)
323                 {
324                     y = 0;
325                 }
326
327                 if (x < 5)
328                 {
329                     x = 5;
330                 }
331                 image.paintIcon(this, g, x, y);
332             }
333         }
334     }
335 }
Popular Tags