org.metacsp.utility.UI.ConstraintNetworkFrame Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of meta-csp-framework Show documentation
Show all versions of meta-csp-framework Show documentation
A Java API for Meta-CSP based reasoning
/*******************************************************************************
* Copyright (c) 2010-2013 Federico Pecora
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
package org.metacsp.utility.UI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Paint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ConcurrentModificationException;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import org.apache.commons.collections15.Transformer;
import org.metacsp.framework.Constraint;
import org.metacsp.framework.Variable;
import edu.uci.ics.jung.algorithms.layout.AbstractLayout;
import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.FRLayout2;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.layout.SpringLayout;
import edu.uci.ics.jung.algorithms.layout.StaticLayout;
import edu.uci.ics.jung.algorithms.layout.util.Relaxer;
import edu.uci.ics.jung.algorithms.layout.util.VisRunner;
import edu.uci.ics.jung.algorithms.util.IterativeContext;
import edu.uci.ics.jung.graph.ObservableGraph;
import edu.uci.ics.jung.graph.event.GraphEvent;
import edu.uci.ics.jung.graph.event.GraphEventListener;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.layout.LayoutTransition;
import edu.uci.ics.jung.visualization.renderers.Renderer;
import edu.uci.ics.jung.visualization.util.Animator;
/**
* Based on variation of AddNodeDemo that animates transitions between graph states, by Tom Nelson.
*
* @author Federico Pecora
*/
public class ConstraintNetworkFrame extends JFrame {
private static final Logger logger = Logger.getLogger(ConstraintNetworkFrame.class.getPackage().getName());
private static final long serialVersionUID = 3896624751126451811L;
private ObservableGraph g = null;
private VisualizationViewer vv = null;
private AbstractLayout layout = null;
boolean done;
protected JButton switchLayout;
protected JButton performOperation;
private Animator animator = null;
private LayoutTransition lt = null;
//An implementation of Animator which ignores ConcurrentModificationExceptions
private class MyAnimator extends Animator {
public MyAnimator(IterativeContext process) { super(process); }
public void run() {
try { super.run(); }
catch (ConcurrentModificationException e) { /* ignore */ }
}
}
public ConstraintNetworkFrame(ObservableGraph graph, String title, final Callback cb) {
super(title);
this.g = graph;
g.addGraphEventListener(new GraphEventListener() {
@Override
public void handleGraphEvent(GraphEvent evt) {
vv.getRenderContext().getPickedVertexState().clear();
vv.getRenderContext().getPickedEdgeState().clear();
try {
layout.initialize();
try {
Relaxer relaxer = new VisRunner((IterativeContext)layout);
relaxer.stop();
relaxer.prerelax();
}
catch (java.lang.ClassCastException e) { e.printStackTrace(); }
StaticLayout staticLayout = new StaticLayout(g, layout);
lt = new LayoutTransition(vv, vv.getGraphLayout(), staticLayout);
animator = new MyAnimator(lt);
animator.start();
//vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
vv.repaint();
}
catch (Exception e) { e.printStackTrace(); }
}
});
//create a graphdraw
//layout = new FRLayout(g);
//layout = new SpringLayout(g);
//layout = new StaticLayout(g,new STNTransformer());
layout = new FRLayout2(g);
//layout = new CircleLayout(g);
//layout = new ISOMLayout(g);
//layout = new KKLayout(g);
layout.setSize(new Dimension(600,600));
try {
Relaxer relaxer = new VisRunner((IterativeContext)layout);
relaxer.stop();
relaxer.prerelax();
}
catch (java.lang.ClassCastException e) { e.printStackTrace(); }
Layout staticLayout = new StaticLayout(g, layout);
vv = new VisualizationViewer(staticLayout, new Dimension(600,600));
JRootPane rp = this.getRootPane();
rp.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
getContentPane().setLayout(new BorderLayout());
getContentPane().setBackground(java.awt.Color.lightGray);
getContentPane().setFont(new Font("Serif", Font.PLAIN, 12));
vv.setGraphMouse(new DefaultModalGraphMouse());
vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.S);
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.setForeground(Color.black);
//draw edge labels
Transformer stringer = new Transformer(){
@Override
public String transform(Constraint e) {
try { return e.getEdgeLabel(); }
catch (NullPointerException ex) { return ""; }
}
};
Transformer vertexPaint = new Transformer() {
public Paint transform(Variable v) {
return v.getColor();
}
};
Transformer constraintPaint = new Transformer() {
public Paint transform(Constraint c) {
return c.getColor();
}
};
vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
// vv.getRenderContext().setEdgeFillPaintTransformer(constraintPaint);
vv.getRenderContext().setEdgeLabelTransformer(stringer);
// vv.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer(vv.getPickedEdgeState(), Color.black, Color.cyan));
vv.getRenderContext().setEdgeDrawPaintTransformer(constraintPaint);
vv.addComponentListener(new ComponentAdapter() {
/**
* @see java.awt.event.ComponentAdapter#componentResized(java.awt.event.ComponentEvent)
*/
@Override
public void componentResized(ComponentEvent arg0) {
super.componentResized(arg0);
layout.setSize(arg0.getComponent().getSize());
}});
getContentPane().add(vv);
performOperation = new JButton("Perform operation");
if (cb == null) performOperation.setEnabled(false);
else performOperation.setEnabled(true);
performOperation.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (cb != null) cb.performOperation();
}
});
switchLayout = new JButton("Switch to SpringLayout");
switchLayout.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
Dimension d = vv.getSize();//new Dimension(600,600);
if (switchLayout.getText().indexOf("Spring") > 0) {
switchLayout.setText("Switch to FRLayout");
//layout = new SpringLayout(g, new ConstantTransformer(EDGE_LENGTH));
layout = new SpringLayout(g);
layout.setSize(d);
try {
Relaxer relaxer = new VisRunner((IterativeContext)layout);
relaxer.stop();
relaxer.prerelax();
} catch (java.lang.ClassCastException e) { e.printStackTrace(); }
StaticLayout staticLayout = new StaticLayout(g, layout);
lt = new LayoutTransition(vv, vv.getGraphLayout(), staticLayout);
Animator animator = new MyAnimator(lt);
animator.start();
// vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
vv.repaint();
} else {
switchLayout.setText("Switch to SpringLayout");
layout = new FRLayout(g, d);
layout.setSize(d);
try {
Relaxer relaxer = new VisRunner((IterativeContext)layout);
relaxer.stop();
relaxer.prerelax();
} catch (java.lang.ClassCastException e) {}
StaticLayout staticLayout = new StaticLayout(g, layout);
lt = new LayoutTransition(vv, vv.getGraphLayout(), staticLayout);
Animator animator = new MyAnimator(lt);
animator.start();
//vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
vv.repaint();
}
}
});
getContentPane().add(performOperation, BorderLayout.SOUTH);
//getContentPane().add(switchLayout, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.pack();
this.setVisible(true);
}
}