下面列出了java.awt.event.MouseEvent#getSource() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private V getVertexUnderMouse(MouseEvent event) {
Object source = event.getSource();
GraphViewer<V, E> viewer = getPrimaryGraphViewer(source);
if (viewer == null) {
return null;
}
VertexMouseInfo<V, E> info =
GraphViewerUtils.convertMouseEventToVertexMouseEvent(viewer, event);
if (info == null) {
return null;
}
V vertex = info.getVertex();
return vertex;
}
@Override
public void mouseClicked(final MouseEvent e) {
final JTableHeader h = (JTableHeader) e.getSource();
final TableColumnModel columnModel = h.getColumnModel();
final int viewColumn = columnModel.getColumnIndexAtX(e.getX());
final int column = columnModel.getColumn(viewColumn).getModelIndex();
if (column != -1) {
int status = getSortingStatus(column);
if (!e.isControlDown()) {
cancelSorting();
}
// Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or
// {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed.
status = status + (e.isShiftDown() ? -1 : 1);
status = ((status + 4) % 3) - 1; // signed mod, returning {-1, 0, 1}
setSortingStatus(column, status);
}
}
private void redispatchEvent(MouseEvent event) {
JList list = (JList) event.getSource();
int index = list.locationToIndex(event.getPoint());
Rectangle cellBounds = list.getCellBounds(index, index);
if (cellBounds == null) {
return;
}
ListModel model = list.getModel();
Object state = model.getElementAt(index);
ListCellRenderer renderer = list.getCellRenderer();
Component rendererComponent =
renderer.getListCellRendererComponent(list, state, index, true, true);
rendererComponent.setBounds(cellBounds);
Point p = event.getPoint();
p.translate(-cellBounds.x, -cellBounds.y);
MouseEvent newEvent =
new MouseEvent(rendererComponent, event.getID(), event.getWhen(), event.getModifiers(),
p.x, p.y, event.getXOnScreen(), event.getYOnScreen(), event.getClickCount(),
event.isPopupTrigger(), event.getButton());
rendererComponent.dispatchEvent(newEvent);
list.repaint();
}
public void mousePressed(MouseEvent e) {
// Mark drag start point just in case user starts dragging
lastDragPoint = e.getPoint();
// Listeners for first step
if(e.getSource() == this && screen.getCurrentStep() == ImportScreen.Step.FIRST){
// Mark header
if(tableFrame.isInHeaderMarkingMode()){
markHeaderAtPoint(e.getPoint());
}
// Mark footer
else if(tableFrame.isInFooterMarkingMode()){
markFooterAtPoint(e.getPoint());
}
// Mark title row
else if(tableFrame.isInTitleMarkingMode()){
markTitleAtPoint(e.getPoint(), true);
}
else {
JOptionPane.showMessageDialog(screen.getFrame(), NO_TOOL_DIALOG_TITLE, NO_TOOL_DIALOG_TEXT, JOptionPane.WARNING_MESSAGE);
}
}
// Listeners for second step
else if(e.getSource() == this && screen.getCurrentStep() == ImportScreen.Step.SECOND){
markColumnAtPoint(e.getPoint());
}
}
public void mouseEntered(MouseEvent e) {
source = (Container) e.getSource();
if (source instanceof JTable){
source = ((JTable)source).getParent();
}
//if (previous_source != null && previous_source == source){
// return;
//}
Rasi ascendant = chartData.getAscendant();
if (ascendant != null){
int bhava = Rasi.ofIndex(housePanel.indexOf(source)).bhava(ascendant);
WindowLabelModel windowModel = new DefaultWindowLabelModel(String.valueOf(bhava));
window = new AstrosoftWindow(windowModel);
window.setPreferredSize(new Dimension(30,20));
Point houseLoc = source.getLocationOnScreen();
Point loc = e.getPoint();
loc.translate(houseLoc.x + 10,houseLoc.y - 10);
window.show(loc);
}
}
public void mousePressed(MouseEvent e) {
ChartPanel chartPanel = (ChartPanel) e.getSource();
Rectangle2D screenDataArea = chartPanel.getScreenDataArea(e.getX(),
e.getY());
if (screenDataArea != null) {
this.zoomPoint = ShapeUtilities.getPointInRectangle(e.getX(),
e.getY(), screenDataArea);
}
else {
this.zoomPoint = null;
}
}
/**
* Updates active of uncertainties if checkboxes in table are clicked.
* This method uses currentRow to check which row was clicked, because
* this listener is added to all checkboxes in active column.
*
* This method will not work correctly if table rows are in different order
* than uncertainties in list in dataModel, but current getValueAt ensures
* that order is the same.
*/
private void checkBoxActionListener(MouseEvent e) {
JCheckBox checkBox = (JCheckBox) e.getSource();
checkBox.setSelected(!checkBox.isSelected());
if (this.pdfDefinitionTableModel.getCurrentColumn() == PDFDefinitionTableModel.COLUMN_IS_ACTIVE) {
PDF pdf = this.pdfDefinitionTableModel.getUncertaintiesObject().getPdf(this.pdfDefinitionTableModel.getCurrentRow());
pdf.getUncertainItem().setActive(checkBox.isSelected());
//if set to inactive.
if (!pdf.getUncertainItem().isActive()) {
//remove autocorrelation for deactivated pdf, if present.
//get autocorrelation object with same Id as pdf.
AutoCorrelationFunction autoCorrelationFunction =
this.pdfDefinitionTableModel.getUncertaintiesObject()
.getAutoCorrelationFunction(pdf.getUncertainItem().getId());
//remove autoCorrelationFunction if present.
if (autoCorrelationFunction != null) {
this.pdfDefinitionTableModel.getUncertaintiesObject()
.removeAutoCorrelationFunction(autoCorrelationFunction);
}
}
//update only correlationTable (via parentController and correlationTableController).
this.parentController.getCorrelationTableController().updateCorrelationTable(this.parentController.getModel());
}
}
/**
*/
void hyperlinkClicked(MouseEvent evt)
{
JPanel link = (JPanel)evt.getSource();
JRPrintHyperlink element = linksMap.get(link);
hyperlinkClicked(element);
}
public void mouseMoved(MouseEvent e) {
check(e);
Component comp = (Component)e.getSource();
movedBounds = comp.getBounds(movedBounds);
cursorType = getCursorType(movedBounds, e.getPoint());
comp.setCursor(Cursor.getPredefinedCursor(cursorType));
}
public @Override void mouseExited(MouseEvent evt) {
Object src = evt.getSource();
if (src instanceof AbstractButton)
{
AbstractButton button = (AbstractButton)evt.getSource();
removeButtonContentAreaAndBorder(button);
}
}
public void handleJavaMouseEvent(MouseEvent e) {
switch (e.getID()) {
case MouseEvent.MOUSE_PRESSED:
// Note that Swing requests focus in its own mouse event handler.
if (target == e.getSource() &&
!((Component)target).isFocusOwner() &&
WKeyboardFocusManagerPeer.shouldFocusOnClick((Component)target))
{
WKeyboardFocusManagerPeer.requestFocusFor((Component)target,
CausedFocusEvent.Cause.MOUSE_EVENT);
}
break;
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
if (e.getSource() == constraints) {
if (constraints.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(Gui.frame, "No constraint selected.", "Must Select a Constraint", JOptionPane.ERROR_MESSAGE);
return;
}
String selected = ((String) constraints.getSelectedValue()).split("\\[")[0];
constraintEditor("OK",selected);
}
}
}
@Override
public void mouseEntered (MouseEvent e)
{
JMenuItem item = (JMenuItem) e.getSource();
InterAction action = (InterAction) item.getAction();
action.publish();
}
void handleJavaMouseEvent(MouseEvent e) {
super.handleJavaMouseEvent(e);
int id = e.getID();
switch (id) {
case MouseEvent.MOUSE_PRESSED:
if (XToolkit.isLeftMouseButton(e) ) {
Button b = (Button) e.getSource();
if(b.contains(e.getX(), e.getY())) {
if (!isEnabled()) {
// Disabled buttons ignore all input...
return;
}
pressed = true;
armed = true;
repaint();
}
}
break;
case MouseEvent.MOUSE_RELEASED:
if (XToolkit.isLeftMouseButton(e)) {
if (armed)
{
action(e.getWhen(),e.getModifiers());
}
pressed = false;
armed = false;
repaint();
}
break;
case MouseEvent.MOUSE_ENTERED:
if (pressed)
armed = true;
break;
case MouseEvent.MOUSE_EXITED:
armed = false;
break;
}
}
@Override
public void mouseMoved(MouseEvent event) {
// This should only be called if underlineOnRollover is true
JTable table = (JTable) event.getSource();
// Locate the table cell under the event location
int oldHitColumnIndex = hitColumnIndex;
int oldHitRowIndex = hitRowIndex;
checkIfPointInsideHyperlink(event.getPoint());
if (hitRowIndex != oldHitRowIndex ||
hitColumnIndex != oldHitColumnIndex) {
if (hitRowIndex != -1) {
if (tableCursor == null) {
tableCursor = table.getCursor();
}
table.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
} else {
table.setCursor(tableCursor);
}
// repaint the cells affected by rollover
Rectangle repaintRect;
if (hitRowIndex != -1 && hitColumnIndex != -1) {
// we need to repaint new cell with rollover underline
// cellRect already contains rect of hit cell
if (oldHitRowIndex != -1 && oldHitColumnIndex != -1) {
// we also need to repaint previously underlined hyperlink cell
// to remove the underline
repaintRect = cellRect.union(
table.getCellRect(oldHitRowIndex, oldHitColumnIndex, false));
} else {
// we don't have a previously underlined hyperlink, so just repaint new one'
repaintRect = table.getCellRect(hitRowIndex, hitColumnIndex, false);
}
} else {
// we just need to repaint previously underlined hyperlink cell
//to remove the underline
repaintRect = table.getCellRect(oldHitRowIndex, oldHitColumnIndex, false);
}
table.repaint(repaintRect);
}
}
/**
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
*/
public void mouseExited(MouseEvent ev) {
Window w = (Window) ev.getSource();
w.setCursor(lastCursor);
}
/**
* {@inheritDoc}
*/
@Override
public void mouseReleased(MouseEvent e) {
if (getMap() == null
|| e.getButton() == MouseEvent.BUTTON1
|| getGUI().getFocus() == null) return;
final JComponent component = (JComponent)e.getSource();
final MapEditorController controller
= getFreeColClient().getMapEditorController();
final boolean isTransformActive = controller.getMapTransform() != null;
endPoint = e.getPoint();
if (startPoint == null) startPoint = endPoint;
drawBox(component, startPoint, endPoint);
Tile start = canvas.convertToMapTile(startPoint.x, startPoint.y);
Tile end = (startPoint == endPoint) ? start
: canvas.convertToMapTile(endPoint.x, endPoint.y);
// edit 2 more conditions in if statement. we need to
// check for coordinator of X and Y if (x,y) outside of
// map then dont focus to that else setfocus to that
// position no option selected, just center map
if (!isTransformActive && end.getX() >= 0 && end.getY() >= 0) {
getGUI().setFocus(end);
return;
}
// find the area to transform
int min_x, max_x, min_y, max_y;
if (start.getX() < end.getX()) {
min_x = start.getX();
max_x = end.getX();
} else {
min_x = end.getX();
max_x = start.getX();
}
if (start.getY() < end.getY()) {
min_y = start.getY();
max_y = end.getY();
} else {
min_y = end.getY();
max_y = start.getY();
}
// apply transformation to all tiles in the area
Tile t = null;
for (int x = min_x; x <= max_x; x++) {
for (int y = min_y; y <= max_y; y++) {
t = getMap().getTile(x, y);
if (t != null) {
controller.transform(t);
}
}
}
if (controller.getMapTransform() instanceof TileTypeTransform) {
for (int x = min_x - 2; x <= max_x + 2; x++) {
for (int y = min_y - 2; y <= max_y + 2; y++) {
t = getMap().getTile(x, y);
if (t != null && t.getType().isWater()) {
TerrainGenerator.encodeStyle(t);
}
}
}
}
getGUI().refresh();
canvas.requestFocus();
}
void handleJavaMouseEvent(MouseEvent e) {
super.handleJavaMouseEvent(e);
int id = e.getID();
switch (id) {
case MouseEvent.MOUSE_PRESSED:
if (XToolkit.isLeftMouseButton(e) ) {
Button b = (Button) e.getSource();
if(b.contains(e.getX(), e.getY())) {
if (!isEnabled()) {
// Disabled buttons ignore all input...
return;
}
pressed = true;
armed = true;
repaint();
}
}
break;
case MouseEvent.MOUSE_RELEASED:
if (XToolkit.isLeftMouseButton(e)) {
if (armed)
{
action(e.getWhen(),e.getModifiers());
}
pressed = false;
armed = false;
repaint();
}
break;
case MouseEvent.MOUSE_ENTERED:
if (pressed)
armed = true;
break;
case MouseEvent.MOUSE_EXITED:
armed = false;
break;
}
}
/**
* v1.0.1:修复自定义拖拽区域BUG, 增加边界判断
*/
public void mousePressed(MouseEvent e)
{
Window window = (Window) e.getSource();
JRootPane root = LuckWindowUtil.getRootPane(window);
// 不包含窗体装饰直接返回
if (root == null || root.getWindowDecorationStyle() == JRootPane.NONE)
{
return;
}
if (window != null)
{
window.toFront();
}
// 如果是单击标题栏, 则标记接下来的拖动事件为移动窗口, 判断当前鼠标是否超出边界
if (dragArea.contains(e.getPoint())
&& dragCursor == Cursor.DEFAULT_CURSOR)
{
if(window instanceof JFrame)
{
JFrame frame = (JFrame)window;
// 如果当前窗体是全屏状态则直接返回
if(frame.getExtendedState() == JFrame.MAXIMIZED_BOTH)
{
return;
}
}
// 设置为可以移动并记录当前坐标
isMovingWindow = true;
dragOffsetX = e.getPoint().x;
dragOffsetY = e.getPoint().y;
}
else if(LuckWindowUtil.isResizable(window))
{
dragOffsetX = e.getPoint().x;
dragOffsetY = e.getPoint().y;
dragWidth = window.getWidth();
dragHeight = window.getHeight();
JRootPane rootPane = LuckWindowUtil.getRootPane(window);
if(rootPane != null && LuckWindowUtil.isResizable(window))
{
dragCursor = getCursor(dragWidth, dragHeight, e.getPoint(), rootPane.getInsets());
}
}
}
/**
* Returns a point where the given popup menu should be shown. The point is
* calculated by adjusting the X and Y coordinates from the given mouse
* event so that the popup menu will not be clipped by the screen
* boundaries.
*
* @param popup
* the popup menu
* @param event
* the mouse event
* @return the point where the popup menu should be shown
*/
public static Point getPopupMenuShowPoint(JPopupMenu popup, MouseEvent event) {
Component source = (Component) event.getSource();
Point topLeftSource = source.getLocationOnScreen();
Point ptRet = getPopupMenuShowPoint(popup,
topLeftSource.x + event.getX(), topLeftSource.y + event.getY());
ptRet.translate(-topLeftSource.x, -topLeftSource.y);
return ptRet;
}