001    /*
002      Copyright (C) 2002-2003 Renaud Pawlak <renaud@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017    */
018    
019    package org.objectweb.jac.ide.diagrams;
020    
021    import java.util.Iterator;
022    import java.util.StringTokenizer;
023    import java.util.Vector;
024    import org.objectweb.jac.aspects.gui.DisplayContext;
025    import org.objectweb.jac.aspects.gui.EventHandler;
026    import org.objectweb.jac.aspects.gui.ObjectUpdate;
027    import org.objectweb.jac.aspects.gui.Utils;
028    import org.objectweb.jac.core.rtti.ClassRepository;
029    import org.objectweb.jac.core.rtti.CollectionItem;
030    import org.objectweb.jac.ide.Class;
031    import org.objectweb.jac.ide.Method;
032    import org.objectweb.jac.ide.ModelElement;
033    import org.objectweb.jac.util.Log;
034    
035    public class MethodFigure extends MemberFigure 
036        implements ModelElementFigure, ObjectUpdate, Selectable
037    {
038        Method method;
039    
040        public MethodFigure(Method method, ClassFigure parentFigure) {
041            super(parentFigure);
042            this.method  = method;
043            setText();
044            Utils.registerObject(method,this);
045        }
046    
047        public void close() {
048            Utils.unregisterObject(method,this);
049        }
050    
051        protected void setText() {
052            super.setText(method.getName()+"("+method.getParameterNames()+")");
053        }
054       
055        /**
056         * Get the value of substance.
057         * @return value of substance.
058         */
059        public ModelElement getSubstance() {
060            return method;
061        }
062       
063        public String getPrototype() {
064            String text = getText();
065            int sep = text.indexOf(':');
066            if (sep == -1) {
067                return "void "+text;
068            } else {
069                return text.substring(sep+1)+" "+text.substring(0,sep);
070            }      
071        }
072       
073        public String getName() {
074            String name = super.getName();
075            int sep = name.indexOf('(');
076            if (sep == -1) {
077                return name;
078            } else {
079                return name.substring(0,sep).trim();
080            }      
081        }
082    
083        public Vector getArgs() {
084            Vector result = new Vector();
085            String text = getText();
086            String args = text.substring(text.indexOf('(')+1);
087            args = args.substring(0,args.indexOf(')'));
088            StringTokenizer st = new StringTokenizer(args,",");
089            while (st.hasMoreTokens()) {
090                String s = st.nextToken();
091                s=s.trim();
092                String type = s.substring(0,s.indexOf(' '));
093                String name = s.substring(s.indexOf(' ')+1);
094                Log.trace("diagram","parameter: "+type+" "+name);
095                result.add(new String[]{name,type});
096            }
097            return result;
098        }
099    
100        boolean updating = false;
101    
102        public synchronized void setText(String 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 it = new Vector(method.getParameters()).iterator();
115                while(it.hasNext()) {
116                    method.getParameters().remove(it.next());
117                }
118                it = getArgs().iterator();
119                while(it.hasNext()) {
120                    String[] param = (String[])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 object, Object 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.class)
142                .getCollection("methods");
143            EventHandler.get().onSelection(
144                context,coll,getSubstance(),null,null);
145        }
146    }