List of usage examples for com.intellij.openapi.ui.popup LightweightWindowEvent isOk
public boolean isOk()
From source file:com.intellij.ide.util.gotoByName.ChooseByNameBase.java
License:Apache License
/** * @param modalityState - if not null rebuilds list in given {@link ModalityState} *///from w w w . j av a2 s . c o m protected void initUI(final ChooseByNamePopupComponent.Callback callback, final ModalityState modalityState, final boolean allowMultipleSelection) { myPreviouslyFocusedComponent = WindowManagerEx.getInstanceEx().getFocusedComponent(myProject); myActionListener = callback; myTextFieldPanel.setLayout(new BoxLayout(myTextFieldPanel, BoxLayout.Y_AXIS)); final JPanel hBox = new JPanel(); hBox.setLayout(new BoxLayout(hBox, BoxLayout.X_AXIS)); JPanel caption2Tools = new JPanel(new BorderLayout()); if (myModel.getPromptText() != null) { JLabel label = new JLabel(myModel.getPromptText()); if (UIUtil.isUnderAquaLookAndFeel()) { label.setBorder(new CompoundBorder(new EmptyBorder(0, 9, 0, 0), label.getBorder())); } label.setFont(UIUtil.getLabelFont().deriveFont(Font.BOLD)); caption2Tools.add(label, BorderLayout.WEST); } caption2Tools.add(hBox, BorderLayout.EAST); myCardContainer.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 4)); // space between checkbox and filter/show all in view buttons final String checkBoxName = myModel.getCheckBoxName(); myCheckBox = new JCheckBox(checkBoxName != null ? checkBoxName + (myCheckBoxShortcut != null ? " (" + KeymapUtil.getShortcutsText(myCheckBoxShortcut.getShortcuts()) + ")" : "") : ""); myCheckBox.setAlignmentX(SwingConstants.RIGHT); if (!SystemInfo.isMac) { myCheckBox.setBorder(null); } myCheckBox.setSelected(myModel.loadInitialCheckBoxState()); if (checkBoxName == null) { myCheckBox.setVisible(false); } addCard(myCheckBox, CHECK_BOX_CARD); addCard(new HintLabel(myModel.getNotInMessage()), NOT_FOUND_IN_PROJECT_CARD); addCard(new HintLabel(IdeBundle.message("label.choosebyname.no.matches.found")), NOT_FOUND_CARD); JPanel searching = new JPanel(new BorderLayout(5, 0)); searching.add(new AsyncProcessIcon("searching"), BorderLayout.WEST); searching.add(new HintLabel(IdeBundle.message("label.choosebyname.searching")), BorderLayout.CENTER); addCard(searching, SEARCHING_CARD); myCard.show(myCardContainer, CHECK_BOX_CARD); if (isCheckboxVisible()) { hBox.add(myCardContainer); } final DefaultActionGroup group = new DefaultActionGroup(); group.add(new ShowFindUsagesAction() { @Override public PsiElement[][] getElements() { final Object[] objects = myListModel.toArray(); final List<PsiElement> prefixMatchElements = new ArrayList<PsiElement>(objects.length); final List<PsiElement> nonPrefixMatchElements = new ArrayList<PsiElement>(objects.length); List<PsiElement> curElements = prefixMatchElements; for (Object object : objects) { if (object instanceof PsiElement) { curElements.add((PsiElement) object); } else if (object instanceof DataProvider) { final PsiElement psi = CommonDataKeys.PSI_ELEMENT.getData((DataProvider) object); if (psi != null) { curElements.add(psi); } } else if (object == NON_PREFIX_SEPARATOR) { curElements = nonPrefixMatchElements; } } return new PsiElement[][] { PsiUtilCore.toPsiElementArray(prefixMatchElements), PsiUtilCore.toPsiElementArray(nonPrefixMatchElements) }; } }); final ActionToolbar actionToolbar = ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, group, true); actionToolbar.setLayoutPolicy(ActionToolbar.NOWRAP_LAYOUT_POLICY); final JComponent toolbarComponent = actionToolbar.getComponent(); toolbarComponent.setBorder(null); if (myToolArea == null) { myToolArea = new JLabel(EmptyIcon.create(1, 24)); } hBox.add(myToolArea); hBox.add(toolbarComponent); myTextFieldPanel.add(caption2Tools); final ActionMap actionMap = new ActionMap(); actionMap.setParent(myTextField.getActionMap()); actionMap.put(DefaultEditorKit.copyAction, new AbstractAction() { @Override public void actionPerformed(@NotNull ActionEvent e) { if (myTextField.getSelectedText() != null) { actionMap.getParent().get(DefaultEditorKit.copyAction).actionPerformed(e); return; } final Object chosenElement = getChosenElement(); if (chosenElement instanceof PsiElement) { CopyReferenceAction.doCopy((PsiElement) chosenElement, myProject); } } }); myTextField.setActionMap(actionMap); myTextFieldPanel.add(myTextField); Font editorFont = getEditorFont(); myTextField.setFont(editorFont); if (checkBoxName != null) { if (myCheckBox != null && myCheckBoxShortcut != null) { new AnAction("change goto check box", null, null) { @RequiredDispatchThread @Override public void actionPerformed(@NotNull AnActionEvent e) { myCheckBox.setSelected(!myCheckBox.isSelected()); } }.registerCustomShortcutSet(myCheckBoxShortcut, myTextField); } } if (isCloseByFocusLost()) { myTextField.addFocusListener(new FocusAdapter() { @Override public void focusLost(@NotNull final FocusEvent e) { cancelListUpdater(); // cancel thread as early as possible myHideAlarm.addRequest(new Runnable() { @Override public void run() { JBPopup popup = JBPopupFactory.getInstance().getChildFocusedPopup(e.getComponent()); if (popup != null) { popup.addListener(new JBPopupListener.Adapter() { @Override public void onClosed(@NotNull LightweightWindowEvent event) { if (event.isOk()) { hideHint(); } } }); } else { Component oppositeComponent = e.getOppositeComponent(); if (oppositeComponent == myCheckBox) { IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true); return; } if (oppositeComponent != null && !(oppositeComponent instanceof JFrame) && myList.isShowing() && (oppositeComponent == myList || SwingUtilities.isDescendingFrom(myList, oppositeComponent))) { IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);// Otherwise me may skip some KeyEvents return; } if (oppositeComponent != null) { ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(myProject); ToolWindow toolWindow = toolWindowManager .getToolWindow(toolWindowManager.getActiveToolWindowId()); if (toolWindow != null) { JComponent toolWindowComponent = toolWindow.getComponent(); if (SwingUtilities.isDescendingFrom(oppositeComponent, toolWindowComponent)) { return; // Allow toolwindows to gain focus (used by QuickDoc shown in a toolwindow) } } } EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue(); if (queue instanceof IdeEventQueue) { if (!((IdeEventQueue) queue).wasRootRecentlyClicked(oppositeComponent)) { Component root = SwingUtilities.getRoot(myTextField); if (root != null && root.isShowing()) { IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true); return; } } } hideHint(); } } }, 5); } }); } if (myCheckBox != null) { myCheckBox.addItemListener(new ItemListener() { @Override public void itemStateChanged(@NotNull ItemEvent e) { rebuildList(false); } }); myCheckBox.setFocusable(false); } myTextField.getDocument().addDocumentListener(new DocumentAdapter() { @Override protected void textChanged(DocumentEvent e) { clearPostponedOkAction(false); rebuildList(false); } }); final Set<KeyStroke> upShortcuts = getShortcuts(IdeActions.ACTION_EDITOR_MOVE_CARET_UP); final Set<KeyStroke> downShortcuts = getShortcuts(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN); myTextField.addKeyListener(new KeyAdapter() { @Override public void keyPressed(@NotNull KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER && (e.getModifiers() & InputEvent.SHIFT_MASK) != 0) { myClosedByShiftEnter = true; close(true); } if (!myListScrollPane.isVisible()) { return; } final int keyCode; // Add support for user-defined 'caret up/down' shortcuts. KeyStroke stroke = KeyStroke.getKeyStrokeForEvent(e); if (upShortcuts.contains(stroke)) { keyCode = KeyEvent.VK_UP; } else if (downShortcuts.contains(stroke)) { keyCode = KeyEvent.VK_DOWN; } else { keyCode = e.getKeyCode(); } switch (keyCode) { case KeyEvent.VK_DOWN: ListScrollingUtil.moveDown(myList, e.getModifiersEx()); break; case KeyEvent.VK_UP: ListScrollingUtil.moveUp(myList, e.getModifiersEx()); break; case KeyEvent.VK_PAGE_UP: ListScrollingUtil.movePageUp(myList); break; case KeyEvent.VK_PAGE_DOWN: ListScrollingUtil.movePageDown(myList); break; case KeyEvent.VK_TAB: close(true); break; case KeyEvent.VK_ENTER: if (myList.getSelectedValue() == EXTRA_ELEM) { myMaximumListSizeLimit += myListSizeIncreasing; rebuildList(myList.getSelectedIndex(), myRebuildDelay, ModalityState.current(), null); e.consume(); } break; } if (myList.getSelectedValue() == NON_PREFIX_SEPARATOR) { if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_PAGE_UP) { ListScrollingUtil.moveUp(myList, e.getModifiersEx()); } else { ListScrollingUtil.moveDown(myList, e.getModifiersEx()); } } } }); myTextField.addActionListener(new ActionListener() { @Override public void actionPerformed(@NotNull ActionEvent actionEvent) { doClose(true); } }); myList.setFocusable(false); myList.setSelectionMode(allowMultipleSelection ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION : ListSelectionModel.SINGLE_SELECTION); new ClickListener() { @Override public boolean onClick(@NotNull MouseEvent e, int clickCount) { if (!myTextField.hasFocus()) { IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true); } if (clickCount == 2) { int selectedIndex = myList.getSelectedIndex(); Rectangle selectedCellBounds = myList.getCellBounds(selectedIndex, selectedIndex); if (selectedCellBounds != null && selectedCellBounds.contains(e.getPoint())) { // Otherwise it was reselected in the selection listener if (myList.getSelectedValue() == EXTRA_ELEM) { myMaximumListSizeLimit += myListSizeIncreasing; rebuildList(selectedIndex, myRebuildDelay, ModalityState.current(), null); } else { doClose(true); } } return true; } return false; } }.installOn(myList); myList.setCellRenderer(myModel.getListCellRenderer()); myList.setFont(editorFont); myList.addListSelectionListener(new ListSelectionListener() { private int myPreviousSelectionIndex = 0; @Override public void valueChanged(@NotNull ListSelectionEvent e) { if (myList.getSelectedValue() != NON_PREFIX_SEPARATOR) { myPreviousSelectionIndex = myList.getSelectedIndex(); chosenElementMightChange(); updateDocumentation(); } else if (allowMultipleSelection) { myList.setSelectedIndex(myPreviousSelectionIndex); } } }); myListScrollPane = ScrollPaneFactory.createScrollPane(myList); myListScrollPane.setViewportBorder(new EmptyBorder(0, 0, 0, 0)); myTextFieldPanel.setBorder(new EmptyBorder(2, 2, 2, 2)); showTextFieldPanel(); myInitialized = true; if (modalityState != null) { rebuildList(myInitialIndex, 0, modalityState, null); } }
From source file:com.intellij.notification.impl.NotificationsManagerImpl.java
License:Apache License
private static void showNotification(final Notification notification, @Nullable final Project project) { Application application = ApplicationManager.getApplication(); if (application instanceof ApplicationEx && !((ApplicationEx) application).isLoaded()) { application.invokeLater(new Runnable() { @Override/*from ww w.ja va 2 s. co m*/ public void run() { showNotification(notification, project); } }, ModalityState.current()); return; } String groupId = notification.getGroupId(); final NotificationSettings settings = NotificationsConfigurationImpl.getSettings(groupId); NotificationDisplayType type = settings.getDisplayType(); String toolWindowId = NotificationsConfigurationImpl.getInstanceImpl().getToolWindowId(groupId); if (type == NotificationDisplayType.TOOL_WINDOW && (toolWindowId == null || project == null || !ToolWindowManager.getInstance(project).canShowNotification(toolWindowId))) { type = NotificationDisplayType.BALLOON; } switch (type) { case NONE: return; //case EXTERNAL: // notifyByExternal(notification); // break; case STICKY_BALLOON: case BALLOON: default: Balloon balloon = notifyByBalloon(notification, type, project); if (!settings.isShouldLog() || type == NotificationDisplayType.STICKY_BALLOON) { if (balloon == null) { notification.expire(); } else { balloon.addListener(new JBPopupAdapter() { @Override public void onClosed(LightweightWindowEvent event) { if (!event.isOk()) { notification.expire(); } } }); } } break; case TOOL_WINDOW: MessageType messageType = notification.getType() == NotificationType.ERROR ? MessageType.ERROR : notification.getType() == NotificationType.WARNING ? MessageType.WARNING : MessageType.INFO; final NotificationListener notificationListener = notification.getListener(); HyperlinkListener listener = notificationListener == null ? null : new HyperlinkListener() { @Override public void hyperlinkUpdate(HyperlinkEvent e) { notificationListener.hyperlinkUpdate(notification, e); } }; assert toolWindowId != null; String msg = notification.getTitle(); if (StringUtil.isNotEmpty(notification.getContent())) { if (StringUtil.isNotEmpty(msg)) { msg += "<br>"; } msg += notification.getContent(); } //noinspection SSBasedInspection ToolWindowManager.getInstance(project).notifyByBalloon(toolWindowId, messageType, msg, notification.getIcon(), listener); } }
From source file:com.intellij.vcs.log.ui.GoToRefAction.java
License:Apache License
@Override public void actionPerformed(AnActionEvent e) { Project project = e.getProject();//from w ww .j a v a 2s . c o m final VcsLog log = e.getData(VcsLogDataKeys.VCS_LOG); if (project == null || log == null) { return; } Collection<String> refs = ContainerUtil.map(log.getAllReferences(), new Function<VcsRef, String>() { @Override public String fun(VcsRef ref) { return ref.getName(); } }); final PopupWithTextFieldWithAutoCompletion textField = new PopupWithTextFieldWithAutoCompletion(project, refs); JBPopup popup = textField.createPopup(); popup.addListener(new JBPopupListener.Adapter() { @Override public void onClosed(LightweightWindowEvent event) { if (event.isOk()) { log.jumpToReference(textField.getText().trim()); } } }); popup.showUnderneathOf(log.getToolbar()); }