All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ingenias.editor.ObjectManager Maven / Gradle / Ivy

Go to download

Meta-language editor built over INGENME. It is the most basic editor to be used in the rest of projects

There is a newer version: 1.0.9
Show newest version



/** 
 * Copyright (C) 2010  Jorge J. Gomez-Sanz
 * 
 * 
 * This file is part of the INGENME tool. INGENME is an open source meta-editor
 * which produces customized editors for user-defined modeling languages
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation version 3 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see 
 **/

package ingenias.editor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import org.jgraph.JGraph;
import org.jgraph.graph.*;
import javax.swing.tree.*;
import javax.swing.JTree;
import ingenias.editor.entities.*;

public class ObjectManager extends javax.swing.tree.DefaultMutableTreeNode implements java.io.Serializable {

 

 public  JTree arbolObjetos=null;

  javax.swing.tree.DefaultMutableTreeNode root=new javax.swing.tree.DefaultMutableTreeNode("SystemObjects");




  public javax.swing.tree.DefaultMutableTreeNode PropertyFieldNode=null;

  public javax.swing.tree.DefaultMutableTreeNode MetaObjectTypeWrapperNode=null;

  public javax.swing.tree.DefaultMutableTreeNode MetaDiagramNode=null;

  public javax.swing.tree.DefaultMutableTreeNode ExternalTypeWrapperNode=null;

  public javax.swing.tree.DefaultMutableTreeNode MetaRelationshipNode=null;

  public javax.swing.tree.DefaultMutableTreeNode DefaultValueNode=null;

  public javax.swing.tree.DefaultMutableTreeNode PropertyOrderNode=null;

  public javax.swing.tree.DefaultMutableTreeNode DocumentationNode=null;

  public javax.swing.tree.DefaultMutableTreeNode PreferredOrderNode=null;

  public javax.swing.tree.DefaultMutableTreeNode MetaObjectNode=null;

  public javax.swing.tree.DefaultMutableTreeNode BasicRepresentationNode=null;

  public javax.swing.tree.DefaultMutableTreeNode VisualRepresentationNode=null;

  public javax.swing.tree.DefaultMutableTreeNode TypeWrapperNode=null;

  public javax.swing.tree.DefaultMutableTreeNode UMLCommentNode=null;

  public javax.swing.tree.DefaultMutableTreeNode MetaRoleNode=null;

  public javax.swing.tree.DefaultMutableTreeNode MetaDiagramTypeWrapperNode=null;



  public static ObjectManager initialise(javax.swing.tree.DefaultMutableTreeNode root,JTree arbolObjetos){
  ObjectManager om=new ObjectManager(root,arbolObjetos);
   return om;
  }

  private ObjectManager(javax.swing.tree.DefaultMutableTreeNode root,JTree arbolObjetos) {
     super("System Objects");
     this.root=root;
     this.arbolObjetos=arbolObjetos;

  PropertyFieldNode=new javax.swing.tree.DefaultMutableTreeNode("PropertyField");

  MetaObjectTypeWrapperNode=new javax.swing.tree.DefaultMutableTreeNode("MetaObjectTypeWrapper");

  MetaDiagramNode=new javax.swing.tree.DefaultMutableTreeNode("MetaDiagram");

  ExternalTypeWrapperNode=new javax.swing.tree.DefaultMutableTreeNode("ExternalTypeWrapper");

  MetaRelationshipNode=new javax.swing.tree.DefaultMutableTreeNode("MetaRelationship");

  DefaultValueNode=new javax.swing.tree.DefaultMutableTreeNode("DefaultValue");

  PropertyOrderNode=new javax.swing.tree.DefaultMutableTreeNode("PropertyOrder");

  DocumentationNode=new javax.swing.tree.DefaultMutableTreeNode("Documentation");

  PreferredOrderNode=new javax.swing.tree.DefaultMutableTreeNode("PreferredOrder");

  MetaObjectNode=new javax.swing.tree.DefaultMutableTreeNode("MetaObject");

  BasicRepresentationNode=new javax.swing.tree.DefaultMutableTreeNode("BasicRepresentation");

  VisualRepresentationNode=new javax.swing.tree.DefaultMutableTreeNode("VisualRepresentation");

  TypeWrapperNode=new javax.swing.tree.DefaultMutableTreeNode("TypeWrapper");

  UMLCommentNode=new javax.swing.tree.DefaultMutableTreeNode("UMLComment");

  MetaRoleNode=new javax.swing.tree.DefaultMutableTreeNode("MetaRole");

  MetaDiagramTypeWrapperNode=new javax.swing.tree.DefaultMutableTreeNode("MetaDiagramTypeWrapper");






     // 1st level nodes

     addNodeInSortedOrder( root,PropertyFieldNode);

     addNodeInSortedOrder( root,MetaDiagramNode);

     addNodeInSortedOrder( root,MetaRelationshipNode);

     addNodeInSortedOrder( root,DefaultValueNode);

     addNodeInSortedOrder( root,PropertyOrderNode);

     addNodeInSortedOrder( root,DocumentationNode);

     addNodeInSortedOrder( root,PreferredOrderNode);

     addNodeInSortedOrder( root,MetaObjectNode);

     addNodeInSortedOrder( root,BasicRepresentationNode);

     addNodeInSortedOrder( root,VisualRepresentationNode);

     addNodeInSortedOrder( root,TypeWrapperNode);

     addNodeInSortedOrder( root,UMLCommentNode);

     addNodeInSortedOrder( root,MetaRoleNode);


     // 2nd and lower nodes

    addNodeInSortedOrder( TypeWrapperNode,MetaObjectTypeWrapperNode);

    addNodeInSortedOrder( TypeWrapperNode,ExternalTypeWrapperNode);

    addNodeInSortedOrder( TypeWrapperNode,MetaDiagramTypeWrapperNode);

  }

// Function is a contribution from Ike http://www.groupsrv.com/computers/about116987.html
	private void addNodeInSortedOrder(DefaultMutableTreeNode parent,
			DefaultMutableTreeNode child){
		int n = parent.getChildCount();
		if(n==0){
			parent.add(child);
			return;
		}
		DefaultMutableTreeNode node=null;
		for(int i=0;i0){
				parent.insert(child, i);
				return;
			}
		}
		parent.add(child);
		return;
	}


  
  public PropertyField createPropertyField(String id){
    PropertyField object=new     PropertyField(id);
    DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
    PropertyFieldNode.insert(nn, PropertyFieldNode.getChildCount());
    nn.setParent(PropertyFieldNode);
    this.reload();
    arbolObjetos.repaint();
    return object;
  }

  public Object getPropertyField(String object){
    Object o=findUserObject(PropertyFieldNode,object);
    return o;
  }
  
  public MetaObjectTypeWrapper createMetaObjectTypeWrapper(String id){
    MetaObjectTypeWrapper object=new     MetaObjectTypeWrapper(id);
    DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
    MetaObjectTypeWrapperNode.insert(nn, MetaObjectTypeWrapperNode.getChildCount());
    nn.setParent(MetaObjectTypeWrapperNode);
    this.reload();
    arbolObjetos.repaint();
    return object;
  }

  public Object getMetaObjectTypeWrapper(String object){
    Object o=findUserObject(MetaObjectTypeWrapperNode,object);
    return o;
  }
  
  public MetaDiagram createMetaDiagram(String id){
    MetaDiagram object=new     MetaDiagram(id);
    DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
    MetaDiagramNode.insert(nn, MetaDiagramNode.getChildCount());
    nn.setParent(MetaDiagramNode);
    this.reload();
    arbolObjetos.repaint();
    return object;
  }

  public Object getMetaDiagram(String object){
    Object o=findUserObject(MetaDiagramNode,object);
    return o;
  }
  
  public ExternalTypeWrapper createExternalTypeWrapper(String id){
    ExternalTypeWrapper object=new     ExternalTypeWrapper(id);
    DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
    ExternalTypeWrapperNode.insert(nn, ExternalTypeWrapperNode.getChildCount());
    nn.setParent(ExternalTypeWrapperNode);
    this.reload();
    arbolObjetos.repaint();
    return object;
  }

  public Object getExternalTypeWrapper(String object){
    Object o=findUserObject(ExternalTypeWrapperNode,object);
    return o;
  }
  
  public MetaRelationship createMetaRelationship(String id){
    MetaRelationship object=new     MetaRelationship(id);
    DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
    MetaRelationshipNode.insert(nn, MetaRelationshipNode.getChildCount());
    nn.setParent(MetaRelationshipNode);
    this.reload();
    arbolObjetos.repaint();
    return object;
  }

  public Object getMetaRelationship(String object){
    Object o=findUserObject(MetaRelationshipNode,object);
    return o;
  }
  
  public DefaultValue createDefaultValue(String id){
    DefaultValue object=new     DefaultValue(id);
    DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
    DefaultValueNode.insert(nn, DefaultValueNode.getChildCount());
    nn.setParent(DefaultValueNode);
    this.reload();
    arbolObjetos.repaint();
    return object;
  }

  public Object getDefaultValue(String object){
    Object o=findUserObject(DefaultValueNode,object);
    return o;
  }
  
  public PropertyOrder createPropertyOrder(String id){
    PropertyOrder object=new     PropertyOrder(id);
    DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
    PropertyOrderNode.insert(nn, PropertyOrderNode.getChildCount());
    nn.setParent(PropertyOrderNode);
    this.reload();
    arbolObjetos.repaint();
    return object;
  }

  public Object getPropertyOrder(String object){
    Object o=findUserObject(PropertyOrderNode,object);
    return o;
  }
  
  public Documentation createDocumentation(String id){
    Documentation object=new     Documentation(id);
    DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
    DocumentationNode.insert(nn, DocumentationNode.getChildCount());
    nn.setParent(DocumentationNode);
    this.reload();
    arbolObjetos.repaint();
    return object;
  }

  public Object getDocumentation(String object){
    Object o=findUserObject(DocumentationNode,object);
    return o;
  }
  
  public PreferredOrder createPreferredOrder(String id){
    PreferredOrder object=new     PreferredOrder(id);
    DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
    PreferredOrderNode.insert(nn, PreferredOrderNode.getChildCount());
    nn.setParent(PreferredOrderNode);
    this.reload();
    arbolObjetos.repaint();
    return object;
  }

  public Object getPreferredOrder(String object){
    Object o=findUserObject(PreferredOrderNode,object);
    return o;
  }
  
  public MetaObject createMetaObject(String id){
    MetaObject object=new     MetaObject(id);
    DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
    MetaObjectNode.insert(nn, MetaObjectNode.getChildCount());
    nn.setParent(MetaObjectNode);
    this.reload();
    arbolObjetos.repaint();
    return object;
  }

  public Object getMetaObject(String object){
    Object o=findUserObject(MetaObjectNode,object);
    return o;
  }
  
  public BasicRepresentation createBasicRepresentation(String id){
    BasicRepresentation object=new     BasicRepresentation(id);
    DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
    BasicRepresentationNode.insert(nn, BasicRepresentationNode.getChildCount());
    nn.setParent(BasicRepresentationNode);
    this.reload();
    arbolObjetos.repaint();
    return object;
  }

  public Object getBasicRepresentation(String object){
    Object o=findUserObject(BasicRepresentationNode,object);
    return o;
  }
  
  public VisualRepresentation createVisualRepresentation(String id){
    VisualRepresentation object=new     VisualRepresentation(id);
    DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
    VisualRepresentationNode.insert(nn, VisualRepresentationNode.getChildCount());
    nn.setParent(VisualRepresentationNode);
    this.reload();
    arbolObjetos.repaint();
    return object;
  }

  public Object getVisualRepresentation(String object){
    Object o=findUserObject(VisualRepresentationNode,object);
    return o;
  }
  
  public UMLComment createUMLComment(String id){
    UMLComment object=new     UMLComment(id);
    DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
    UMLCommentNode.insert(nn, UMLCommentNode.getChildCount());
    nn.setParent(UMLCommentNode);
    this.reload();
    arbolObjetos.repaint();
    return object;
  }

  public Object getUMLComment(String object){
    Object o=findUserObject(UMLCommentNode,object);
    return o;
  }
  
  public MetaRole createMetaRole(String id){
    MetaRole object=new     MetaRole(id);
    DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
    MetaRoleNode.insert(nn, MetaRoleNode.getChildCount());
    nn.setParent(MetaRoleNode);
    this.reload();
    arbolObjetos.repaint();
    return object;
  }

  public Object getMetaRole(String object){
    Object o=findUserObject(MetaRoleNode,object);
    return o;
  }
  
  public MetaDiagramTypeWrapper createMetaDiagramTypeWrapper(String id){
    MetaDiagramTypeWrapper object=new     MetaDiagramTypeWrapper(id);
    DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
    MetaDiagramTypeWrapperNode.insert(nn, MetaDiagramTypeWrapperNode.getChildCount());
    nn.setParent(MetaDiagramTypeWrapperNode);
    this.reload();
    arbolObjetos.repaint();
    return object;
  }

  public Object getMetaDiagramTypeWrapper(String object){
    Object o=findUserObject(MetaDiagramTypeWrapperNode,object);
    return o;
  }
  

   public static Vector getValidEntitiesClasses(){
    Vector result=new Vector();

    result.add(PropertyField.class);

    result.add(MetaObjectTypeWrapper.class);

    result.add(MetaDiagram.class);

    result.add(ExternalTypeWrapper.class);

    result.add(MetaRelationship.class);

    result.add(DefaultValue.class);

    result.add(PropertyOrder.class);

    result.add(Documentation.class);

    result.add(PreferredOrder.class);

    result.add(MetaObject.class);

    result.add(BasicRepresentation.class);

    result.add(VisualRepresentation.class);

    result.add(TypeWrapper.class);

    result.add(UMLComment.class);

    result.add(MetaRole.class);

    result.add(MetaDiagramTypeWrapper.class);


    result.add(MetamodelModelEntity.class);

    return result;
  }


  public void reload(){
    Enumeration expanded=arbolObjetos.getExpandedDescendants(new TreePath(root.getPath()));
    ((DefaultTreeModel)arbolObjetos.getModel()).reload();
    while (expanded!=null && expanded.hasMoreElements()){
     TreePath tp=(TreePath)expanded.nextElement();

     arbolObjetos.expandPath(tp);
    }

  }

 public Entity getEntity(String id,String type){
   Object o=this.findUserObjectInTree(root,id,type);
   return (Entity)o;
  }

   public Vector getAllObjects(){
    Vector result=new Vector();
    javax.swing.tree.DefaultMutableTreeNode dfn=this.root.getFirstLeaf();
    while (dfn!=null){
      TreeNode[] path=dfn.getPath();
      Object uo=((DefaultMutableTreeNode)(path[path.length-1])).getUserObject();
      if (uo instanceof Entity)
        result.add(uo);
      dfn=dfn.getNextLeaf();
    }
    return result;
  }




  private Object findUserObject(DefaultMutableTreeNode dtn,String name){
		if (dtn.getChildCount()==0)
			return null;
		DefaultMutableTreeNode node=(DefaultMutableTreeNode)dtn.getFirstChild();
		while (node!=null){
			if (node.getUserObject() instanceof    ingenias.editor.entities.Entity){
				ingenias.editor.entities.Entity uo=(ingenias.editor.entities.Entity)node.getUserObject();
				if (uo.getId().equalsIgnoreCase(name))
					return uo;
			}
			node=node.getNextNode();
		}
		return null;
	}

 public Vector findUserObject(String name){
  	Vector found=new Vector();
  	DefaultMutableTreeNode node= root.getFirstLeaf();
    while (node!=null){
       if (ingenias.editor.entities.Entity.class.isAssignableFrom(node.getUserObject().getClass())){
      ingenias.editor.entities.Entity uo=(ingenias.editor.entities.Entity)node.getUserObject();
      if (uo.getId().equalsIgnoreCase(name))              	
      	found.add(uo);        
       }
        node=node.getNextLeaf();
       
    }
    return found;
  }

  private Object findUserObjectInTree(DefaultMutableTreeNode dtn,String name,String type){
   DefaultMutableTreeNode node= root.getFirstLeaf();
   while (node!=null){
      if (ingenias.editor.entities.Entity.class.isAssignableFrom(node.getUserObject().getClass())){
     ingenias.editor.entities.Entity uo=(ingenias.editor.entities.Entity)node.getUserObject();
     if (uo.getId().equalsIgnoreCase(name) &&
       uo.getClass().getName().indexOf(type)!=-1)
       return uo;
       }
       node=node.getNextLeaf();
     }
   return null;
  }

 public DefaultMutableTreeNode findNodeInTree(DefaultMutableTreeNode dtn,String name,String type){
 DefaultMutableTreeNode node= root.getFirstLeaf();
 while (node!=null){
    if (ingenias.editor.entities.Entity.class.isAssignableFrom(node.getUserObject().getClass())){
   ingenias.editor.entities.Entity uo=(ingenias.editor.entities.Entity)node.getUserObject();
   if (uo.getId().equalsIgnoreCase(name) )//     uo.getClass().getName().indexOf(type)!=-1)
     return node;
     }
     node=node.getNextLeaf();
   }
 return null;
 }

 private void findInstancesInTree(DefaultMutableTreeNode dtn,Class type, Vector result){
    DefaultMutableTreeNode node= root.getFirstLeaf();
    while (node!=null){
      String tcand=node.getUserObject().getClass().getName();
      if (type.isInstance(node.getUserObject()))
          result.add(node.getUserObject());
      node=node.getNextLeaf();

    }
  }
  
    public Vector findUserObjectPathRegexp(String nameregexp){
	  	Vector found=new Vector();
	  	DefaultMutableTreeNode node= root.getFirstLeaf();
	    while (node!=null){
	       if (ingenias.editor.entities.Entity.class.isAssignableFrom(node.getUserObject().getClass())){
	      ingenias.editor.entities.Entity uo=(ingenias.editor.entities.Entity)node.getUserObject();
	      if (java.util.regex.Pattern.matches(nameregexp.toLowerCase(),uo.getId().toLowerCase()))              	
	      	found.add(new TreePath(node.getPath()));        
	       }
	        node=node.getNextLeaf();       
	    }
	    return found;
	  }

 public Vector getInstances(String type){
    int index=type.lastIndexOf(".");
    String className=type.substring(index+1,type.length());
    Vector result=new Vector();
    
    try {
		this.findInstancesInTree(root,Class.forName(type),result);
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    return result;
  }

  public static Vector getInheritors(Class type){
    Vector validClasses=getValidEntitiesClasses();
    Vector result=new Vector();
    Enumeration enumeration=validClasses.elements();
    while (enumeration.hasMoreElements()){
      Class current=(Class)enumeration.nextElement();
      if (type.isAssignableFrom(current) && !type.equals(current))
       result.add(current);
    }
    return result;
  }


  

  public void replaceReferencesOM(Vector entities,Entity oldent, Entity newent)
    throws java.lang.IllegalAccessException{
    Enumeration enumeration=entities.elements();
    while (enumeration.hasMoreElements()){
      Entity current=(Entity)enumeration.nextElement();
      this.replaceReferencesOM(current,oldent,newent);
    }
  }

  private void replaceReferencesOM(Entity current,Entity oldent, Entity newent)
   throws java.lang.IllegalAccessException{
   java.lang.reflect.Field[] fs=current.getClass().getDeclaredFields();
   for (int k=0;k




© 2015 - 2025 Weber Informatics LLC | Privacy Policy