ontologyvisualizationviewer.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,510 行 · 第 1/3 页
JAVA
1,510 行
public synchronized void restartThreadOnly() {
if (visRunnerIsRunning) {
throw new FatalException("Can't init while a visrunner is running");
}
relaxer = new VisRunner();
relaxer.setPriority(Thread.MIN_PRIORITY);
relaxer.start();
}
/**
* Pre-relaxes and starts a visRunner thread
*/
public synchronized void init() {
if (visRunnerIsRunning) {
throw new FatalException("Can't init while a visrunner is running");
}
prerelax();
relaxer = new VisRunner();
relaxer.start();
}
/**
* Restarts layout, then calls init();
*/
public synchronized void restart() {
if (visRunnerIsRunning) {
throw new FatalException(
"Can't restart while a visrunner is running");
}
layout.restart();
init();
repaint();
}
/**
*
* @see javax.swing.JComponent#setVisible(boolean)
*/
public void setVisible(boolean aFlag) {
super.setVisible(aFlag);
layout.resize(this.getSize());
}
/**
* Runs the visualization forward a few hundred iterations (for half a
* second)
*/
public void prerelax() {
suspend();
int i = 0;
if (layout.isIncremental()) {
// then increment layout for half a second
long timeNow = System.currentTimeMillis();
while (System.currentTimeMillis() - timeNow < 500
&& !layout.incrementsAreDone()) {
i++;
layout.advancePositions();
}
}
unsuspend();
}
/**
* If the visualization runner is not yet running, kick it off.
*/
protected synchronized void start() {
suspended = false;
synchronized (pauseObject) {
pauseObject.notifyAll();
}
}
public synchronized void suspend() {
manualSuspend = true;
}
public synchronized void unsuspend() {
manualSuspend = false;
synchronized (pauseObject) {
pauseObject.notifyAll();
}
}
/**
* @deprecated Use <code>getPickedState.isPicked(e)</code>.
*/
public boolean isPicked(Vertex v) {
return pickedState.isPicked(v);
}
/**
* @deprecated Use <code>getPickedState.isPicked(e)</code>.
*/
public boolean isPicked(Edge e) {
return pickedState.isPicked(e);
}
/**
* @deprecated Use <code>getPickedState.pick(picked, b)</code>.
*/
protected void pick(Vertex picked, boolean b) {
pickedState.pick(picked, b);
}
long[] relaxTimes = new long[5];
long[] paintTimes = new long[5];
int relaxIndex = 0;
int paintIndex = 0;
double paintfps, relaxfps;
boolean stop = false;
boolean visRunnerIsRunning = false;
protected class VisRunner extends Thread {
public VisRunner() {
super("Relaxer Thread");
}
public void run() {
visRunnerIsRunning = true;
try {
while (!layout.incrementsAreDone() && !stop) {
synchronized (pauseObject) {
while ((suspended || manualSuspend) && !stop) {
try {
pauseObject.wait();
} catch (InterruptedException e) {
}
}
}
long start = System.currentTimeMillis();
layout.advancePositions();
long delta = System.currentTimeMillis() - start;
if (stop)
return;
String status = layout.getStatus();
if (statusCallback != null && status != null) {
statusCallback.callBack(status);
}
if (stop)
return;
relaxTimes[relaxIndex++] = delta;
relaxIndex = relaxIndex % relaxTimes.length;
relaxfps = average(relaxTimes);
if (stop)
return;
repaint();
if (stop)
return;
try {
sleep(relaxerThreadSleepTime);
} catch (InterruptedException ie) {
}
}
} finally {
visRunnerIsRunning = false;
}
}
}
/**
* Returns a flag that says whether the visRunner thread is running. If it
* is not, then you may need to restart the thread (with
*
* @return
*/
public boolean isVisRunnerRunning() {
return visRunnerIsRunning;
}
/**
* setter for the scale fires a PropertyChangeEvent with the
* AffineTransforms representing the previous and new values for scale and
* offset
*
* @param scalex
* @param scaley
*/
public void scale(double scalex, double scaley) {
scale(scalex, scaley, null);
}
public void scale(double scalex, double scaley, Point2D from) {
if (from == null) {
Dimension d = getSize();
from = new Point2D.Float(d.width / 2.f, d.height / 2.f);
}
AffineTransform xf = AffineTransform.getTranslateInstance(from.getX(),
from.getY());
xf.scale(scalex, scaley);
xf.translate(-from.getX(), -from.getY());
inverse = null;
transform.preConcatenate(xf);
fireStateChanged();
repaint();
}
public void setScale(double scalex, double scaley) {
setScale(scalex, scaley, null);
}
/**
* setter for the scale fires a PropertyChangeEvent with the
* AffineTransforms representing the previous and new values for scale and
* offset
*
* @param scalex
* @param scaley
*/
public void setScale(double scalex, double scaley, Point2D from) {
if (from == null) {
Dimension d = getSize();
from = new Point2D.Float(d.width / 2.f, d.height / 2.f);
}
inverse = null;
transform.setToIdentity();
transform.translate(from.getX(), from.getY());
transform.scale(scalex, scaley);
transform.translate(-from.getX(), -from.getY());
fireStateChanged();
repaint();
}
/**
* getter for scalex
*
* @return scalex
*/
public double getScaleX() {
return transform.getScaleX();
}
/**
* getter for scaley
*
* @return
*/
public double getScaleY() {
return transform.getScaleY();
}
/**
* getter for offsetx
*
* @return
*/
public double getOffsetX() {
return getTranslateX();
}
public double getTranslateX() {
return transform.getTranslateX();
}
/**
* getter for offsety
*
* @return
*/
public double getOffsetY() {
return getTranslateY();
}
public double getTranslateY() {
return transform.getTranslateY();
}
/**
* set the offset values that will be used in the translation component of
* the graph rendering transform. Changes the transform to the identity
* transform, then sets the translation conponents to the passed values
* Fires a PropertyChangeEvent with the AffineTransforms representing the
* previous and new values for the transform
*
* @param offsetx
* @param offsety
*/
public void setOffset(double offsetx, double offsety) {
setTranslate(offsetx, offsety);
}
public void setTranslate(double tx, double ty) {
float scalex = (float) transform.getScaleX();
float scaley = (float) transform.getScaleY();
inverse = null;
transform.setTransform(scalex, 0, 0, scaley, tx, ty);
fireStateChanged();
repaint();
}
public void translate(double offsetx, double offsety) {
inverse = null;
transform.translate(offsetx, offsety);
fireStateChanged();
repaint();
currentX = currentX - offsetx;
currentY = currentY - offsety;
}
/**
* Transform the mouse point with the inverse transform of the
* VisualizationViewer. This maps from screen coordinates to graph
* coordinates.
*
* @param p
* the point to transform (typically, a mouse point)
* @return a transformed Point2D
*/
public Point2D transform(Point2D p) {
if (inverse == null) {
try {
inverse = transform.createInverse();
} catch (NoninvertibleTransformException e) {
throw new IllegalArgumentException(e.toString());
}
}
return inverse.transform(p, null);
}
public Point2D transformGraph2Screen(Point2D p) {
return transform.transform(p, null);
}
/**
* @return Returns the renderingHints.
*/
public Map getRenderingHints() {
return renderingHints;
}
/**
* @param renderingHints
* The renderingHints to set.
*/
public void setRenderingHints(Map renderingHints) {
this.renderingHints = renderingHints;
}
protected synchronized void paintComponent(Graphics g)
{
start();
super.paintComponent(g);
//Graphics2D g2d = (Graphics2D) g;
//long tic = System.currentTimeMillis();
renderGraph();
// tw7: removed the following line for optimization
// (not really sure what it does anyway)
//g2d.drawImage(offscreen, null, 0, 0);
//long toc = System.currentTimeMillis();
//System.out.println( "Time taken to render: " + (toc - tic) + " milisecond" );
}
protected void renderGraph()
{
if (offscreenG2d == null)
return;
offscreenG2d.setRenderingHints(renderingHints);
long start = System.currentTimeMillis();
// the size of the VisualizationViewer
Dimension d = getSize();
// composites to control layering of the rendering
AlphaComposite srcOver = AlphaComposite
.getInstance(AlphaComposite.SRC_OVER);
AlphaComposite clear = AlphaComposite.getInstance(AlphaComposite.CLEAR);
// clear the offscreen image
offscreenG2d.setComposite(clear);
offscreenG2d.fillRect(0, 0, d.width, d.height);
AffineTransform oldXform = offscreenG2d.getTransform();
offscreenG2d.setTransform(transform);
offscreenG2d.setComposite(srcOver);
// if there are preRenderers set, paint them
for (Iterator iterator = preRenderers.iterator(); iterator.hasNext();) {
Paintable paintable = (Paintable) iterator.next();
if (paintable.useTransform()) {
paintable.paint(offscreenG2d);
} else {
offscreenG2d.setTransform(oldXform);
paintable.paint(offscreenG2d);
offscreenG2d.setTransform(transform);
}
}
// paint all the edges
for (Iterator iter = layout.getVisibleEdges().iterator(); iter
.hasNext();) {
Edge e = (Edge) iter.next();
Vertex v1 = (Vertex) e.getEndpoints().getFirst();
Vertex v2 = (Vertex) e.getEndpoints().getSecond();
renderer.paintEdge(offscreenG2d, e, (int) layout.getX(v1),
(int) layout.getY(v1), (int) layout.getX(v2), (int) layout
.getY(v2));
}
// paint all the vertices
for (Iterator iter = layout.getVisibleVertices().iterator(); iter
.hasNext();) {
Vertex v = (Vertex) iter.next();
renderer.paintVertex(offscreenG2d, v, (int) layout.getX(v),
(int) layout.getY(v));
}
// paint overlay edges and such
//OverlayGraph overlayGraph = myGraph.getOverlayGraph();
//overlayGraph.paint( offscreenG2d );
long delta = System.currentTimeMillis() - start;
paintTimes[paintIndex++] = delta;
paintIndex = paintIndex % paintTimes.length;
paintfps = average(paintTimes);
// if there are postRenderers set, do it
for (Iterator iterator = postRenderers.iterator(); iterator.hasNext();) {
Paintable paintable = (Paintable) iterator.next();
if (paintable.useTransform()) {
paintable.paint(offscreenG2d);
} else {
offscreenG2d.setTransform(oldXform);
paintable.paint(offscreenG2d);
offscreenG2d.setTransform(transform);
}
}
offscreenG2d.setTransform(oldXform);
// visual debugging
if (DEBUG)
{
offscreenG2d.setColor( Color.BLACK );
Point2D boxTL = transformGraph2Screen( new Point2D.Double(DEBUGX, DEBUGY) );
Point2D boxBR = transformGraph2Screen( new Point2D.Double(DEBUGX + DEBUGW, DEBUGY + DEBUGH) );
offscreenG2d.drawRect( (int)boxTL.getX(), (int)boxTL.getY(), (int)(boxBR.getX() - boxTL.getX()), (int)(boxBR.getY() - boxTL.getY()) );
offscreenG2d.drawLine( (int)((boxTL.getX()+boxBR.getX())/2), (int)boxTL.getY(), (int)((boxTL.getX()+boxBR.getX())/2), (int)boxBR.getY() );
offscreenG2d.drawLine( (int)boxTL.getX(), (int)((boxTL.getY()+boxBR.getY())/2), (int)boxBR.getX(), (int)((boxTL.getY()+boxBR.getY())/2) );
offscreenG2d.fillOval( (this.getWidth()/2)-5, (this.getHeight()/2)-5, 10, 10 );
offscreenG2d.setColor( Color.RED );
Point2D focus = transformGraph2Screen( new Point2D.Double(DX, DY) );
offscreenG2d.drawRect( (int)(focus.getX()-5), (int)(focus.getY()-5), 10, 10 );
}
}
/**
* Returns the double average of a number of long values.
*
* @param paintTimes
* an array of longs
* @return the average of the doubles
*/
protected double average(long[] paintTimes) {
double l = 0;
for (int i = 0; i < paintTimes.length; i++) {
l += paintTimes[i];
}
return l / paintTimes.length;
}
protected class VisualizationListener extends ComponentAdapter {
protected OntologyVisualizationViewer vv;
public VisualizationListener(OntologyVisualizationViewer vv) {
this.vv = vv;
}
/**
* create a new offscreen image for the graph whenever the window is
* resied
*/
public void componentResized(ComponentEvent e) {
Dimension d = vv.getSize();
offscreen = new BufferedImage(d.width, d.height,
BufferedImage.TYPE_INT_ARGB);
offscreenG2d = offscreen.createGraphics();
}
}
/**
* @param scb
*/
public void setTextCallback(StatusCallback scb) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?