more convenient behaviour at zooming in the DataSetDialog

This commit is contained in:
hneemann 2017-06-29 17:57:51 +02:00
parent 054c3e9764
commit a947b2139d
2 changed files with 23 additions and 5 deletions

View File

@ -13,6 +13,7 @@ import java.awt.*;
*/
public class DataSetComponent extends JComponent {
private final DataSet dataSet;
private JScrollPane scrollPane;
/**
* Creates a new dataSet
@ -23,7 +24,7 @@ public class DataSetComponent extends JComponent {
this.dataSet = dataSet;
addMouseWheelListener(e -> {
double f = Math.pow(0.9, e.getWheelRotation());
scale(f); // ToDo keep relative mouse position
scale(f, e.getX());
});
}
@ -46,12 +47,17 @@ public class DataSetComponent extends JComponent {
/**
* Apply a scaling factor
*
* @param f the factor
* @param f the factor
* @param xPos fixed position
*/
public void scale(double f) {
public void scale(double f, int xPos) {
revalidate();
repaint();
dataSet.scale(f);
int x = (int) (xPos * f) - (xPos - (int) scrollPane.getViewport().getViewRect().getX());
if (x < 0) x = 0;
scrollPane.getViewport().setViewPosition(new Point(x, 0));
}
/**
@ -64,4 +70,13 @@ public class DataSetComponent extends JComponent {
revalidate();
repaint();
}
/**
* Sets the used scroll pane
*
* @param scrollPane the scroll pane witch contains this component
*/
public void setScrollPane(JScrollPane scrollPane) {
this.scrollPane = scrollPane;
}
}

View File

@ -68,6 +68,7 @@ public class DataSetDialog extends JDialog implements ModelStateObserver {
dsc = new DataSetComponent(dataSet);
scrollPane = new JScrollPane(dsc);
getContentPane().add(scrollPane);
dsc.setScrollPane(scrollPane);
JToolBar toolBar = new JToolBar();
ToolTipAction maximize = new ToolTipAction(Lang.get("menu_maximize"), ICON_EXPAND) {
@ -79,13 +80,15 @@ public class DataSetDialog extends JDialog implements ModelStateObserver {
ToolTipAction zoomIn = new ToolTipAction(Lang.get("menu_zoomIn"), ICON_ZOOM_IN) {
@Override
public void actionPerformed(ActionEvent e) {
dsc.scale(1.25f);
Rectangle r = scrollPane.getViewport().getViewRect();
dsc.scale(1.25f, r.x + r.width / 2);
}
}.setAccelerator("control PLUS");
ToolTipAction zoomOut = new ToolTipAction(Lang.get("menu_zoomOut"), ICON_ZOOM_OUT) {
@Override
public void actionPerformed(ActionEvent e) {
dsc.scale(0.8f);
Rectangle r = scrollPane.getViewport().getViewRect();
dsc.scale(0.8f, r.x + r.width / 2);
}
}.setAccelerator("control MINUS");