Anonymous avatar Anonymous committed 0f33c7c

finally instantiation is supported...
that was a real hardwork :)

Comments (0)

Files changed (10)

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

 
 abstract public class LimeSubpart extends LimeElement {
     static final long serialVersionUID = 1;
-
+    //it is used to allow changes for instances if it is not from UI
+    //in UI it should be disabled, it is only set in setPropertyValue
+    protected boolean changeFromUI = true;
     //counts how many instances have been instantiated so far
     protected static IPropertyDescriptor[] descriptors = null;
     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;
 
+    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
+     */
+	public List<LimeSubpart> getInstances() {
+		return instances;
+	}
+	
+	public void addToInstances(LimeSubpart subpart){
+		instances.add(subpart);
+	}
     static {
         descriptors = new IPropertyDescriptor[] {
                 new PropertyDescriptor(ID_SIZE,
      * @param value  Value to be set to the given parameter.
      */
     public void setPropertyValue(Object id, Object value) {
+    	if (getInstances().size()!=0){
+    		List<LimeSubpart> myInstances = getInstances();
+    		for (LimeSubpart subpart : myInstances){
+    			subpart.changeFromUI = false;
+    			subpart.setPropertyValue(id, value);
+    			subpart.changeFromUI = true;
+    		}
+    	}
         if (ID_SIZE.equals(id)) {
             setSize((Dimension) value);
         } else if (ID_LOCATION.equals(id)) {

src/nl/tudelft/lime/model/commands/DeleteCommand.java

 import nl.tudelft.lime.model.LimeElement;
 import nl.tudelft.lime.model.LimeGuide;
 import nl.tudelft.lime.model.LimeSubpart;
+import nl.tudelft.lime.model.component.LimeComponent;
 import nl.tudelft.lime.model.component.LimeDiagram;
 import nl.tudelft.lime.model.connection.Arc;
 import nl.tudelft.lime.model.connection.Association;
     private List<Arc> targetArcs = new ArrayList<Arc>();
     private List<Association> sourceAssociations = new ArrayList<Association>();
     private List<Association> targetAssociations = new ArrayList<Association>();
+    
+    private List<DeleteCommand> childDeleteCommands = new ArrayList<DeleteCommand>();
+    private List<LimeSubpart> instances = new ArrayList<LimeSubpart>();
 
     public DeleteCommand() {
         super(LimeMessages.DeleteCommand_Label);
     }
 
-    /**
+    @Override
+	public boolean canExecute() {
+    	if 	((child instanceof LimeComponent && ((LimeComponent)child).isInstance()) ||
+    		(parent instanceof LimeComponent && ((LimeComponent)parent).isInstance()))
+    		return false;
+    	
+    	//if it is an association and its source is a child of an instace then don't 
+    	//delete it as well
+    	if (child instanceof Association){
+    		LimeComponent component = (LimeComponent)((Association)child).getSource().getParent();
+    		if (component.isInstance())
+    			return false;
+    	}
+		return super.canExecute();
+	}
+
+	/**
      * Keeps history of the connections (arcs and associations) from/to the component {@code part}
      * in order to be able to return them back by undo action
      * <br/>
     }
 
     protected void primExecute() {
+    	//delete all the instances first
+    	for (LimeSubpart kid : child.getInstances()){
+    		DeleteCommand kidDeleteCommand = new DeleteCommand();
+    		kidDeleteCommand.instances = child.getInstances();
+    		kidDeleteCommand.setChild(kid);
+    		kidDeleteCommand.setParent((LimeDiagram)kid.getParent());
+    		childDeleteCommands.add(kidDeleteCommand);
+    		kidDeleteCommand.execute();
+    	}
         deleteConnections(child);
         detachFromGuides(child);
         index = parent.getChildren().indexOf(child);
         parent.addChild(child, index);
         restoreConnections();
         reattachToGuides(child);
+    	//redo all child delete commands
+    	for (DeleteCommand kidDeleteCommand : childDeleteCommands){
+    		kidDeleteCommand.undo();
+    	}    	
     }
 }

src/nl/tudelft/lime/model/commands/connection/ConnectionCommand.java

 
 import nl.tudelft.lime.constants.LimeMessages;
 import nl.tudelft.lime.model.LimeSubpart;
+import nl.tudelft.lime.model.component.LimeComponent;
+import nl.tudelft.lime.model.connection.Association;
 import nl.tudelft.lime.model.connection.Connection;
 
 import org.eclipse.gef.commands.Command;
         super(LimeMessages.ConnectionCommand_Label);
     }
 
-    public void execute() {
+    @Override
+	public boolean canExecute() {
+    	//if it is an association and its source is a child of an instace then don't 
+    	//delete it as well
+    	if (source == null && target == null){
+    		LimeComponent component = (LimeComponent) oldSource.getParent();
+    		if (component.isInstance())
+    			return false;
+    	}
+		return super.canExecute();
+	}
+
+	public void execute() {
         if (source != null) {
             conenction.detachSource();
             conenction.setSource(source);

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

 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 org.eclipse.ui.views.properties.IPropertyDescriptor;
 import org.eclipse.ui.views.properties.PropertyDescriptor;
     private boolean instance;
     private String componentRet = "";
     private String componentClass = "";
-    private List<LimeComponent> instances = new ArrayList<LimeComponent>();
     
 
+
 	static {
         PropertyDescriptor componentTypeProp = new TextPropertyDescriptor(Property_ID_Component_Type,
                 LimeMessages.PropertyDescriptor_Component_Type_Label);
 	 * @param componentClassString
 	 */
 	public void setComponentClass(String componentClassString) {
+		Integer oldOnlyInstances = getOnlyInstances();
+		setOnlyInstances(1);
 		this.componentClass = componentClassString;
     	LimeComponent realComponentClass = findComponentClass(componentClassString);
     	if (realComponentClass != null){
 			//first step
 			for (LimeElement child : this.children){
-				removeChild(child);
+		        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!
 			for (LimeElement child : realComponentClass.getChildren()){
 				LimeSubpart newChild = child.getCopy();
+				if (newChild instanceof LimeComponent) {
+					((LimeComponent)newChild).setInstance(true);
+				}				
 				newChild.setParent(this);
 				addChild(newChild);
 		    }
-    		realComponentClass.addToInstances(this);
     	}
+		setOnlyInstances(oldOnlyInstances);
         firePropertyChange(Property_ID_Component_InstanceOf, null, realComponentClass);
 	}
 	
      */
     @Override
     public void setPropertyValue(Object id, Object value) {
-    	if (!instance){
+    	//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 ((!instance && changeFromUI) ||
+    		(instance && !changeFromUI)){
 	        if (Property_ID_Component_Ret.equals(id)) {
 	            setComponentRet((String) value);
 	        } else if (Property_ID_Component_Type.equals(id)) {
 	        }
     	}
     }
-    /**
-     * adds a component to instances of this class... this is used when :
-     * a component class is deleted and all its instances should also be deleted
-     * !!!currently we don't do that because if class is deleted there is no reason to delete
-     * its children!!! but it is possible 
-     * @param limeComponent
-     */
-    private void addToInstances(LimeComponent limeComponent) {
-    	//this method is used if something special should be done
-    	instances.add(limeComponent);
-	}
 
 	private LimeComponent findComponentClass(String componentName) {
     	LimeSubpart diagram = this;
 	public LimeSubpart getCopy() {
 		LimeComponent newComponent = new LimeComponent();
 		//use reflection further
-		for (LimeElement child : this.children)
-			newComponent.addChild(child.getCopy());
-		for (Association association : this.getTargetAssociations()){
+		
+		for (LimeElement child : this.children){
+			//if child is a component and this component is also 
+			//an instance set it also as instance
+			LimeSubpart childCopy = child.getCopy();
+			newComponent.addChild(childCopy);
+			childCopy.setParent(newComponent);
+			if (isInstance() && (childCopy instanceof LimeComponent)) {
+				((LimeComponent)childCopy).setInstance(true);
+			}
 		}
+		
 		newComponent.componentRet = componentRet;
 		newComponent.type = type;
 		newComponent.location = location;

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

     public Integer showOnlyInstances() {
 		return onlyInstances;
 	}
-
+    /**
+     * Sets if the diagram should be in only instance mode
+     * remember that diagram is the parent of a component
+     * @param onlyInstances
+     */
 	public void setOnlyInstances(Integer onlyInstances) {
 		Integer oldOnlyInstances = this.onlyInstances;
 		this.onlyInstances = onlyInstances;
         firePropertyChange(Property_ID_DIAGRAM_ONLY_INSTANCES, oldOnlyInstances, onlyInstances);
 	}
+	
+    public Integer getOnlyInstances() {
+		return onlyInstances;
+	}
 
-    public LimeDiagram() {
+	public LimeDiagram() {
         size = new Dimension(-1, -1);
         location.x = 20;
         location.y = 20;

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

 package nl.tudelft.lime.model.connection;
 
+import java.util.List;
+
 import nl.tudelft.lime.constants.LimeMessages;
 import nl.tudelft.lime.constants.SystemSettings;
+import nl.tudelft.lime.model.LimeSubpart;
 import nl.tudelft.lime.model.component.LimeDiagram;
+import nl.tudelft.lime.model.port.Port;
 
 import org.eclipse.swt.graphics.Image;
 
     private static final String DEFAULT_NAME = LimeMessages.Association_LabelText;
     private static final Image Association_Icon = createImage(LimeDiagram.class,
             SystemSettings.Association_Normal_Icon_Path); //$NON-NLS-1$
-
+    /**
+     * If attach source is called then find the parrent of the 
+     * port being connected, use its parent to find all the instances
+     * and attach source of them as well
+     */
     public void attachSource() {
-        if ((getSource() == null) ||
-                getSource().getSourceArcs().contains(this)) {
+        if ((source == null) ||
+                source.getSourceArcs().contains(this)) {
             return;
         }
-
+        // create associations for all its instances if it has any
         getSource().connectOutputAssociation(this);
     }
 
     protected String getNewID() {
         return DEFAULT_NAME + count++;
     }
+    
+    public void createAssociation(LimeSubpart sourceElement, LimeSubpart targetElement){
+        this.setSource(sourceElement);
+        //TODO: not safe but it is possible that they are in the same order
+        List<LimeSubpart> sourceInstances = sourceElement.getInstances();
+        List<LimeSubpart> targetInstances = targetElement.getInstances();
+        if (sourceInstances.size() != 0){
+        	for (int i = 0; i < sourceInstances.size(); i ++){
+        	Association newAssociation = new Association();
+        		newAssociation.createAssociation(sourceInstances.get(i), targetInstances.get(i)); 
+        	}
+        }
+        this.setTarget(targetElement);
+        this.setTargetTerminal(Port.CONNECTION_TERMINAL);
+        this.setSourceTerminal(Port.CONNECTION_TERMINAL);
+        this.attachSource();
+        this.attachTarget();
+    }
 }

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

 import nl.tudelft.lime.constants.LimeMessages;
 import nl.tudelft.lime.constants.SystemSettings;
 import nl.tudelft.lime.model.LimeSubpart;
+import nl.tudelft.lime.model.connection.Association;
 
 import org.eclipse.swt.graphics.Image;
 
 	public LimeSubpart getCopy() {
 		InputPort newPort = new InputPort();
 		//use reflection further
-		
 		newPort.portConstp = portConstp;
 		newPort.portRestrict = portRestrict;
 		newPort.portSize = portSize;
 		newPort.location = location;
 		newPort.size = size;
 		newPort.name = name;
+		addToInstances(newPort);
 		return newPort;
 	}
 

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

 	public LimeSubpart getCopy() {
 		OutputPort newPort = new OutputPort();
 		//use reflection further
-		
 		newPort.portConstp = portConstp;
 		newPort.portRestrict = portRestrict;
 		newPort.portSize = portSize;
 		newPort.location = location;
 		newPort.size = size;
 		newPort.name = name;
+		addToInstances(newPort);
 		return newPort;
 	}
 

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

                         setPortConst((String)value);
                 } else */
     	//if its parent is an instance it shouldn't be editable
-    	if (!((LimeComponent)this.parent).isInstance()){
+    	boolean isParentAnInstance = ((LimeComponent)this.parent).isInstance();
+		
+    	if ((!isParentAnInstance && changeFromUI) ||
+    		(isParentAnInstance && !changeFromUI)) 
+    		{
 	        if (Property_ID_PORT_CONSTP.equals(id)) {
 	            setPortConstp((Integer) value);
 	        } else if (Property_ID_PORT_RESTRICT.equals(id)) {

src/nl/tudelft/lime/xml/XMLFactory.java

                 Port port2 = findPort(diagram, toPort);
 
                 if ((port1 != null) && (port2 != null)) {
-                    association.setSource(port1);
-                    association.setTarget(port2);
-                    association.setTargetTerminal(Port.CONNECTION_TERMINAL);
-                    association.setSourceTerminal(Port.CONNECTION_TERMINAL);
-                    association.attachSource();
-                    association.attachTarget();
+                	association.createAssociation(port1, port2);
                 }
             } catch (Exception e) {
                 System.err.println(arcElement.asXML());
         // we keep the rule that we only write outgoing arcs,
         // so there are no duplicate entries
         if ((associations != null) && (associations.size() != 0)) {
-            root.add(getAssociationsXML(associations));
+        	//if it is an instance or a port with parent as an instance
+        	boolean isInstanceComponent = (limeSubpart instanceof LimeComponent) && ((LimeComponent) limeSubpart).isInstance();
+        	boolean hasInstanceParent = (limeSubpart instanceof Port) && ((LimeComponent)limeSubpart.getParent()).isInstance();
+        	//dont create the association
+        	if (!isInstanceComponent && !hasInstanceParent)
+        			root.add(getAssociationsXML(associations));
         }
 
         //add extra information which are kept as embeddedXML for an element
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.