KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > explorer > CORBA > ChangeAttributeValue


1 /*===========================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@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.openccm.explorer.CORBA;
28
29 import org.objectweb.util.explorer.api.MenuItem;
30 import org.objectweb.util.explorer.api.MenuItemTreeView;
31 import org.objectweb.util.explorer.api.TreeView;
32 import org.objectweb.util.explorer.swing.gui.api.DialogAction;
33 import org.objectweb.util.explorer.swing.gui.api.DialogBox;
34 import org.objectweb.util.explorer.swing.gui.lib.DefaultDialogBox;
35 import org.objectweb.util.explorer.swing.gui.lib.LabelBox;
36
37 /**
38  * This action allows us to change the value of an attibute.
39  *
40  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
41  *
42  * @version 0.1
43  */

44 public class ChangeAttributeValue
45   implements MenuItem, DialogAction
46 {
47
48     //==================================================================
49
//
50
// Internal states.
51
//
52
//==================================================================
53

54     /** The associated Object. */
55     protected org.omg.CORBA.Object JavaDoc object_ = null;
56
57     /** The name of the attribute. */
58     protected String JavaDoc name_ = null;
59
60     /** The LabelBox to field the new value. */
61     protected LabelBox attributeLabel_ = null;
62
63     //==================================================================
64
//
65
// No constructor.
66
//
67
//==================================================================
68

69     //==================================================================
70
//
71
// Internal method.
72
//
73
//==================================================================
74

75     /**
76      * Must be replaced by a class with the given interface:
77      * public interface TypeWrapper{
78      * public String toString(Object)
79      * public Object fromString(String)
80      * }
81      */

82     public Object JavaDoc fromString(String JavaDoc value, Class JavaDoc type){
83         String JavaDoc typeName = type.getName();
84         if(type.equals(Integer.TYPE))
85             return new Integer JavaDoc(value);
86         else if(type.equals(Character.TYPE))
87             return new Character JavaDoc(value.charAt(0));
88         else if(type.equals(Double.TYPE))
89             return new Double JavaDoc(value);
90         else if(type.equals(Boolean.TYPE))
91             return new Boolean JavaDoc(value);
92         else if(type.equals(Short.TYPE))
93             return new Short JavaDoc(value);
94         else if(type.equals(Long.TYPE))
95             return new Long JavaDoc(value);
96         else if(type.equals(Float.TYPE))
97             return new Float JavaDoc(value);
98         return value;
99     }
100  
101     //==================================================================
102
//
103
// Public methods for MenuItem interface.
104
//
105
//==================================================================
106

107     /**
108      * Enabled if a method which allows us to fix a value exists.
109      */

110     public int getStatus(TreeView treeView) {
111         ViewAttributeWrapper viewAttr = (ViewAttributeWrapper)treeView.getSelectedObject();
112         org.omg.CORBA.Object JavaDoc o = viewAttr.getObject();
113         String JavaDoc attrName = viewAttr.getName();
114         try{
115             Class JavaDoc c = o.getClass();
116             c.getMethod(attrName,new Class JavaDoc[]{c.getMethod(attrName,null).getReturnType()});
117             return MenuItem.ENABLED_STATUS;
118         } catch(NoSuchMethodException JavaDoc e) {
119             return MenuItem.DISABLED_STATUS;
120         }
121     }
122
123     /**
124      * Provides a dialog box in order to change the value of the attribute.
125      */

126     public void actionPerformed(MenuItemTreeView treeView) throws Exception JavaDoc {
127         ViewAttributeWrapper viewAttr = (ViewAttributeWrapper)treeView.getSelectedObject();
128         object_ = viewAttr.getObject();
129         name_ = viewAttr.getName();
130         DialogBox dialog = new DefaultDialogBox("Change the value of this attribute");
131         Class JavaDoc c = object_.getClass();
132         attributeLabel_ = new LabelBox(name_,c.getMethod(name_,null).invoke(object_,null).toString());
133         dialog.addElementBox(attributeLabel_);
134         dialog.setValidateAction(this);
135         dialog.show();
136     }
137
138     //==================================================================
139
//
140
// Public methods for DialogAction interface.
141
//
142
//==================================================================
143

144     /**
145      * Fixes the value of the attribute.
146      */

147     public void executeAction() throws Exception JavaDoc {
148         String JavaDoc value = attributeLabel_.getLabel();
149         Class JavaDoc c = object_.getClass();
150         Class JavaDoc type = c.getMethod(name_,null).getReturnType();
151         java.lang.reflect.Method JavaDoc method = c.getMethod(name_,new Class JavaDoc[]{type});
152         method.invoke(object_,new Object JavaDoc[]{fromString(value,type)});
153     }
154
155 }
156
Popular Tags