KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > diagrams > MethodFigure


1 /*
2   Copyright (C) 2002-2003 Renaud Pawlak <renaud@aopsys.com>
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,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU 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 */

18
19 package org.objectweb.jac.ide.diagrams;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23 import java.util.Vector JavaDoc;
24 import org.objectweb.jac.aspects.gui.DisplayContext;
25 import org.objectweb.jac.aspects.gui.EventHandler;
26 import org.objectweb.jac.aspects.gui.ObjectUpdate;
27 import org.objectweb.jac.aspects.gui.Utils;
28 import org.objectweb.jac.core.rtti.ClassRepository;
29 import org.objectweb.jac.core.rtti.CollectionItem;
30 import org.objectweb.jac.ide.Class;
31 import org.objectweb.jac.ide.Method;
32 import org.objectweb.jac.ide.ModelElement;
33 import org.objectweb.jac.util.Log;
34
35 public class MethodFigure extends MemberFigure
36     implements ModelElementFigure, ObjectUpdate, Selectable
37 {
38     Method method;
39
40     public MethodFigure(Method method, ClassFigure parentFigure) {
41         super(parentFigure);
42         this.method = method;
43         setText();
44         Utils.registerObject(method,this);
45     }
46
47     public void close() {
48         Utils.unregisterObject(method,this);
49     }
50
51     protected void setText() {
52         super.setText(method.getName()+"("+method.getParameterNames()+")");
53     }
54    
55     /**
56      * Get the value of substance.
57      * @return value of substance.
58      */

59     public ModelElement getSubstance() {
60         return method;
61     }
62    
63     public String JavaDoc getPrototype() {
64         String JavaDoc text = getText();
65         int sep = text.indexOf(':');
66         if (sep == -1) {
67             return "void "+text;
68         } else {
69             return text.substring(sep+1)+" "+text.substring(0,sep);
70         }
71     }
72    
73     public String JavaDoc getName() {
74         String JavaDoc name = super.getName();
75         int sep = name.indexOf('(');
76         if (sep == -1) {
77             return name;
78         } else {
79             return name.substring(0,sep).trim();
80         }
81     }
82
83     public Vector JavaDoc getArgs() {
84         Vector JavaDoc result = new Vector JavaDoc();
85         String JavaDoc text = getText();
86         String JavaDoc args = text.substring(text.indexOf('(')+1);
87         args = args.substring(0,args.indexOf(')'));
88         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(args,",");
89         while (st.hasMoreTokens()) {
90             String JavaDoc s = st.nextToken();
91             s=s.trim();
92             String JavaDoc type = s.substring(0,s.indexOf(' '));
93             String JavaDoc name = s.substring(s.indexOf(' ')+1);
94             Log.trace("diagram","parameter: "+type+" "+name);
95             result.add(new String JavaDoc[]{name,type});
96         }
97         return result;
98     }
99
100     boolean updating = false;
101
102     public synchronized void setText(String JavaDoc s) {
103         super.setText(s);
104         Log.trace("diagram","settext("+s+")");
105         Log.trace("diagram","gettext()="+getText());
106         if (method != null && !DiagramView.init) {
107             updating=true;
108             Log.trace("diagram","gettext()="+getText());
109             Log.trace("diagram","name: "+getName());
110             method.setName(getName());
111             Log.trace("diagram","gettext()="+getText());
112             Log.trace("diagram","type: "+getType());
113             method.setType(org.objectweb.jac.ide.Projects.types.resolveType(getType()));
114             Iterator JavaDoc it = new Vector JavaDoc(method.getParameters()).iterator();
115             while(it.hasNext()) {
116                 method.getParameters().remove(it.next());
117             }
118             it = getArgs().iterator();
119             while(it.hasNext()) {
120                 String JavaDoc[] param = (String JavaDoc[])it.next();
121                 org.objectweb.jac.ide.Parameter parameter = new org.objectweb.jac.ide.Parameter();
122                 parameter.setName(param[0]);
123                 parameter.setType(org.objectweb.jac.ide.Projects.types.resolveType(param[1]));
124                 method.addParameter(parameter);
125             }
126             updating=false;
127         }
128     }
129
130     // ObjectUpdate interface
131
public void objectUpdated(Object JavaDoc object, Object JavaDoc extra) {
132         Log.trace("diagram","objectUpdated()"+object);
133         if (!updating) {
134             setText();
135         }
136     }
137
138     // Selectable interface
139

140     public void onSelect(DisplayContext context) {
141         CollectionItem coll = ClassRepository.get().getClass(Class JavaDoc.class)
142             .getCollection("methods");
143         EventHandler.get().onSelection(
144             context,coll,getSubstance(),null,null);
145     }
146 }
147
Popular Tags