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.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
@ -358,8 +359,11 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
/* Zoom in/out via keyboard*/
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);
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);
/* 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 static final long serialVersionUID = -2592452356547803615L;
public void actionPerformed(ActionEvent e) {
@ -575,20 +637,7 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
pixelX = mousePixelPositionX;
}
final long centerTime = (long) (pixelX*currentPixelDivisor);
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);
zoomIn(centerTime, 0.5);
}
};
@ -601,36 +650,18 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
pixelX = popupLocation.x;
popupLocation = null;
}
final long centerTime = (long) (pixelX*currentPixelDivisor);
if (mousePixelPositionX > 0) {
pixelX = mousePixelPositionX;
}
int zoomLevel = 0;
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);
final long centerTime = (long) (pixelX*currentPixelDivisor);
zoomOut(centerTime, 0.5);
}
};
private Action zoomSliderAction = new AbstractAction("Zoom slider (Ctrl+Mouse)") {
private static final long serialVersionUID = -4288046377707363837L;
public void actionPerformed(ActionEvent e) {
int zoomLevel = 0;
while (zoomLevel < ZOOM_LEVELS.length) {
if (currentPixelDivisor <= ZOOM_LEVELS[zoomLevel]) break;
zoomLevel++;
}
final int zoomLevel = zoomGetLevel();
final JSlider zoomSlider = new JSlider(JSlider.VERTICAL, 0, ZOOM_LEVELS.length-1, zoomLevel);
zoomSlider.setInverted(true);
zoomSlider.setPaintTicks(true);
@ -640,12 +671,8 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
zoomSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int zoomLevel = zoomSlider.getValue();
currentPixelDivisor = ZOOM_LEVELS[zoomLevel];
logger.info("Zoom level: " + currentPixelDivisor + " microseconds/pixel " + ((zoomLevel==ZOOM_LEVELS.length-1)?"(MAX)":""));
forceRepaintAndFocus(centerTime, 0.5);
final int zoomLevel = zoomSlider.getValue();
zoomFinishLevel(zoomLevel, centerTime, 0.5);
}
});
@ -1429,11 +1456,13 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
} else if ("split".equals(name)) {
splitPane.setDividerLocation(Integer.parseInt(element.getText()));
} else if ("zoom".equals(name)) {
currentPixelDivisor = ZOOM_LEVELS[Integer.parseInt(element.getText())-1];
forceRepaintAndFocus(0, 0);
/* NB: Historically this is a one-based not zero-based index */
final int zl = Integer.parseInt(element.getText())-1;
zoomFinishLevel(zl, 0, 0);
} else if ("zoomfactor".equals(name)) {
currentPixelDivisor = Double.parseDouble(element.getText());
forceRepaintAndFocus(0, 0);
/* NB: Historically no validation on this option */
final double cpd = Double.parseDouble(element.getText());
zoomFinish(cpd, 0, 0);
}
}
@ -1471,6 +1500,7 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
addMouseListener(mouseAdapter);
addMouseMotionListener(mouseAdapter);
addMouseWheelListener(mouseAdapter);
/* Popup menu */
final JPopupMenu popupMenu = new JPopupMenu();
@ -1575,14 +1605,8 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
double factor = 0.01*(e.getY() - zoomInitialMouseY);
factor = Math.exp(factor);
currentPixelDivisor = zoomInitialPixelDivisor * factor;
if (currentPixelDivisor < ZOOM_LEVELS[0]) {
currentPixelDivisor = ZOOM_LEVELS[0];
}
if (currentPixelDivisor > ZOOM_LEVELS[ZOOM_LEVELS.length-1]) {
currentPixelDivisor = ZOOM_LEVELS[ZOOM_LEVELS.length-1];
}
forceRepaintAndFocus(zoomCenterTime, zoomCenter);
final double cpd = zoomInitialPixelDivisor * factor;
zoomFinish(cpd, zoomCenterTime, zoomCenter);
return;
}
if (e.isAltDown()) {
@ -1674,6 +1698,16 @@ public class TimeLine extends VisPlugin implements HasQuickHelp {
mousePixelPositionX = -1;
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);