Commit 189b4e3d authored by Marius Meyer's avatar Marius Meyer
Browse files

Merge branch 'master' of git.cs.upb.de:pg-andi/Simulator

parents 0b429006 50c6ad4d
......@@ -9,8 +9,12 @@ import gui.playfield.PlayField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JViewport;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JOptionPane;
......@@ -33,16 +37,22 @@ public class DTNSimGUI extends DTNSimUI {
private EventLogPanel eventLogPanel;
private InfoPanel infoPanel;
//global variables for the mouse handler
private Point origin;
private JViewport viewPort;
private void startGUI() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
public void run() {
try {
initGUI();
} catch (AssertionError e) {
processAssertionError(e);
}
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
......@@ -61,6 +71,7 @@ public class DTNSimGUI extends DTNSimUI {
this.field.addMouseListener(new PlayfieldMouseHandler());
this.field.addMouseWheelListener(new PlayfieldMouseHandler());
this.field.addMouseMotionListener(new PlayfieldMouseHandler());
this.guiControls = new GUIControls(this,this.field);
this.eventLogPanel = new EventLogPanel(this);
......@@ -74,6 +85,8 @@ public class DTNSimGUI extends DTNSimUI {
if (scen.getMap() != null ) {
field.setMap(scen.getMap());
}
//get the viewport which is needed for the mouse handler
viewPort = main.getPlayFieldScroll().getViewport();
// if user closes the main window, call closeSim()
this.main.addWindowListener(new WindowAdapter() {
......@@ -142,10 +155,10 @@ public class DTNSimGUI extends DTNSimUI {
String title = e.getClass().getSimpleName() + " (simulation paused)";
String msg = e.getMessage();
String txt = (msg != null ? msg : "") + " at simtime " +
SimClock.getIntTime() + "\n\ncaught at:\n" +
e.getStackTrace()[0].toString() +
"\nNote that the simulation might be in inconsistent state, "+
"continue only with caution.\n\n Show rest of the stack trace?";
SimClock.getIntTime() + "\n\ncaught at:\n" +
e.getStackTrace()[0].toString() +
"\nNote that the simulation might be in inconsistent state, "+
"continue only with caution.\n\n Show rest of the stack trace?";
// rest of the update cycle that caused the exception is skipped
// so the user is warned about the consequences
......@@ -164,7 +177,7 @@ public class DTNSimGUI extends DTNSimUI {
txt += trace.toString()+"\n";
}
JOptionPane.showMessageDialog(getParentFrame(), txt,
"stack trace", JOptionPane.INFORMATION_MESSAGE);
"stack trace", JOptionPane.INFORMATION_MESSAGE);
}
}
......@@ -181,13 +194,13 @@ public class DTNSimGUI extends DTNSimUI {
this.simCancelled = true;
}
/**
* Updates the GUI
*/
public void update(boolean forcedUpdate) {
double guiUpdateInterval = guiControls.getUpdateInterval();
/**
* Updates the GUI
*/
public void update(boolean forcedUpdate) {
double guiUpdateInterval = guiControls.getUpdateInterval();
// update only if long enough simTime has passed (and not forced)
// update only if long enough simTime has passed (and not forced)
if (!forcedUpdate && guiUpdateInterval > (SimClock.getTime()
- this.lastUpdate)) {
return;
......@@ -196,9 +209,10 @@ public class DTNSimGUI extends DTNSimUI {
try {
// run update in EDT, TODO: optimize threading
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
public void run() {
updateView();
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
......@@ -206,101 +220,103 @@ public class DTNSimGUI extends DTNSimUI {
e.printStackTrace();
}
// wait a while if we don't want to run simulation at full speed
if (guiUpdateInterval < 0) {
wait(100*(int)(-guiUpdateInterval));
// wait a while if we don't want to run simulation at full speed
if (guiUpdateInterval < 0) {
wait(100*(int)(-guiUpdateInterval));
}
}
}
/**
* Updates playfield and sim time field
*
*/
private void updateView() {
double simTime = SimClock.getTime();
this.lastUpdate = simTime;
guiControls.setSimTime(simTime); //update time to control panel
this.field.updateField();
}
/**
* Sets the pause of the simulation on/off
* @param paused True if pause should be set on
*/
public void setPaused(boolean paused) {
this.guiControls.setPaused(paused);
}
/**
* Sets a node's graphical presentation in the center of the playfield view
* @param host The node to center
*/
public void setFocus(DTNHost host) {
centerViewAt(host.getLocation());
infoPanel.showInfo(host);
showPath(host.getPath()); // show path on the playfield
}
/**
* Shows a path on the playfield
* @param path The path to show
*/
public void showPath(Path path) {
field.addPath(path);
}
/**
* Returns the world coordinates that are currently in the center
* of the viewport
* @return The coordinates
*/
public Coord getCenterViewCoord() {
JScrollPane sp = main.getPlayFieldScroll();
double midX, midY;
midX = sp.getHorizontalScrollBar().getValue() +
sp.getViewport().getWidth()/2;
midY = sp.getVerticalScrollBar().getValue() +
sp.getViewport().getHeight()/2;
return this.field.getWorldPosition(new Coord(midX, midY));
}
/**
* Sets certain location to be in the center of the playfield view
* @param loc The location to center
*/
public void centerViewAt(Coord loc) {
JScrollPane sp = main.getPlayFieldScroll();
Coord gLoc = this.field.getGraphicsPosition(loc);
int midX, midY;
updateView(); // update graphics to match the values
midX = (int)gLoc.getX() - sp.getViewport().getWidth()/2;
midY = (int)gLoc.getY() - sp.getViewport().getHeight()/2;
sp.getHorizontalScrollBar().setValue(midX);
sp.getVerticalScrollBar().setValue(midY);
}
/**
* Returns the info panel of the GUI
* @return the info panel of the GUI
*/
public InfoPanel getInfoPanel() {
return this.infoPanel;
}
/**
* Returns the parent frame (window) of the gui.
* @return The parent frame
*/
public MainWindow getParentFrame() {
return this.main;
}
/**
* Updates playfield and sim time field
*
*/
private void updateView() {
double simTime = SimClock.getTime();
this.lastUpdate = simTime;
guiControls.setSimTime(simTime); //update time to control panel
this.field.updateField();
}
/**
* Sets the pause of the simulation on/off
* @param paused True if pause should be set on
*/
public void setPaused(boolean paused) {
this.guiControls.setPaused(paused);
}
/**
* Sets a node's graphical presentation in the center of the playfield view
* EDIT: does not center any longer, only displays information
* @param host The node to center
*/
public void setFocus(DTNHost host) {
//centerViewAt(host.getLocation());
infoPanel.showInfo(host);
showPath(host.getPath()); // show path on the playfield
}
/**
* Shows a path on the playfield
* @param path The path to show
*/
public void showPath(Path path) {
field.addPath(path);
}
/**
* Returns the world coordinates that are currently in the center
* of the viewport
* @return The coordinates
*/
public Coord getCenterViewCoord() {
JScrollPane sp = main.getPlayFieldScroll();
double midX, midY;
midX = sp.getHorizontalScrollBar().getValue() +
sp.getViewport().getWidth()/2;
midY = sp.getVerticalScrollBar().getValue() +
sp.getViewport().getHeight()/2;
return this.field.getWorldPosition(new Coord(midX, midY));
}
/**
* Sets certain location to be in the center of the playfield view
* @param loc The location to center
*/
public void centerViewAt(Coord loc) {
JScrollPane sp = main.getPlayFieldScroll();
Coord gLoc = this.field.getGraphicsPosition(loc);
int midX, midY;
updateView(); // update graphics to match the values
midX = (int)gLoc.getX() - sp.getViewport().getWidth()/2;
midY = (int)gLoc.getY() - sp.getViewport().getHeight()/2;
sp.getHorizontalScrollBar().setValue(midX);
sp.getVerticalScrollBar().setValue(midY);
}
/**
* Returns the info panel of the GUI
* @return the info panel of the GUI
*/
public InfoPanel getInfoPanel() {
return this.infoPanel;
}
/**
* Returns the parent frame (window) of the gui.
* @return The parent frame
*/
public MainWindow getParentFrame() {
return this.main;
}
/**
* Suspend thread for ms milliseconds
......@@ -315,22 +331,56 @@ public class DTNSimGUI extends DTNSimUI {
}
/**
* Handler for playfield's mouse clicks.
* Handler for playfield's mouse events.
*/
private class PlayfieldMouseHandler extends MouseAdapter implements
MouseWheelListener {
MouseWheelListener, MouseMotionListener {
/**
* If mouse button is clicked, centers view at that location.
* Record starting point for panning the playfield if a mousebutton is pressed
* @param e The MouseEvent
*/
public void mouseClicked(MouseEvent e) {
public void mousePressed(MouseEvent e) {
java.awt.Point p = e.getPoint();
centerViewAt(field.getWorldPosition(new Coord(p.x, p.y)));
if(SwingUtilities.isLeftMouseButton(e)) { //only on left mouse button
origin = new Point(e.getPoint());
}
}
/**
* Zoom the playfield if the mousewheel is scrolled
* @param e The MouseEvent
*/
public void mouseWheelMoved(java.awt.event.MouseWheelEvent e) {
guiControls.changeZoom(e.getWheelRotation());
guiControls.changeZoom(-e.getWheelRotation());
}
/**
* Pan the playfield if the mouse is dragged
* @param e The MouseEvent
*/
public void mouseDragged(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)) { //only on left mouse button
if (origin != null) {
if (viewPort != null) {
int deltaX = origin.x - e.getX();
int deltaY = origin.y - e.getY();
Rectangle view = viewPort.getViewRect();
view.x += deltaX;
view.y += deltaY;
field.scrollRectToVisible(view);
}
}
updateView(); // update graphics to match the values
}
}
}
}
}
\ No newline at end of file
......@@ -93,7 +93,7 @@ public class NodeGraphic extends PlayFieldGraphic {
// create a copy to prevent concurrent modification exceptions
conList.addAll(node.getConnections());
for (Connection c : conList) {
DTNHost otherNode = c.getOtherNode(node);
DTNHost otherNode = null;
if(c != null) {
otherNode = c.getOtherNode(node);
}
......@@ -111,6 +111,12 @@ public class NodeGraphic extends PlayFieldGraphic {
}
}
if (drawNodeName) {
g2.setColor(hostNameColor);
// Draw node's address next to it
g2.drawString(node.toString(), scale(loc.getX()),
scale(loc.getY()));
}
/* draw node rectangle */
g2.setColor(hostColor);
......@@ -121,13 +127,6 @@ public class NodeGraphic extends PlayFieldGraphic {
g2.setColor(highlightedNodeColor);
g2.fillRect(scale(loc.getX()) - 3 ,scale(loc.getY()) - 3, 6, 6);
}
if (drawNodeName) {
g2.setColor(hostNameColor);
// Draw node's address next to it
g2.drawString(node.toString(), scale(loc.getX()),
scale(loc.getY()));
}
}
/**
......
......@@ -17,6 +17,7 @@ import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.LinkedList;
import javax.swing.JPanel;
......@@ -50,6 +51,8 @@ public class PlayField extends JPanel {
private AffineTransform curTransform;
private double underlayImgDx;
private double underlayImgDy;
private List<DTNHost> highlightedHosts;
/**
* Creates a playfield
......@@ -69,11 +72,15 @@ public class PlayField extends JPanel {
this.imageTransform = null;
this.autoClearOverlay = true;
highlightedHosts = new LinkedList<DTNHost>();
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (focusOnClick) {
focusClosestNode(e.getX(), e.getY());
if(e.getButton() == MouseEvent.BUTTON1) { //only on leftclick
if (focusOnClick) {
focusClosestNode(e.getX(), e.getY());
}
}
}
});
......@@ -194,6 +201,11 @@ public class PlayField extends JPanel {
for (DTNHost h : w.getHosts()) {
new NodeGraphic(h).draw(g2);
}
// draw highlited hosts
for (DTNHost h : highlightedHosts) {
new NodeGraphic(h).draw(g2);
}
// draw overlay graphics
for (int i=0, n=overlayGraphics.size(); i<n; i++) {
......@@ -304,6 +316,9 @@ public class PlayField extends JPanel {
closestDist = dist;
}
}
highlightedHosts.clear();
highlightedHosts.add(closest);
NodeGraphic.setHighlightedNodes(highlightedHosts);
gui.setFocus(closest);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment