//Copyright (c)2005 Holobloc Inc.
//Contributed to the OOONEIDA FBench project under the Common Public License.
//Copyright (c) 2007 University of Auckland
//Contributed to the FBench extension of the OOONEIDA Workbench project under the Common Public License
package fbench.graph;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Area;
import javax.swing.BorderFactory;
import fbench.graph.popup.ContextMenu;
import fbench.graph.popup.FBTypeDialog;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* A GraphNode encapsulating an Event declaration element.
*
* @author JHC, JP
* @version 20070301/JP - Linked with ContextMenu and FBTypeDialog for adding, modifying and
* removing the selected Event component through GUI
* @version 20051006/JHC
*/
public class Event extends GraphNode {
/** The x-location for <TT>getPreferredBounds()</TT>. */
private int prefx = 0;
/** The y-location for <TT>getPreferredBounds()</TT>. */
private int prefy = 0;
private static final javax.swing.border.Border myborder = BorderFactory
.createEmptyBorder(1, 1, 1, 1);
private int currentMouseButton = 0;
private boolean isBeingRightClickDragged = false;
public Event() {
super();
setLayout(new BorderLayout());
setBorder(myborder);
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent evt){
if(evt.getButton() == MouseEvent.BUTTON1 && evt.getClickCount() == 2){
if(evt.getSource() instanceof VarDeclaration){
FBTypeDialog fbTypeDialog = new FBTypeDialog(getElement(), evt);
fbTypeDialog.create("Edit Variable");
}
else {
FBTypeDialog fbTypeDialog = new FBTypeDialog(getElement(), evt);
fbTypeDialog.create("Edit Event");
}
}
else if(evt.getButton() == MouseEvent.BUTTON3){
ContextMenu contextMenu = new ContextMenu(getElement(), evt);
contextMenu.create(getElement().getNodeName());
}
}
public void mousePressed(MouseEvent evt){
currentMouseButton = evt.getButton();
}
public void mouseReleased(MouseEvent evt){
if(isBeingRightClickDragged){
isBeingRightClickDragged = false;
//TODO: Do this more neatly or move to a better place
Point p = evt.getPoint();
p.translate(Event.this.getX(), Event.this.getY());
Component c = getParent().getComponentAt(p);
if(c != null && c instanceof VarDeclaration && c != Event.this){
VarDeclaration var = (VarDeclaration)c;
// Check that the event and var are either both input or both output
boolean isInput = ((Element)getElement().getParentNode())
.getNodeName().equals("EventInputs");
boolean varIsInput = ((Element)var.getElement().getParentNode())
.getNodeName().equals("InputVars");
if(isInput != varIsInput)
return;
String varName = var.getElement().getAttribute("Name");
// Check that it isn't already associated with the target var
boolean associated = false;
NodeList list = getElement().getChildNodes();
for(int i = 0; i < list.getLength(); i++){
Element with = (Element)list.item(i);
if(with.getAttribute("Var").equals(varName))
associated = true;
}
// Add the association
if(!associated){
System.out.println("Associate with " + varName);
Element with = getElement().getOwnerDocument().createElement("With");
with.setAttribute("Var", varName);
getElement().appendChild(with);
// repaint the GraphView
getGraphView(evt).refresh();
}
}
}
}
protected GraphView getGraphView(MouseEvent mouseEvt){
if(mouseEvt.getComponent().getClass().toString().equals("class fbench.graph.GraphView"))
return (GraphView) mouseEvt.getComponent();
Container parent = mouseEvt.getComponent().getParent();
while(parent != null && !parent.getClass().toString()
.equals("class fbench.graph.GraphView")){
parent = parent.getParent();
}
return (GraphView)parent;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent evt) {
if(currentMouseButton == MouseEvent.BUTTON3){
isBeingRightClickDragged = true;
}
}
});
}
protected void initComponents() {
super.initComponents();
mainLabel.setOpaque(false);
add(mainLabel, BorderLayout.CENTER);
}
public String mainLabelText() {
String ans = model.getType();
if (ans.equals(""))
ans = "EVENT";
return ans;
}
public String mainLabelName() {
String ans = model.getName();
return ans;
}
protected Color getDeselectedColor() {
return Color.white;
}
public Rectangle getPreferredBounds() {
Dimension sz = getPreferredSize();
return new Rectangle(prefx, prefy, sz.width, sz.height);
}
/** Sets the preferred (x,y) location. */
public void setPreferredLocation(int x, int y) {
prefx = x;
prefy = y;
}
protected Color getBorderColor(){
return isSelected()? getSelectionColor(): getDeselectedColor();
}
protected void updateShape(){
if(outline.isEmpty()){
Dimension sz = getPreferredSize();
outline.add(new Area(new Rectangle(0,0,sz.width,sz.height)));
}
}
}