001    /*
002      Copyright (C) 2002 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      USA */
018    
019    package org.objectweb.jac.ide;
020    
021    import org.objectweb.jac.core.rtti.ClassItem;
022    import org.objectweb.jac.core.rtti.FieldItem;
023    import org.objectweb.jac.core.rtti.MethodItem;
024    
025    import java.util.List;
026    import java.util.Vector;
027    import java.util.Collection;
028    import java.util.Iterator;
029    
030    /**
031     * this is a class that represent the call of an aspect Method by a ModelElement
032     * @author gregoire Waymel
033     */
034    public class ConfigItem {
035        /** the Aspect */
036        private AspectConfiguration aspectConfiguration;
037    
038        /** Method of the aspect */
039        private MethodItem method;
040    
041        /** Element (1st param of method) which call the method */
042        private ModelElement modelElement;
043    
044        /** the Params of the method */
045        private List param = new Vector();
046    
047        /**
048         * default constructor
049         */
050        public ConfigItem() {
051        }
052    
053        public AspectConfiguration getAspectConfiguration() {
054            return aspectConfiguration;
055        }
056    
057        public ModelElement getModelElement() {
058            return modelElement;
059        }
060    
061        public List getParam() {
062            return param;
063        }
064    
065        public MethodItem getMethod() {
066            return method;
067        }
068    
069        /**
070         * @param aspectConfiguration the new Aspect for this ConfigItem
071         */
072        public void setAspectConfiguration(AspectConfiguration aspectConfiguration) {
073            this.aspectConfiguration = aspectConfiguration;
074        }
075        public void addParam(String param) {
076            this.param.add(param);
077        }
078    
079        public void removeParam(String param) {
080            this.param.remove(param);
081        }
082    
083        public void setModelElement(ModelElement modelElement) {
084            this.modelElement = modelElement;
085        }
086    
087        public void setMethod(MethodItem newMethod) {
088            this.method = newMethod;
089        }
090    
091        public String toString() {
092            return "ConfigItem "+modelElement+" "+method+" "+aspectConfiguration;
093        }
094    
095        /**
096         * Gets available aspect configurations 
097         */
098        public static Collection getAvailableAspects(ConfigItem item) {
099            ModelElement element = item.getModelElement();
100            Vector configs = new Vector();
101            if (element instanceof Class) {
102                Iterator i = ((Class)element).getProject().getApplications().iterator();
103                while (i.hasNext()) {
104                    Application app = (Application)i.next();
105                    configs.addAll(app.getAspectConfigurations());
106                }
107            }
108            return configs;
109        }
110    
111        /**
112         * search the aspect method that can be call by the ModelElement.
113         * @param item the ConfigItem that should have a valid ModelElement and a valid AspectConfiguration.
114         * @return the method name that can be call by the item ModelElement.
115         */
116        public final static Collection getValidMethods(ConfigItem item) throws Exception {
117            Vector list = new Vector();
118            AspectConfiguration aspect = item.getAspectConfiguration();
119            //If the model element or the AspectConfiguration is missing, we cannot determine the valid Method.
120            ModelElement element = item.getModelElement();
121            if ((aspect==null)||(element==null)) {
122                return list;
123            }
124    
125            //Iterate all the config method of the aspect
126            Iterator iteMethods = aspect.getConfigurationMethods().iterator();
127            while(iteMethods.hasNext()) {
128                MethodItem method = (MethodItem)iteMethods.next();
129                //Searching the first param
130                java.lang.Class param;
131                //If the method has no param it's a Project method.
132                if (method.getParameterCount()==0) {
133                    param = Project.class;
134                }else{
135                    param = method.getParameterTypes()[0];
136                }
137    
138                //test whether the method is a class method.
139                if ((param==ClassItem.class)&&(element.getClass()==Class.class)) {
140                    list.add(method);
141                    continue;
142                }
143                //test whether the method is a Field method.
144                if ((param==FieldItem.class)&&(element.getClass()==Field.class)) {
145                    list.add(method);
146                    continue;
147                }
148                //test whether the method is a Method method.
149                if ((param==MethodItem.class)&&(element.getClass()==Method.class)) {
150                    list.add(method);
151                    continue;
152                }
153                //if it's not this then add the method in the project
154                if (element.getClass()==Project.class) {
155                    list.add(method);
156                }
157                //Else Error
158                //We just continue...
159            }
160            return list;
161        }
162    }