protected void showPopup(DataContext context, ListPopup popup, JComponent contextComponent) { Component focusedComponent = contextComponent != null ? contextComponent : PlatformDataKeys.CONTEXT_COMPONENT.getData(context); if (focusedComponent != null) { if (popup instanceof PopupFactoryImpl.ActionGroupPopup && focusedComponent instanceof JLabel) { ((PopupFactoryImpl.ActionGroupPopup)popup).showUnderneathOfLabel((JLabel)focusedComponent); } else { popup.showUnderneathOf(focusedComponent); } } else { Rectangle r; int x; int y; focusedComponent = WindowManagerEx.getInstanceEx().getFocusedComponent((Project)null); r = WindowManagerEx.getInstanceEx().getScreenBounds(); x = r.x + r.width / 2; y = r.y + r.height / 2; Point point = new Point(x, y); SwingUtilities.convertPointToScreen(point, focusedComponent.getParent()); popup.showInScreenCoordinates(focusedComponent.getParent(), point); } }
private JComponent createActionLink(final String text, final String groupId, Icon icon, boolean focusListOnLeft) { final Ref<ActionLink> ref = new Ref<ActionLink>(null); AnAction action = new AnAction() { @Override public void actionPerformed(@NotNull AnActionEvent e) { ActionGroup configureGroup = (ActionGroup)ActionManager.getInstance().getAction(groupId); final PopupFactoryImpl.ActionGroupPopup popup = (PopupFactoryImpl.ActionGroupPopup)JBPopupFactory.getInstance() .createActionGroupPopup(null, new IconsFreeActionGroup(configureGroup), e.getDataContext(), JBPopupFactory.ActionSelectionAid.SPEEDSEARCH, false, ActionPlaces.WELCOME_SCREEN); popup.showUnderneathOfLabel(ref.get()); UsageTrigger.trigger("welcome.screen." + groupId); } }; ref.set(new ActionLink(text, icon, action)); ref.get().setPaintUnderline(false); ref.get().setNormalColor(getLinkNormalColor()); NonOpaquePanel panel = new NonOpaquePanel(new BorderLayout()); panel.setBorder(JBUI.Borders.empty(4, 6, 4, 6)); panel.add(ref.get()); panel.add(createArrow(ref.get()), BorderLayout.EAST); installFocusable(panel, action, KeyEvent.VK_UP, KeyEvent.VK_DOWN, focusListOnLeft); return panel; }
private static void selectItemByText(@NotNull final JList list, @NotNull final String text) { final Integer appIndex = execute(new GuiQuery<Integer>() { @Override protected Integer executeInEDT() throws Throwable { ListPopupModel popupModel = (ListPopupModel)list.getModel(); for (int i = 0; i < popupModel.getSize(); ++i) { PopupFactoryImpl.ActionItem actionItem = (PopupFactoryImpl.ActionItem)popupModel.get(i); assertNotNull(actionItem); if (text.equals(actionItem.getText())) { return i; } } return -1; } }); //noinspection ConstantConditions assertTrue(appIndex >= 0); execute(new GuiTask() { @Override protected void executeInEDT() throws Throwable { list.setSelectedIndex(appIndex); } }); assertEquals(text, ((PopupFactoryImpl.ActionItem)list.getSelectedValue()).getText()); }
private int preselectedIndex(Iterator items) { for( int index = 0; items.hasNext(); index++ ) { Object item = items.next(); GuardPopupAction<?> action = null; if ( item instanceof GuardPopupAction ) { action = (GuardPopupAction)item; } else if ( item instanceof PopupFactoryImpl.ActionItem && ((PopupFactoryImpl.ActionItem)item).getAction() instanceof GuardPopupAction ) { action= (GuardPopupAction)((PopupFactoryImpl.ActionItem)item).getAction(); } if ( action == null ) { continue; } if ( action.getSelectionKey() != null && action.getSelectionKey().isSelectableBy(selection) ) { return index; } } return -1; }
@SuppressWarnings("SuspiciousMethodCalls") @Override public void actionPerformed(@NotNull AnActionEvent event) { AnAction perform = whenSelected.get(list.getSelectedValue()); if ( perform != null) { PopupFactoryImpl.ActionItem selected = (PopupFactoryImpl.ActionItem)list.getSelectedValue(); if ( selected.isEnabled() && selected.equals(list.getSelectedValue()) ) { Presentation presentation = perform.getTemplatePresentation().clone(); perform.update(event); perform.beforeActionPerformedUpdate(event); event = new AnActionEvent( event.getInputEvent(), event.getDataContext(), event.getPlace(), presentation, ActionManager.getInstance(), event.getModifiers()); if ( presentation.isEnabled() ) { perform.actionPerformed(event); } } } else if ( selectItem != null ) { if ( selectItem.isEnabled() ) { list.setSelectedValue(selectItem, true); popup.handleSelect(selectHandleFinalChoices); } } }
private void registerActions(final ListPopupImpl popup) { popup.registerAction("delete", KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), new AbstractAction() { public void actionPerformed(ActionEvent e) { JList list = popup.getList(); int selectedIndex = list.getSelectedIndex(); ListPopupModel model = (ListPopupModel) list.getModel(); PopupFactoryImpl.ActionItem selectedItem = (PopupFactoryImpl.ActionItem) model.get(selectedIndex); if (selectedItem != null && selectedItem.getAction() instanceof RunGoalAction) { RunGoalAction action = (RunGoalAction) selectedItem.getAction(); boolean deleted = ApplicationComponent.getInstance().getState().removeGoal(action.getGoal()); if (deleted) { model.deleteItem(selectedItem); if (selectedIndex == list.getModel().getSize()) { // is last list.setSelectedIndex(selectedIndex - 1); } else { list.setSelectedIndex(selectedIndex); } } } } }); }
protected void showPopup(DataContext context, ListPopup popup, JComponent contextComponent) { Component focusedComponent = contextComponent != null ? contextComponent : context.getData(PlatformDataKeys.CONTEXT_COMPONENT); if (focusedComponent != null) { if (popup instanceof PopupFactoryImpl.ActionGroupPopup && focusedComponent instanceof JLabel) { ((PopupFactoryImpl.ActionGroupPopup)popup).showUnderneathOfLabel((JLabel)focusedComponent); } else { popup.showUnderneathOf(focusedComponent); } } else { Rectangle r; int x; int y; focusedComponent = WindowManagerEx.getInstanceEx().getFocusedComponent((Project)null); r = WindowManagerEx.getInstanceEx().getScreenBounds(); x = r.x + r.width / 2; y = r.y + r.height / 2; Point point = new Point(x, y); SwingUtilities.convertPointToScreen(point, focusedComponent.getParent()); popup.showInScreenCoordinates(focusedComponent.getParent(), point); } }
private JComponent createActionLink(final String text, final String groupId, Icon icon, boolean focusListOnLeft) { final Ref<ActionLink> ref = new Ref<>(null); AnAction action = new AnAction() { @RequiredDispatchThread @Override public void actionPerformed(@Nonnull AnActionEvent e) { ActionGroup configureGroup = (ActionGroup)ActionManager.getInstance().getAction(groupId); final PopupFactoryImpl.ActionGroupPopup popup = (PopupFactoryImpl.ActionGroupPopup)JBPopupFactory.getInstance() .createActionGroupPopup(null, new IconsFreeActionGroup(configureGroup), e.getDataContext(), JBPopupFactory.ActionSelectionAid.SPEEDSEARCH, false, ActionPlaces.WELCOME_SCREEN); popup.showUnderneathOfLabel(ref.get()); UsageTrigger.trigger("welcome.screen." + groupId); } }; JComponent panel = createActionLink(text, icon, ref, action); installFocusable(panel, action, KeyEvent.VK_UP, KeyEvent.VK_DOWN, focusListOnLeft); return panel; }
private void showPopupBalloon(final String result) { ApplicationManager.getApplication().invokeLater(() -> { mEditor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POSITION, null);//解决因为TranslationPlugin而导致的泡泡显示错位问题 JBPopupFactory factory = JBPopupFactory.getInstance(); factory.createHtmlTextBalloonBuilder(result, null, new JBColor(Gray._242, Gray._0), null) .createBalloon() .show(factory.guessBestPopupLocation(mEditor), Balloon.Position.below); }); }
@Nullable private static RootAction getRootAction(Object value) { if (value instanceof PopupFactoryImpl.ActionItem) { AnAction action = ((PopupFactoryImpl.ActionItem)value).getAction(); if (action instanceof RootAction) { return (RootAction)action; } } return null; }
@Override public void run() { myAlarm.cancelAllRequests(); // Skip the request if it's outdated (the mouse is moved other another element). DelayedQuickDocInfo info = myDelayedQuickDocInfo; if (info == null || !info.targetElement.equals(myActiveElements.get(info.editor))) { return; } // Skip the request if there is a control shown as a result of explicit 'show quick doc' (Ctrl + Q) invocation. if (info.docManager.getDocInfoHint() != null && !info.docManager.isCloseOnSneeze()) { return; } // We don't want to show a quick doc control if there is an active hint (e.g. the mouse is under an invalid element // and corresponding error info is shown). if (!info.docManager.hasActiveDockedDocWindow() && myHintManager.hasShownHintsThatWillHideByOtherHint(false)) { myAlarm.addRequest(this, EditorSettingsExternalizable.getInstance().getQuickDocOnMouseOverElementDelayMillis()); return; } info.editor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POSITION, info.editor.offsetToVisualPosition(info.originalElement.getTextRange().getStartOffset())); try { info.docManager.showJavaDocInfo(info.editor, info.targetElement, info.originalElement, myHintCloseCallback, true); myDocumentationManager = new WeakReference<DocumentationManager>(info.docManager); } finally { info.editor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POSITION, null); } }
@NotNull private JList getRunConfigList() { final JList runConfigList = robot.finder().findByType(JBListWithHintProvider.class); pause(new Condition("Wait until the list is populated.") { @Override public boolean test() { //noinspection ConstantConditions return execute(new GuiQuery<Boolean>() { @Override protected Boolean executeInEDT() throws Throwable { return runConfigList.getComponentCount() >= 2; // At least 2, since there is always one option present (the option to edit). } }); } }, SHORT_TIMEOUT); Object selectedValue = execute(new GuiQuery<Object>() { @Override protected Object executeInEDT() throws Throwable { return runConfigList.getSelectedValue(); } }); assertThat(selectedValue).isInstanceOf(PopupFactoryImpl.ActionItem.class); return runConfigList; }
@Override public void run() { myAlarm.cancelAllRequests(); // Skip the request if it's outdated (the mouse is moved other another element). DelayedQuickDocInfo info = myDelayedQuickDocInfo; if (info == null || !info.targetElement.equals(myActiveElements.get(info.editor))) { return; } // Skip the request if there is a control shown as a result of explicit 'show quick doc' (Ctrl + Q) invocation. if (info.docManager.getDocInfoHint() != null && !info.docManager.isCloseOnSneeze()) { return; } // We don't want to show a quick doc control if there is an active hint (e.g. the mouse is under an invalid element // and corresponding error info is shown). if (!info.docManager.hasActiveDockedDocWindow() && myHintManager.hasShownHintsThatWillHideByOtherHint(false)) { myAlarm.addRequest(this, EditorSettingsExternalizable.getInstance().getQuickDocOnMouseOverElementDelayMillis()); return; } info.editor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POSITION, info.editor.offsetToVisualPosition(info.originalElement.getTextRange().getStartOffset())); try { info.docManager.showJavaDocInfo(info.editor, info.targetElement, info.originalElement, myHintCloseCallback, true, true); myDocumentationManager = new WeakReference<DocumentationManager>(info.docManager); myDocumentationManager = new WeakReference<DocumentationManager>(info.docManager); } finally { info.editor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POSITION, null); } }
private void preselect(final ListPopup popup) { Iterator items = popup.getListStep().getValues().iterator(); int index = preselectedIndex(items); if ( index >= 0 ) { findJList(popup).setSelectedIndex(index); AnAction action = ((PopupFactoryImpl.ActionItem)popup.getListStep().getValues().get(index)).getAction(); if ( action instanceof ActionGroup ) { if ( preselectedIndex(Arrays.asList(((ActionGroup)action).getChildren(null)).iterator()) >= 0 ) { // ensure that the next sub-menu gets activated Window window = (Window)SwingUtilities.getRoot(popup.getContent()); if ( window.isVisible() ) { popup.handleSelect(false); } else { window.addWindowListener( new WindowAdapter() { @Override public void windowOpened(WindowEvent e) { popup.handleSelect(false); e.getWindow().removeWindowListener(this); } }); } } } } }
void install(JComponent component) { for( Object item : popup.getListStep().getValues() ) { if ( item instanceof PopupFactoryImpl.ActionItem ) { if ( ((PopupFactoryImpl.ActionItem)item).getAction() instanceof GuardPopupAction ) { ((GuardPopupAction)((PopupFactoryImpl.ActionItem)item).getAction()) .extendKeyboardActions(new Extender((PopupFactoryImpl.ActionItem)item)); } } } for( Map.Entry<KeyStroke, Mapping> mapping : mappings.entrySet() ) { mapping.getValue().registerCustomShortcutSet(new CustomShortcutSet(mapping.getKey()), component); } }
private static <T> T getSpecificAction(Object value, @Nonnull Class<T> clazz) { if (value instanceof PopupFactoryImpl.ActionItem) { AnAction action = ((PopupFactoryImpl.ActionItem)value).getAction(); if (clazz.isInstance(action)) { return clazz.cast(action); } else if (action instanceof EmptyAction.MyDelegatingActionGroup) { ActionGroup group = ((EmptyAction.MyDelegatingActionGroup)action).getDelegate(); return clazz.isInstance(group) ? clazz.cast(group) : null; } } return null; }
protected void showBalloon() { final JComponent component = getComponent(); if (component == null) return; if (ApplicationManager.getApplication().isHeadlessEnvironment()) return; final BalloonBuilder balloonBuilder = JBPopupFactory.getInstance().createDialogBalloonBuilder(component, null).setSmallVariant(true); myBalloon = balloonBuilder.createBalloon(); final Editor topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(myEditor); Disposer.register(myProject, myBalloon); Disposer.register(myBalloon, new Disposable() { @Override public void dispose() { releaseIfNotRestart(); topLevelEditor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POSITION, null); } }); topLevelEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); final JBPopupFactory popupFactory = JBPopupFactory.getInstance(); myBalloon.show(new PositionTracker<Balloon>(topLevelEditor.getContentComponent()) { @Override public RelativePoint recalculateLocation(Balloon object) { if (myTarget != null && !popupFactory.isBestPopupLocationVisible(topLevelEditor)) { return myTarget; } if (myCaretRangeMarker != null && myCaretRangeMarker.isValid()) { topLevelEditor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POSITION, topLevelEditor.offsetToVisualPosition(myCaretRangeMarker.getStartOffset())); } final RelativePoint target = popupFactory.guessBestPopupLocation(topLevelEditor); final Point screenPoint = target.getScreenPoint(); int y = screenPoint.y; if (target.getPoint().getY() > topLevelEditor.getLineHeight() + myBalloon.getPreferredSize().getHeight()) { y -= topLevelEditor.getLineHeight(); } myTarget = new RelativePoint(new Point(screenPoint.x, y)); return myTarget; } }, Balloon.Position.above); }
private Extender(PopupFactoryImpl.ActionItem actionItem) { this.actionItem = actionItem; }
@Nullable @Override public ListPopup getPopupStep() { ActionGroup popupGroup = getActions(); return new PopupFactoryImpl.ActionGroupPopup("Symfony Profiler", popupGroup, SimpleDataContext.getProjectContext(getProject()), false, false, false, true, null, -1, null, null); }
@Override public final boolean shouldBeShowing(Object value) { if (!super.shouldBeShowing(value)) return false; if (!(value instanceof PopupFactoryImpl.ActionItem)) return true; return shouldBeShowing(((PopupFactoryImpl.ActionItem)value).getAction()); }
@Override public void run() { Ref<PsiElement> targetElementRef = new Ref<>(); QuickDocUtil.runInReadActionWithWriteActionPriorityWithRetries(() -> { if (originalElement.isValid()) { targetElementRef.set(docManager.findTargetElement(editor, offset, originalElement.getContainingFile(), originalElement)); } }, 5000, 100, myProgressIndicator); ApplicationManager.getApplication().invokeLater(() -> { myCurrentRequest = null; if (editor.isDisposed()) return; PsiElement targetElement = targetElementRef.get(); if (targetElement == null) { closeQuickDocIfPossible(); return; } myAlarm.cancelAllRequests(); if (!originalElement.equals(SoftReference.dereference(myActiveElements.get(editor)))) { return; } // Skip the request if there is a control shown as a result of explicit 'show quick doc' (Ctrl + Q) invocation. if (docManager.getDocInfoHint() != null && !docManager.isCloseOnSneeze()) { return; } editor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POSITION, editor.offsetToVisualPosition(originalElement.getTextRange().getStartOffset())); try { docManager.showJavaDocInfo(editor, targetElement, originalElement, myHintCloseCallback, true); myDocumentationManager = new WeakReference<>(docManager); } finally { editor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POSITION, null); } }, ApplicationManager.getApplication().getNoneModalityState()); }