List of usage examples for com.intellij.openapi.util ExpirableRunnable ExpirableRunnable
ExpirableRunnable
From source file:com.intellij.ide.IdeEventQueue.java
License:Apache License
public void fixStickyFocusedComponents(@Nullable AWTEvent e) { if (e != null && !(e instanceof InputEvent)) return; final KeyboardFocusManager mgr = KeyboardFocusManager.getCurrentKeyboardFocusManager(); if (Registry.is("actionSystem.fixStickyFocusedWindows")) { fixStickyWindow(mgr, mgr.getActiveWindow(), "setGlobalActiveWindow"); fixStickyWindow(mgr, mgr.getFocusedWindow(), "setGlobalFocusedWindow"); }//from w w w .ja v a2s . c o m if (Registry.is("actionSystem.fixNullFocusedComponent")) { final Component focusOwner = mgr.getFocusOwner(); if (focusOwner == null || !focusOwner.isShowing() || focusOwner instanceof JFrame || focusOwner instanceof JDialog) { final Application app = ApplicationManager.getApplication(); if (app instanceof ApplicationEx && !((ApplicationEx) app).isLoaded()) { return; } boolean mouseEventsAhead = isMouseEventAhead(e); boolean focusTransferredNow = IdeFocusManager.getGlobalInstance().isFocusBeingTransferred(); boolean okToFixFocus = !mouseEventsAhead && !focusTransferredNow; if (okToFixFocus) { Window showingWindow = mgr.getActiveWindow(); if (showingWindow == null) { Method getNativeFocusOwner = ReflectionUtil.getDeclaredMethod(KeyboardFocusManager.class, "getNativeFocusOwner"); if (getNativeFocusOwner != null) { try { Object owner = getNativeFocusOwner.invoke(mgr); if (owner instanceof Component) { showingWindow = UIUtil.getWindow((Component) owner); } } catch (Exception e1) { LOG.debug(e1); } } } if (showingWindow != null) { final IdeFocusManager fm = IdeFocusManager.findInstanceByComponent(showingWindow); ExpirableRunnable maybeRequestDefaultFocus = new ExpirableRunnable() { @Override public void run() { if (getPopupManager().requestDefaultFocus(false)) return; final Application app = ApplicationManager.getApplication(); if (app != null && app.isActive()) { fm.requestDefaultFocus(false); } } @Override public boolean isExpired() { return !UIUtil.isMeaninglessFocusOwner(mgr.getFocusOwner()); } }; fm.revalidateFocus(maybeRequestDefaultFocus); } } } } }
From source file:com.intellij.ui.BalloonImpl.java
License:Apache License
private void show(PositionTracker<Balloon> tracker, AbstractPosition position) { assert !myDisposed : "Balloon is already disposed"; if (isVisible()) return;/*from ww w .j a v a 2s .co m*/ final Component comp = tracker.getComponent(); if (!comp.isShowing()) return; myTracker = tracker; myTracker.init(this); JRootPane root = null; JDialog dialog = IJSwingUtilities.findParentOfType(comp, JDialog.class); if (dialog != null) { root = dialog.getRootPane(); } else { JWindow jwindow = IJSwingUtilities.findParentOfType(comp, JWindow.class); if (jwindow != null) { root = jwindow.getRootPane(); } else { JFrame frame = IJSwingUtilities.findParentOfType(comp, JFrame.class); if (frame != null) { root = frame.getRootPane(); } else { assert false; } } } myVisible = true; myLayeredPane = root.getLayeredPane(); myPosition = position; UIUtil.setFutureRootPane(myContent, root); myFocusManager = IdeFocusManager.findInstanceByComponent(myLayeredPane); final Ref<Component> originalFocusOwner = new Ref<Component>(); final Ref<FocusRequestor> focusRequestor = new Ref<FocusRequestor>(); final Ref<ActionCallback> proxyFocusRequest = new Ref<ActionCallback>(new ActionCallback.Done()); boolean mnemonicsFix = myDialogMode && SystemInfo.isMac && Registry.is("ide.mac.inplaceDialogMnemonicsFix"); if (mnemonicsFix) { final IdeGlassPaneEx glassPane = (IdeGlassPaneEx) IdeGlassPaneUtil.find(myLayeredPane); assert glassPane != null; proxyFocusRequest.set(new ActionCallback()); myFocusManager.doWhenFocusSettlesDown(new ExpirableRunnable() { @Override public boolean isExpired() { return isDisposed(); } @Override public void run() { IdeEventQueue.getInstance().disableInputMethods(BalloonImpl.this); originalFocusOwner.set(myFocusManager.getFocusOwner()); myFocusManager.requestFocus(glassPane.getProxyComponent(), true) .notify(proxyFocusRequest.get()); focusRequestor.set(myFocusManager.getFurtherRequestor()); } }); } myLayeredPane.addComponentListener(myComponentListener); myTargetPoint = myPosition.getShiftedPoint(myTracker.recalculateLocation(this).getPoint(myLayeredPane), myCalloutShift); int positionChangeFix = 0; if (myShowPointer) { Rectangle rec = getRecForPosition(myPosition, true); if (!myPosition.isOkToHavePointer(myTargetPoint, rec, getPointerLength(myPosition), getPointerWidth(myPosition), getArc())) { rec = getRecForPosition(myPosition, false); Rectangle lp = new Rectangle(new Point(myContainerInsets.left, myContainerInsets.top), myLayeredPane.getSize()); lp.width -= myContainerInsets.right; lp.height -= myContainerInsets.bottom; if (!lp.contains(rec)) { Rectangle2D currentSquare = lp.createIntersection(rec); double maxSquare = currentSquare.getWidth() * currentSquare.getHeight(); AbstractPosition targetPosition = myPosition; for (AbstractPosition eachPosition : myPosition.getOtherPositions()) { Rectangle2D eachIntersection = lp .createIntersection(getRecForPosition(eachPosition, false)); double eachSquare = eachIntersection.getWidth() * eachIntersection.getHeight(); if (maxSquare < eachSquare) { maxSquare = eachSquare; targetPosition = eachPosition; } } myPosition = targetPosition; positionChangeFix = myPosition.getChangeShift(position, myPositionChangeXShift, myPositionChangeYShift); } } } if (myPosition != position) { myTargetPoint = myPosition.getShiftedPoint(myTracker.recalculateLocation(this).getPoint(myLayeredPane), myCalloutShift > 0 ? myCalloutShift + positionChangeFix : positionChangeFix); } createComponent(); myComp.validate(); Rectangle rec = myComp.getContentBounds(); if (myShowPointer && !myPosition.isOkToHavePointer(myTargetPoint, rec, getPointerLength(myPosition), getPointerWidth(myPosition), getArc())) { myShowPointer = false; myComp.removeAll(); myLayeredPane.remove(myComp); createComponent(); if (!new Rectangle(myLayeredPane.getSize()).contains(new Rectangle(myComp.getSize()))) { // Balloon is bigger than window, don't show it at all. myLayeredPane = null; hide(); return; } } for (JBPopupListener each : myListeners) { each.beforeShown(new LightweightWindowEvent(this)); } runAnimation(true, myLayeredPane, null); myLayeredPane.revalidate(); myLayeredPane.repaint(); if (mnemonicsFix) { proxyFocusRequest.get().doWhenDone(new Runnable() { @Override public void run() { myFocusManager.requestFocus(originalFocusOwner.get(), true); } }); } Toolkit.getDefaultToolkit().addAWTEventListener(myAwtActivityListener, AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK); if (ApplicationManager.getApplication() != null) { ActionManager.getInstance().addAnActionListener(new AnActionListener.Adapter() { @Override public void beforeActionPerformed(AnAction action, DataContext dataContext, AnActionEvent event) { if (myHideOnAction) { hide(); } } }, this); } if (myHideOnLinkClick) { final Ref<JEditorPane> ref = Ref.create(null); new AwtVisitor(myContent) { @Override public boolean visit(Component component) { if (component instanceof JEditorPane) { ref.set((JEditorPane) component); return true; } return false; } }; if (!ref.isNull()) { ref.get().addHyperlinkListener(new HyperlinkAdapter() { @Override protected void hyperlinkActivated(HyperlinkEvent e) { hide(); } }); } } }