ingenias.editor.MarqueeHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sociaalmled Show documentation
Show all versions of sociaalmled Show documentation
A editor for modelling scenarios for PHAT simulator
/**
* Copyright (C) 2010 Jorge J. Gomez-Sanz, Ruben Fuentes
*
* 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 ingenias.editor.CommonMenuEntriesActionFactory;
import ingenias.editor.actions.HyperlinkAction;
import ingenias.editor.cell.NAryEdge;
import ingenias.editor.editiondialog.MyJLabel;
import ingenias.editor.entities.Entity;
import ingenias.editor.entities.RoleEntity;
import ingenias.editor.widget.GraphicsUtils;
import ingenias.exception.NotInitialised;
import ingenias.exception.NullEntity;
import ingenias.generator.browser.BrowserImp;
import ingenias.generator.browser.Graph;
import ingenias.generator.browser.GraphEntity;
import ingenias.generator.browser.GraphRelationship;
import ingenias.generator.browser.GraphRelationshipImp;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.event.*;
import javax.swing.event.HyperlinkEvent.EventType;
import java.awt.event.*;
import java.util.Map;
import java.util.Hashtable;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Hashtable;
import java.util.ArrayList;
import java.util.Set;
import javax.swing.event.UndoableEditEvent;
import javax.swing.tree.TreePath;
import org.jgraph.JGraph;
import org.jgraph.graph.*;
import org.jgraph.event.*;
import java.util.Vector;
import java.util.concurrent.TimeUnit;
import org.jgraph.JGraph;
import org.jgraph.graph.*;
import org.jgraph.event.*;
import org.jgraph.plaf.basic.*;
//MarqueeHandler that Connects Vertices and Displays PopupMenus
public class MarqueeHandler extends BasicMarqueeHandler implements java.io.Serializable {
/* public Preferences getPref() {
return pref;
}*/
// Holds the Start and the Current Point
/**
* Description of the Field
*/
protected Point start, current;
// Holds the First and the Current Port
/**
* Description of the Field
*/
protected PortView port, firstPort;
private ModelJGraph graph;
private Vector additionalActions=new Vector();
private CommonMenuEntriesActionFactory af;
private DiagramMenuEntriesActionsFactory daf;
private GUIResources resources;
private IDEState ids;
private Rectangle oldrect;
public MarqueeHandler(ModelJGraph graph,
GUIResources resources,
IDEState ids, DiagramMenuEntriesActionsFactory daf){
this.graph=graph;
this.af=new CommonMenuEntriesActionFactory(resources,ids);
this.daf=daf;
this.resources=resources;
this.ids=ids;
}
public MarqueeHandler(ModelJGraph graph,
IDEState ids, DiagramMenuEntriesActionsFactory daf){
this.graph=graph;
this.daf=daf;
this.af=new CommonMenuEntriesActionFactory(null,ids);
this.ids=ids;
}
public void addContextualMenuAction(AbstractAction action){
additionalActions.add(action);
}
protected Point convert(java.awt.geom.Point2D p2d){
Point p=new Point((int)p2d.getX(),(int)p2d.getY());
return p;
}
public static DefaultGraphCell getOtherExtremeFromAryEdge(GraphModel m,DefaultEdge de){
DefaultPort sourcePort = (DefaultPort) ( (Edge) de).getSource();
Object source = m.getParent(sourcePort);
Port targetPort = (Port) ( (Edge) de).getTarget();
Object target = m.getParent(targetPort);
if (!(source instanceof NAryEdge)) {
return (DefaultGraphCell) source;
}
if (!(target instanceof NAryEdge)) {
return (DefaultGraphCell) target;
}
return null;
}
public static ingenias.editor.cell.NAryEdge getNAryEdge(GraphModel m,DefaultEdge de){
DefaultPort sourcePort = (DefaultPort) ( (Edge) de).getSource();
Object source = m.getParent(sourcePort);
Port targetPort = (Port) ( (Edge) de).getTarget();
Object target = m.getParent(targetPort);
if (source instanceof NAryEdge) {
return (NAryEdge) source;
}
if (target instanceof NAryEdge) {
return (NAryEdge) target;
}
return null;
}
// Find a Cell at point and Return its first Port as a PortView
/**
* Gets the targetPortAt attribute of the MarqueeHandler object
*
*@param point Description of Parameter
*@return The targetPortAt value
*/
protected PortView getTargetPortAt(Point point) {
// Find Cell at point (No scaling needed here)
Object cell = getGraph().getFirstCellForLocation(point.x, point.y);
// Loop Children to find PortView
for (int i = 0; i < getGraph().getModel().getChildCount(cell); i++) {
// Get Child from Model
Object tmp = getGraph().getModel().getChild(cell, i);
// Get View for Child using the Graph's View as a Cell Mapper
tmp = getGraph().getGraphLayoutCache().getMapping(tmp, false);
// If Child View is a Port View and not equal to First Port
if (tmp instanceof PortView && tmp != firstPort) {
// Return as PortView
return (PortView) tmp;
}
}
// No Port View found
return getSourcePortAt(point);
}
// Use Xor-Mode on getGraph()ics to Paint Connector
/**
* Description of the Method
*
*@param fg Description of Parameter
*@param bg Description of Parameter
*@param g Description of Parameter
*/
protected void paintConnector(Color fg, Color bg, Graphics g) {
// Set Foreground
g.setColor(fg);
// Set Xor-Mode Color
//
// Highlight the Current Port
paintPort(getGraph().getGraphics());
// If Valid First Port, Start and Current Point
if (firstPort != null && start != null && current != null) {
// Then Draw A Line From Start to Current Point
g.setXORMode(bg);
g.drawLine(start.x, start.y, current.x, current.y);
g.setPaintMode();
}
}
// Use the Preview Flag to Draw a Highlighted Port
/**
* Description of the Method
*
*@param g Description of Parameter
*/
protected void paintPort(Graphics g) {
// If Current Port is Valid
if (port != null) {
// If Not Floating Port...
boolean o = (GraphConstants.getOffset(port.getAttributes()) != null);
// ...Then use Parent's Bounds
Rectangle r = (o) ? port.getBounds().getBounds() : port.getParentView().getBounds().getBounds();
// Scale from Model to Screen
r = getGraph().toScreen(new Rectangle(r)).getBounds();
// Add Space For the Highlight Border
r.setBounds(r.x - 3, r.y - 3, r.width + 6, r.height + 6);
// Paint Port in Preview (=Highlight) Mode
//getGraph().getUI().paintCell(g, port, r, true);
}
}
protected ModelJGraph getGraph(){
return graph;
}
// Override to Gain Control (for PopupMenu and ConnectMode)
/**
* Gets the forceMarqueeEvent attribute of the MarqueeHandler object
*
*@param e Description of Parameter
*@return The forceMarqueeEvent value
*/
public boolean isForceMarqueeEvent(MouseEvent e) {
// If Right Mouse Button we want to Display the PopupMenu
if (SwingUtilities.isRightMouseButton(e)) {
// Return Immediately
return true;
}
// Find and Remember Port
port = getSourcePortAt(e.getPoint());
// If Port Found and in ConnectMode (=Ports Visible)
if (port != null && getGraph().isPortsVisible()) {
return true;
}
// Else Call Superclass
return super.isForceMarqueeEvent(e);
}
/**
* Gets the sourcePortAt attribute of the MarqueeHandler object
*
*@param point Description of Parameter
*@return The sourcePortAt value
*/
public PortView getSourcePortAt(Point point) {
// Scale from Screen to Model
Point tmp = convert(getGraph().fromScreen(new Point(point)));
// Find a Port View in Model Coordinates and Remember
return getGraph().getPortViewAt(tmp.x, tmp.y);
}
// Display PopupMenu or Remember Start Location and First Port
/**
* Description of the Method
*
*@param e Description of Parameter
*/
public void mousePressed(final MouseEvent e) {
// If Right Mouse Button
if (SwingUtilities.isRightMouseButton(e)) {
// Scale From Screen to Model
Point loc = convert(getGraph().fromScreen(e.getPoint()));
// Find Cell in Model Coordinates
Object cell = getGraph().getFirstCellForLocation(loc.x, loc.y);
JPopupMenu menu=new JPopupMenu();
menu.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
if (cell instanceof DefaultEdge){
menu.add("Relationship");
menu.addSeparator();
NAryEdge nary=this.getNAryEdge((Model)(getGraph().getModel()),(DefaultEdge)cell);
createRelationshipMenu(menu, nary);
}
else
if (cell instanceof NAryEdge){
menu.add("Relationship");
menu.addSeparator();
NAryEdge nary=(NAryEdge)cell;
createRelationshipMenu(menu, nary);
}
else {
if (cell instanceof DefaultGraphCell){
// Create PopupMenu for the Cell
menu.add("Entity");
menu.addSeparator();
addActionsToPopupMenu(menu,af.createCellActions((DefaultGraphCell)cell, getGraph()));
menu.addSeparator();
menu.add(createMenu("Views",daf.createChangeViewActions((DefaultGraphCell)cell, getGraph())));
final ingenias.editor.entities.Entity ent=((ingenias.editor.entities.Entity)((DefaultGraphCell)cell).getUserObject());
Vector actions = af.createEntityActions(ent);
for (AbstractAction action:actions){
menu.add(action);
}
menu.add(createMenu("Refinement",af.createCellRefinementActions(ent)));
} else {
if (getGraph().getSelectionCells()!=null && getGraph().getSelectionCells().length>1){
GraphCell[] gc=new GraphCell[getGraph().getSelectionCells().length];
System.arraycopy(getGraph().getSelectionCells(),0,gc,0,gc.length);
addActionsToPopupMenu(menu,af.createDiagramIndependentActions(e.getPoint(),
gc, getGraph()));
}
addActionsToPopupMenu(menu,af.createDiagramOperations(getGraph()));
menu.addSeparator();
Vector insertActions = daf.createDiagramSpecificInsertActions(e.getPoint(),graph);
Collections.sort(insertActions, new Comparator(){
@Override
public int compare(AbstractAction o1,
AbstractAction o2) {
return o1.getValue(AbstractAction.NAME).toString().compareTo(o2.getValue(AbstractAction.NAME).toString());
}
});
addActionsToPopupMenuInsideComponent(menu,insertActions);
}
}
// Display PopupMenu
menu.show(getGraph(), e.getX(), e.getY());
// Else if in ConnectMode and Remembered Port is Valid
}
else if (port != null && !e.isConsumed() && getGraph().isPortsVisible()) {
// Remember Start Location
start = convert(getGraph().toScreen(port.getLocation(null)));
// Remember First Port
firstPort = port;
// Consume Event
e.consume();
}
else {
// Call Superclass
}
super.mousePressed(e);
}
public static void removeAction(
final ModelJGraph graph,
final GraphManager gm,
final ObjectManager om) {
if (graph != null) {
if (!graph.isSelectionEmpty()) {
Object[] cells =
graph.getSelectionCells();
for (Object cell:cells){
if (cell instanceof DefaultEdge){
DefaultEdge de=(DefaultEdge)cell;
if (!(((DefaultPort)de.getSource()).getParent() instanceof NAryEdge) &&
!(((DefaultPort)de.getTarget()).getParent() instanceof NAryEdge))
graph.removeSelectionCell(cell);
}
}
cells=graph.getSelectionCells();
HashSet