1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * ident        "%Z%%M% %I%     %E% SMI"
  24  *
  25  * Copyright 2002 by Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 package com.sun.dhcpmgr.ui;
  29 
  30 import javax.swing.JButton;
  31 import javax.swing.ImageIcon;
  32 import java.io.InputStream;
  33 import java.io.BufferedInputStream;
  34 import java.io.ByteArrayOutputStream;
  35 import java.io.IOException;
  36 
  37 /**
  38  * A button with an image loaded from a gif file.  To use this class, extend it
  39  * and in your constructor call setImage().
  40  */
  41 public abstract class ImageButton extends JButton {
  42     
  43     /**
  44      * Sets the button's icon to the image loaded from the file; falls back the 
  45      * specified text if for some reason the icon can't be loaded.  Base class
  46      * is used to find the gif image in the same directory as the class that's
  47      * using it, a convention we use.
  48      * @param baseClass the name of the class we're doing this on behalf of
  49      * @param file the name of the file the gif image is stored in
  50      * @param mnText is the mnemonic/text to be used for the button 
  51      */
  52     public void setImage(Class baseClass, String file, Mnemonic mnText) {
  53         try {
  54             InputStream resource = baseClass.getResourceAsStream(file);
  55             if (resource != null) {
  56                 BufferedInputStream in = new BufferedInputStream(resource);
  57                 ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
  58                 byte [] buffer = new byte[1024];
  59                 int n;
  60                 while ((n = in.read(buffer)) > 0) {
  61                     out.write(buffer, 0, n);
  62                 }
  63                 in.close();
  64                 out.flush();
  65                 buffer = out.toByteArray();
  66                 setIcon(new ImageIcon(buffer));
  67             }
  68         } catch (IOException ioe) {
  69         }
  70         // Added for accessibility
  71         setText(mnText.getString());
  72         setToolTipText(mnText.getString());
  73         setMnemonic(mnText.getMnemonic());
  74     }
  75 }