/**
* creates a mouse listener to open a Menu by right clicking or to select
* the node by left clicking
*/
private void addDefaultMouseListener() {
final GSSGraph graph = (GSSGraph) getGraphModel();
final GSSElementGraphNode node = this;
elementFigure.addMouseListener(new MouseListener() {
@Override
public void mousePressed(MouseEvent me) {
}
@Override
public void mouseReleased(MouseEvent me) {
if (me.button == 1) {
// left mouse button was pressed
setSelection(me.getState());
} else if (me.button == 3) {
// right mouse button was pressed
// get location of the mouse in the workspace
int x = graph.getParent().getParent().getParent()
.getParent().getParent().getParent().getParent()
.getParent().getParent().getParent().getLocation().x;
int y = graph.getParent().getParent().getParent()
.getParent().getParent().getParent().getParent()
.getParent().getParent().getParent().getLocation().y;
x += graph.getParent().getParent().getParent().getParent()
.getParent().getParent().getParent().getParent()
.getParent().getLocation().x;
y += graph.getParent().getParent().getParent().getParent()
.getParent().getParent().getParent().getParent()
.getParent().getLocation().y;
x += graph.getParent().getParent().getParent().getParent()
.getParent().getParent().getParent().getParent()
.getParent().getParent().getParent().getLocation().x;
y += graph.getParent().getParent().getParent().getParent()
.getParent().getParent().getParent().getParent()
.getParent().getParent().getParent().getLocation().y;
x += graph.getParent().getParent().getParent().getParent()
.getParent().getParent().getParent().getParent()
.getParent().getParent().getParent().getParent()
.getParent().getParent().getLocation().x;
y += graph.getParent().getParent().getParent().getParent()
.getParent().getParent().getParent().getParent()
.getParent().getParent().getParent().getParent()
.getParent().getParent().getLocation().y;
openMenu(me.x + x, me.y + y);
}
}
private void openMenu(int xPos, int yPos) {
if (menu != null) {
menu.setLocation(xPos, yPos);
menu.setVisible(true);
}
}
private void setSelection(int state) {
if (state == 786432) {
// control was pressed & hold
if (graph.getSelection().contains(node)) {
// remove selected node
graph.deselectNode(node, false);
} else {
// add selected node
graph.selectNode(node, false);
}
} else if (graph.getSelection().contains(node)) {
// clear selection
graph.deselectNode(node, true);
} else {
// add selected node
graph.selectNode(node, true);
}
}