ingenias.editor.GraphManager Maven / Gradle / Ivy
/**
* 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.util.*;
import org.jgraph.JGraph;
import org.jgraph.graph.*;
import javax.swing.tree.*;
import javax.swing.JTree;
import ingenias.editor.entities.NAryEdgeEntity;
import ingenias.editor.widget.DnDJTree;
import ingenias.exception.NotInitialised;
import ingenias.generator.browser.BrowserImp;
public class GraphManager implements java.io.Serializable {
public javax.swing.tree.DefaultMutableTreeNode root=null;
public DnDJTree arbolProyecto=null;
// Hashtable models=new Hashtable();
JGraph current;
public Vector toExpad=new Vector();
private int idCounter;
public GraphManager(javax.swing.tree.DefaultMutableTreeNode root, DnDJTree arbolProyecto) {
this.root=root;
this.arbolProyecto=arbolProyecto;
}
public void addModel(Object[]path, String nombre, JGraph model1){
DefaultMutableTreeNode dmn=this.getPath(path);
if (dmn!=null){
DefaultMutableTreeNode nn=new DefaultMutableTreeNode(model1);
dmn.insert(nn,dmn.getChildCount());
nn.setParent(dmn);
// this.models.put(pathToString(path)+","+nombre,model1);
this.reload();
}
}
public void addPackage(Object[]path, String nombre){
DefaultMutableTreeNode dmn=this.getPath(path);
boolean found=false;
if (dmn!=null && dmn.getChildCount()!=0){
DefaultMutableTreeNode node=(DefaultMutableTreeNode)dmn.getFirstChild();
while (node!=null && !found){
//System.err.println("mirando "+node.getUserObject().toString());
if (node.getUserObject().toString().equalsIgnoreCase(nombre)){
/*System.err.print("nodo "+nombre+" encontrado en ");
for (int k=0;k getCell(JGraph model, Object o){
Object[] roots=model.getRoots();
Vector results=new Vector();
for (int k=0;k dgcs=this.getCell(jg,ent);
if (!dgcs.isEmpty()){
Object[] cells=dgcs.toArray();
//cells = ButtonToolBar.this.editor.graph.getDescendants(cells);
jg.getGraphLayoutCache().remove(cells,true,true);
jg.getModel().remove(cells);
//removeConnectedEdges(jg,dgc);
//dgc.removeAllChildren();
//cells=jg.getDescendants(cells);
//jg.getGraphLayoutCache().remove(cells,true,true);
}
}
}
public Vector getModels(javax.swing.tree.DefaultMutableTreeNode root){
Vector result=new Vector();
if (root.getChildCount()>0){
javax.swing.tree.DefaultMutableTreeNode dfn=(DefaultMutableTreeNode) root.getFirstChild();
while (dfn!=null){
TreeNode[] path=dfn.getPath();
Object uo=((DefaultMutableTreeNode)(path[path.length-1])).getUserObject();
if (uo instanceof ModelJGraph)
result.add(path);
result.addAll(getModels(dfn));
dfn=dfn.getNextSibling();
}
}
return result;
}
public Vector getModels(){
return getModels(this.root);
}
public Vector getUOModels(javax.swing.tree.DefaultMutableTreeNode root){
Vector result=new Vector();
int k=0;
while ( k getUOModels(){
Vector result=new Vector();
javax.swing.tree.DefaultMutableTreeNode dfn=this.root;
return getUOModels(this.root);
}
public boolean isDuplicated(String id){
Enumeration enumeration=this.getUOModels().elements();
int found=0;
while (enumeration.hasMoreElements() && found<2){
ModelJGraph mjg=(ModelJGraph)enumeration.nextElement();
if (mjg.getID().equalsIgnoreCase(id))
found++;
}
return found>=2;
}
public Vector getLeafPackages(){
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 ModelJGraph))
result.add(path);
dfn=dfn.getNextLeaf();
}
return result;
}
public boolean existsModel(String id){
Vector models=this.getUOModels();
Enumeration enumeration=models.elements();
boolean found=false;
while (enumeration.hasMoreElements() && !found){
ModelJGraph mjg=(ModelJGraph)enumeration.nextElement();
//System.err.println(mjg.getID());
found=mjg.getID()!=null && mjg.getID().equalsIgnoreCase(id);
}
return found;
}
public void createPath(Object[] path){
}
/*public static GraphManager createIndependentCopy(javax.swing.tree.DefaultMutableTreeNode root, DnDJTree arbolProyecto){
GraphManager instance=new GraphManager(root,arbolProyecto);
return instance;
}*/
/*public static void updateCopy(GraphManager copygm){
instance=copygm;
}*/
/*public static GraphManager getInstance(){
if (instance==null)
throw new RuntimeException("There is no graph manager instance initialized");
return instance;
}*/
public static GraphManager initInstance(javax.swing.tree.DefaultMutableTreeNode root, DnDJTree arbolProyecto){
GraphManager instance=new GraphManager(root,arbolProyecto);
return instance;
}
private void findInstancesInTree(DefaultMutableTreeNode dtn,String type, Vector result){
Enumeration enumeration=this.getUOModels().elements();
while (enumeration.hasMoreElements()){
ModelJGraph model=(ModelJGraph)enumeration.nextElement();
int index=model.getClass().getName().lastIndexOf(".");
String className=model.getClass().getName().substring(index+1,model.getClass().getName().length());
if (className.equalsIgnoreCase(type+"modeljgraph"))
result.add(model.getID());
}
}
public Vector getInstances(String type){
int index=type.lastIndexOf(".");
String className=type.substring(index+1,type.length());
Vector result=new Vector();
this.findInstancesInTree(root,className,result);
return result;
}
public String[] getModelPath(String id) throws ingenias.exception.NotFound{
String[] result=null;
boolean found=false;
javax.swing.tree.DefaultMutableTreeNode dfn=this.root.getFirstLeaf();
Object uo=null;
TreeNode[] path=null;
while (dfn!=null && !found){
path=dfn.getPath();
uo=((DefaultMutableTreeNode)(path[path.length-1])).getUserObject();
found= (uo instanceof ModelJGraph) &&
(((ModelJGraph)uo).getID().equals(id));
dfn=dfn.getNextLeaf();
}
if (found){
result=new String[path.length];
for (int k=0;k postOrderEnumeration= root.postorderEnumeration();
while (found==null && postOrderEnumeration.hasMoreElements()){
DefaultMutableTreeNode current=postOrderEnumeration.nextElement();
if (ModelJGraph.class.isAssignableFrom(current.getUserObject().getClass())){
ModelJGraph uo=(ModelJGraph)current.getUserObject();
if (nameregexp.toLowerCase().equals(uo.getName().toLowerCase()))
found=new TreePath(current.getPath());
}
if (String.class.isAssignableFrom(current.getUserObject().getClass())){
if (nameregexp.toLowerCase().equals(current.getUserObject().toString().toLowerCase()))
found=new TreePath(current.getPath());
}
}
return found;
}
public DnDJTree getArbolProyecto() {
return arbolProyecto;
}
public void setArbolProyecto(DnDJTree arbolProyecto) {
this.arbolProyecto = arbolProyecto;
}
}