Source for gnu.javax.activation.viewers.TextEditor

   1: /* TextEditor.java -- Simple text editor component.
   2:    Copyright (C) 2004 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.javax.activation.viewers;
  39: 
  40: import java.awt.Dimension;
  41: import java.awt.TextArea;
  42: import java.awt.event.ActionEvent;
  43: import java.awt.event.ActionListener;
  44: import java.io.ByteArrayOutputStream;
  45: import java.io.InputStream;
  46: import java.io.IOException;
  47: import java.io.OutputStream;
  48: import javax.activation.CommandObject;
  49: import javax.activation.DataHandler;
  50: 
  51: /**
  52:  * Simple text editor component.
  53:  *
  54:  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  55:  * @version 1.0.2
  56:  */
  57: public class TextEditor extends TextArea
  58:     implements CommandObject, ActionListener
  59: {
  60: 
  61:     private transient DataHandler dh;
  62: 
  63:     public TextEditor()
  64:     {
  65:         super("", 24, 80, 1);
  66:     }
  67: 
  68:     public Dimension getPreferredSize()
  69:     {
  70:         return getMinimumSize(24, 80);
  71:     }
  72: 
  73:     public void setCommandContext(String verb, DataHandler dh)
  74:         throws IOException
  75:     {
  76:         this.dh = dh;
  77:         InputStream in = dh.getInputStream();
  78:         ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  79:         byte[] buf = new byte[4096];
  80:         for (int len = in.read(buf); len != -1; len = in.read(buf))
  81:             bytes.write(buf, 0, len);
  82:         in.close();
  83:         setText(bytes.toString());
  84:     }
  85: 
  86:     public void actionPerformed(ActionEvent event)
  87:     {
  88:         if ("save".equals(event.getActionCommand()) && dh != null)
  89:         {
  90:             OutputStream out = null;
  91:             try
  92:             {
  93:                 out = dh.getOutputStream();
  94:                 if (out != null)
  95:                     out.write(getText().getBytes());
  96:             }
  97:             catch (IOException e)
  98:             {
  99:                 e.printStackTrace(System.err);
 100:             }
 101:             finally
 102:             {
 103:                 if (out != null)
 104:                 {
 105:                     try
 106:                     {
 107: 
 108:                         out.close();
 109:                     }
 110:                     catch (IOException e)
 111:                     {
 112:                         e.printStackTrace(System.err);
 113:                     }
 114:                 }
 115:             }
 116:         }
 117:     }
 118: 
 119: }