From 6d3d2709d38c0ee81140894bdb3a24172d3c6f1c Mon Sep 17 00:00:00 2001 From: anna Date: Thu, 25 Aug 2011 10:28:47 +0200 Subject: [PATCH] update definitions/documentations view in show usages (IDEA-58145 ) --- .../find/actions/ShowUsagesAction.java | 28 ++++--- .../intellij/ui/JBTableWithHintProvider.java | 84 +++++++++++++++++++ .../ui/popup/PopupUpdateProcessor.java | 9 +- 3 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/ui/JBTableWithHintProvider.java diff --git a/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesAction.java b/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesAction.java index 47240970e441..7479c624adc0 100644 --- a/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesAction.java +++ b/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesAction.java @@ -50,6 +50,7 @@ import com.intellij.psi.search.ProjectScope; import com.intellij.psi.search.PsiElementProcessor; import com.intellij.psi.search.SearchScope; import com.intellij.ui.InplaceButton; +import com.intellij.ui.JBTableWithHintProvider; import com.intellij.ui.SpeedSearchBase; import com.intellij.ui.TableScrollingUtil; import com.intellij.ui.awt.RelativePoint; @@ -681,7 +682,7 @@ public class ShowUsagesAction extends AnAction implements PopupAction { return newFileEditor instanceof TextEditor ? ((TextEditor)newFileEditor).getEditor() : null; } - private static class MyTable extends Table implements DataProvider { + private static class MyTable extends JBTableWithHintProvider implements DataProvider { @Override public boolean getScrollableTracksViewportWidth() { return true; @@ -691,16 +692,21 @@ public class ShowUsagesAction extends AnAction implements PopupAction { if (LangDataKeys.PSI_ELEMENT.is(dataId)) { final int[] selected = getSelectedRows(); if (selected.length == 1) { - final Object at = getValueAt(selected[0], 0); - if (at instanceof UsageNode) { - final Usage usage = ((UsageNode)at).getUsage(); - if (usage instanceof UsageInfo2UsageAdapter) { - final PsiElement element = ((UsageInfo2UsageAdapter)usage).getElement(); - if (element != null) { - final PsiElement view = UsageToPsiElementProvider.findAppropriateParentFrom(element); - return view == null ? element : view; - } - } + return getPsiElementForHint(getValueAt(selected[0], 0)); + } + } + return null; + } + + @Override + protected PsiElement getPsiElementForHint(Object selectedValue) { + if (selectedValue instanceof UsageNode) { + final Usage usage = ((UsageNode)selectedValue).getUsage(); + if (usage instanceof UsageInfo2UsageAdapter) { + final PsiElement element = ((UsageInfo2UsageAdapter)usage).getElement(); + if (element != null) { + final PsiElement view = UsageToPsiElementProvider.findAppropriateParentFrom(element); + return view == null ? element : view; } } } diff --git a/platform/lang-impl/src/com/intellij/ui/JBTableWithHintProvider.java b/platform/lang-impl/src/com/intellij/ui/JBTableWithHintProvider.java new file mode 100644 index 000000000000..3628dd3fd44c --- /dev/null +++ b/platform/lang-impl/src/com/intellij/ui/JBTableWithHintProvider.java @@ -0,0 +1,84 @@ +/* + * Copyright 2000-2011 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. + */ +package com.intellij.ui; + +import com.intellij.openapi.ui.popup.JBPopup; +import com.intellij.openapi.ui.popup.PopupChooserBuilder; +import com.intellij.psi.PsiElement; +import com.intellij.ui.components.JBList; +import com.intellij.ui.popup.PopupUpdateProcessor; +import com.intellij.ui.table.JBTable; + +import javax.swing.*; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import javax.swing.table.TableModel; +import java.util.Collection; + +public abstract class JBTableWithHintProvider extends JBTable { + private JBPopup myHint; + + public JBTableWithHintProvider() { + addSelectionListener(); + } + + protected JBTableWithHintProvider(TableModel model) { + super(model); + } + + private void addSelectionListener() { + getSelectionModel().addListSelectionListener(new ListSelectionListener() { + @Override + public void valueChanged(final ListSelectionEvent e) { + if (getClientProperty(PopupChooserBuilder.SELECTED_BY_MOUSE_EVENT) != Boolean.TRUE) { + + final int selected = ((ListSelectionModel)e.getSource()).getLeadSelectionIndex(); + if (selected == -1) return; + + final PsiElement element = getPsiElementForHint(getValueAt(selected, 0)); + if (element != null && element.isValid()) { + updateHint(element); + } + } + } + }); + } + + protected abstract PsiElement getPsiElementForHint(final Object selectedValue); + + public void registerHint(final JBPopup hint) { + hideHint(); + myHint = hint; + } + + public void hideHint() { + if (myHint != null && myHint.isVisible()) { + myHint.cancel(); + } + + myHint = null; + } + + public void updateHint(PsiElement element) { + if (myHint == null || !myHint.isVisible()) return; + + final PopupUpdateProcessor updateProcessor = myHint.getUserData(PopupUpdateProcessor.class); + if (updateProcessor != null) { + updateProcessor.updatePopup(element); + } + } + +} diff --git a/platform/lang-impl/src/com/intellij/ui/popup/PopupUpdateProcessor.java b/platform/lang-impl/src/com/intellij/ui/popup/PopupUpdateProcessor.java index 21e2c17bb52f..2d0dcde1b5fc 100644 --- a/platform/lang-impl/src/com/intellij/ui/popup/PopupUpdateProcessor.java +++ b/platform/lang-impl/src/com/intellij/ui/popup/PopupUpdateProcessor.java @@ -26,6 +26,7 @@ import com.intellij.openapi.ui.popup.LightweightWindowEvent; import com.intellij.openapi.wm.ex.WindowManagerEx; import com.intellij.psi.PsiElement; import com.intellij.ui.JBListWithHintProvider; +import com.intellij.ui.JBTableWithHintProvider; import java.awt.*; @@ -70,8 +71,12 @@ public abstract class PopupUpdateProcessor extends JBPopupAdapter { if (fromQuickSearch) { ChooseByNameBase.JPanelProvider panelProvider = (ChooseByNameBase.JPanelProvider)focusedComponent.getParent(); panelProvider.registerHint(windowEvent.asPopup()); - } else if (focusedComponent != null && focusedComponent instanceof JBListWithHintProvider) { - ((JBListWithHintProvider)focusedComponent).registerHint(windowEvent.asPopup()); + } else if (focusedComponent != null) { + if (focusedComponent instanceof JBListWithHintProvider) { + ((JBListWithHintProvider)focusedComponent).registerHint(windowEvent.asPopup()); + } else if (focusedComponent instanceof JBTableWithHintProvider) { + ((JBTableWithHintProvider)focusedComponent).registerHint(windowEvent.asPopup()); + } } } }