use of com.intellij.navigation.GotoRelatedItem in project intellij-community by JetBrains.
the class NavigationUtil method getPsiElementPopup.
private static JBPopup getPsiElementPopup(final Object[] elements, final Map<PsiElement, GotoRelatedItem> itemsMap, final String title, final boolean showContainingModules, final Processor<Object> processor) {
final Ref<Boolean> hasMnemonic = Ref.create(false);
final DefaultPsiElementCellRenderer renderer = new DefaultPsiElementCellRenderer() {
{
setFocusBorderEnabled(false);
}
@Override
public String getElementText(PsiElement element) {
String customName = itemsMap.get(element).getCustomName();
return (customName != null ? customName : super.getElementText(element));
}
@Override
protected Icon getIcon(PsiElement element) {
Icon customIcon = itemsMap.get(element).getCustomIcon();
return customIcon != null ? customIcon : super.getIcon(element);
}
@Override
public String getContainerText(PsiElement element, String name) {
String customContainerName = itemsMap.get(element).getCustomContainerName();
if (customContainerName != null) {
return customContainerName;
}
PsiFile file = element.getContainingFile();
return file != null && !getElementText(element).equals(file.getName()) ? "(" + file.getName() + ")" : null;
}
@Override
protected DefaultListCellRenderer getRightCellRenderer(Object value) {
return showContainingModules ? super.getRightCellRenderer(value) : null;
}
@Override
protected boolean customizeNonPsiElementLeftRenderer(ColoredListCellRenderer renderer, JList list, Object value, int index, boolean selected, boolean hasFocus) {
final GotoRelatedItem item = (GotoRelatedItem) value;
Color color = list.getForeground();
final SimpleTextAttributes nameAttributes = new SimpleTextAttributes(SimpleTextAttributes.STYLE_PLAIN, color);
final String name = item.getCustomName();
if (name == null)
return false;
renderer.append(name, nameAttributes);
renderer.setIcon(item.getCustomIcon());
return true;
}
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
final JPanel component = (JPanel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (!hasMnemonic.get())
return component;
final JPanel panelWithMnemonic = new JPanel(new BorderLayout());
final int mnemonic = getMnemonic(value, itemsMap);
final JLabel label = new JLabel("");
if (mnemonic != -1) {
label.setText(mnemonic + ".");
label.setDisplayedMnemonicIndex(0);
}
label.setPreferredSize(new JLabel("8.").getPreferredSize());
final JComponent leftRenderer = (JComponent) component.getComponents()[0];
component.remove(leftRenderer);
panelWithMnemonic.setBorder(BorderFactory.createEmptyBorder(0, 7, 0, 0));
panelWithMnemonic.setBackground(leftRenderer.getBackground());
label.setBackground(leftRenderer.getBackground());
panelWithMnemonic.add(label, BorderLayout.WEST);
panelWithMnemonic.add(leftRenderer, BorderLayout.CENTER);
component.add(panelWithMnemonic);
return component;
}
};
final ListPopupImpl popup = new ListPopupImpl(new BaseListPopupStep<Object>(title, Arrays.asList(elements)) {
@Override
public boolean isSpeedSearchEnabled() {
return true;
}
@Override
public String getIndexedString(Object value) {
if (value instanceof GotoRelatedItem) {
//noinspection ConstantConditions
return ((GotoRelatedItem) value).getCustomName();
}
PsiElement element = (PsiElement) value;
if (!element.isValid())
return "INVALID";
return renderer.getElementText(element) + " " + renderer.getContainerText(element, null);
}
@Override
public PopupStep onChosen(Object selectedValue, boolean finalChoice) {
processor.process(selectedValue);
return super.onChosen(selectedValue, finalChoice);
}
}) {
};
popup.getList().setCellRenderer(new PopupListElementRenderer(popup) {
Map<Object, String> separators = new HashMap<>();
{
final ListModel model = popup.getList().getModel();
String current = null;
boolean hasTitle = false;
for (int i = 0; i < model.getSize(); i++) {
final Object element = model.getElementAt(i);
final GotoRelatedItem item = itemsMap.get(element);
if (item != null && !StringUtil.equals(current, item.getGroup())) {
current = item.getGroup();
separators.put(element, current);
if (!hasTitle && !StringUtil.isEmpty(current)) {
hasTitle = true;
}
}
}
if (!hasTitle) {
separators.remove(model.getElementAt(0));
}
}
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
final Component component = renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
final String separator = separators.get(value);
if (separator != null) {
JPanel panel = new JPanel(new BorderLayout());
panel.add(component, BorderLayout.CENTER);
final SeparatorWithText sep = new SeparatorWithText() {
@Override
protected void paintComponent(Graphics g) {
g.setColor(new JBColor(Color.WHITE, UIUtil.getSeparatorColor()));
g.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
};
sep.setCaption(separator);
panel.add(sep, BorderLayout.NORTH);
return panel;
}
return component;
}
});
popup.setMinimumSize(new Dimension(200, -1));
for (Object item : elements) {
final int mnemonic = getMnemonic(item, itemsMap);
if (mnemonic != -1) {
final Action action = createNumberAction(mnemonic, popup, itemsMap, processor);
popup.registerAction(mnemonic + "Action", KeyStroke.getKeyStroke(String.valueOf(mnemonic)), action);
popup.registerAction(mnemonic + "Action", KeyStroke.getKeyStroke("NUMPAD" + String.valueOf(mnemonic)), action);
hasMnemonic.set(true);
}
}
return popup;
}
use of com.intellij.navigation.GotoRelatedItem in project intellij-community by JetBrains.
the class GotoTestRelatedProvider method getItems.
@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull DataContext context) {
final PsiFile file = CommonDataKeys.PSI_FILE.getData(context);
if (file == null)
return Collections.emptyList();
Collection<PsiElement> result;
final boolean isTest = TestFinderHelper.isTest(file);
if (isTest) {
result = TestFinderHelper.findClassesForTest(file);
} else {
result = TestFinderHelper.findTestsForClass(file);
}
if (!result.isEmpty()) {
final List<GotoRelatedItem> items = new ArrayList<>();
for (PsiElement element : result) {
items.add(new GotoRelatedItem(element, isTest ? "Tests" : "Testee classes"));
}
return items;
}
return Collections.emptyList();
}
use of com.intellij.navigation.GotoRelatedItem in project intellij-community by JetBrains.
the class GotoPropertyDeclarationsProvider method getItems.
@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull DataContext context) {
final FileEditor editor = PlatformDataKeys.FILE_EDITOR.getData(context);
if (!(editor instanceof ResourceBundleEditor)) {
return Collections.emptyList();
}
final ResourceBundleEditor resourceBundleEditor = (ResourceBundleEditor) editor;
final Collection<ResourceBundleEditorViewElement> elements = resourceBundleEditor.getSelectedElements();
if (elements.size() != 1) {
return Collections.emptyList();
}
final IProperty[] properties = ContainerUtil.getFirstItem(elements).getProperties();
if (properties == null || properties.length != 1 || !(properties[0] instanceof Property)) {
return Collections.emptyList();
}
final IProperty property = properties[0];
final String propertyKey = property.getKey();
final PropertiesFile file = PropertiesImplUtil.getPropertiesFile(property.getPsiElement().getContainingFile());
assert file != null;
final ResourceBundle resourceBundle = file.getResourceBundle();
return ContainerUtil.mapNotNull(resourceBundle.getPropertiesFiles(), (NullableFunction<PropertiesFile, GotoRelatedItem>) f -> {
final IProperty foundProperty = f.findPropertyByKey(propertyKey);
return foundProperty == null ? null : new GotoRelatedItem(foundProperty.getPsiElement(), "Property Declarations");
});
}
use of com.intellij.navigation.GotoRelatedItem in project intellij-community by JetBrains.
the class GotoResourceBundleLocalizationsProvider method getItems.
@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull final DataContext context) {
final FileEditor editor = PlatformDataKeys.FILE_EDITOR.getData(context);
if (editor instanceof ResourceBundleEditor) {
return Collections.emptyList();
}
final PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(context);
if (psiFile == null || !(psiFile instanceof PropertiesFile)) {
return Collections.emptyList();
}
final ResourceBundle resourceBundle = ((PropertiesFile) psiFile).getResourceBundle();
final List<PropertiesFile> bundlePropertiesFiles = resourceBundle.getPropertiesFiles();
assert bundlePropertiesFiles.size() != 0;
if (bundlePropertiesFiles.size() != 1) {
final ArrayList<PropertiesFile> propertiesFilesWithoutCurrent = ContainerUtil.newArrayList(bundlePropertiesFiles);
propertiesFilesWithoutCurrent.remove(psiFile);
return ContainerUtil.map(propertiesFilesWithoutCurrent, propertiesFile -> new GotoRelatedItem((PsiElement) propertiesFile, "Other Localizations"));
} else {
return Collections.emptyList();
}
}
use of com.intellij.navigation.GotoRelatedItem in project intellij-community by JetBrains.
the class JavaFxRelatedItemLineMarkerProvider method collectNavigationMarkers.
@Override
protected void collectNavigationMarkers(@NotNull PsiElement element, final Collection<? super RelatedItemLineMarkerInfo> result) {
if (element instanceof PsiField) {
final PsiField field = (PsiField) element;
if (JavaFxPsiUtil.isVisibleInFxml(field) && !field.hasModifierProperty(PsiModifier.STATIC) && !field.hasModifierProperty(PsiModifier.FINAL)) {
final PsiClass containingClass = field.getContainingClass();
if (containingClass != null && containingClass.hasModifierProperty(PsiModifier.PUBLIC) && containingClass.getQualifiedName() != null) {
final PsiMethod[] constructors = containingClass.getConstructors();
boolean defaultConstructor = constructors.length == 0;
for (PsiMethod constructor : constructors) {
if (constructor.getParameterList().getParametersCount() == 0) {
defaultConstructor = true;
break;
}
}
if (!defaultConstructor)
return;
final ArrayList<GotoRelatedItem> targets = new ArrayList<>();
collectTargets(field, targets, element1 -> new GotoRelatedItem(element1), true);
if (targets.isEmpty())
return;
result.add(new RelatedItemLineMarkerInfo<>(field, field.getNameIdentifier().getTextRange(), AllIcons.FileTypes.Xml, Pass.LINE_MARKERS, null, new JavaFXIdIconNavigationHandler(), GutterIconRenderer.Alignment.LEFT, targets));
}
}
}
}
Aggregations