package org.apache.wsdm.interop.wcm.impl.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import org.apache.interop.wcmgr.CommandListener;
import org.apache.wsdm.interop.wcm.impl.ui.jgraph.CustomCellViewFactory;
import org.apache.wsdm.interop.wcm.impl.ui.jgraph.WcGraphCell;
import org.apache.wsdm.interop.wcm.impl.ui.jgraph.WsGraphCell;
import org.apache.wsdm.interop.wcm.model.IWs;
import org.jgraph.JGraph;
import org.jgraph.graph.AttributeMap;
import org.jgraph.graph.ConnectionSet;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;
/**
* This code was generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* *************************************
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED
* for this machine, so Jigloo or this code cannot be used legally
* for any corporate or commercial purpose.
* *************************************
*/
public class StatusJPanel extends javax.swing.JPanel {
private JGraph jGraphMain;
private JScrollPane jScrollPaneGraph;
private JPopupMenu popupMenu;
private CommandListener commandLister;
/**
* Auto-generated main method to display this
* JPanel inside a new JFrame.
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new StatusJPanel());
frame.pack();
frame.show();
}
public StatusJPanel() {
super();
initGUI();
popupMenu = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("Recalibrate");
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
recalibrate();
}});
popupMenu.add(menuItem);
changeRengerView();
emptyGraph();
}
public void setPopupListener(CommandListener listener){
this.commandLister=listener;
}
/**
*
*/
protected void recalibrate() {
WsGraphCell cell=(WsGraphCell)jGraphMain.getSelectionCell();
commandLister.recalibrate(cell.toString());
}
private void changeRengerView(){
// Work on the Graph's View
jGraphMain.getGraphLayoutCache().setFactory(new CustomCellViewFactory());
}
/**
*
*/
private void emptyGraph() {
//GraphModel model = jGraphMain.getModel();
GraphModel model = new DefaultGraphModel();
jGraphMain.setModel(model);
}
public static DefaultGraphCell createVertex(String name, double x,
double y, double w, double h, Color bg, boolean raised,int type) {
// Create vertex with the given name
DefaultGraphCell cell =null;
if(type==0)
cell = new WsGraphCell(name);
else
cell = new WcGraphCell(name);
// Set bounds
GraphConstants.setBounds(cell.getAttributes(),
new Rectangle2D.Double(x, y, w, h));
//horizontalTextPosition
GraphConstants.setHorizontalAlignment(cell.getAttributes(), SwingConstants.RIGHT);//Color.orange
GraphConstants.setVerticalAlignment(cell.getAttributes(), SwingConstants.BOTTOM);//Color.orange
// Set fill color
if (bg != null) {
GraphConstants.setGradientColor(
cell.getAttributes(), bg);//Color.orange
GraphConstants.setOpaque(
cell.getAttributes(), true);
}
// Set raised border
if (raised)
GraphConstants.setBorder(
cell.getAttributes(),
BorderFactory.createRaisedBevelBorder());
else
// Set black border
GraphConstants.setBorderColor(
cell.getAttributes(), Color.black);
// Add a Port
DefaultPort port = new DefaultPort();
cell.add(port);
port.setParent(cell);
return cell;
}
private void initGUI() {
try {
BorderLayout thisLayout = new BorderLayout();
this.setLayout(thisLayout);
setPreferredSize(new Dimension(400, 300));
{
jScrollPaneGraph = new JScrollPane();
this.add(jScrollPaneGraph, BorderLayout.CENTER);
{
jGraphMain = new JGraph();
jScrollPaneGraph.setViewportView(jGraphMain);
jGraphMain.setBackground(new java.awt.Color(0,0,0));
jGraphMain.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
System.out
.println("jGraphMain.mousePressed, event="
+ evt);
handleMousePressed(evt);
}
});
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param evt
*/
protected void handleMousePressed(MouseEvent e) {
// If Right Mouse Button
if (SwingUtilities.isRightMouseButton(e)) {
// Scale From Screen to Model
Point l = (Point) jGraphMain.fromScreen(e.getPoint());
// Find Cell in Model Coordinates
Object c = jGraphMain.getFirstCellForLocation(l.x,l.y);
// Create PopupMenu for the Cell
JPopupMenu menu = createPopupMenu(e.getPoint(), c);
// Display PopupMenu
menu.show(jGraphMain, e.getX(), e.getY());
// Else if in ConnectMode and Remembered Port is Valid
}
// else if (port != null && !e.isConsumed() &&
// jGraphMain.isPortsVisible()) {
// // Remember Start Location
// start = jGraphMain.toScreen(port.getLocation(null));
// // Remember First Port
// firstPort = port;
// // Consume Event
// e.consume();
// } else
// // Call Superclass
// super.mousePressed(e);
}
/**
* @param point
* @param c
* @return
*/
private JPopupMenu createPopupMenu(Point point, Object c) {
return popupMenu;
}
/**
* @param wc
*/
public void setModel(IWs[] wsArry) {
//GraphModel model = jGraphMain.getModel();
GraphModel model = new DefaultGraphModel();
jGraphMain.setModel(model);
// Insert all three cells in one call, so we need an
// array to store them
DefaultGraphCell[] cells = new DefaultGraphCell[wsArry.length+1];
// Create WC
cells[0] = createVertex(wsArry[0].getName(), 20, 20, 50, 50,
Color.GREEN, true,1);
// Remaining WSs
for(int index=1;index<wsArry.length;index++){
cells[index] = createVertex(wsArry[index].getName(), 20+100*(index-1), 140, 50, 50,
Color.GRAY, true,0);
}
// Create Edge
DefaultEdge edge = new DefaultEdge();
// Fetch the ports from the new vertices,
// and connect them with the edge
edge.setSource(cells[0].getChildAt(0));
edge.setTarget(cells[1].getChildAt(0));
cells[wsArry.length] = edge;
// Set Arrow Style for edge
int arrow = GraphConstants.ARROW_CLASSIC;
GraphConstants.setLineEnd(edge.getAttributes(), arrow);
GraphConstants.setEndFill(edge.getAttributes(), true);
setLineAttribs(edge);
// Insert the cells via the cache, so they get selected
jGraphMain.getGraphLayoutCache().setSelectsAllInsertedCells(false);
jGraphMain.getGraphLayoutCache().insert(cells);
}
/**
* @param node
*/
public void updateNode(IWs node) {
DefaultGraphModel model = (DefaultGraphModel)jGraphMain.getModel();
List theRoots = model.getRoots();
DefaultGraphCell selectedElement=null;
findNode(node.getName());
System.out.println();
}
/**
* @param node
* @param theRoots
*/
public DefaultGraphCell findNode(String nodeName) {
System.out.println("FindNode "+nodeName);
DefaultGraphModel model = (DefaultGraphModel)jGraphMain.getModel();
List theRoots = model.getRoots();
DefaultGraphCell selectedElement;
for (Iterator iter = theRoots.iterator(); iter.hasNext();) {
DefaultGraphCell element = (DefaultGraphCell) iter.next();
if(element.toString()!=null)
if(element.toString().equals(nodeName)){
// Update this node
return element;
}
}
return null;
}
public void setCurrentWc(String name){
DefaultGraphCell wcCell=findNode("Wc");
DefaultGraphCell wsCell=findNode(name);
if(wcCell==null||wsCell==null)
return;
DefaultPort zeroChild = (DefaultPort)wcCell.getChildAt(0);
Set edges = zeroChild.getEdges();
DefaultEdge edge1=(DefaultEdge)edges.iterator().next();
DefaultGraphModel model = (DefaultGraphModel)jGraphMain.getModel();
model.remove(new Object[]{edge1});
// Create Edge
DefaultEdge edge = new DefaultEdge();
int arrow = GraphConstants.ARROW_CLASSIC;
GraphConstants.setLineEnd(edge.getAttributes(), arrow);
GraphConstants.setEndFill(edge.getAttributes(), true);
setLineAttribs(edge);
// Create ConnectionSet for Insertion
ConnectionSet cs = new ConnectionSet(edge, wcCell.getChildAt(0), wsCell.getChildAt(0));
// Add Edge and Connections to the Model
Object[] insert = new Object[]{edge};
model.insert(insert, null,cs, null, null);
}
public void setColor(final String name, final Color color){
//jGraphMain.set
DefaultGraphCell wsCell=findNode(name);
jGraphMain.startEditingAtCell(wsCell);
if(wsCell!=null){
AttributeMap attribs = wsCell.getAttributes();
attribs.put("gradientColor",color);
wsCell.changeAttributes(attribs);
System.err.println("SetColor "+name+" "+color);
}
jGraphMain.stopEditing();
jGraphMain.clearSelection();
//setCurrentWc(name);
}
private void setLineAttribs(DefaultEdge edge){
GraphConstants.setLineColor(edge.getAttributes(), Color.GREEN);
GraphConstants.setLineWidth(edge.getAttributes(), 4);
}
public Object[] getRoots(){
DefaultGraphModel model = (DefaultGraphModel)jGraphMain.getModel();
List roots = model.getRoots();
return roots.toArray();
}
}