ui: merge two link-with-dropdown component implementations

This commit is contained in:
Aleksey Pivovarov
2019-02-06 17:05:48 +03:00
parent e291fca43d
commit 598904fedc
3 changed files with 36 additions and 100 deletions
@@ -3,9 +3,11 @@ package com.intellij.ui.components.labels;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.ui.popup.IPopupChooserBuilder;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.Consumer;
import com.intellij.util.containers.Convertor;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -24,30 +26,37 @@ public class DropDownLink<T> extends LinkLabel<Object> {
init();
}
public DropDownLink(@NotNull T initialItem, @NotNull List<T> items, @Nullable Consumer<? super T> itemChosenAction, boolean updateLabel) {
super(initialItem.toString(), AllIcons.General.LinkDropTriangle);
chosenItem = initialItem;
IPopupChooserBuilder<T> popupBuilder = JBPopupFactory.getInstance().createPopupChooserBuilder(items).
setRenderer(new LinkCellRenderer<>(this)).
setItemChosenCallback(t -> {
chosenItem = t;
if (updateLabel) {
setText(t.toString());
}
public DropDownLink(@NotNull T value, @NotNull Convertor<? super DropDownLink, ? extends JBPopup> popupBuilder) {
super(value.toString(), AllIcons.General.LinkDropTriangle);
chosenItem = value;
if (itemChosenAction != null) {
itemChosenAction.consume(t);
}
});
setListener((s, d) -> {
setListener((linkLabel, d) -> {
JBPopup popup = popupBuilder.convert((DropDownLink)linkLabel);
Point showPoint = new Point(0, getHeight() + JBUI.scale(4));
popupBuilder.createPopup().show(new RelativePoint(this, showPoint));
popup.show(new RelativePoint(this, showPoint));
}, null);
init();
}
public DropDownLink(@NotNull T initialItem, @NotNull List<T> items, @Nullable Consumer<? super T> itemChosenAction, boolean updateLabel) {
this(initialItem, (linkLabel) -> {
IPopupChooserBuilder<T> popupBuilder = JBPopupFactory.getInstance().createPopupChooserBuilder(items).
setRenderer(new LinkCellRenderer<>(linkLabel)).
setItemChosenCallback(t -> {
linkLabel.chosenItem = t;
if (updateLabel) {
linkLabel.setText(t.toString());
}
if (itemChosenAction != null) {
itemChosenAction.consume(t);
}
});
return popupBuilder.createPopup();
});
}
private void init() {
setIconTextGap(JBUI.scale(1));
setHorizontalAlignment(SwingConstants.LEADING);
@@ -1,79 +0,0 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ui.components.labels;
import com.intellij.icons.AllIcons;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.ui.ClickListener;
import com.intellij.ui.popup.PopupFactoryImpl;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.MouseEventAdapter;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
public class ActionGroupLink extends JPanel {
private final MyLinkLabel myLinkLabel;
public ActionGroupLink(@NotNull String text, @Nullable Icon icon, @NotNull ActionGroup actionGroup) {
setOpaque(false);
myLinkLabel = new MyLinkLabel(text, icon);
myLinkLabel.setListener(new LinkListener() {
@Override
public void linkSelected(LinkLabel aSource, Object aLinkData) {
DataContext dataContext = DataManager.getInstance().getDataContext(myLinkLabel);
ListPopup popup = JBPopupFactory.getInstance().createActionGroupPopup(null, actionGroup, dataContext,
JBPopupFactory.ActionSelectionAid.SPEEDSEARCH, false,
ActionPlaces.UNKNOWN);
((PopupFactoryImpl.ActionGroupPopup)popup).showUnderneathOfLabel(myLinkLabel);
}
}, null);
JLabel arrow = new JLabel(AllIcons.General.LinkDropTriangle);
arrow.setVerticalAlignment(SwingConstants.BOTTOM);
arrow.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
new ClickListener() {
@Override
public boolean onClick(@NotNull MouseEvent e, int clickCount) {
final MouseEvent newEvent = MouseEventAdapter.convert(e, myLinkLabel, e.getX(), e.getY());
myLinkLabel.doClick(newEvent);
return true;
}
}.installOn(arrow);
setLayout(new GridBagLayout());
add(JBUI.Panels.simplePanel(myLinkLabel).addToRight(arrow), new GridBagConstraints());
}
public LinkLabel getLinkLabel() {
return myLinkLabel;
}
private static class MyLinkLabel extends LinkLabel implements DataProvider {
MyLinkLabel(String text, @Nullable Icon icon) {
super(text, icon);
}
@Override
public Object getData(@NotNull @NonNls String dataId) {
if (PlatformDataKeys.DOMINANT_HINT_AREA_RECTANGLE.is(dataId)) {
final Point p = SwingUtilities.getRoot(this).getLocationOnScreen();
return new Rectangle(p.x, p.y + getHeight(), 0, 0);
}
if (PlatformDataKeys.CONTEXT_MENU_POINT.is(dataId)) {
return SwingUtilities.convertPoint(this, 0, getHeight(), UIUtil.getRootPane(this));
}
return null;
}
}
}
@@ -2,6 +2,7 @@
package com.intellij.openapi.vcs.ex
import com.intellij.diff.util.Side
import com.intellij.ide.DataManager
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnActionEvent
@@ -19,6 +20,7 @@ import com.intellij.openapi.editor.event.DocumentListener
import com.intellij.openapi.keymap.KeymapUtil
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.popup.JBPopupFactory
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.util.text.StringUtil
@@ -29,7 +31,7 @@ import com.intellij.openapi.vcs.changes.LocalChangeList
import com.intellij.openapi.vcs.ex.DocumentTracker.Block
import com.intellij.openapi.vcs.impl.LineStatusTrackerManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.ui.components.labels.ActionGroupLink
import com.intellij.ui.components.labels.DropDownLink
import com.intellij.util.EventDispatcher
import com.intellij.util.containers.ContainerUtil
import com.intellij.util.containers.WeakList
@@ -564,18 +566,22 @@ class ChangelistsLocalLineStatusTracker(project: Project,
group.add(MoveToAnotherChangeListAction(editor, range, mousePosition))
val link = ActionGroupLink(rangeList.name, null, group)
val link = DropDownLink(rangeList.name) { linkLabel ->
val dataContext = DataManager.getInstance().getDataContext(linkLabel)
JBPopupFactory.getInstance()
.createActionGroupPopup(null, group, dataContext, JBPopupFactory.ActionSelectionAid.SPEEDSEARCH, false)
}
val moveChangesShortcutSet = ActionManager.getInstance().getAction("Vcs.MoveChangedLinesToChangelist").shortcutSet
object : DumbAwareAction() {
override fun actionPerformed(e: AnActionEvent) {
link.linkLabel.doClick()
link.doClick()
}
}.registerCustomShortcutSet(moveChangesShortcutSet, editor.component, disposable)
val shortcuts = moveChangesShortcutSet.shortcuts
if (shortcuts.isNotEmpty()) {
link.linkLabel.toolTipText = "Move lines to another changelist (${KeymapUtil.getShortcutText(shortcuts.first())})"
link.toolTipText = "Move lines to another changelist (${KeymapUtil.getShortcutText(shortcuts.first())})"
}
val panel = JPanel(BorderLayout())