//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.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.border.BevelBorder;
import fbench.IconFactory;
import fbench.graph.popup.ParameterDialog;
import org.w3c.dom.Element;
/**
* A GraphNode encsapsulating an IEC 61499 <TT>Parameter</TT> input of an FB,
* Device or Resource.
*
* @author JHC, JP
*
* @version 20070430/JP - Linked with ContextMenu and ParameterDialog for adding, modifying and
* removing the selected Parameter through GUI
* @version 20050831/JHC
*/
public class Parameter extends GraphNode {
/** The GraphNode containing the parameter destination. */
protected GraphElement destContainer;
/** The Component which is the destination of this parameter. */
protected Component destCompt;
/**
* Initializes and returns a new graphic Parameter instance.
*
* @param el An IEC 61499 DOM Parameter element.
* @param destNode The GraphNode containing the parameter destination.
*/
public Parameter(Element el, GraphElement destContainer) {
super();
this.destContainer = destContainer;
setLayout(new BorderLayout());
setBorder(GraphElement.emborder);
setElement(el);
addMouseListener(new MouseAdapter(){
private ParameterDialog dialog;
public void mouseClicked(MouseEvent evt){
boolean isDoubleClicked = false;
boolean isRightClicked = false;
if(evt.getButton() == 1 && evt.getClickCount() == 2)
isDoubleClicked = true;
if(evt.getButton() == 3)
isRightClicked = true;
if(isDoubleClicked || isRightClicked){
dialog = new ParameterDialog(getElement(), evt);
if(isDoubleClicked)
dialog.create("Edit Parameter");
else if(isRightClicked){
JPopupMenu contextMenu = new JPopupMenu();
contextMenu.setBorder(new BevelBorder(BevelBorder.RAISED));
JMenuItem editParameter = new JMenuItem("Edit", IconFactory.getIcon("Edit16.gif"));
editParameter.setActionCommand("Edit Parameter");
editParameter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
dialog.create("Edit Parameter");
}
});
contextMenu.add(editParameter);
JMenuItem deleteParameter = new JMenuItem("Delete", IconFactory.getIcon("Delete16.gif"));
deleteParameter.setActionCommand("Delete Parameter");
deleteParameter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
dialog.createRemoveDialog();
dialog.setVisible(true);
}
});
contextMenu.add(deleteParameter);
contextMenu.show(evt.getComponent(), evt.getX()-3, evt.getY()-3);
}
}
}
});
}
public void setElement(Element el) {
super.setElement(el);
String compname = model.getName();
setToolTipText(compname + " := " + getElement().getAttribute("Value"));
destCompt = destContainer.getComponentNamed(compname);
}
protected void updateShape() {
Dimension sz = getPreferredSize();
Rectangle destBounds = GraphEdge.getBounds(destCompt);
if (destBounds == null)
destBounds = new Rectangle();
setBounds(destBounds.x - sz.width, destBounds.y
+ (destBounds.height - sz.height) / 2, sz.width, sz.height);
}
protected void initComponents() {
super.initComponents();
add(mainLabel, BorderLayout.CENTER);
mainLabel.setOpaque(false);
mainLabel.setIcon(IconFactory.getIcon("hline15"));
mainLabel.setHorizontalTextPosition(JLabel.LEFT);
}
/**
* Return the test for the main label. Default is the model's <TT>Name
* </TT> attribute.
*/
protected String mainLabelText() {
return getElement().getAttribute("Value");
}
public void paintComponent(Graphics g) {
Color oldcolor = g.getColor();
g.setColor(isSelected() ? new Color(119, 190, 255) : Color.white);
Dimension sz = getSize();
g.fillRect(0, 0, sz.width, sz.height);
g.setColor(oldcolor);
}
}