public void actionPerformed(ActionEvent event)
{
if (m_zoom != 0.0f)
{
Dimension size = getSize();
JViewport viewPort = (JViewport)getParent();
assert viewPort != null;
Dimension dimView = viewPort.getExtentSize();
setViewPosition(new Point((int)(size.width/2 - dimView.width/2), (int)(size.height/2 - dimView.height)));
}
}
});
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD0, 0), "ctrl_scrollhome");
getActionMap().put("ctrl_scrollhome", new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
if (m_zoom != 0.0f)
setViewPosition(new Point(0, 0));
}
});
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DECIMAL, 0), "ctrl_scrollend");
getActionMap().put("ctrl_scrollend", new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
if (m_zoom != 0.0f)
{
Dimension size = getSize();
setViewPosition(new Point((int)size.width, (int)size.height));
}
}
});
// Mouse
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent ev)
{
if (getZoom() != 0.0f)
{
m_moveStartPoint = ev.getPoint();
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
m_bMoveOutSide = false;
m_bMove = true;
}
}
public void mouseReleased(MouseEvent ev)
{
if (m_bMove)
{
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
m_bMove = false;
}
}
public void mouseExited(MouseEvent ev)
{
if (m_bMove)
{
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
m_bMoveOutSide = true;
}
}
public void mouseEntered(MouseEvent ev)
{
if (m_bMove)
{
if (m_bMoveOutSide)
{
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
m_bMoveOutSide = false;
}
}
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent ev)
{
if (m_bMove && !m_bMoveOutSide)
{
JViewport viewPort = (JViewport)getParent();
assert viewPort != null;
Rectangle rect = viewPort.getViewRect();
if (rect.contains(ev.getPoint()))
{
Point pt = new Point(viewPort.getViewPosition());
pt.translate(m_moveStartPoint.x - ev.getPoint().x, m_moveStartPoint.y - ev.getPoint().y);
setViewPosition(pt);
}
}
}