Merge pull request #271 from pabigot/push/cooja-zoom

Push/cooja zoom
This commit is contained in:
Fredrik Österlind 2013-06-18 04:57:28 -07:00
commit e3e981f412

View File

@ -43,6 +43,7 @@ import java.awt.event.ActionListener;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -358,8 +359,11 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
/* Zoom in/out via keyboard*/ /* Zoom in/out via keyboard*/
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, KeyEvent.CTRL_DOWN_MASK), "zoomIn"); getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, KeyEvent.CTRL_DOWN_MASK), "zoomIn");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, KeyEvent.SHIFT_DOWN_MASK | KeyEvent.CTRL_DOWN_MASK), "zoomIn");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD, KeyEvent.CTRL_DOWN_MASK), "zoomIn");
getActionMap().put("zoomIn", zoomInAction); getActionMap().put("zoomIn", zoomInAction);
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, KeyEvent.CTRL_DOWN_MASK), "zoomOut"); getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, KeyEvent.CTRL_DOWN_MASK), "zoomOut");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, KeyEvent.CTRL_DOWN_MASK), "zoomOut");
getActionMap().put("zoomOut", zoomOutAction); getActionMap().put("zoomOut", zoomOutAction);
/* getContentPane().add(splitPane);*/ /* getContentPane().add(splitPane);*/
@ -562,6 +566,64 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
}); });
} }
private int zoomGetLevel (final double zoomDivisor) {
int zoomLevel = 0;
while (zoomLevel < ZOOM_LEVELS.length) {
if (zoomDivisor <= ZOOM_LEVELS[zoomLevel]) break;
zoomLevel++;
}
return zoomLevel;
}
private int zoomGetLevel () {
return zoomGetLevel(currentPixelDivisor);
}
private double zoomLevelToDivisor (int zoomLevel) {
if (0 > zoomLevel) {
zoomLevel = 0;
} else if (ZOOM_LEVELS.length <= zoomLevel) {
zoomLevel = ZOOM_LEVELS.length - 1;
}
return ZOOM_LEVELS[zoomLevel];
}
private void zoomFinish (final double zoomDivisor,
final long focusTime,
final double focusCenter) {
currentPixelDivisor = zoomDivisor;
String note = "";
if (ZOOM_LEVELS[0] >= zoomDivisor) {
currentPixelDivisor = ZOOM_LEVELS[0];
note = " (MIN)";
} else if (ZOOM_LEVELS[ZOOM_LEVELS.length-1] <= zoomDivisor) {
currentPixelDivisor = ZOOM_LEVELS[ZOOM_LEVELS.length-1];
note = " (MAX)";
}
if (zoomDivisor != currentPixelDivisor) {
logger.info("Zoom level: adjusted out-of-range " + zoomDivisor + " us/pixel");
}
logger.info("Zoom level: " + currentPixelDivisor + " microseconds/pixel " + note);
forceRepaintAndFocus(focusTime, focusCenter);
}
private void zoomFinishLevel (final int zoomLevel,
final long focusTime,
final double focusCenter) {
final double cpd = zoomLevelToDivisor(zoomLevel);
zoomFinish(cpd, focusTime, focusCenter);
}
private void zoomIn (final long focusTime,
final double focusCenter) {
zoomFinishLevel(zoomGetLevel()-1, focusTime, focusCenter);
}
private void zoomOut (final long focusTime,
final double focusCenter) {
zoomFinishLevel(zoomGetLevel()+1, focusTime, focusCenter);
}
private Action zoomInAction = new AbstractAction("Zoom in (Ctrl+)") { private Action zoomInAction = new AbstractAction("Zoom in (Ctrl+)") {
private static final long serialVersionUID = -2592452356547803615L; private static final long serialVersionUID = -2592452356547803615L;
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -575,20 +637,7 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
pixelX = mousePixelPositionX; pixelX = mousePixelPositionX;
} }
final long centerTime = (long) (pixelX*currentPixelDivisor); final long centerTime = (long) (pixelX*currentPixelDivisor);
zoomIn(centerTime, 0.5);
int zoomLevel = 0;
while (zoomLevel < ZOOM_LEVELS.length) {
if (currentPixelDivisor <= ZOOM_LEVELS[zoomLevel]) break;
zoomLevel++;
}
if (zoomLevel > 0) {
zoomLevel--; /* zoom in */
}
currentPixelDivisor = ZOOM_LEVELS[zoomLevel];
logger.info("Zoom level: " + currentPixelDivisor + " microseconds/pixel " + ((zoomLevel==0)?"(MIN)":""));
forceRepaintAndFocus(centerTime, 0.5);
} }
}; };
@ -601,36 +650,18 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
pixelX = popupLocation.x; pixelX = popupLocation.x;
popupLocation = null; popupLocation = null;
} }
final long centerTime = (long) (pixelX*currentPixelDivisor);
if (mousePixelPositionX > 0) { if (mousePixelPositionX > 0) {
pixelX = mousePixelPositionX; pixelX = mousePixelPositionX;
} }
final long centerTime = (long) (pixelX*currentPixelDivisor);
int zoomLevel = 0; zoomOut(centerTime, 0.5);
while (zoomLevel < ZOOM_LEVELS.length) {
if (currentPixelDivisor <= ZOOM_LEVELS[zoomLevel]) break;
zoomLevel++;
}
if (zoomLevel < ZOOM_LEVELS.length-1) {
zoomLevel++; /* zoom out */
}
currentPixelDivisor = ZOOM_LEVELS[zoomLevel];
logger.info("Zoom level: " + currentPixelDivisor + " microseconds/pixel " + ((zoomLevel==ZOOM_LEVELS.length-1)?"(MAX)":""));
forceRepaintAndFocus(centerTime, 0.5);
} }
}; };
private Action zoomSliderAction = new AbstractAction("Zoom slider (Ctrl+Mouse)") { private Action zoomSliderAction = new AbstractAction("Zoom slider (Ctrl+Mouse)") {
private static final long serialVersionUID = -4288046377707363837L; private static final long serialVersionUID = -4288046377707363837L;
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
int zoomLevel = 0; final int zoomLevel = zoomGetLevel();
while (zoomLevel < ZOOM_LEVELS.length) {
if (currentPixelDivisor <= ZOOM_LEVELS[zoomLevel]) break;
zoomLevel++;
}
final JSlider zoomSlider = new JSlider(JSlider.VERTICAL, 0, ZOOM_LEVELS.length-1, zoomLevel); final JSlider zoomSlider = new JSlider(JSlider.VERTICAL, 0, ZOOM_LEVELS.length-1, zoomLevel);
zoomSlider.setInverted(true); zoomSlider.setInverted(true);
zoomSlider.setPaintTicks(true); zoomSlider.setPaintTicks(true);
@ -640,12 +671,8 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
zoomSlider.addChangeListener(new ChangeListener() { zoomSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
int zoomLevel = zoomSlider.getValue(); final int zoomLevel = zoomSlider.getValue();
zoomFinishLevel(zoomLevel, centerTime, 0.5);
currentPixelDivisor = ZOOM_LEVELS[zoomLevel];
logger.info("Zoom level: " + currentPixelDivisor + " microseconds/pixel " + ((zoomLevel==ZOOM_LEVELS.length-1)?"(MAX)":""));
forceRepaintAndFocus(centerTime, 0.5);
} }
}); });
@ -1429,11 +1456,13 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
} else if ("split".equals(name)) { } else if ("split".equals(name)) {
splitPane.setDividerLocation(Integer.parseInt(element.getText())); splitPane.setDividerLocation(Integer.parseInt(element.getText()));
} else if ("zoom".equals(name)) { } else if ("zoom".equals(name)) {
currentPixelDivisor = ZOOM_LEVELS[Integer.parseInt(element.getText())-1]; /* NB: Historically this is a one-based not zero-based index */
forceRepaintAndFocus(0, 0); final int zl = Integer.parseInt(element.getText())-1;
zoomFinishLevel(zl, 0, 0);
} else if ("zoomfactor".equals(name)) { } else if ("zoomfactor".equals(name)) {
currentPixelDivisor = Double.parseDouble(element.getText()); /* NB: Historically no validation on this option */
forceRepaintAndFocus(0, 0); final double cpd = Double.parseDouble(element.getText());
zoomFinish(cpd, 0, 0);
} }
} }
@ -1471,6 +1500,7 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
addMouseListener(mouseAdapter); addMouseListener(mouseAdapter);
addMouseMotionListener(mouseAdapter); addMouseMotionListener(mouseAdapter);
addMouseWheelListener(mouseAdapter);
/* Popup menu */ /* Popup menu */
final JPopupMenu popupMenu = new JPopupMenu(); final JPopupMenu popupMenu = new JPopupMenu();
@ -1575,14 +1605,8 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
double factor = 0.01*(e.getY() - zoomInitialMouseY); double factor = 0.01*(e.getY() - zoomInitialMouseY);
factor = Math.exp(factor); factor = Math.exp(factor);
currentPixelDivisor = zoomInitialPixelDivisor * factor; final double cpd = zoomInitialPixelDivisor * factor;
if (currentPixelDivisor < ZOOM_LEVELS[0]) { zoomFinish(cpd, zoomCenterTime, zoomCenter);
currentPixelDivisor = ZOOM_LEVELS[0];
}
if (currentPixelDivisor > ZOOM_LEVELS[ZOOM_LEVELS.length-1]) {
currentPixelDivisor = ZOOM_LEVELS[ZOOM_LEVELS.length-1];
}
forceRepaintAndFocus(zoomCenterTime, zoomCenter);
return; return;
} }
if (e.isAltDown()) { if (e.isAltDown()) {
@ -1674,6 +1698,16 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
mousePixelPositionX = -1; mousePixelPositionX = -1;
repaint(); repaint();
} }
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.isControlDown()) {
final int nticks = e.getWheelRotation();
final int zoomLevel = zoomGetLevel() + nticks;
final long zct = (long) (e.getX()*currentPixelDivisor);
final double zc = (double) (e.getX() - timeline.getVisibleRect().x) / timeline.getVisibleRect().width;
zoomFinishLevel(zoomLevel, zct, zc);
return;
}
}
}; };
private final Color SEPARATOR_COLOR = new Color(220, 220, 220); private final Color SEPARATOR_COLOR = new Color(220, 220, 220);