1 32 33 package com.nqadmin.swingSet; 34 35 import javax.swing.JLabel ; 36 import javax.swing.ImageIcon ; 37 import java.awt.Dimension ; 38 import javax.sql.RowSet ; 39 import javax.sql.RowSetEvent ; 40 import javax.sql.RowSetListener ; 41 import java.sql.SQLException ; 42 import javax.swing.JPanel ; 43 import javax.swing.JButton ; 44 import java.awt.GridBagLayout ; 45 import java.awt.GridBagConstraints ; 46 import java.awt.event.ActionListener ; 47 import java.awt.event.ActionEvent ; 48 import java.io.FileInputStream ; 49 import java.io.File ; 50 import javax.swing.JFileChooser ; 51 import java.io.IOException ; 52 import javax.swing.JScrollPane ; 53 import com.nqadmin.swingSet.datasources.SSRowSet; 54 55 public class SSImage extends JPanel { 65 66 69 protected ImageIcon img; 70 71 74 protected JLabel lblImage = new JLabel ("No Picture"); 75 76 79 protected JButton btnUpdateImage = new JButton ("Update"); 80 81 84 protected SSRowSet sSRowSet; 85 86 89 protected String columnName = ""; 90 91 94 protected Dimension preferredSize = new Dimension (200,200); 95 96 99 private final MyRowSetListener sSRowSetListener = new MyRowSetListener(); 100 101 105 public SSImage() { 106 init(); 107 } 108 109 115 public SSImage(SSRowSet _sSRowSet, String _columnName) { 116 sSRowSet = _sSRowSet; 117 columnName = _columnName; 118 init(); 119 bind(); 120 } 121 122 127 public void setSSRowSet(SSRowSet _sSRowSet) { 128 SSRowSet oldValue = sSRowSet; 129 sSRowSet = _sSRowSet; 130 firePropertyChange("sSRowSet", oldValue, sSRowSet); 131 bind(); 132 } 133 134 139 public SSRowSet getSSRowSet() { 140 return sSRowSet; 141 } 142 143 149 public void setColumnName(String _columnName) { 150 String oldValue = columnName; 151 columnName = _columnName; 152 firePropertyChange("columnName", oldValue, columnName); 153 bind(); 154 } 155 156 161 public String getColumnName() { 162 return columnName; 163 } 164 165 170 184 185 188 193 194 199 public void setPreferredSize(Dimension _preferredSize) { 200 Dimension oldValue = preferredSize; 201 preferredSize = _preferredSize; 202 firePropertyChange("preferredSize", oldValue, preferredSize); 203 204 lblImage.setPreferredSize(new Dimension ((int)_preferredSize.getWidth(), (int)_preferredSize.getHeight() - 20)); 205 btnUpdateImage.setPreferredSize(new Dimension ((int)_preferredSize.getWidth(), 20)); 206 super.setPreferredSize(_preferredSize); 207 } 208 209 214 public Dimension getPreferredSize() { 215 return preferredSize; 216 } 217 218 221 public void clearImage(){ 222 lblImage.setIcon(null); 223 lblImage.setText("No Picture"); 224 Dimension dimension = getPreferredSize(); 225 lblImage.setPreferredSize(new Dimension ((int)dimension.getWidth(), (int)dimension.getHeight()-20)); 226 updateUI(); 227 } 228 229 235 public void bind(SSRowSet _sSRowSet, String _columnName) { 236 SSRowSet oldValue = sSRowSet; 237 sSRowSet = _sSRowSet; 238 firePropertyChange("sSRowSet", oldValue, sSRowSet); 239 240 String oldValue2 = columnName; 241 columnName = _columnName; 242 firePropertyChange("columnName", oldValue2, columnName); 243 244 bind(); 245 } 246 247 250 protected void init() { 251 252 btnUpdateImage.addActionListener(new ActionListener () { 254 public void actionPerformed(ActionEvent ae) { 255 try{ 256 if (sSRowSet != null) { 257 FileInputStream inStream = null; 258 File inFile = null; 259 JFileChooser fileChooser = new JFileChooser (); 260 if(fileChooser.showOpenDialog(btnUpdateImage) == JFileChooser.APPROVE_OPTION){ 261 inFile = fileChooser.getSelectedFile(); 262 inStream = new FileInputStream (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 (bytes); 275 lblImage.setPreferredSize(new Dimension (img.getIconWidth(), img.getIconHeight())); 276 lblImage.setIcon(img); 277 lblImage.setText(""); 278 updateUI(); 279 } else { 280 return; 281 } 282 } 283 }catch(SQLException se){ 284 se.printStackTrace(); 285 }catch(IOException ioe){ 286 ioe.printStackTrace(); 287 } 288 } 289 }); 290 291 setPreferredSize(preferredSize); 293 294 addComponents(); 296 } 297 298 301 protected void addComponents() { 302 setLayout(new GridBagLayout ()); 303 GridBagConstraints constraints = new GridBagConstraints (); 304 constraints.gridx = 0; 305 constraints.gridy= 0; 306 JScrollPane scrollPane = new JScrollPane (lblImage, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 307 scrollPane.setPreferredSize(new Dimension (200, 180)); 308 btnUpdateImage.setPreferredSize(new Dimension (200,20)); 309 add(scrollPane, constraints); 310 constraints.gridy = 1; 311 add(btnUpdateImage, constraints); 312 } 313 314 317 protected void bind() { 318 319 if (columnName==null || columnName.trim().equals("") || sSRowSet==null) { 321 return; 322 } 323 324 removeListeners(); 326 327 updateDisplay(); 329 330 addListeners(); 332 } 333 334 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 (imageData); 344 lblImage.setPreferredSize(new Dimension (img.getIconWidth(), img.getIconHeight())); 345 lblImage.setText(""); 346 } else { 347 img = null; 348 lblImage.setText("No Picture"); 349 } 350 } catch(SQLException se) { 351 se.printStackTrace(); 352 img = null; 353 } 354 355 lblImage.setIcon(img); 356 updateUI(); 357 358 } 359 360 363 private void addListeners() { 364 sSRowSet.addRowSetListener(sSRowSetListener); 365 } 366 367 370 private void removeListeners() { 371 sSRowSet.removeRowSetListener(sSRowSetListener); 372 } 373 374 377 private class MyRowSetListener implements RowSetListener { 378 public void cursorMoved(RowSetEvent rse){ 379 updateDisplay(); 381 } 382 383 public void rowChanged(RowSetEvent rse){ 384 updateDisplay(); 386 } 387 388 public void rowSetChanged(RowSetEvent rse){ 389 updateDisplay(); 391 } 392 393 } 394 } 395 396 | Popular Tags |