ingenias.editor.ObjectManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nodereled Show documentation
Show all versions of nodereled Show documentation
A simple node-relationship editor
/**
* 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 NRNodeNode=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;
NRNodeNode=new javax.swing.tree.DefaultMutableTreeNode("NRNode");
// 1st level nodes
addNodeInSortedOrder( root,NRNodeNode);
// 2nd and lower nodes
}
// 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 NRNode createNRNode(String id){
NRNode object=new NRNode(id);
DefaultMutableTreeNode nn=new DefaultMutableTreeNode(object);
NRNodeNode.insert(nn, NRNodeNode.getChildCount());
nn.setParent(NRNodeNode);
this.reload();
arbolObjetos.repaint();
return object;
}
public Object getNRNode(String object){
Object o=findUserObject(NRNodeNode,object);
return o;
}
public static Vector getValidEntitiesClasses(){
Vector result=new Vector();
result.add(NRNode.class);
result.add(NRGraphModelEntity.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