svn commit: r18320 - trunk/src/argouml-core-model-euml/src/org/argouml/model/euml/CoreHelperEUMLImpl.java

[email protected]
Newsgroups gmane.comp.lang.uml.argouml.cvs
Message-ID <[email protected]>
Author: andreas
Date: 2010-04-21 07:54:05-0700
New Revision: 18320

Modified:
   trunk/src/argouml-core-model-euml/src/org/argouml/model/euml/CoreHelperEUMLImpl.java

Log:
Added setBody method for operations.


Modified: trunk/src/argouml-core-model-euml/src/org/argouml/model/euml/CoreHelperEUMLImpl.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-core-model-euml/src/org/argouml/model/euml/CoreHelperEUMLImpl.java?view=diff&pathrev=18320&r1=18319&r2=18320
==============================================================================
--- trunk/src/argouml-core-model-euml/src/org/argouml/model/euml/CoreHelperEUMLImpl.java	(original)
+++ trunk/src/argouml-core-model-euml/src/org/argouml/model/euml/CoreHelperEUMLImpl.java	2010-04-21 07:54:05-0700
@@ -53,6 +53,7 @@
 import org.eclipse.uml2.uml.NamedElement;
 import org.eclipse.uml2.uml.Namespace;
 import org.eclipse.uml2.uml.Node;
+import org.eclipse.uml2.uml.OpaqueBehavior;
 import org.eclipse.uml2.uml.Operation;
 import org.eclipse.uml2.uml.Parameter;
 import org.eclipse.uml2.uml.ParameterDirectionKind;
@@ -65,6 +66,7 @@
 import org.eclipse.uml2.uml.StructuralFeature;
 import org.eclipse.uml2.uml.Type;
 import org.eclipse.uml2.uml.TypedElement;
+import org.eclipse.uml2.uml.UMLFactory;
 import org.eclipse.uml2.uml.UMLPackage;
 import org.eclipse.uml2.uml.ValueSpecification;
 import org.eclipse.uml2.uml.VisibilityKind;
@@ -1313,14 +1315,62 @@
     }
 
     public void setBody(Object handle, Object expr) {
-        throw new NotYetImplementedException();
+	throw new NotYetImplementedException();
     }
 
     public void setBody(Object handle, String body) {
-        if (!(handle instanceof Comment)) {
-            throw new IllegalArgumentException();
-        }
-        ((Comment) handle).setBody(body);
+        
+	if( handle instanceof Comment) {
+	    ((Comment) handle).setBody(body);
+	    return;
+	}
+
+	if( handle instanceof Operation) {
+
+	    // We need a method (operation implementation) to store the method body.
+	    OpaqueBehavior methodImpl = null;
+
+	    // Maybe this operation already has a method, that fits our purpose?
+	    // In this case, try to reuse it, instead of creating a new implementation.
+	    for( Behavior impl : ((Operation)handle).getMethods()) {
+		if( impl instanceof OpaqueBehavior) {
+		    methodImpl = (OpaqueBehavior)impl;
+		    break;
+		}
+	    }
+
+	    // Check, if we have to create a new implementation.
+	    if( methodImpl == null) {
+		methodImpl = UMLFactory.eINSTANCE.createOpaqueBehavior();	// Create a new implementation.
+		methodImpl.setSpecification( (Operation)handle);		// And set the specification to the current operation.
+		((Operation)handle).getMethods().add( methodImpl);  		// Add it to the operation's methods.
+	    }
+
+	    // Look, if there's already a java implementation
+	    if( methodImpl.isSetLanguages()) {
+		int bodyIndex = 0;
+
+		// Search for our current target language.
+		for( String language : methodImpl.getLanguages()) {
+		    if( "java".equals( language)) {
+
+			// Try to get the corresponding body and set it to the current body
+			// This _should_ work, if all the bodies were stored with their corresponding languages.
+			methodImpl.getBodies().set( bodyIndex, body);
+			return;		// Job done.
+		    }
+		    bodyIndex++;
+		}
+	    }
+  
+	    // It seems, there was no implementation of our current target language, so we just add one.
+	    methodImpl.getLanguages().add( "java");
+	    methodImpl.getBodies().add( body);
+	    return;
+	}
+
+	// We cannot set the body of this model element type.
+	throw new IllegalArgumentException();
     }
 
     @Deprecated

------------------------------------------------------
http://argouml.tigris.org/ds/viewMessage.do?dsForumId=5905&dsMessageId=2591680

To unsubscribe from this discussion, e-mail: [[email protected]].
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.