KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > bridge > CallableImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.bridge;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23
24 import java.util.*;
25 import org.netbeans.jmi.javamodel.ClassDefinition;
26 import org.netbeans.jmi.javamodel.JavaClass;
27
28 import org.openide.src.*;
29 import javax.jmi.reflect.RefObject;
30 import javax.jmi.reflect.RefBaseObject;
31 import javax.jmi.reflect.InvalidObjectException;
32 import org.netbeans.api.mdr.events.*;
33
34 import org.netbeans.jmi.javamodel.CallableFeature;
35 import org.netbeans.jmi.javamodel.Type;
36 import org.netbeans.jmi.javamodel.TypeClass;
37 import org.netbeans.jmi.javamodel.Parameter;
38 import org.netbeans.jmi.javamodel.ParameterClass;
39 import org.netbeans.jmi.javamodel.IsOfType;
40 import org.netbeans.jmi.javamodel.MultipartId;
41 import org.netbeans.jmi.javamodel.Throws;
42 import org.netbeans.jmi.javamodel.TypeReference;
43 import org.netbeans.modules.javacore.jmiimpl.javamodel.TypeClassImpl;
44
45 /**
46  *
47  * @author sdedic
48  * @version
49  */

50 abstract class CallableImpl extends MemberElementImpl implements ConstructorElement.Impl {
51     private static final boolean DEBUG = false;
52     
53     /** Special tag value for methods without parameters (performance enhancement)
54      */

55     static final MethodParameter[] NO_PARAMETERS = new MethodParameter[0];
56     
57     /** Flyweight support for PROP_EXCEPTION property.
58      */

59     private static IdentifierArrayProperty exceptionSupport;
60     
61     /** Flyweight support for PROP_PARAMETERS property.
62      */

63     private static MethodParamSupport paramSupport;
64     
65     private transient long bodyHash = -1;
66     
67     private static final long serialVersionUID = -357084137587082234L;
68     
69     // Construction
70
///////////////////////////////////////////////////////////////////////////////////
71

72     CallableImpl(DefaultLangModel model, CallableFeature javaElement) {
73         super(model, javaElement);
74         init ();
75     }
76     
77     private void init () {
78         if (paramSupport == null) {
79             paramSupport = new MethodParamSupport();
80             exceptionSupport = new ExceptionSupport();
81         }
82
83         javadoc = new JavaDocImpl.Method(null, this);
84     }
85     
86     public org.openide.src.JavaDoc.Method getJavaDoc() {
87         updateJavadoc();
88         return (org.openide.src.JavaDoc.Method)javadoc;
89     }
90     
91     /*
92     protected void copyBody(String bodyContent) {
93         cachedBody = bodyContent;
94     }
95     
96     void updateBody(String content) {
97         if (content == null) {
98             bodyHash = -1;
99             return;
100         }
101         long newHash = computeHash(content);
102         if (newHash == bodyHash)
103             return;
104         if (DEBUG) {
105             System.err.println("[" + this + "] updating body to \"" + content + "\""); // NOI18N
106             System.err.println("[" + this + "] hash is " + newHash + " old hash = " + bodyHash); // NOI18N
107         }
108         if (bodyHash != -1) {
109             addPropertyChange(new PropertyChangeEvent(getElement(), PROP_BODY, null, null));
110         }
111         bodyHash = newHash;
112     }
113      */

114     
115     // Getters
116
///////////////////////////////////////////////////////////////////////////////
117

118     /**
119      * Returns parameters for the method.
120      * @return array of parameter descriptions.
121      */

122     public final MethodParameter[] getParameters() {
123         MethodParameter[] parameters;
124         repository.beginTrans (false);
125         try {
126             if (javaElement.isValid()) {
127                 setClassPath();
128                 Collection list = ((CallableFeature) javaElement).getParameters ();
129                 Iterator iter;
130                 if (list.size () == 0)
131                     parameters = NO_PARAMETERS;
132                 else {
133                     iter = list.iterator ();
134                     parameters = new MethodParameter [list.size ()];
135                     for (int x = 0; iter.hasNext (); x++) {
136                         Parameter param = (Parameter) iter.next ();
137                         parameters [x] = createParameter(param);
138                     } // for
139
} // else
140
} else {
141                 return NO_PARAMETERS;
142             }
143         } finally {
144             repository.endTrans (false);
145         }
146         return parameters;
147     }
148
149     /** Retrieves checked exceptions declared to be thrown from the callable.
150      * @return Identifiers representing exception types thrown from the method.
151      */

152     public final Identifier[] getExceptions() {
153         Identifier[] exceptions;
154         repository.beginTrans (false);
155         try {
156             if (javaElement.isValid()) {
157                 setClassPath();
158                 Collection list = ((CallableFeature) javaElement).getExceptionNames ();
159                 if (list.size () == 0)
160                     exceptions = IdentifierArrayProperty.EMPTY;
161                 else {
162                     Iterator iter = list.iterator ();
163                     exceptions = new Identifier [list.size ()];
164                     for (int x = 0; iter.hasNext (); x++) {
165                         MultipartId ex = (MultipartId) iter.next ();
166                         exceptions [x] = createClassIdentifier (ex);
167                     } // for
168
} // else
169
} else {
170                 return new Identifier [0];
171             }
172         } finally {
173             repository.endTrans (false);
174         }
175         return exceptions;
176     }
177     
178     /** Retrieves body of the method.
179      */

180     public final String JavaDoc getBody() {
181         repository.beginTrans (false);
182         try {
183             if (javaElement.isValid()) {
184                 setClassPath();
185                 return ((CallableFeature)javaElement).getBodyText();
186             } else {
187                 return null;
188             }
189         } finally {
190             repository.endTrans (false);
191         }
192     }
193     
194     /*
195     protected void notifyCreate() {
196         super.notifyCreate();
197         if (cachedBody != null) {
198             String body;
199             
200             cachedBody = null;
201             body = getBody();
202             if (body != null)
203                 bodyHash = computeHash(body);
204         }
205
206     }
207      */

208     
209     private long computeHash(String JavaDoc bodyContent) {
210         java.util.zip.CRC32 JavaDoc crc = new java.util.zip.CRC32 JavaDoc();
211         crc.update(bodyContent.getBytes());
212         return crc.getValue();
213     }
214     
215     protected void createFromModel(Element model) throws SourceException {
216         super.createFromModel(model);
217
218         ConstructorElement m = (ConstructorElement)model;
219         setParameters(m.getParameters());
220         setExceptions(m.getExceptions());
221         setBody(m.getBody());
222         setJavaDocText(m.getJavaDoc().getRawText(), true);
223     }
224     
225     // Setters
226
///////////////////////////////////////////////////////////////////////////////
227

228     public void setParameters(MethodParameter[] params) throws SourceException {
229         checkWritable(true);
230         checkDocument();
231         boolean failed = true;
232         repository.beginTrans (true);
233         try {
234             if (javaElement.isValid()) {
235                 PropertyChangeEvent JavaDoc evt;
236                 setClassPath();
237                 params = resolveParams(params);
238                 evt = paramSupport.createChangeEvent(getElement(),
239                     params == null ? NO_PARAMETERS : getParameters (), params);
240                 if (evt == null) {
241                     failed = false;
242                     return;
243                 }
244
245                 checkVetoablePropertyChange(evt);
246                 if (params == null)
247                     params = NO_PARAMETERS;
248
249                 ParameterClass proxy = javaModelPackage.getParameter ();
250                 Collection pars = ((CallableFeature) javaElement).getParameters ();
251                 Collection temp = new LinkedList ();
252                 pars.clear();
253                 for (int x = 0; x < params.length; x++) {
254                     Parameter p = proxy.createParameter (
255                         params [x].getName (),
256                         null,
257                         params [x].isFinal (),
258                         null,
259                         0,
260                         false
261                     );
262                     org.openide.src.Type t = params[x].getType();
263                     if (t.isPrimitive()) {
264                         p.setType(typeToDescr(t));
265                     } else {
266                         p.setTypeName(typeToTypeReference(t));
267                     }
268                     temp.add (p);
269                 }
270                 pars.addAll (temp);
271                 failed = false;
272             } else {
273                 failed = false;
274                 throwIsInvalid ();
275             }
276         } finally {
277             repository.endTrans (failed);
278         }
279     }
280     
281     MethodParameter createParameter (Parameter param) {
282         // this method is always called from a mdr transaction
283
try {
284             org.openide.src.Type type = param.isVarArg() ? stringToType("java.lang.Object[]") : descrToType(param.getType()); // NOI18N
285
return new MethodParameter (
286                 param.getName (),
287                 type,
288                 param.isFinal ()
289             );
290         } catch (InvalidObjectException e) {
291             return new MethodParameter ("", null, false);
292         }
293     }
294     
295     MethodParameter createParameterConn (Parameter param) {
296         // this method is always called from a mdr transaction
297
try {
298             org.openide.src.Type type = null;
299             if (param.getType() instanceof ClassDefinition) {
300                 type = org.openide.src.Type.createClass(Identifier.create(param.getType().getName()));
301             } else {
302                 type = descrToType(param.getType());
303             }
304             return new MethodParameter (
305                 param.getName (),
306                 type,
307                 param.isFinal ()
308             );
309         } catch (InvalidObjectException e) {
310             return new MethodParameter ("", null, false);
311         }
312     }
313     
314     private MethodParameter resolveParameter(MethodParameter par) {
315         org.openide.src.Type t = resolveType(par.getType());
316         if (t == par.getType())
317             return par;
318         return new MethodParameter(
319             par.getName(), t, par.isFinal());
320     }
321     
322     private MethodParameter[] resolveParams(MethodParameter[] params) {
323         return params;
324         // [PENDING]
325
/*
326         if (!isConstrained())
327             return params;
328         
329         int[] map = paramSupport.pairItems(this.parameters, params);
330         MethodParameter[] newParams = null;
331         
332         for (int i = 0; i< map.length; i++) {
333             if (map[i] == -1) {
334                 MethodParameter p = resolveParameter(params[i]);
335                 
336                 if (p == params[i])
337                     continue;
338                 if (newParams == null) {
339                     newParams = new MethodParameter[params.length];
340                     System.arraycopy(params, 0, newParams, 0, params.length);
341                 }
342                 newParams[i] = p;
343             }
344         }
345         return newParams == null ? params : newParams;
346          */

347     }
348     
349     public void setExceptions(Identifier[] except) throws SourceException {
350         checkWritable(false);
351         checkDocument();
352         boolean failed = true;
353         repository.beginTrans (true);
354         try {
355             if (javaElement.isValid()) {
356                 setClassPath();
357                 except = resolveIdentifiers(except);
358                 PropertyChangeEvent JavaDoc evt;
359                 evt = exceptionSupport.createChangeEvent(getElement(),
360                     getExceptions (), except);
361                 if (evt == null) {
362                     failed = false;
363                     return;
364                 }
365                 checkVetoablePropertyChange(evt);
366
367                 if (except == null)
368                     except = IdentifierArrayProperty.EMPTY;
369
370                 Collection excs = ((CallableFeature)javaElement).getExceptionNames ();
371                 Collection temp = new LinkedList ();
372                 for (int x = 0; x < except.length; x++) {
373                     MultipartId id = javaModelPackage.getMultipartId().createMultipartId(except[x].getFullName(), null, null);
374                     temp.add (id);
375                 }
376                 excs.clear();
377                 excs.addAll (temp);
378                 failed = false;
379             } else {
380                 failed = false;
381                 throwIsInvalid ();
382             }
383         } finally {
384             repository.endTrans (failed);
385         }
386     }
387
388     /**
389      * Sets the text of the body to the passed value. Note, that PropertyChangeEvent
390      * fired on behalf of this change may contain other value because the underlying
391      * binding mechanism may change the exact layout of the body (no semantic changes
392      * are permitted).
393      * @throw SourceException if a VetoableListener disagrees or other error occurs.
394      */

395     public final void setBody(String JavaDoc body) throws SourceException {
396         checkWritable(false);
397         checkDocument();
398         repository.beginTrans (true);
399         boolean rollback = true;
400         try {
401             if (javaElement.isValid()) {
402                 PropertyChangeEvent JavaDoc evt;
403                 setClassPath();
404                 String JavaDoc cachedBody = getBody ();
405
406                 if (cachedBody == body ||
407                     (cachedBody != null && body != null && cachedBody.equals(body))) {
408                     return;
409                 }
410                 evt = new PropertyChangeEvent JavaDoc(getElement(), PROP_BODY, cachedBody, body);
411                 checkVetoablePropertyChange(evt);
412                 ((CallableFeature)javaElement).setBodyText(body);
413                 rollback = false;
414             } else {
415                 rollback = false;
416                 throwIsInvalid ();
417             }
418         } finally {
419             repository.endTrans (rollback);
420         }
421     }
422     
423     public void fireChangeParameters (MethodParameter[] oldPars, MethodParameter[] newPars, MethodParameter[] oldParsConn) {
424         PropertyChangeEvent JavaDoc evt;
425         evt = paramSupport.createChangeEvent(
426             getElement(),
427             oldPars,
428             newPars
429         );
430         if (evt == null) {
431             return;
432         }
433         fireOwnPropertyChange (evt);
434         
435         ConstructorElement elem = (ConstructorElement) cloneSelf ();
436         try {
437             elem.setParameters (oldParsConn);
438         } catch (SourceException e) {
439             e.printStackTrace ();
440         }
441         notifyConnectionChange (elem);
442     }
443     
444     public void fireChangeExceptions(Identifier[] oldExcs, Identifier[] newExcs) {
445         oldExcs = resolveIdentifiers(oldExcs);
446         newExcs = resolveIdentifiers(newExcs);
447         PropertyChangeEvent JavaDoc evt;
448         evt = exceptionSupport.createChangeEvent(getElement(), oldExcs, newExcs);
449         if (evt == null) {
450             return;
451         }
452         
453         fireOwnPropertyChange (evt);
454         
455         ConstructorElement elem = (ConstructorElement) cloneSelf ();
456         try {
457             elem.setExceptions (oldExcs);
458         } catch (SourceException e) {
459             e.printStackTrace ();
460         }
461         notifyConnectionChange (elem);
462     }
463     
464     public void fireChangeBody (String JavaDoc oldVal, String JavaDoc newVal) {
465         PropertyChangeEvent JavaDoc evt = new PropertyChangeEvent JavaDoc(getElement(), PROP_BODY, oldVal, newVal);
466         fireOwnPropertyChange(evt);
467     }
468     
469     // Private protocol
470
/////////////////////////////////////////////////////////////////////////////////
471

472     protected void initializeListenerSupport() {
473         if (exceptionSupport == null) {
474             synchronized (CallableImpl.class) {
475                 if (exceptionSupport == null)
476                     exceptionSupport = new ExceptionSupport();
477             }
478         }
479         if (paramSupport == null) {
480             synchronized (CallableImpl.class) {
481                 if (paramSupport == null)
482                     paramSupport = new MethodParamSupport();
483             }
484         }
485     }
486     
487     // Support classes
488
/////////////////////////////////////////////////////////////////////////////////
489

490     private static final class MethodParamSupport extends FlyweightIndexedProperty {
491         MethodParamSupport() {
492             super(PROP_PARAMETERS);
493         }
494         
495         protected final Object JavaDoc[] createEmpty() {
496             return NO_PARAMETERS;
497         }
498         
499         protected final Object JavaDoc[] createValue(int size) {
500             if (size == 0)
501                 return createEmpty();
502             return new MethodParameter[size];
503         }
504         
505         protected boolean compareValues(Object JavaDoc a, Object JavaDoc b) {
506             MethodParameter par1 = (MethodParameter)a;
507             MethodParameter par2 = (MethodParameter)b;
508             
509             org.openide.src.Type t1 = par1.getType();
510             org.openide.src.Type t2 = par2.getType();
511             if (!par1.getName().equals(par2.getName()))
512                 return false;
513             if (par1.isFinal() != par2.isFinal())
514                 return false;
515             return compareSourceTypes(t1, t2);
516         }
517         
518         protected final Object JavaDoc[] getValue(ElementImpl impl) {
519             return ((CallableImpl)impl).getParameters ();
520         }
521     }
522     
523     private static final class ExceptionSupport extends IdentifierArrayProperty {
524         ExceptionSupport() {
525             super(PROP_EXCEPTIONS);
526         }
527         
528         public Object JavaDoc[] getAsArray(ElementImpl bean) {
529             return ((CallableImpl)bean).getExceptions ();
530         }
531
532         protected final Object JavaDoc[] getValue(ElementImpl impl) {
533             return ((CallableImpl)impl).getExceptions ();
534         }
535         
536         public boolean compareValues(Object JavaDoc one, Object JavaDoc two) {
537             return compareSourceIdentifiers((Identifier)one, (Identifier)two);
538         }
539     }
540     
541     protected void copyCallableProperties(ConstructorElement el) {
542         try {
543             el.setName(getName());
544             el.setModifiers(getModifiers());
545             el.setParameters(getParameters());
546             el.setExceptions(getExceptions());
547         } catch (SourceException ex) {
548         }
549     }
550     
551     // ..........................................................................
552

553     static class CallableListener extends MemberElementImpl.MemberElementListener {
554         
555         private static final MethodParameter[] NO_PARAMETERS = new MethodParameter[0];
556         private static final Identifier[] NO_IDENTIFIERS = new Identifier[0];
557         
558         ParametersListener params = new ParametersListener (this);
559         ExceptionsListener exceptions = new ExceptionsListener (this);
560                 
561         CallableListener (CallableImpl impl) {
562             super (impl);
563         }
564         
565         public void connect () {
566             if (REGISTER_LISTENER) {
567                 super.connect ();
568                 impl = (ElementImpl) ref.get();
569                 if (impl != null) {
570                     params.initElements ();
571                     exceptions.initElements ();
572                 }
573                 impl = null;
574             }
575         }
576         
577         public void remove () {
578             super.remove ();
579             params.removeAll ();
580             exceptions.removeAll ();
581         }
582                 
583         public void doChange(MDRChangeEvent event) {
584             super.doChange (event);
585             if (event instanceof AttributeEvent) {
586                 AttributeEvent attrEv = (AttributeEvent) event;
587                 if (attrEv.getAttributeName ().equals ("bodyText")) { // NOI18N
588
((CallableImpl) impl).fireChangeBody (
589                         (String JavaDoc) attrEv.getOldElement (),
590                         (String JavaDoc) attrEv.getNewElement ()
591                      );
592                 }
593             }
594         }
595                 
596     } // CallableListener .......................................................
597

598     static class ExceptionsListener implements MDRPreChangeListener {
599
600         protected ElementImpl.ElementListener parentListener;
601         protected RefObject javaElement;
602         private ArrayList exIds = new ArrayList();
603         private Map eventsMap = new HashMap();
604
605         ExceptionsListener(ElementImpl.ElementListener parentListener) {
606             this.parentListener = parentListener;
607             javaElement = parentListener.javaElement;
608         }
609
610         // -----------
611

612         public List getElements() {
613             return ((CallableFeature) javaElement).getExceptions();
614         }
615         
616         public String JavaDoc getEndName() {
617             return "exceptions"; // NOI18N
618
}
619         
620         public void fireChange (ElementImpl impl, ArrayList oldValues, ArrayList newValues) {
621             Identifier [] oldPars = (Identifier []) oldValues.toArray (CallableListener.NO_IDENTIFIERS);
622             Identifier [] newPars = (Identifier []) newValues.toArray (CallableListener.NO_IDENTIFIERS);
623             ((CallableImpl) impl).fireChangeExceptions (oldPars, newPars);
624         }
625         
626         // -----------
627

628         public void initElements () {
629             Iterator iter = getElements().iterator();
630             while (iter.hasNext()) {
631                 JavaClass ex = (JavaClass) iter.next();
632                 exIds.add(Identifier.create(ex.getName()));
633             }
634             ((MDRChangeSource) javaElement).addListener(this, AssociationEvent.EVENTMASK_ASSOCIATION);
635         }
636         
637         public void removeAll () {
638             exIds.clear();
639             try {
640                 ((MDRChangeSource) javaElement).removeListener (this);
641             } catch (InvalidObjectException e) {
642             }
643         }
644
645         public int findIndex (String JavaDoc name) {
646             int size = exIds.size ();
647             Iterator iter = exIds.iterator ();
648             for (int x = 0; x < size; x++) {
649                 Identifier id = (Identifier) iter.next();
650                 if (id.getName().equals(name))
651                     return x;
652             }
653             return -1;
654         }
655         
656         public final void change(MDRChangeEvent event) {
657             if (!parentListener.isValid)
658                 return;
659             ElementImpl impl = parentListener.getImpl ();
660             if (impl == null) {
661                 parentListener.doRemove ();
662                 return;
663             }
664             if (event instanceof AssociationEvent) {
665                 AssociationEvent assocEvent = (AssociationEvent) event;
666                 String JavaDoc endName = getEndName();
667                 if (getEndName().equals(assocEvent.getEndName())) {
668                     EventInfo info = (EventInfo)eventsMap.remove(event);
669                     ArrayList old = (ArrayList) exIds.clone();
670                     int index;
671                     int position = info.index;
672                     if (event.isOfType (AssociationEvent.EVENT_ASSOCIATION_SET)) {
673                         index = position != AssociationEvent.POSITION_NONE ? position : findIndex(info.oldName);
674                         if (index < 0 || index >= exIds.size()) // [PENDING]
675
return;
676                         exIds.set (index, Identifier.create(info.newName));
677                     } else if (event.isOfType (AssociationEvent.EVENT_ASSOCIATION_REMOVE)) {
678                         index = position != AttributeEvent.POSITION_NONE ? position : findIndex(info.oldName);
679                         if (index < 0 || index >= exIds.size()) // [PENDING]
680
return;
681                         exIds.remove (index);
682                     } else { // EVENT_ATTRIBUTE_ADD
683
if (position == AttributeEvent.POSITION_NONE)
684                             position = exIds.size ();
685                         if (position < 0 || position > exIds.size()) // [PENDING]
686
return;
687                         exIds.add(position, Identifier.create(info.newName));
688                     }
689                     fireChange (impl, old, exIds);
690                 }
691             }
692             
693         }
694         
695         public void changeCancelled(MDRChangeEvent e) {
696             if (!parentListener.isValid)
697                 return;
698             eventsMap.remove(e);
699         }
700         
701         public void plannedChange(MDRChangeEvent event) {
702             if (!parentListener.isValid)
703                 return;
704             if (event instanceof AssociationEvent) {
705                 AssociationEvent assocEvent = (AssociationEvent) event;
706                 String JavaDoc endName = assocEvent.getEndName();
707                 if (getEndName().equals(endName)) {
708                     EventInfo info = new EventInfo();
709                     JavaClass jc = (JavaClass) assocEvent.getNewElement();
710                     JavaClass old = (JavaClass) assocEvent.getOldElement();
711                     info.index = assocEvent.getPosition();
712                     info.newName = jc != null ? jc.getName() : null;
713                     info.oldName = old != null ? old.getName() : null;
714                     eventsMap.put(event, info);
715                 }
716             }
717         }
718         
719         class EventInfo {
720             String JavaDoc newName;
721             String JavaDoc oldName;
722             int index;
723         }
724         
725     } // ExceptionsListener .....................................................
726

727     static class ParametersListener implements MDRChangeListener {
728
729         private ElementImpl.ElementListener parentListener;
730         private CallableFeature javaElement;
731         private ArrayList array = new ArrayList ();
732         private ArrayList arrayConn = new ArrayList ();
733         private ArrayList params = new ArrayList ();
734
735         ParametersListener(ElementImpl.ElementListener parentListener) {
736             this.parentListener = parentListener;
737             javaElement = (CallableFeature) parentListener.javaElement;
738         }
739
740         public void initElements () {
741             Iterator iter = javaElement.getParameters ().iterator ();
742             for (int x = 0; iter.hasNext (); x++) {
743                 Parameter param = (Parameter) iter.next ();
744                 array.add (((CallableImpl)parentListener.getImpl ()).createParameter (param));
745                 arrayConn.add (((CallableImpl)parentListener.getImpl ()).createParameterConn (param));
746                 params.add (param);
747                 ((MDRChangeSource) param).addListener (this);
748             }
749             ((MDRChangeSource) javaElement).addListener (this);
750         }
751
752         public void removeAll () {
753             Iterator iter = params.iterator ();
754             while (iter.hasNext ()) {
755                 MDRChangeSource javaElem = (MDRChangeSource) iter.next ();
756                 javaElem.removeListener (this);
757             }
758             array.clear();
759             arrayConn.clear();
760             params.clear();
761             try {
762                 ((MDRChangeSource) javaElement).removeListener (this);
763             } catch (InvalidObjectException e) {
764             }
765         }
766
767         public int findParamIndex (RefBaseObject param) {
768             int size = params.size ();
769             Iterator iter = params.iterator ();
770             for (int x = 0; x < size; x++) {
771                 RefObject obj = (RefObject) iter.next ();
772                 if (obj.equals (param))
773                     return x;
774             }
775             return -1;
776         }
777         
778         public void fireChange (CallableImpl impl, ArrayList oldValues, ArrayList newValues, ArrayList oldValuesConn) {
779             MethodParameter [] oldPars = (MethodParameter []) oldValues.toArray (CallableListener.NO_PARAMETERS);
780             MethodParameter [] newPars = (MethodParameter []) newValues.toArray (CallableListener.NO_PARAMETERS);
781             MethodParameter [] oldParsConn = (MethodParameter []) oldValuesConn.toArray (CallableListener.NO_PARAMETERS);
782             impl.fireChangeParameters (oldPars, newPars, oldParsConn);
783         }
784
785         public final void change(MDRChangeEvent event) {
786             if (!parentListener.isValid)
787                 return;
788
789             CallableImpl impl = (CallableImpl) parentListener.getImpl ();
790             if (impl == null) {
791                 parentListener.doRemove ();
792                 return;
793             }
794             RefBaseObject source = (RefBaseObject) event.getSource ();
795             if (source instanceof IsOfType) {
796                 RefObject fixed = ((AssociationEvent) event).getFixedElement ();
797                 if (fixed instanceof Parameter) {
798                     ArrayList old = (ArrayList) array.clone();
799                     ArrayList oldConn = (ArrayList) arrayConn.clone();
800                     int index = findParamIndex (fixed);
801                     if (index != -1) {
802                         array.set (index, ((CallableImpl)parentListener.getImpl ()).createParameter ((Parameter) fixed));
803                         arrayConn.set (index, ((CallableImpl)parentListener.getImpl ()).createParameterConn ((Parameter) fixed));
804                         fireChange (impl, old, array, oldConn);
805                     }
806                 }
807             } else if (event instanceof AttributeEvent) {
808                 AttributeEvent attrEvent = (AttributeEvent) event;
809                 String JavaDoc attrName = attrEvent.getAttributeName ();
810                 if (source instanceof Parameter) {
811                     if (attrName.equals ("isFinal")) { // NOI18N
812
ArrayList old = (ArrayList) array.clone ();
813                         ArrayList oldConn = (ArrayList) arrayConn.clone ();
814                         int index = findParamIndex (source);
815                         if (index != -1) {
816                             MethodParameter param = (MethodParameter) array.get (index);
817                             boolean isFinal = !param.isFinal ();
818                             param.setFinal (isFinal);
819                             param = (MethodParameter) arrayConn.get (index);
820                             param.setFinal (isFinal);
821                             fireChange (impl, old, array, oldConn);
822                         }
823                     } else if (attrName.equals ("name")) { // NOI18N
824
ArrayList old = (ArrayList) array.clone ();
825                         ArrayList oldConn = (ArrayList) arrayConn.clone ();
826                         int index = findParamIndex (source);
827                         if (index != -1) {
828                             MethodParameter param = (MethodParameter) array.get (index);
829                             String JavaDoc n = (String JavaDoc) attrEvent.getNewElement ();
830                             param.setName(n);
831                             param = (MethodParameter) arrayConn.get (index);
832                             param.setName(n);
833                             fireChange (impl, old, array, oldConn);
834                         }
835                     } else if (attrName.equals("isVarArg") || attrName.equals("typeArguments")) { // NOI18N
836
ArrayList old = (ArrayList) array.clone ();
837                         ArrayList oldConn = (ArrayList) arrayConn.clone ();
838                         int index = findParamIndex (source);
839                         if (index != -1) {
840                             MethodParameter param = ((CallableImpl)parentListener.getImpl()).createParameter ((Parameter) source);
841                             array.set(index, param);
842                             param = ((CallableImpl)parentListener.getImpl()).createParameterConn((Parameter) source);
843                             arrayConn.set(index, param);
844                             fireChange (impl, old, array, oldConn);
845                         }
846                     }
847                 } else if (source instanceof CallableFeature) {
848                     if (attrName.equals ("parameters")) { // NOI18N
849
ArrayList old = (ArrayList) array.clone ();
850                         ArrayList oldConn = (ArrayList) arrayConn.clone ();
851                         int position = attrEvent.getPosition ();
852                         if (event.isOfType (AttributeEvent.EVENT_ATTRIBUTE_REMOVE)) {
853                             RefObject oldPar = (RefObject) attrEvent.getOldElement ();
854                             if (position == AttributeEvent.POSITION_NONE)
855                                 position = findParamIndex (oldPar);
856                             if (position != -1) {
857                                 array.remove(position);
858                                 arrayConn.remove(position);
859                                 MDRChangeSource cs = (MDRChangeSource) params.remove (position);
860                                 cs.removeListener (this);
861                             }
862                         } else if (event.isOfType (AttributeEvent.EVENT_ATTRIBUTE_SET)) {
863                             Parameter fps = (Parameter) attrEvent.getNewElement ();
864                             array.set (position, ((CallableImpl)parentListener.getImpl()).createParameter (fps));
865                             arrayConn.set (position, ((CallableImpl)parentListener.getImpl()).createParameterConn(fps));
866                             MDRChangeSource cs = (MDRChangeSource) params.set (position, fps);
867                             cs.removeListener (this);
868                             ((MDRChangeSource) fps).addListener (this);
869                         } else if (event.isOfType (AttributeEvent.EVENT_ATTRIBUTE_ADD)) {
870                             Parameter fps = (Parameter) attrEvent.getNewElement ();
871                             if (position == AttributeEvent.POSITION_NONE)
872                                 position = array.size ();
873                             array.add (position, ((CallableImpl)parentListener.getImpl()).createParameter (fps));
874                             arrayConn.add (position, ((CallableImpl)parentListener.getImpl()).createParameterConn(fps));
875                             params.add (position, fps);
876                             ((MDRChangeSource) fps).addListener (this);
877                         }
878                         fireChange (impl, old, array, oldConn);
879                     }
880                 }
881             }
882         }
883
884     } // ParametersListener .....................................................
885

886 }
887
888
Popular Tags