KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > ArrayEditor


1 /*
2   Copyright (C) 2001 Renaud Pawlak, Laurent Martelli
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.aspects.gui.swing;
20
21
22
23 import java.awt.Dimension JavaDoc;
24 import java.lang.reflect.Array JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27 import java.util.Vector JavaDoc;
28 import javax.swing.JTextPane JavaDoc;
29 import org.objectweb.jac.aspects.gui.Length;
30
31 /**
32  * A Swing editor component for array values.
33  */

34
35 public class ArrayEditor extends JTextPane JavaDoc
36 // implements ValueEditor
37
{
38    
39     /**
40     * Constructs a new array editor. */

41
42     Class JavaDoc type;
43     public ArrayEditor(Class JavaDoc type) {
44         setPreferredSize(new Dimension JavaDoc( 200, 200 ));
45         this.type = type;
46     }
47
48     /**
49     * Gets the value of the edited array.
50     *
51     * @return an array object */

52  
53     public Object JavaDoc getValue() {
54         String JavaDoc s = getText();
55         Collection JavaDoc c = null;
56         if ( type.isArray() ) {
57             c = new Vector JavaDoc();
58         } else {
59             try {
60                 c = (Collection JavaDoc) type.newInstance();
61             } catch ( Exception JavaDoc e ) { e.printStackTrace(); }
62         }
63         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc ( s, "\n" );
64         while ( st.hasMoreTokens() ) {
65             c.add( st.nextToken() );
66         }
67         if ( type.isArray() ) {
68             Object JavaDoc[] a = c.toArray();
69             Object JavaDoc array = Array.newInstance( type.getComponentType(), c.size() );
70             for ( int j = 0; j < c.size(); j++ ) {
71                 Array.set( array, j, a[j] );
72             }
73             return array;
74         } else {
75             return c;
76         }
77     }
78
79     /**
80     * Sets the value of the edited array
81     **/

82
83     public void setValue(Object JavaDoc value) {}
84
85     public void setSize(Length width, Length height) {
86         SwingUtils.setSize(this, width, height);
87     }
88
89     public void onClose() {}
90 }
91
Popular Tags