KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > core > panel > FieldGUI


1 /*===========================================================================
2
3 ObjectWeb Naming Context Framework
4 Copyright (C) 2002 USTL - LIFL - GOAL
5 Contact: architecture@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jérôme Moroy.
23 Contributor(s): ______________________________________.
24
25 ===========================================================================*/

26
27 package org.objectweb.util.browser.core.panel;
28
29 /** To use the Field class */
30 import java.lang.reflect.Field JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.JLabel JavaDoc;
33 import javax.swing.BoxLayout JavaDoc;
34 import java.awt.Dimension JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.awt.event.ActionEvent JavaDoc;
37 import java.awt.Color JavaDoc;
38 import javax.swing.Box JavaDoc;
39
40 /**
41  * This class represents a Field
42  *
43  *
44  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
45  * @version 0.1
46  *
47  */

48 public class FieldGUI extends Box JavaDoc {
49
50     protected Object JavaDoc object_;
51     protected Field JavaDoc field_;
52
53     public FieldGUI(Field JavaDoc field, Object JavaDoc object) {
54         super(BoxLayout.X_AXIS);
55         field_ = field;
56         object_ = object;
57         setBackground(Color.white);
58         add(Box.createHorizontalGlue());
59         Class JavaDoc type = field_.getType();
60         int lastIndex = type.getName().lastIndexOf('.');
61         JLabel JavaDoc label = null;
62         if (lastIndex != -1)
63             label = new JLabel JavaDoc(field.getName() + " : " + type.getName().substring(lastIndex + 1));
64         else
65             label = new JLabel JavaDoc(field.getName() + " : " + type.getName());
66         label.setPreferredSize(new Dimension JavaDoc(350, 20));
67         add(label);
68         add(Box.createHorizontalStrut(10));
69         JButton JavaDoc value = new JButton JavaDoc("Value");
70         value.setPreferredSize(new Dimension JavaDoc(80, 20));
71         value.addActionListener(new ValueAction());
72         add(value);
73         add(Box.createHorizontalGlue());
74     }
75
76     protected class ValueAction implements ActionListener JavaDoc {
77         public void actionPerformed(ActionEvent JavaDoc e) {
78             try {
79                 System.out.println(field_.get(object_).toString());
80             } catch (java.lang.IllegalAccessException JavaDoc ex) {
81                 ex.printStackTrace();
82             } catch (java.lang.IllegalArgumentException JavaDoc ex) {
83                 ex.printStackTrace();
84             }
85         }
86     }
87 }
88
Popular Tags