001    /*
002    
003      Copyright (C) 2001 Renaud Pawlak, Laurent Martelli
004      
005      This program is free software; you can redistribute it and/or modify
006      it under the terms of the GNU Lesser General Public License as
007      published by the Free Software Foundation; either version 2 of the
008      License, or (at your option) any later version.
009    
010      This program is distributed in the hope that it will be useful, but
011      WITHOUT ANY WARRANTY; without even the implied warranty of
012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013      Lesser General Public License for more details.
014    
015      You should have received a copy of the GNU Lesser General Public
016      License along with this program; if not, write to the Free Software
017      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
018      USA */
019    
020    package org.objectweb.jac.aspects.gui.swing;
021    
022    import java.lang.reflect.Array;
023    
024    import java.util.Collection;
025    import java.util.StringTokenizer;
026    import java.util.Vector;
027    
028    import java.awt.Dimension;
029    import javax.swing.JTextPane;
030    
031    /**
032     * A Swing editor component for array values.
033     */
034    
035    public class ArrayEditor extends JTextPane 
036      //   implements ValueEditor 
037    {
038       
039       /**
040        * Constructs a new array editor. */
041    
042       Class type;
043       public ArrayEditor(Class type) {
044          setPreferredSize(new Dimension( 200, 200 ));
045          this.type = type;
046       }
047    
048       /**
049        * Gets the value of the edited array.
050        *
051        * @return an array object */
052     
053       public Object getValue() {
054          String s = getText();
055          Collection c = null;
056          if ( type.isArray() ) {
057             c = new Vector();
058          } else {
059             try {
060                c = (Collection) type.newInstance();
061             } catch ( Exception e ) { e.printStackTrace(); }
062          }
063          StringTokenizer st = new StringTokenizer ( s, "\n" );
064          while ( st.hasMoreTokens() ) {
065             c.add( st.nextToken() );
066          }
067          if ( type.isArray() ) {
068             Object[] a = c.toArray();
069             Object array = Array.newInstance( type.getComponentType(), c.size() );
070             for ( int j = 0; j < c.size(); j++ ) {
071                Array.set( array, j, a[j] );
072             }
073             return array;
074          } else {
075             return c;
076          }
077       }
078    
079       /**
080        * Sets the value of the edited array
081        **/
082    
083       public void setValue(Object value) {}
084    
085       public void setWidth(int width) {
086          Dimension dim = getSize();
087          Dimension newDim = new Dimension(width,(int)dim.getHeight());
088          setSize(newDim);
089       }
090    
091       public void setHeight(int height) {
092          Dimension dim = getSize();
093          Dimension newDim = new Dimension((int)dim.getWidth(),height);
094          setSize(newDim);
095       }
096    
097       public void onClose() {}
098    }