ingenias.editor.models.NRGraphModelJGraph 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 over original code from Ruben Fuentes and Juan Pavon
*
* Modifications over original code from jgraph.sourceforge.net
*
* 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.models;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.Map;
import java.util.Hashtable;
import java.awt.Point;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Hashtable;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.URL;
import java.util.*;
import java.util.Hashtable;
import java.util.ArrayList;
import javax.swing.event.UndoableEditEvent;
import org.jgraph.JGraph;
import org.jgraph.graph.*;
import org.jgraph.event.*;
import java.util.Vector;
import org.jgraph.JGraph;
import org.jgraph.graph.*;
import org.jgraph.event.*;
import org.jgraph.plaf.basic.*;
import ingenias.editor.entities.*;
import ingenias.editor.cell.*;
import ingenias.editor.rendererxml.*;
import ingenias.editor.events.*;
import ingenias.exception.InvalidEntity;
import ingenias.editor.*;
import java.util.concurrent.TimeUnit;
public class NRGraphModelJGraph extends ModelJGraph {
private Preferences prefs;
public NRGraphModelJGraph(NRGraphDataEntity mde,
String nombre, ObjectManager om, Model
m, BasicMarqueeHandler mh, Preferences prefs) {
super(mde, nombre, m, mh,om);
this.prefs=prefs;
ToolTipManager.sharedInstance().registerComponent(this);
this.getModel().addGraphModelListener(new ChangeNARYEdgeLocation(this));
this.getModel().addGraphModelListener(new ChangeEntityLocation(this));
this.getGraphLayoutCache().setFactory(new ingenias.editor.cellfactories.NRGraphCellViewFactory());
}
//
// Adding Tooltips
//
// Return Cell Label as a Tooltip
public String getToolTipText(MouseEvent e) {
if (e != null) {
// Fetch Cell under Mousepointer
Object c = getFirstCellForLocation(e.getX(), e.getY());
if (c != null) {
// Convert Cell to String and Return
return convertValueToString(c);
}
}
return null;
}
public JToolBar getPaleta() {
return toolbar;
}
protected void creaToolBar() {
if (toolbar==null){
toolbar = new FilteredJToolBar("NRGraph");
toolbar.setFloatable(false);
ImageIcon undoIcon = null;
JButton jb = null;
if (true){
Image img_NRNode =
ImageLoader.getImage("images/mnode.png");
undoIcon = new ImageIcon(img_NRNode);
Action NRNode=
new AbstractAction("NRNode", undoIcon) {
public void actionPerformed(ActionEvent e) {
try{
insert(new Point(0, 0), "NRNode");
} catch (InvalidEntity e1) {
e1.printStackTrace();
}
}
};
NRNode.setEnabled(true);
jb = new JButton(NRNode){
@Override
public JToolTip createToolTip() {
return new ingenias.editor.editiondialog.JMultiLineToolTip();
}
};
jb.setText("");
jb.setName("NRNode");
jb.setToolTipText("NRNode:"+new NRNode("").getHelpDesc()+"\n\n"+new NRNode("").getHelpRecom());
jb.addMouseListener(new MouseAdapter() {
final int defaultDismissTimeout = ToolTipManager.sharedInstance().getDismissDelay();
final int dismissDelayMinutes = (int) TimeUnit.MINUTES.toMillis(10); // 10 minutes
@Override
public void mouseEntered(MouseEvent me) {
ToolTipManager.sharedInstance().setDismissDelay(dismissDelayMinutes);
}
@Override
public void mouseExited(MouseEvent me) {
ToolTipManager.sharedInstance().setDismissDelay(defaultDismissTimeout);
}
});
toolbar.add(jb);
}
}
}
public Vector getAllowedRelationships(){
Vector relationships=new Vector();
relationships.add("NREdge");
return relationships;
}
public Vector getAllowedEntities(){
Vector entities=new Vector();
entities.add("NRNode");
return entities;
}
// Gets the name of the possible relationships for the selected GraphCells.
// A relationship can be binary (DefaultEdge) or n-ary (NAryEdge).
// The requested action is slightly different depending on selected items.
// According to the number of Edges in selected, the action can be:
// 0 => Propose a relationship between selected according included classes.
// 1 and it is NAryEdge => The class of that NAryEdge if it is possible according implements java.io.Serializable
// current cardinality and included classes..
// other cases => Error, no relationships are allowed.
public Object[] getPossibleRelationships(GraphCell[] selected) {
// Possible relationships initialization.
Vector v = new Vector();
// Search for NAryEdges in selected.
int nAryEdgesNum = 0;
int edgesNum = 0;
NAryEdge selectedEdge = null;
for (int i = 0; i < selected.length; i++) {
if (selected[i] instanceof NAryEdge) {
nAryEdgesNum++;
selectedEdge = (NAryEdge) selected[i];
}
else if (selected[i] instanceof DefaultEdge) {
edgesNum++;
// Connections are only possible with two or more elements and without binary edges.
}
}
if (selected.length >= 2 && edgesNum == 0) {
// The number of NAryEdges is considered.
if (nAryEdgesNum == 0) {
// acceptConnection methods only admits vertex parameters.
// Binary relationships.
// N-ary relationships. Sometimes they can be also binary.
if (NREdgeEdge.acceptConnection(this.getModel(), selected)) {
v.add("NREdge");
}
}
else if (nAryEdgesNum == 1) {
if (selectedEdge instanceof NREdgeEdge &&
(NREdgeEdge.acceptConnection(this.getModel(), selected))) {
v.add("NREdge");
}
}
}
return v.toArray();
}
public DefaultGraphCell getInstanciaNRelacion(String relacion,
GraphCell[] selected) {
// Search for NAryEdges in selected.
int nAryEdgesNum = 0;
int edgesNum = 0;
NAryEdge selectedEdge = null;
for (int i = 0; i < selected.length; i++) {
if (selected[i] instanceof NAryEdge) {
nAryEdgesNum++;
selectedEdge = (NAryEdge) selected[i];
}
else if (selected[i] instanceof DefaultEdge) {
edgesNum++;
}
}
if (nAryEdgesNum <= 1 && edgesNum == 0) {
if (relacion.equalsIgnoreCase("NREdge")) {
// ResponsibleNEdge already exists.
if (nAryEdgesNum == 1 && selectedEdge instanceof NREdgeEdge) {
return selectedEdge;
}
// There is no NAryEdges in selected.
else if (nAryEdgesNum == 0) {
return new NREdgeEdge(new ingenias.editor.entities.NREdge(getMJGraph().getNewId()));
}
}
}
return null;
}
public DefaultGraphCell createCell(String entity) throws InvalidEntity{
if (entity.equalsIgnoreCase("NRNode")) {
NRNode nentity=getOM().createNRNode(getMJGraph().getNewId("NRNode"));
DefaultGraphCell vertex = new
NRNodeCell(nentity);
// Default Size for the cell with the new entity
return vertex;
}
else
throw new ingenias.exception.InvalidEntity("Entity type "+entity+" is not allowed in this diagram");
}
public Dimension getDefaultSize(Entity entity) throws InvalidEntity{
if (entity.getType().equalsIgnoreCase("NRNode")) {
return NRNodeView.getSize((ingenias.editor.entities.NRNode)entity);
}
else
if (entity.getType().equalsIgnoreCase("NREdge")) {
return NREdgeView.getSize((ingenias.editor.entities.NREdge)entity);
}
throw new ingenias.exception.InvalidEntity("Entity type "+entity+" is not allowed in this diagram");
}
public DefaultGraphCell insert(Point point, String entity) throws InvalidEntity {
// CellView information is not available after creating the cell.
// Create a Map that holds the attributes for the Vertex
Map map = new Hashtable();
// Snap the Point to the Grid
point = convert(this.snap(new Point(point)));
// Construct Vertex with no Label
DefaultGraphCell vertex;
Dimension size;
vertex=this.createCell(entity);
size=this.getDefaultSize((Entity)vertex.getUserObject());
// Add a Bounds Attribute to the Map
GraphConstants.setBounds(map, new Rectangle(point, size));
// Construct a Map from cells to Maps (for insert)
Hashtable attributes = new Hashtable();
// Associate the Vertex with its Attributes
attributes.put(vertex, map);
// Insert the Vertex and its Attributes
this.getModel().insert(new Object[] {vertex},attributes
, null, null, null);
Entity newEntity=(Entity) vertex.getUserObject();
if (prefs.getModelingLanguage()==Preferences.ModelingLanguage.UML)
newEntity.getPrefs(null).setView(ViewPreferences.ViewType.UML);
if (prefs.getModelingLanguage()==Preferences.ModelingLanguage.INGENIAS)
newEntity.getPrefs(null).setView(ViewPreferences.ViewType.INGENIAS);
getGraphLayoutCache().setVisible(vertex,true);// makes the cell visible because
// the graphlayoutcache has partial set to true
return vertex;
}
public DefaultGraphCell insertDuplicated(Point point, ingenias.editor.entities.Entity
entity) {
// CellView information is not available after creating the cell.
// Create a Map that holds the attributes for the Vertex
Map map =new Hashtable();
// Snap the Point to the Grid
point = convert(this.snap(new Point(point)));
// Construct Vertex with no Label
DefaultGraphCell vertex = null;
Dimension size = null;
if (entity.getClass().equals(NRNode.class)) {
vertex = new NRNodeCell( (NRNode) entity);
// Default Size for the new Vertex with the new entity within
size = NRNodeView.getSize((NRNode) entity);
}
else
{}; // Just in case there is no allowed entity in the diagram
if (vertex == null) {
JOptionPane.showMessageDialog(this,
"Object not allowed in this diagram "+this.getID()+":"+
entity.getId()+":"+entity.getClass().getName()+
this.getClass().getName(),"Warning", JOptionPane.WARNING_MESSAGE); }
else {
// Add a Bounds Attribute to the Map
GraphConstants.setBounds(map, new Rectangle(point, size));
// Construct a Map from cells to Maps (for insert)
Hashtable attributes = new Hashtable();
// Associate the Vertex with its Attributes
attributes.put(vertex, map);
// Insert the Vertex and its Attributes
this.getModel().insert(new Object[] {vertex},attributes
, null, null, null);
getGraphLayoutCache().setVisible(vertex,true);// makes the cell visible because
// the graphlayoutcache has partial set to true
// waits for the cellview to be created
boolean created=false;
VertexView vv=null;
while (!created){
CellView[] cellviews = this.getGraphLayoutCache().getCellViews();
for (CellView cv:cellviews){
if (cv.getCell()==vertex){
// created!
created=true;
vv=(VertexView)cv;
}
}
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// This should be used when the corresponding view for the entity has containers!
// It can be known by inspecting its renderer. To get it, a cellview is needed,
// but the cellview is created after a while via succesive callbacks to
// the view factories
if (!ListenerContainer.evaluate((CompositeRenderer) vv.getRenderer(),entity,null).isEmpty()){
// there are container renderers that need new cells corresponding to children to
// be inserted
Hashtable renderers = ListenerContainer.evaluate((CompositeRenderer) vv.getRenderer(),entity,null);
for (String field:renderers.keySet()){
Method obtainenumeration;
try {
obtainenumeration = entity.getClass().getMethod("get"+field+"Elements");
Enumeration enom=(Enumeration) obtainenumeration.invoke(entity,new Object[]{});
while (enom.hasMoreElements()){
DefaultGraphCell child=this.insertDuplicated(new Point(40,10), enom.nextElement());
try {
getListenerContainer().setParent(child,vertex);
} catch (WrongParent e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return vertex;
}
public synchronized JGraph cloneJGraph(IDEState ids){
NRGraphModelJGraph jg=new NRGraphModelJGraph(
(NRGraphDataEntity) this.mde,name, ids.om,
new Model(ids),new BasicMarqueeHandler(),ids.prefs);
this.setSelectionCells(getGraphLayoutCache().getCells(false,true,false,false));
Action copyaction =new EventRedirectorForGraphCopy(this,this.getTransferHandler().getCopyAction(),null );
Action pasteaction =new EventRedirectorPasteForGraphCopy(jg,jg.getTransferHandler().getPasteAction(),null );
copyaction.actionPerformed(new ActionEvent(this,0,"hello"));
pasteaction.actionPerformed(new ActionEvent(this,0,"hello"));
jg.invalidate();
jg.doLayout();
return jg;
}
public String toString() {
return this.getID();
}
}