下面列出了java.awt.event.MouseEvent#getWhen() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private MouseEvent translateEvent(MouseEvent e)
{
Dimension stretchedDimensions = client.getStretchedDimensions();
Dimension realDimensions = client.getRealDimensions();
int newX = (int) (e.getX() / (stretchedDimensions.width / realDimensions.getWidth()));
int newY = (int) (e.getY() / (stretchedDimensions.height / realDimensions.getHeight()));
MouseEvent mouseEvent = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), e.getModifiersEx(),
newX, newY, e.getClickCount(), e.isPopupTrigger(), e.getButton());
if (e.isConsumed())
{
mouseEvent.consume();
}
return mouseEvent;
}
protected MouseEvent convertEvent(MouseEvent e){
int column = 0;
int row = rowAtPoint(e.getPoint());
//move the event from table to tree coordinates
Rectangle tableCellRect = getCellRect(row, column, false);
Rectangle treeCellRect = tree.getRowBounds(row);
int dx = 0;
if(tableCellRect != null) dx = -tableCellRect.x;
int dy = 0;
if(tableCellRect !=null && treeCellRect != null)
dy = treeCellRect.y -tableCellRect.y;
e.translatePoint(dx, dy);
return new MouseEvent(
tree, e.getID(), e.getWhen(), e.getModifiers(),
e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()
);
}
private MouseEvent translateEvent(MouseEvent e)
{
Dimension stretchedDimensions = client.getStretchedDimensions();
Dimension realDimensions = client.getRealDimensions();
int newX = (int) (e.getX() / (stretchedDimensions.width / realDimensions.getWidth()));
int newY = (int) (e.getY() / (stretchedDimensions.height / realDimensions.getHeight()));
MouseEvent mouseEvent = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), e.getModifiersEx(),
newX, newY, e.getClickCount(), e.isPopupTrigger(), e.getButton());
if (e.isConsumed())
{
mouseEvent.consume();
}
return mouseEvent;
}
/**
* Overridden to return false, and if the event is a mouse event
* it is forwarded to the tree.<p>
* The behavior for this is debatable, and should really be offered
* as a property. By returning false, all keyboard actions are
* implemented in terms of the table. By returning true, the
* tree would get a chance to do something with the keyboard
* events. For the most part this is ok. But for certain keys,
* such as left/right, the tree will expand/collapse where as
* the table focus should really move to a different column. Page
* up/down should also be implemented in terms of the table.
* By returning false this also has the added benefit that clicking
* outside of the bounds of the tree node, but still in the tree
* column will select the row, whereas if this returned true
* that wouldn't be the case.
* <p>By returning false we are also enforcing the policy that
* the tree will never be editable (at least by a key sequence).
*/
public boolean isCellEditable(EventObject e) {
if (e instanceof MouseEvent) {
for (int counter = getColumnCount() - 1; counter >= 0;
counter--) {
if (getColumnClass(counter) == TreeTableModel.class) {
MouseEvent me = (MouseEvent)e;
MouseEvent newME = new MouseEvent(tree, me.getID(),
me.getWhen(), me.getModifiers(),
me.getX() - getCellRect(0, counter, true).x,
me.getY(), me.getClickCount(),
me.isPopupTrigger());
tree.dispatchEvent(newME);
break;
}
}
}
return false;
}
@Override
public void mousePressed(MouseEvent e) {
if (isEditing) {
return;
}
activateEdit();
// This next bit will generate a second mouse event and pass it on to the edit component so that
// the edit cursor appears under the mouse pointer, not a the start of the component.
final MouseEvent e2 = new MouseEvent(editComponent, e.getID(), e.getWhen(), e.getModifiers(), e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger(), e.getButton());
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
editComponent.dispatchEvent(e2);
}
});
}
/**
* Overridden to return false, and if the event is a mouse event
* it is forwarded to the tree.<p>
* The behavior for this is debatable, and should really be offered
* as a property. By returning false, all keyboard actions are
* implemented in terms of the table. By returning true, the
* tree would get a chance to do something with the keyboard
* events. For the most part this is ok. But for certain keys,
* such as left/right, the tree will expand/collapse where as
* the table focus should really move to a different column. Page
* up/down should also be implemented in terms of the table.
* By returning false this also has the added benefit that clicking
* outside of the bounds of the tree node, but still in the tree
* column will select the row, whereas if this returned true
* that wouldn't be the case.
* <p>By returning false we are also enforcing the policy that
* the tree will never be editable (at least by a key sequence).
*/
@Override
public boolean isCellEditable(EventObject e) {
if (e instanceof MouseEvent) {
for (int counter = getColumnCount() - 1; counter >= 0;
counter--) {
if (getColumnClass(counter) == TreeTableModel.class) {
MouseEvent me = (MouseEvent)e;
MouseEvent newME = new MouseEvent(tree, me.getID(),
me.getWhen(), me.getModifiers(),
me.getX() - getCellRect(0, counter, true).x,
me.getY(), me.getClickCount(),
me.isPopupTrigger());
tree.dispatchEvent(newME);
break;
}
}
}
return false;
}
@Override
public void mouseClicked(MouseEvent e) {
if( !list.isEnabled() )
return;
if( e.getClickCount() > 1 ) {
selIndex = getValidIndex( e.getPoint() );
if( selIndex >= 0 ) {
list.setSelectedIndex( selIndex );
Item item = (Item)list.getModel().getElementAt( selIndex );
ActionEvent ae = new ActionEvent( e.getSource(), e.getID(), "doubleclick", e.getWhen(), e.getModifiers() );
item.invokePreferredAction( ae );
e.consume();
}
}
}
/**
* Overridden to return false, and if the event is a mouse event
* it is forwarded to the tree.<p>
* The behavior for this is debatable, and should really be offered
* as a property. By returning false, all keyboard actions are
* implemented in terms of the table. By returning true, the
* tree would get a chance to do something with the keyboard
* events. For the most part this is ok. But for certain keys,
* such as left/right, the tree will expand/collapse where as
* the table focus should really move to a different column. Page
* up/down should also be implemented in terms of the table.
* By returning false this also has the added benefit that clicking
* outside of the bounds of the tree node, but still in the tree
* column will select the row, whereas if this returned true
* that wouldn't be the case.
* <p>By returning false we are also enforcing the policy that
* the tree will never be editable (at least by a key sequence).
* @param e
* @return true if cell editable
*/
@Override
public boolean isCellEditable(EventObject e)
{
if (e instanceof MouseEvent)
{
for (int counter = getColumnCount() - 1; counter >= 0; counter--)
{
if (getColumnClass(counter) == TreeTableNode.class)
{
MouseEvent me = (MouseEvent) e;
int column = JTreeTable.this.columnAtPoint(me.getPoint());
Rectangle cell = JTreeTable.this.getCellRect(0, column, true);
MouseEvent newME = new MouseEvent(tree, me.getID(), me.getWhen(), me.getModifiers(), me.getX(),
me.getY(), me.getClickCount(), me.isPopupTrigger());
//we translate the event into the tree's coordinate system
newME.translatePoint(-cell.x, 0);
tree.dispatchEvent(newME);
break;
}
}
}
return false;
}
public void mouseClicked(MouseEvent e) {
// hide the balloon by any click
hide();
if (e.getButton() == MouseEvent.BUTTON1) {
ActionEvent aev = new ActionEvent(target, ActionEvent.ACTION_PERFORMED,
liveArguments.getActionCommand(),
e.getWhen(), e.getModifiers());
XToolkit.postEvent(XToolkit.targetToAppContext(aev.getSource()), aev);
}
}
public MouseEvent translateMouseEventFromVertexToViewSpace(V v, MouseEvent e) {
Point viewerPoint = translatePointFromVertexToViewSpace(v, e.getPoint());
VisualizationViewer<V, E> newSource = getPrimaryGraphViewer();
return new MouseEvent(newSource, e.getID(), e.getWhen(), e.getModifiers(),
(int) viewerPoint.getX(), (int) viewerPoint.getY(), e.getClickCount(),
e.isPopupTrigger(), e.getButton());
}
@Override
public boolean editCellAt(int row, int column, EventObject e) {
boolean editResult = super.editCellAt(row, column, e);
if (e instanceof MouseEvent && isTreeColumn(column)){
MouseEvent me = (MouseEvent)e;
int y = me.getY();
if (getRowHeight() != myTree.getRowHeight()) {
// fix y if row heights are not equal
// [todo]: review setRowHeight to synchronize heights correctly!
final Rectangle tableCellRect = getCellRect(row, column, true);
y = Math.min(y - tableCellRect.y, myTree.getRowHeight() - 1) + row * myTree.getRowHeight();
}
MouseEvent newEvent = new MouseEvent(myTree, me.getID(),
me.getWhen(), me.getModifiers(),
me.getX() - getCellRect(0, column, true).x,
y, me.getClickCount(),
me.isPopupTrigger()
);
myTree.dispatchEvent(newEvent);
// Some LAFs, for example, Aqua under MAC OS X
// expand tree node by MOUSE_RELEASED event. Unfortunately,
// it's not possible to find easy way to wedge in table's
// event sequense. Therefore we send "synthetic" release event.
if (newEvent.getID()==MouseEvent.MOUSE_PRESSED) {
MouseEvent newME2 = new MouseEvent(
myTree,
MouseEvent.MOUSE_RELEASED,
me.getWhen(), me.getModifiers(),
me.getX() - getCellRect(0, column, true).x,
y - getCellRect(0, column, true).y, me.getClickCount(),
me.isPopupTrigger()
);
myTree.dispatchEvent(newME2);
}
}
return editResult;
}
@Override protected void processMouseMotionEvent(MouseEvent e) {
if (pointOutsidePrefSize(e.getPoint())) {
MouseEvent ev = new MouseEvent(
e.getComponent(), MouseEvent.MOUSE_EXITED, e.getWhen(),
e.getModifiersEx(), e.getX(), e.getY(), e.getXOnScreen(), e.getYOnScreen(),
e.getClickCount(), e.isPopupTrigger(), MouseEvent.NOBUTTON);
super.processMouseEvent(ev);
} else {
super.processMouseMotionEvent(e);
}
}
@Override
public void mousePressed(MouseEvent e) {
int idx = updateMouseLocation(e);
tabState.setPressed(idx);
//One a double click, preserve the tab that was initially clicked, in case
//a re-layout happened. We'll pass that to the action.
long time = e.getWhen();
if (time - pressTime > 200) {
lastPressedTab = idx;
}
pressTime = time;
lastPressedTab = idx;
if (idx != -1) {
TabCellRenderer tcr = getTabCellRenderer(idx);
getTabRect(idx, scratch);
int state = tabState.getState(idx);
//First find the command for the location with the default button -
//TabState may trigger a repaint
String command = tcr.getCommandAtPoint (e.getPoint(), state, scratch);
if (TabDisplayer.COMMAND_CLOSE == command) {
tabState.setCloseButtonContainsMouse(idx);
tabState.setMousePressedInCloseButton(idx);
//We're closing, don't try to maximize this tab if it turns out to be
//a double click
pressTime = -1;
lastPressedTab = -1;
}
potentialCommand (idx, e, state, tcr, scratch);
} else {
tabState.setMousePressedInCloseButton(-1); //just in case
if( e.isPopupTrigger() ) {
displayer.repaint();
performCommand (TabDisplayer.COMMAND_POPUP_REQUEST, -1, e);
}
}
}
@Override
public void mousePressed(MouseEvent event) {
DockLayoutNode over = over(event.getX(), event.getY());
if (over instanceof DockLayout) {
mDividerDragLayout = (DockLayout) over;
mDividerDragStartedAt = event.getWhen();
mDividerDragStartX = event.getX();
mDividerDragStartY = event.getY();
mDividerDragInitialEventPosition = mDividerDragLayout.isHorizontal() ? event.getX() : event.getY();
mDividerDragInitialDividerPosition = mDividerDragLayout.getDividerPosition();
mDividerDragIsValid = false;
MouseCapture.start(this, mDividerDragLayout.isHorizontal() ? Cursors.HORIZONTAL_RESIZE : Cursors.VERTICAL_RESIZE);
}
}
Input(MouseEvent e) {
x = e.getX();
y = e.getY();
when = e.getWhen();
}
@Nonnull
private MouseEvent convertMouseEvent(@Nonnull MouseEvent e) {
// create a new event, almost exactly the same, but in the header
return new MouseEvent(e.getComponent(), e.getID(), e.getWhen(), e.getModifiers(), e.getX(), 0, e.getXOnScreen(), header.getY(),
e.getClickCount(), e.isPopupTrigger(), e.getButton());
}
private void handleClick(MouseEvent e) {
HeapViewerNodeAction.Actions nodeActions = HeapViewerNodeAction.Actions.forNode(windowNode, actionProviders, context, actions);
ActionEvent ae = new ActionEvent(e.getSource(), e.getID(), "left button", e.getWhen(), e.getModifiers()); // NOI18N
nodeActions.performDefaultAction(ae);
}
/**
* This is overridden to forward the event to the tree and start editor timer.
*/
@Override
public boolean isCellEditable(EventObject e) {
if (lastRow != -1) {
TreePath tp = tree.getPathForRow(lastRow);
org.openide.nodes.Node n = tp != null ? Visualizer.findNode(tp.getLastPathComponent()) : null;
if ((n == null) || !n.canRename()) {
//return false;
canEdit = false;
}
}
if (canEdit && (e != null) && (e.getSource() instanceof Timer)) {
return true;
}
if (canEdit && shouldStartEditingTimer(e)) {
startEditingTimer();
} else if (shouldStopEditingTimer(e)) {
timer.stop();
}
if (e instanceof MouseEvent) {
MouseEvent me = (MouseEvent) e;
int column = getTreeColumnIndex();
if (SwingUtilities.isLeftMouseButton(me) && (me.getClickCount() == 2)) {
TreePath path = tree.getPathForRow(TreeTable.this.rowAtPoint(me.getPoint()));
Rectangle r = tree.getPathBounds(path);
if ((me.getX() < (r.x - positionX)) || (me.getX() > (r.x - positionX + r.width))) {
me.translatePoint(r.x - me.getX(), 0);
}
}
MouseEvent newME = new MouseEvent(
TreeTable.this.tree, me.getID(), me.getWhen(), me.getModifiers()+me.getModifiersEx(),
me.getX() - getCellRect(0, column, true).x + positionX, me.getY(), me.getClickCount(),
me.isPopupTrigger()
);
TreeTable.this.tree.dispatchEvent(newME);
}
return false;
}
private void handleMiddleClick(MouseEvent e) {
HeapViewerNodeAction.Actions nodeActions = HeapViewerNodeAction.Actions.forNode(windowNode, actionProviders, context, actions);
ActionEvent ae = new ActionEvent(e.getSource(), e.getID(), "middle button", e.getWhen(), e.getModifiers()); // NOI18N
nodeActions.performMiddleButtonAction(ae);
}
/**
* Clones a {@link MouseEvent}.
*
* @param event The event to clone.
* @param source Pass in a new source.
* @param where Pass in a new location.
* @param refreshTime Pass in {@code true} to generate a new time stamp.
* @return The new {@link MouseEvent}.
*/
public static final MouseEvent cloneMouseEvent(MouseEvent event, Component source, Point where, boolean refreshTime) {
if (event instanceof MouseWheelEvent) {
MouseWheelEvent old = (MouseWheelEvent) event;
return new MouseWheelEvent(source, old.getID(), refreshTime ? System.currentTimeMillis() : event.getWhen(), old.getModifiersEx(), where.x, where.y, old.getClickCount(), old.isPopupTrigger(), old.getScrollType(), old.getScrollAmount(), old.getWheelRotation());
}
return new MouseEvent(source, event.getID(), refreshTime ? System.currentTimeMillis() : event.getWhen(), event.getModifiersEx(), where.x, where.y, event.getClickCount(), event.isPopupTrigger());
}