ClickListener - helper class to avoid default mouseClicked() sensitivity problem. Reports onClick() if there was minor mouse movement between press and release events.

This commit is contained in:
Maxim Shafirov
2012-06-09 13:46:55 +04:00
parent bc84d3b85f
commit 3b81035b0c
3 changed files with 78 additions and 16 deletions
@@ -0,0 +1,60 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
/*
* @author max
*/
package com.intellij.ui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public abstract class ClickListener {
private static final int EPS = 4;
public abstract void onClick(MouseEvent event);
public void installOn(final JComponent c) {
MouseAdapter adapter = new MouseAdapter() {
Point clickPoint;
@Override
public void mousePressed(MouseEvent e) {
clickPoint = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e) {
Point releasedAt = e.getPoint();
Point clickedAt = clickPoint;
clickPoint = null;
if (releasedAt.x < 0 || releasedAt.y < 0 || releasedAt.x >= c.getWidth() || releasedAt.y >= c.getWidth()) return;
if (clickedAt == null) return;
if (Math.abs(clickedAt.x - releasedAt.x) < EPS && Math.abs(clickedAt.y - releasedAt.y) < EPS) {
onClick(e);
}
}
};
c.addMouseListener(adapter);
c.addMouseMotionListener(adapter);
}
}
@@ -288,12 +288,6 @@ public class LinkLabel extends JLabel {
}
private class MyMouseHandler extends MouseAdapter implements MouseMotionListener {
public void mouseClicked(MouseEvent e) {
if (isInClickableArea(e.getPoint()) && e.getClickCount() == 1) {
doClick(e);
}
}
public void mousePressed(MouseEvent e) {
if (isInClickableArea(e.getPoint())) {
setActive(true);
@@ -301,6 +295,9 @@ public class LinkLabel extends JLabel {
}
public void mouseReleased(MouseEvent e) {
if (myIsLinkActive && isInClickableArea(e.getPoint())) {
doClick(e);
}
setActive(false);
}
@@ -275,18 +275,19 @@ public class DefaultWelcomeScreen implements WelcomeScreen {
actionLabel.setFont(new Font(CAPTION_FONT_NAME, Font.PLAIN, 12));
actionLabel.setForeground(CAPTION_COLOR);
actionLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
actionLabel.addMouseListener(new MouseAdapter() {
new ClickListener() {
@Override
public void mouseClicked(MouseEvent e) {
public void onClick(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
DataContext dataContext = DataManager.getInstance().getDataContext(myWelcomePanel);
int fragment = actionLabel.findFragmentAt(e.getX());
if (fragment == SimpleColoredComponent.FRAGMENT_ICON) {
final int rc = Messages.showOkCancelDialog(PlatformDataKeys.PROJECT.getData(dataContext),
"Remove '" + action.getTemplatePresentation().getText() +
"' from recent projects list?",
"Remove Recent Project",
Messages.getQuestionIcon());
"Remove '" + action.getTemplatePresentation().getText() +
"' from recent projects list?",
"Remove Recent Project",
Messages.getQuestionIcon());
if (rc == 0) {
final RecentProjectsManagerBase manager = RecentProjectsManagerBase.getInstance();
assert action instanceof ReopenProjectAction : action;
@@ -310,7 +311,9 @@ public class DefaultWelcomeScreen implements WelcomeScreen {
}
}
}
}.installOn(actionLabel);
actionLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
actionLabel.setIcon(ICON);
@@ -695,11 +698,13 @@ public class DefaultWelcomeScreen implements WelcomeScreen {
myCount++;
JLabel name = new JLabel(underlineHtmlText(commandLink));
name.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
button.onPress(e);
new ClickListener() {
@Override
public void onClick(MouseEvent event) {
button.onPress(event);
}
});
}.installOn(name);
name.setForeground(CAPTION_COLOR);
name.setFont(LINK_FONT);
name.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));