cleanup
[fedora-idea.git] / platform / platform-api / src / com / intellij / ui / PopupHandler.java
bloba46b95a7e967cd4578adae4234ff34a3e9a2d602
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.ui;
18 import com.intellij.openapi.actionSystem.ActionGroup;
19 import com.intellij.openapi.actionSystem.ActionManager;
20 import com.intellij.openapi.actionSystem.ActionPlaces;
21 import com.intellij.openapi.actionSystem.ActionPopupMenu;
22 import com.intellij.openapi.application.ApplicationManager;
23 import org.jetbrains.annotations.NonNls;
24 import org.jetbrains.annotations.NotNull;
26 import javax.swing.*;
27 import java.awt.*;
28 import java.awt.event.MouseAdapter;
29 import java.awt.event.MouseEvent;
30 import java.awt.event.MouseListener;
31 import java.util.Arrays;
33 /**
34 * @author Eugene Belyaev
36 public abstract class PopupHandler extends MouseAdapter {
37 public abstract void invokePopup(Component comp, int x, int y);
39 public void mouseClicked(MouseEvent e) {
40 if (e.isPopupTrigger()) {
41 invokePopup(e.getComponent(), e.getX(), e.getY());
42 e.consume();
46 public void mousePressed(MouseEvent e) {
47 if (e.isPopupTrigger()) {
48 invokePopup(e.getComponent(), e.getX(), e.getY());
49 e.consume();
53 public void mouseReleased(MouseEvent e) {
54 if (e.isPopupTrigger()) {
55 invokePopup(e.getComponent(), e.getX(), e.getY());
56 e.consume();
60 public static void installPopupHandler(JComponent component, @NonNls String groupId, String place) {
61 ActionGroup group = (ActionGroup)ActionManager.getInstance().getAction(groupId);
62 installPopupHandler(component, group, place, ActionManager.getInstance());
65 public static MouseListener installPopupHandler(JComponent component, @NotNull final ActionGroup group, final String place, final ActionManager actionManager) {
66 if (ApplicationManager.getApplication() == null) return new MouseAdapter(){};
67 PopupHandler popupHandler = new PopupHandler() {
68 public void invokePopup(Component comp, int x, int y) {
69 final ActionPopupMenu popupMenu = actionManager.createActionPopupMenu(place, group);
70 popupMenu.getComponent().show(comp, x, y);
73 component.addMouseListener(popupHandler);
74 return popupHandler;
77 public static MouseListener installFollowingSelectionTreePopup(final JTree tree, @NotNull final ActionGroup group, final String place, final ActionManager actionManager){
78 if (ApplicationManager.getApplication() == null) return new MouseAdapter(){};
79 PopupHandler handler = new PopupHandler() {
80 public void invokePopup(Component comp, int x, int y) {
81 if (tree.getPathForLocation(x, y) != null && Arrays.binarySearch(tree.getSelectionRows(), tree.getRowForLocation(x, y)) > -1) { //do not show popup menu on rows other than selection
82 final ActionPopupMenu popupMenu = actionManager.createActionPopupMenu(place, group);
83 popupMenu.getComponent().show(comp, x, y);
87 tree.addMouseListener(handler);
88 return handler;
91 public static MouseListener installUnknownPopupHandler(JComponent component, ActionGroup group, ActionManager actionManager) {
92 return installPopupHandler(component, group, ActionPlaces.UNKNOWN, actionManager);