KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nqadmin > swingSet > SSImage


1 /* $Id: SSImage.java,v 1.12 2005/02/21 16:31:33 prasanth Exp $
2  *
3  * Tab Spacing = 4
4  *
5  * Copyright (c) 2003-2005, The Pangburn Company and Prasanth R. Pasala
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer. Redistributions in binary
13  * form must reproduce the above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or other materials
15  * provided with the distribution. The names of its contributors may not be
16  * used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  */

32
33 package com.nqadmin.swingSet;
34
35 import javax.swing.JLabel JavaDoc;
36 import javax.swing.ImageIcon JavaDoc;
37 import java.awt.Dimension JavaDoc;
38 import javax.sql.RowSet JavaDoc;
39 import javax.sql.RowSetEvent JavaDoc;
40 import javax.sql.RowSetListener JavaDoc;
41 import java.sql.SQLException JavaDoc;
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.JButton JavaDoc;
44 import java.awt.GridBagLayout JavaDoc;
45 import java.awt.GridBagConstraints JavaDoc;
46 import java.awt.event.ActionListener JavaDoc;
47 import java.awt.event.ActionEvent JavaDoc;
48 import java.io.FileInputStream JavaDoc;
49 import java.io.File JavaDoc;
50 import javax.swing.JFileChooser JavaDoc;
51 import java.io.IOException JavaDoc;
52 import javax.swing.JScrollPane JavaDoc;
53 import com.nqadmin.swingSet.datasources.SSRowSet;
54
55 /**
56  * SSImage.java
57  *<p>
58  * SwingSet - Open Toolkit For Making Swing Controls Database-Aware
59  *<p><pre>
60  * Used to load, store, & display images stored in a database.
61  *</pre><p>
62  * @author $Author: prasanth $
63  * @version $Revision: 1.12 $
64  */
public class SSImage extends JPanel JavaDoc {
65
66     /**
67      * ImageIcon to store the image.
68      */

69     protected ImageIcon JavaDoc img;
70
71     /**
72      * Label to display the image
73      */

74     protected JLabel JavaDoc lblImage = new JLabel JavaDoc("No Picture");
75
76     /**
77      * Button to update the image.
78      */

79     protected JButton JavaDoc btnUpdateImage = new JButton JavaDoc("Update");
80
81     /**
82      * SSRowSet from which component will get/set values.
83      */

84     protected SSRowSet sSRowSet;
85
86     /**
87      * SSRowSet column to which the component will be bound.
88      */

89     protected String JavaDoc columnName = "";
90
91     /**
92      * The preferred size of the image component.
93      */

94     protected Dimension JavaDoc preferredSize = new Dimension JavaDoc(200,200);
95
96     /**
97      * RowSet listener
98      */

99     private final MyRowSetListener sSRowSetListener = new MyRowSetListener();
100
101     /**
102      *
103      * Construct a default SSImage Object.
104      */

105     public SSImage() {
106         init();
107     }
108
109     /**
110      * Constructs a SSImage Object bound to the specified column in the specified sSRowSet.
111      *
112      * @param _sSRowSet - sSRowSet from/to which data has to be read/written
113      * @param _columnName - column in the sSRowSet to which the component should be bound.
114      */

115     public SSImage(SSRowSet _sSRowSet, String JavaDoc _columnName) {
116         sSRowSet = _sSRowSet;
117         columnName = _columnName;
118         init();
119         bind();
120     }
121
122     /**
123      * Sets the SSRowSet to which the component is bound.
124      *
125      * @param _sSRowSet SSRowSet to which the component is bound
126      */

127     public void setSSRowSet(SSRowSet _sSRowSet) {
128         SSRowSet oldValue = sSRowSet;
129         sSRowSet = _sSRowSet;
130         firePropertyChange("sSRowSet", oldValue, sSRowSet);
131         bind();
132     }
133
134     /**
135      * Returns the SSRowSet to which the component is bound.
136      *
137      * @return SSRowSet to which the component is bound
138      */

139     public SSRowSet getSSRowSet() {
140         return sSRowSet;
141     }
142
143     /**
144      * Sets the SSRowSet column name to which the component is bound.
145      *
146      * @param _columnName column name in the SSRowSet to which the component
147      * is bound
148      */

149     public void setColumnName(String JavaDoc _columnName) {
150         String JavaDoc oldValue = columnName;
151         columnName = _columnName;
152         firePropertyChange("columnName", oldValue, columnName);
153         bind();
154     }
155
156     /**
157      * Returns the SSRowSet column name to which the component is bound.
158      *
159      * @return column name to which the component is bound
160      */

161     public String JavaDoc getColumnName() {
162         return columnName;
163     }
164
165     /**
166      * Changes the image to the specified image.
167      *
168      * @param _img GIF or JPEG to store to sSRowSet & display
169      */

170 /*
171     public void setImage(ImageIcon _img){
172         img = _img;
173         if(img != null){
174             lblImage.setIcon(img);
175             lblImage.setText("");
176         }
177         else{
178             lblImage.setIcon(null);
179             lblImage.setText("No Picture");
180         }
181         updateUI();
182     }
183 */

184
185     /**
186      * Returns the current image.
187      */

188 /*
189     public void getImage(ImageIcon _img) {
190         return(img);
191     }
192 */

193
194     /**
195      * Sets the preferred size of the image component.
196      *
197      * @param _preferredSize - preferred size of the image component
198      */

199     public void setPreferredSize(Dimension JavaDoc _preferredSize) {
200         Dimension JavaDoc oldValue = preferredSize;
201         preferredSize = _preferredSize;
202         firePropertyChange("preferredSize", oldValue, preferredSize);
203
204         lblImage.setPreferredSize(new Dimension JavaDoc((int)_preferredSize.getWidth(), (int)_preferredSize.getHeight() - 20));
205         btnUpdateImage.setPreferredSize(new Dimension JavaDoc((int)_preferredSize.getWidth(), 20));
206         super.setPreferredSize(_preferredSize);
207     }
208
209     /**
210      * Returns the preferred size of the image component.
211      *
212      * @return returns preferred size of the image component
213      */

214     public Dimension JavaDoc getPreferredSize() {
215         return preferredSize;
216     }
217
218     /**
219      * Removes the current image. The image is not removed from the underlying sSRowSet.
220      */

221     public void clearImage(){
222         lblImage.setIcon(null);
223         lblImage.setText("No Picture");
224         Dimension JavaDoc dimension = getPreferredSize();
225         lblImage.setPreferredSize(new Dimension JavaDoc((int)dimension.getWidth(), (int)dimension.getHeight()-20));
226         updateUI();
227     }
228
229     /**
230      * Sets the SSRowSet and column name to which the component is to be bound.
231      *
232      * @param _sSRowSet datasource to be used.
233      * @param _columnName Name of the column to which this check box should be bound
234      */

235     public void bind(SSRowSet _sSRowSet, String JavaDoc _columnName) {
236         SSRowSet oldValue = sSRowSet;
237         sSRowSet = _sSRowSet;
238         firePropertyChange("sSRowSet", oldValue, sSRowSet);
239
240         String JavaDoc oldValue2 = columnName;
241         columnName = _columnName;
242         firePropertyChange("columnName", oldValue2, columnName);
243
244         bind();
245     }
246
247     /**
248      * Initialization code.
249      */

250     protected void init() {
251
252         // ADD UPDATE BUTTON LISTENER
253
btnUpdateImage.addActionListener(new ActionListener JavaDoc() {
254                 public void actionPerformed(ActionEvent JavaDoc ae) {
255                     try{
256                         if (sSRowSet != null) {
257                             FileInputStream JavaDoc inStream = null;
258                             File JavaDoc inFile = null;
259                             JFileChooser JavaDoc fileChooser = new JFileChooser JavaDoc();
260                             if(fileChooser.showOpenDialog(btnUpdateImage) == JFileChooser.APPROVE_OPTION){
261                                 inFile = fileChooser.getSelectedFile();
262                                 inStream = new FileInputStream JavaDoc(inFile);
263                                 int totalLength = (int)inFile.length();
264                                 byte[] bytes = new byte[totalLength];
265                                 int bytesRead = inStream.read(bytes);
266                                 while (bytesRead < totalLength){
267                                     int read = inStream.read(bytes, bytesRead, totalLength - bytesRead);
268                                     if(read == -1)
269                                         break;
270                                     else
271                                         bytesRead += read;
272                                 }
273                                 sSRowSet.updateBytes(columnName, bytes);
274                                 img = new ImageIcon JavaDoc(bytes);
275                                 lblImage.setPreferredSize(new Dimension JavaDoc(img.getIconWidth(), img.getIconHeight()));
276                                 lblImage.setIcon(img);
277                                 lblImage.setText("");
278                                 updateUI();
279                             } else {
280                                 return;
281                             }
282                         }
283                     }catch(SQLException JavaDoc se){
284                         se.printStackTrace();
285                     }catch(IOException JavaDoc ioe){
286                         ioe.printStackTrace();
287                     }
288                 }
289             });
290
291         // SET PREFERRED DIMENSIONS
292
setPreferredSize(preferredSize);
293
294         // ADD LABEL & BUTTON TO PANEL
295
addComponents();
296     }
297
298     /**
299      * Adds the label and button to the panel
300      */

301     protected void addComponents() {
302         setLayout(new GridBagLayout JavaDoc());
303         GridBagConstraints JavaDoc constraints = new GridBagConstraints JavaDoc();
304         constraints.gridx = 0;
305         constraints.gridy= 0;
306         JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(lblImage, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
307         scrollPane.setPreferredSize(new Dimension JavaDoc(200, 180));
308         btnUpdateImage.setPreferredSize(new Dimension JavaDoc(200,20));
309         add(scrollPane, constraints);
310         constraints.gridy = 1;
311         add(btnUpdateImage, constraints);
312     }
313
314     /**
315      * Method for handling binding of component to a SSRowSet column.
316      */

317     protected void bind() {
318
319         // CHECK FOR NULL COLUMN/ROWSET
320
if (columnName==null || columnName.trim().equals("") || sSRowSet==null) {
321                 return;
322             }
323
324         // REMOVE LISTENERS TO PREVENT DUPLICATION
325
removeListeners();
326
327         // UPDATE DISPLAY
328
updateDisplay();
329
330         // ADD BACK LISTENERS
331
addListeners();
332     }
333
334     /**
335      * Updates the value displayed in the component based on the SSRowSet column
336      * binding.
337      */

338     protected void updateDisplay() {
339
340         try {
341             byte[] imageData = sSRowSet.getRow() >0 ? sSRowSet.getBytes(columnName) : null;
342             if(imageData != null){
343                 img = new ImageIcon JavaDoc(imageData);
344                 lblImage.setPreferredSize(new Dimension JavaDoc(img.getIconWidth(), img.getIconHeight()));
345                 lblImage.setText("");
346             } else {
347                 img = null;
348                 lblImage.setText("No Picture");
349             }
350         } catch(SQLException JavaDoc se) {
351             se.printStackTrace();
352             img = null;
353         }
354
355         lblImage.setIcon(img);
356         updateUI();
357
358     }
359
360     /**
361      * Adds listeners for component and bound text field (where applicable).
362      */

363     private void addListeners() {
364         sSRowSet.addRowSetListener(sSRowSetListener);
365     }
366
367     /**
368      * Removes listeners for component and bound text field (where applicable).
369      */

370     private void removeListeners() {
371         sSRowSet.removeRowSetListener(sSRowSetListener);
372     }
373
374     /**
375      * Listener for the RowSet.
376      */

377     private class MyRowSetListener implements RowSetListener JavaDoc {
378         public void cursorMoved(RowSetEvent JavaDoc rse){
379             //System.out.println("Cursor Moved");
380
updateDisplay();
381         }
382
383         public void rowChanged(RowSetEvent JavaDoc rse){
384             //System.out.println("Row Changed");
385
updateDisplay();
386         }
387
388         public void rowSetChanged(RowSetEvent JavaDoc rse){
389             //System.out.println("RowSet Changed");
390
updateDisplay();
391         }
392
393     }
394 }
395
396 /*
397  *$Log: SSImage.java,v $
398  *Revision 1.12 2005/02/21 16:31:33 prasanth
399  *In bind checking for empty columnName before binding the component.
400  *
401  *Revision 1.11 2005/02/13 15:38:20 yoda2
402  *Removed redundant PropertyChangeListener and VetoableChangeListener class variables and methods from components with JComponent as an ancestor.
403  *
404  *Revision 1.10 2005/02/12 03:29:26 yoda2
405  *Added bound properties (for beans).
406  *
407  *Revision 1.9 2005/02/11 22:59:46 yoda2
408  *Imported PropertyVetoException and added some bound properties.
409  *
410  *Revision 1.8 2005/02/11 20:16:05 yoda2
411  *Added infrastructure to support property & vetoable change listeners (for beans).
412  *
413  *Revision 1.7 2005/02/10 20:13:02 yoda2
414  *Setter/getter cleanup & method reordering for consistency.
415  *
416  *Revision 1.6 2005/02/10 03:46:47 yoda2
417  *Replaced all setDisplay() methods & calls with updateDisplay() methods & calls to prevent any setter/getter confusion.
418  *
419  *Revision 1.5 2005/02/10 03:39:17 yoda2
420  *Added JavaDoc for class description.
421  *
422  *Revision 1.4 2005/02/07 22:54:52 yoda2
423  *JavaDoc cleanup.
424  *
425  *Revision 1.3 2005/02/04 22:48:54 yoda2
426  *API cleanup & updated Copyright info.
427  *
428  *Revision 1.2 2005/02/02 23:37:19 yoda2
429  *API cleanup.
430  *
431  *Revision 1.1 2005/01/18 20:59:13 prasanth
432  *Initial Commit.
433  *
434  */
Popular Tags