/* (@)MouseTracker.java */
/* Copyright 2009 Sebastian Haufe
* Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
[url]http://www.apache.org/licenses/LICENSE-2.0[/url]
* Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package com.ebenius;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
/**
* TODO: Javadoc me!
*
* @version $Revision$ as of $Date$
* @author Sebastian Haufe
* @since Playground-3.8
*/
public class MouseTracker {
public static void main(String[] args) {
final DefaultTableModel feedBackModel = new DefaultTableModel(0, 4) {
@Override
public Class<?> getColumnClass(int columnIndex) {
return columnIndex == 0 ? String.class : Boolean.class;
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
feedBackModel.setColumnIdentifiers(new String[] { "Event", "Left Down",
"Middle Down", "Right Down" });
final JTable feedBackTable = new JTable(feedBackModel);
final JLabel testLabel =
new JLabel("<html><body>"
+ "Test your mouse motions here! Works with:"
+ "<ol><li>mouse pressed"
+ "<li>mouse released"
+ "<li>mouse clicked"
+ "<li>mouse entered"
+ "<li>mouse exited"
+ "<li>mouse moved"
+ "<li>mouse dragged"
+ "<li>mouse wheel"
+ "</ol></body></html>");
class MouseVoodoo
implements MouseListener, MouseMotionListener, MouseWheelListener {
private void updateModel(MouseEvent e) {
final String eventName;
switch (e.getID()) {
case MouseEvent.MOUSE_CLICKED:
eventName = "CLICKED";
break;
case MouseEvent.MOUSE_PRESSED:
eventName = "PRESSED";
break;
case MouseEvent.MOUSE_RELEASED:
eventName = "RELEASED";
break;
case MouseEvent.MOUSE_ENTERED:
eventName = "ENTERED";
break;
case MouseEvent.MOUSE_EXITED:
eventName = "EXITED";
break;
case MouseEvent.MOUSE_MOVED:
eventName = "MOVED";
break;
case MouseEvent.MOUSE_DRAGGED:
eventName = "DRAGGED";
break;
case MouseEvent.MOUSE_WHEEL:
eventName = "WHEEL";
break;
default:
eventName = "UNKNOWN";
}
feedBackModel.insertRow(0, new Object[] { eventName,
Boolean.valueOf(SwingUtilities.isLeftMouseButton(e)),
Boolean.valueOf(SwingUtilities.isMiddleMouseButton(e)),
Boolean.valueOf(SwingUtilities.isRightMouseButton(e)), });
if (feedBackModel.getRowCount() > 100) {
feedBackModel.removeRow(100);
}
}
public void mouseClicked(MouseEvent e) {
updateModel(e);
}
public void mouseEntered(MouseEvent e) {
updateModel(e);
}
public void mouseExited(MouseEvent e) {
updateModel(e);
}
public void mousePressed(MouseEvent e) {
updateModel(e);
}
public void mouseReleased(MouseEvent e) {
updateModel(e);
}
public void mouseDragged(MouseEvent e) {
updateModel(e);
}
public void mouseMoved(MouseEvent e) {
updateModel(e);
}
public void mouseWheelMoved(MouseWheelEvent e) {
updateModel(e);
}
}
final MouseVoodoo mouseVoodoo = new MouseVoodoo();
testLabel.addMouseListener(mouseVoodoo);
testLabel.addMouseMotionListener(mouseVoodoo);
testLabel.addMouseWheelListener(mouseVoodoo);
final JPanel contentPane = new JPanel(new BorderLayout(6, 6));
contentPane.add(testLabel, BorderLayout.NORTH);
contentPane.add(new JScrollPane(feedBackTable), BorderLayout.CENTER);
final JFrame f = new JFrame("Test Frame: MouseTracker"); //$NON-NLS-1$
f.setContentPane(contentPane);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
}