Change mouse button handling in simulation plot. Left click drag zooms
area, and ctrl left click drag pans.
This commit is contained in:
parent
06e702839f
commit
b16917995d
@ -1099,7 +1099,7 @@ TCMotorSelPan.noDescription = No description available.
|
|||||||
|
|
||||||
! PlotDialog
|
! PlotDialog
|
||||||
PlotDialog.CheckBox.Showdatapoints = Show data points
|
PlotDialog.CheckBox.Showdatapoints = Show data points
|
||||||
PlotDialog.lbl.Chart = mouse wheel to zoom. alt-mouse wheel to zoom x axis only. drag to pan.
|
PlotDialog.lbl.Chart = left click drag to zoom area. mouse wheel to zoom. ctrl-mouse wheel to zoom x axis only. ctrl-left click drag to pan. right click drag to zoom dynamically.
|
||||||
|
|
||||||
|
|
||||||
! "main" prefix is used for the main application dialog
|
! "main" prefix is used for the main application dialog
|
||||||
|
@ -15,7 +15,6 @@ import javax.swing.BorderFactory;
|
|||||||
import org.jfree.chart.ChartPanel;
|
import org.jfree.chart.ChartPanel;
|
||||||
import org.jfree.chart.ChartRenderingInfo;
|
import org.jfree.chart.ChartRenderingInfo;
|
||||||
import org.jfree.chart.JFreeChart;
|
import org.jfree.chart.JFreeChart;
|
||||||
import org.jfree.chart.plot.Pannable;
|
|
||||||
import org.jfree.chart.plot.PiePlot;
|
import org.jfree.chart.plot.PiePlot;
|
||||||
import org.jfree.chart.plot.Plot;
|
import org.jfree.chart.plot.Plot;
|
||||||
import org.jfree.chart.plot.PlotOrientation;
|
import org.jfree.chart.plot.PlotOrientation;
|
||||||
@ -41,18 +40,21 @@ public class SimulationChart extends ChartPanel {
|
|||||||
private double panW;
|
private double panW;
|
||||||
private double panH;
|
private double panH;
|
||||||
|
|
||||||
private enum Interaction {ZOOM, PAN};
|
private enum Interaction {
|
||||||
private Interaction interaction = Interaction.PAN;
|
ZOOM
|
||||||
|
};
|
||||||
|
|
||||||
|
private Interaction interaction = null;
|
||||||
|
|
||||||
private MouseWheelHandler mouseWheelHandler = null;
|
private MouseWheelHandler mouseWheelHandler = null;
|
||||||
|
|
||||||
public SimulationChart(JFreeChart chart) {
|
public SimulationChart(JFreeChart chart) {
|
||||||
super(chart,
|
super(chart,
|
||||||
/* properties */false,
|
/* properties */false,
|
||||||
/* save */ true,
|
/* save */true,
|
||||||
/* print */ false,
|
/* print */false,
|
||||||
/* zoom */ true,
|
/* zoom */true,
|
||||||
/* tooltips */ true);
|
/* tooltips */true);
|
||||||
this.setMouseWheelEnabled(true);
|
this.setMouseWheelEnabled(true);
|
||||||
this.setEnforceFileExtensions(true);
|
this.setEnforceFileExtensions(true);
|
||||||
this.setInitialDelay(500);
|
this.setInitialDelay(500);
|
||||||
@ -66,10 +68,10 @@ public class SimulationChart extends ChartPanel {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setMouseWheelEnabled(boolean flag) {
|
public void setMouseWheelEnabled(boolean flag) {
|
||||||
if ( flag && mouseWheelHandler == null ) {
|
if (flag && mouseWheelHandler == null) {
|
||||||
this.mouseWheelHandler = new MouseWheelHandler();
|
this.mouseWheelHandler = new MouseWheelHandler();
|
||||||
this.addMouseWheelListener(this.mouseWheelHandler);
|
this.addMouseWheelListener(this.mouseWheelHandler);
|
||||||
} else if ( !flag && mouseWheelHandler != null ) {
|
} else if (!flag && mouseWheelHandler != null) {
|
||||||
this.removeMouseWheelListener(this.mouseWheelHandler);
|
this.removeMouseWheelListener(this.mouseWheelHandler);
|
||||||
this.mouseWheelHandler = null;
|
this.mouseWheelHandler = null;
|
||||||
}
|
}
|
||||||
@ -77,39 +79,41 @@ public class SimulationChart extends ChartPanel {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mousePressed(MouseEvent e) {
|
public void mousePressed(MouseEvent e) {
|
||||||
if ( e.getButton() == MouseEvent.BUTTON1 || e.getButton() == MouseEvent.BUTTON3) {
|
if (e.getButton() == MouseEvent.BUTTON3) {
|
||||||
// if no modifiers, use pan
|
|
||||||
Rectangle2D screenDataArea = getScreenDataArea(e.getX(), e.getY());
|
Rectangle2D screenDataArea = getScreenDataArea(e.getX(), e.getY());
|
||||||
|
|
||||||
if ( screenDataArea != null && screenDataArea.contains(e.getPoint())) {
|
if (screenDataArea != null && screenDataArea.contains(e.getPoint())) {
|
||||||
this.panW = screenDataArea.getWidth();
|
this.panW = screenDataArea.getWidth();
|
||||||
this.panH = screenDataArea.getHeight();
|
this.panH = screenDataArea.getHeight();
|
||||||
this.panLast = e.getPoint();
|
this.panLast = e.getPoint();
|
||||||
this.startPoint = e.getPoint();
|
this.startPoint = e.getPoint();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ( e.getButton() == MouseEvent.BUTTON2 ) {
|
|
||||||
// middle/scroll button
|
|
||||||
} else if ( e.getButton() == MouseEvent.BUTTON3 ) { // right button
|
|
||||||
interaction = Interaction.ZOOM;
|
interaction = Interaction.ZOOM;
|
||||||
setCursor (Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
|
setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
|
||||||
} else if ( e.getButton() == MouseEvent.BUTTON1 ) { // left button
|
|
||||||
interaction = Interaction.PAN;
|
}
|
||||||
setCursor( Cursor.getPredefinedCursor( Cursor.MOVE_CURSOR));
|
else {
|
||||||
|
interaction = null;
|
||||||
|
super.mousePressed(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseDragged(MouseEvent e) {
|
public void mouseDragged(MouseEvent e) {
|
||||||
if ( panLast == null ) {
|
if (interaction == null) {
|
||||||
|
super.mouseDragged(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (panLast == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
double dx = e.getX() - this.panLast.getX();
|
double dx = e.getX() - this.panLast.getX();
|
||||||
double dy = e.getY() - this.panLast.getY();
|
double dy = e.getY() - this.panLast.getY();
|
||||||
if ( dx == 0.0 && dy == 0.0 ) {
|
if (dx == 0.0 && dy == 0.0) {
|
||||||
return ;
|
return;
|
||||||
}
|
}
|
||||||
double wPercent = -dx / this.panW;
|
double wPercent = -dx / this.panW;
|
||||||
double hPercent = dy / this.panH;
|
double hPercent = dy / this.panH;
|
||||||
@ -117,34 +121,23 @@ public class SimulationChart extends ChartPanel {
|
|||||||
this.getChart().getPlot().setNotify(false);
|
this.getChart().getPlot().setNotify(false);
|
||||||
|
|
||||||
switch (interaction) {
|
switch (interaction) {
|
||||||
case PAN:
|
case ZOOM:
|
||||||
Pannable p = (Pannable) this.getChart().getPlot();
|
Zoomable pz = (Zoomable) this.getChart().getPlot();
|
||||||
if ( p.getOrientation() == PlotOrientation.VERTICAL){
|
|
||||||
p.panDomainAxes( wPercent, this.getChartRenderingInfo().getPlotInfo(),panLast);
|
|
||||||
p.panRangeAxes( hPercent, this.getChartRenderingInfo().getPlotInfo(),panLast);
|
|
||||||
} else {
|
|
||||||
p.panDomainAxes( hPercent, this.getChartRenderingInfo().getPlotInfo(),panLast);
|
|
||||||
p.panRangeAxes( wPercent, this.getChartRenderingInfo().getPlotInfo(),panLast);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
double zoomx = 1 + 2 * wPercent;
|
||||||
case ZOOM:
|
double zoomy = 1 + 2 * hPercent;
|
||||||
Zoomable pz = (Zoomable) this.getChart().getPlot();
|
|
||||||
|
|
||||||
double zoomx = 1 + 2*wPercent;
|
Point2D anchor = SimulationChart.this.translateScreenToJava2D(startPoint);
|
||||||
double zoomy = 1 + 2*hPercent;
|
|
||||||
|
|
||||||
Point2D anchor = SimulationChart.this.translateScreenToJava2D(startPoint);
|
if (pz.getOrientation() == PlotOrientation.VERTICAL) {
|
||||||
|
pz.zoomDomainAxes(zoomx, this.getChartRenderingInfo().getPlotInfo(), anchor, true);
|
||||||
|
pz.zoomRangeAxes(zoomy, this.getChartRenderingInfo().getPlotInfo(), anchor, true);
|
||||||
|
} else {
|
||||||
|
pz.zoomRangeAxes(zoomx, this.getChartRenderingInfo().getPlotInfo(), anchor, true);
|
||||||
|
pz.zoomDomainAxes(zoomy, this.getChartRenderingInfo().getPlotInfo(), anchor, true);
|
||||||
|
}
|
||||||
|
|
||||||
if ( pz.getOrientation() == PlotOrientation.VERTICAL) {
|
break;
|
||||||
pz.zoomDomainAxes(zoomx, this.getChartRenderingInfo().getPlotInfo(), anchor, true);
|
|
||||||
pz.zoomRangeAxes(zoomy, this.getChartRenderingInfo().getPlotInfo(), anchor, true);
|
|
||||||
} else {
|
|
||||||
pz.zoomRangeAxes(zoomx, this.getChartRenderingInfo().getPlotInfo(), anchor, true);
|
|
||||||
pz.zoomDomainAxes(zoomy, this.getChartRenderingInfo().getPlotInfo(), anchor, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -154,10 +147,16 @@ public class SimulationChart extends ChartPanel {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseReleased(MouseEvent e) {
|
public void mouseReleased(MouseEvent e) {
|
||||||
if ( this.panLast != null ) {
|
if (interaction == null) {
|
||||||
|
super.mouseReleased(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.panLast != null) {
|
||||||
this.panLast = null;
|
this.panLast = null;
|
||||||
setCursor(Cursor.getDefaultCursor());
|
setCursor(Cursor.getDefaultCursor());
|
||||||
}
|
}
|
||||||
|
interaction = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -174,95 +173,95 @@ public class SimulationChart extends ChartPanel {
|
|||||||
*/
|
*/
|
||||||
class MouseWheelHandler implements MouseWheelListener, Serializable {
|
class MouseWheelHandler implements MouseWheelListener, Serializable {
|
||||||
|
|
||||||
/** The zoom factor. */
|
/** The zoom factor. */
|
||||||
double zoomFactor;
|
double zoomFactor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance for the specified chart panel.
|
* Creates a new instance for the specified chart panel.
|
||||||
*
|
*
|
||||||
* @param chartPanel the chart panel (<code>null</code> not permitted).
|
* @param chartPanel the chart panel (<code>null</code> not permitted).
|
||||||
*/
|
*/
|
||||||
public MouseWheelHandler() {
|
public MouseWheelHandler() {
|
||||||
this.zoomFactor = 0.10;
|
this.zoomFactor = 0.10;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current zoom factor. The default value is 0.10 (ten
|
* Returns the current zoom factor. The default value is 0.10 (ten
|
||||||
* percent).
|
* percent).
|
||||||
*
|
*
|
||||||
* @return The zoom factor.
|
* @return The zoom factor.
|
||||||
*
|
*
|
||||||
* @see #setZoomFactor(double)
|
* @see #setZoomFactor(double)
|
||||||
*/
|
*/
|
||||||
public double getZoomFactor() {
|
public double getZoomFactor() {
|
||||||
return this.zoomFactor;
|
return this.zoomFactor;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the zoom factor.
|
* Sets the zoom factor.
|
||||||
*
|
*
|
||||||
* @param zoomFactor the zoom factor.
|
* @param zoomFactor the zoom factor.
|
||||||
*
|
*
|
||||||
* @see #getZoomFactor()
|
* @see #getZoomFactor()
|
||||||
*/
|
*/
|
||||||
public void setZoomFactor(double zoomFactor) {
|
public void setZoomFactor(double zoomFactor) {
|
||||||
this.zoomFactor = zoomFactor;
|
this.zoomFactor = zoomFactor;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles a mouse wheel event from the underlying chart panel.
|
* Handles a mouse wheel event from the underlying chart panel.
|
||||||
*
|
*
|
||||||
* @param e the event.
|
* @param e the event.
|
||||||
*/
|
*/
|
||||||
public void mouseWheelMoved(MouseWheelEvent e) {
|
public void mouseWheelMoved(MouseWheelEvent e) {
|
||||||
JFreeChart chart = SimulationChart.this.getChart();
|
JFreeChart chart = SimulationChart.this.getChart();
|
||||||
if (chart == null) {
|
if (chart == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Plot plot = chart.getPlot();
|
Plot plot = chart.getPlot();
|
||||||
if (plot instanceof Zoomable) {
|
if (plot instanceof Zoomable) {
|
||||||
Zoomable zoomable = (Zoomable) plot;
|
Zoomable zoomable = (Zoomable) plot;
|
||||||
handleZoomable(zoomable, e);
|
handleZoomable(zoomable, e);
|
||||||
}
|
}
|
||||||
else if (plot instanceof PiePlot) {
|
else if (plot instanceof PiePlot) {
|
||||||
PiePlot pp = (PiePlot) plot;
|
PiePlot pp = (PiePlot) plot;
|
||||||
pp.handleMouseWheelRotation(e.getWheelRotation());
|
pp.handleMouseWheelRotation(e.getWheelRotation());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle the case where a plot implements the {@link Zoomable} interface.
|
* Handle the case where a plot implements the {@link Zoomable} interface.
|
||||||
*
|
*
|
||||||
* @param zoomable the zoomable plot.
|
* @param zoomable the zoomable plot.
|
||||||
* @param e the mouse wheel event.
|
* @param e the mouse wheel event.
|
||||||
*/
|
*/
|
||||||
private void handleZoomable(Zoomable zoomable, MouseWheelEvent e) {
|
private void handleZoomable(Zoomable zoomable, MouseWheelEvent e) {
|
||||||
// don't zoom unless the mouse pointer is in the plot's data area
|
// don't zoom unless the mouse pointer is in the plot's data area
|
||||||
ChartRenderingInfo info = SimulationChart.this.getChartRenderingInfo();
|
ChartRenderingInfo info = SimulationChart.this.getChartRenderingInfo();
|
||||||
PlotRenderingInfo pinfo = info.getPlotInfo();
|
PlotRenderingInfo pinfo = info.getPlotInfo();
|
||||||
Point2D p = SimulationChart.this.translateScreenToJava2D(e.getPoint());
|
Point2D p = SimulationChart.this.translateScreenToJava2D(e.getPoint());
|
||||||
if (!pinfo.getDataArea().contains(p)) {
|
if (!pinfo.getDataArea().contains(p)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Plot plot = (Plot) zoomable;
|
Plot plot = (Plot) zoomable;
|
||||||
// do not notify while zooming each axis
|
// do not notify while zooming each axis
|
||||||
boolean notifyState = plot.isNotify();
|
boolean notifyState = plot.isNotify();
|
||||||
plot.setNotify(false);
|
plot.setNotify(false);
|
||||||
int clicks = e.getWheelRotation();
|
int clicks = e.getWheelRotation();
|
||||||
double zf = 1.0 + this.zoomFactor;
|
double zf = 1.0 + this.zoomFactor;
|
||||||
if (clicks < 0) {
|
if (clicks < 0) {
|
||||||
zf = 1.0 / zf;
|
zf = 1.0 / zf;
|
||||||
}
|
}
|
||||||
if (SimulationChart.this.isDomainZoomable()) {
|
if (SimulationChart.this.isDomainZoomable()) {
|
||||||
zoomable.zoomDomainAxes(zf, pinfo, p, true);
|
zoomable.zoomDomainAxes(zf, pinfo, p, true);
|
||||||
}
|
}
|
||||||
boolean domainOnly = ( e.getModifiers() & InputEvent.ALT_MASK ) != 0;
|
boolean domainOnly = (e.getModifiers() & InputEvent.CTRL_MASK) != 0;
|
||||||
if (SimulationChart.this.isRangeZoomable() && !domainOnly ) {
|
if (SimulationChart.this.isRangeZoomable() && !domainOnly) {
|
||||||
zoomable.zoomRangeAxes(zf, pinfo, p, true);
|
zoomable.zoomRangeAxes(zf, pinfo, p, true);
|
||||||
}
|
}
|
||||||
plot.setNotify(notifyState); // this generates the change event too
|
plot.setNotify(notifyState); // this generates the change event too
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user