Anonymous avatar Anonymous committed 3a1321f

New Features: - bound-to is now created for instances based on what is in the classes - deleting an instance is now undoable... previously it was resulting in an error because of separating instances and classes - refactoring is done

Comments (0)

Files changed (7)

src/nl/tudelft/lime/model/LimeSubpart.java

     public static String ID_SIZE = SystemSettings.PropertyDescriptor_Size_ID;
     public static String ID_LOCATION = SystemSettings.PropertyDescriptor_Location_ID;
     public static String ID_NAME = SystemSettings.PropertyDescriptor_Name_ID;
+    //this is used for the ones which are classes
+    private List<LimeSubpart> instances = new ArrayList<LimeSubpart>();
+    //this is used for the ones which are instances
+    private LimeSubpart anInstanceOf = null;
+    /**
+     * if it is an instance what is its parent class
+     * @return
+     */
+    public LimeSubpart getAnInstanceOf() {
+		return anInstanceOf;
+	}
+    /**
+     * if it is an instance sets its parent class
+     * @return
+     */
+	public void setAnInstanceOf(LimeSubpart anInstanceOf) {
+		this.anInstanceOf = anInstanceOf;
+	}
 
-    private List<LimeSubpart> instances = new ArrayList<LimeSubpart>();
-    
-    /**
+	/**
      * @return list of instances of  this class.  if the component is
      * an instance the list should be empty
      */
 
     //keep input and output association lists
     protected Hashtable<String, Association> inputAssociations = new Hashtable<String, Association>();
-    protected Vector<Association> outputAssociations = new Vector<Association>();
+    protected Vector<Association> associations = new Vector<Association>();
     protected Point location = new Point(0, 0);
     protected Dimension size = new Dimension(-1, -1);
     protected String name = "";
     }
 
     public void connectOutputAssociation(Association association) {
-        outputAssociations.addElement(association);
+        associations.addElement(association);
         update();
         fireStructureChange(OUTPUTS, association);
     }
     }
 
     public void disconnectOutputAssociation(Association association) {
-        outputAssociations.removeElement(association);
+        associations.removeElement(association);
         update();
         fireStructureChange(OUTPUTS, association);
     }
      */
     @SuppressWarnings("unchecked")
     public Vector<Association> getSourceAssociations() {
-        return (Vector<Association>) outputAssociations.clone();
+        return (Vector<Association>) associations.clone();
     }
 
     /**

src/nl/tudelft/lime/model/component/LimeComponent.java

 package nl.tudelft.lime.model.component;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import nl.tudelft.lime.constants.LimeMessages;
 import nl.tudelft.lime.constants.SystemSettings;
 import nl.tudelft.lime.model.LimeElement;
 import nl.tudelft.lime.model.LimeSubpart;
 import nl.tudelft.lime.model.codefragment.LimeCodeFragment;
+import nl.tudelft.lime.model.connection.Association;
+import nl.tudelft.lime.model.port.Port;
 
 import org.eclipse.ui.views.properties.IPropertyDescriptor;
 import org.eclipse.ui.views.properties.PropertyDescriptor;
 		setOnlyInstances(1);
 		this.componentClass = componentClassString;
     	LimeComponent realComponentClass = findComponentClass(componentClassString);
+    	
+    	//this method should keep the associations for a class instance
+    	//and copy it to the new instance so we keep a dictionary of the associations while
+    	//reading
     	if (realComponentClass != null){
     		//this is used later for deleting and adding purposes
     		realComponentClass.addToInstances(this);
 			//first step
-			for (LimeElement child : this.children){
-		        fireChildRemoved(CHILDREN, child);
-		    }
 			children.clear();
 			//the problem is that when the property is changed
 			//we have to change $this$ indeed so we rewrite the code in getCopy then!
+			//this map keeps who is related to who
+			Map<Port, Port> portMap = new HashMap<Port, Port>();
+			List<Association> associations = new ArrayList<Association>();
 			for (LimeElement child : realComponentClass.getChildren()){
 				LimeSubpart newChild = child.getCopy();
-				if (newChild instanceof LimeComponent) {
+				//keep info about ports and associations
+				if (child instanceof Port){
+					portMap.put((Port)child, (Port)newChild);
+					associations.addAll(((Port)child).getPortAssociations());
+				}
+				if (child instanceof LimeComponent) {
+					//get dictionary from the child
+					portMap.putAll(((LimeComponent)newChild).getPortDictionary());
 					((LimeComponent)newChild).setInstance(true);
 				}				
 				newChild.setParent(this);
 				addChild(newChild);
 		    }
+			//create associations for this component
+			//now read the map and try connecting things together
+			for (Association association : associations){
+				Port newSource = portMap.get(association.getSource());
+				Port newTarget = portMap.get(association.getTarget());
+				if ((newSource != null) && (newTarget != null)){
+					Association newAssociation = new Association();
+					newAssociation.createAssociation(newSource, newTarget);
+				}
+			}
     	}
 		setOnlyInstances(oldOnlyInstances);
         firePropertyChange(Property_ID_Component_InstanceOf, null, realComponentClass);
 	}
 	
-
-    /**
+	/**
+	 * 
+	 * @return map from class-> instance for all the included ports
+	 */
+    private Map<Port, Port> getPortDictionary() {
+    	Map<Port, Port> dictionary = new HashMap<Port, Port>();
+    	for (LimeElement child : this.children){
+    		if (child instanceof Port){
+    			dictionary.put((Port)((Port)child).getAnInstanceOf(), (Port) child);
+    		}
+    		else if (child instanceof LimeComponent){
+    			dictionary.putAll(((LimeComponent) child).getPortDictionary());
+    		}
+    	}
+    	return dictionary;
+	}
+	/**
      * Components can be both instances and classes (in LIME definition not Java definition of class and instance)
      * if it is an instance it can only be added to a diagram in its instance mode 
      * @return if the component is an instance indeed
     	//if it is not instance and the change from UI
     	//or if it is an instance and the change is not from UI then accept the change
     	if (Property_ID_Component_InstanceOf.equals(id)) {
-	        	//find its parent and set its children
-	        	setComponentClass((String)value);
+    			if (componentClass.equals(""))
+    				//find its parent and set its children
+		        	setComponentClass((String)value);
         }
     	if ((!instance && changeFromUI) ||
     		(instance && !changeFromUI)){
 		newComponent.size = size;
 		newComponent.name = name;
 		addToInstances(newComponent);
+		newComponent.setAnInstanceOf(this);
 		return newComponent;
 	}
 	

src/nl/tudelft/lime/model/component/LimeDiagram.java

     		((LimeDiagram)instance).addChild(child.getCopy(), index);
     	}
     		
-        if (index >= 0) {
+        if (index >= 0) {//
+        	//the index is further used for visible elements only
+        	//so it should be reduced if necessary
+        	for (LimeElement kid : children){
+        		//if it is a component and not an instance
+        		//decrease index by one
+        		if ((kid instanceof LimeComponent)&&
+        				!((LimeComponent)kid).isInstance())
+        			index --;
+        	}
             children.add(index, child);
         } else {
             children.add(child);

src/nl/tudelft/lime/model/connection/Association.java

         }
 
         getSource().disconnectOutputAssociation(this);
+        if (getSource() instanceof Port) 
+        	((Port)getSource()).removeFromPortAssociations(this);
+        
     }
 
     public void detachTarget() {
         this.setSourceTerminal(Port.CONNECTION_TERMINAL);
         this.attachSource();
         this.attachTarget();
+        //keep associations in only source
+        if (sourceElement instanceof Port) 
+        	((Port)sourceElement).addToPortAssociations(this);
     }
 }

src/nl/tudelft/lime/model/port/InputPort.java

 		newPort.size = size;
 		newPort.name = name;
 		addToInstances(newPort);
+		newPort.setAnInstanceOf(this);
 		return newPort;
 	}
 

src/nl/tudelft/lime/model/port/OutputPort.java

 		newPort.size = size;
 		newPort.name = name;
 		addToInstances(newPort);
+		newPort.setAnInstanceOf(this);
 		return newPort;
 	}
 

src/nl/tudelft/lime/model/port/Port.java

 package nl.tudelft.lime.model.port;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import nl.tudelft.lime.constants.LimeMessages;
 import nl.tudelft.lime.constants.SystemSettings;
 import nl.tudelft.lime.figures.LimeConstants;
 import nl.tudelft.lime.model.LimeSubpart;
 import nl.tudelft.lime.model.component.LimeComponent;
 import nl.tudelft.lime.model.connection.Arc;
+import nl.tudelft.lime.model.connection.Association;
 
 import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
 import org.eclipse.ui.views.properties.IPropertyDescriptor;
                                                     //public static String TERMINAL_B = "B"; //$NON-NLS-1$
 
     protected static IPropertyDescriptor[] newDescriptors = null;
+    List<Association> portAssociations = new ArrayList<Association>();
 
     //aditiional fields for the port
     //    protected String portConst = "";
     public void update() {
         setOutput(TERMINAL_OUT, true);
     }
+
+	public List<Association> getPortAssociations() {
+		return portAssociations;
+	}
+
+	public void addToPortAssociations(Association portAssociation) {
+		this.portAssociations.add(portAssociation);
+	}
+	public void removeFromPortAssociations(Association portAssociation) {
+		this.portAssociations.remove(portAssociations);
+	}
+    
+    
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.