Skip to content

Commit 6dd1474

Browse files
committed
migrate deprecated apis
1 parent 91bf9c5 commit 6dd1474

File tree

8 files changed

+237
-106
lines changed

8 files changed

+237
-106
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ dependencies {
2424

2525
// See https://github.com/JetBrains/gradle-intellij-plugin/
2626
intellij {
27+
pluginName "Gist Snippet"
2728
version ideaVersion
2829
plugins = ["github"]
29-
pluginName "Gist Snippet"
3030
sameSinceUntilBuild Boolean.valueOf(isEAP)
3131
patchPluginXml {
3232
untilBuild customUtilBuild

changeNotes.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<ul>
2-
<li>Fixed minor issues</li>
2+
<li>Migrate deprecated apis</li>
3+
<li>Improve ui operation</li>
34
</ul>

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version = 1.0.2
1+
version = 1.0.3
22
ideaVersion = IC-2019.1
33
customUtilBuild = 299.*
44
isEAP = false

src/main/java/com/chuntung/plugin/gistsnippet/action/InsertAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
public class InsertAction extends AnAction implements DumbAware {
2727
public InsertAction() {
28-
super(IconLoader.getIcon("/images/gist.png"));
28+
super(IconLoader.getIcon("/images/gist.png", InsertAction.class));
2929
}
3030

3131
@Override

src/main/java/com/chuntung/plugin/gistsnippet/dto/SnippetNodeDTO.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
public class SnippetNodeDTO extends SimpleNode {
2525
private static Map<String, Icon> avatarCache = new ConcurrentHashMap<>();
26-
private Icon publicIcon = IconLoader.getIcon("/images/public.png");
27-
private Icon secretIcon = IconLoader.getIcon("/images/secret.png");
26+
private Icon publicIcon = IconLoader.getIcon("/images/public.png", SnippetNodeDTO.class);
27+
private Icon secretIcon = IconLoader.getIcon("/images/secret.png", SnippetNodeDTO.class);
2828

2929
public static final Pattern TITLE_PATTERN = Pattern.compile("#(.+)#");
3030
public static final Pattern TAG_PATTERN = Pattern.compile("\\[([^\\[\\]]+)\\]");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright 2000-2016 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.chuntung.plugin.gistsnippet.view;
17+
18+
import com.intellij.openapi.actionSystem.ActionPlaces;
19+
import com.intellij.openapi.actionSystem.AnAction;
20+
import com.intellij.openapi.actionSystem.DataProvider;
21+
import com.intellij.openapi.actionSystem.PlatformDataKeys;
22+
import com.intellij.openapi.actionSystem.ex.ActionUtil;
23+
import com.intellij.ui.components.labels.LinkLabel;
24+
import com.intellij.ui.components.labels.LinkListener;
25+
import com.intellij.util.ui.EmptyIcon;
26+
import com.intellij.util.ui.JBUI;
27+
import com.intellij.util.ui.UIUtil;
28+
import org.jetbrains.annotations.NonNls;
29+
import org.jetbrains.annotations.NotNull;
30+
import org.jetbrains.annotations.Nullable;
31+
import org.jetbrains.annotations.TestOnly;
32+
33+
import javax.swing.*;
34+
import java.awt.*;
35+
import java.awt.event.InputEvent;
36+
37+
/**
38+
* @author Konstantin Bulenkov
39+
*/
40+
public class CustomActionLink extends LinkLabel<Object> implements DataProvider {
41+
private static final EmptyIcon ICON = JBUI.scale(EmptyIcon.create(0, 12));
42+
private final AnAction myAction;
43+
private final String myPlace = ActionPlaces.UNKNOWN;
44+
private InputEvent myEvent;
45+
private Color myVisitedColor;
46+
private Color myActiveColor;
47+
private Color myNormalColor;
48+
49+
public CustomActionLink(String text, @NotNull AnAction action) {
50+
this(text, ICON, action);
51+
}
52+
53+
public CustomActionLink(String text, Icon icon, @NotNull AnAction action) {
54+
this(text, icon, action, null);
55+
}
56+
57+
public CustomActionLink(String text, Icon icon, @NotNull AnAction action, @Nullable final Runnable onDone) {
58+
super(text, icon);
59+
setListener(new LinkListener<Object>() {
60+
@Override
61+
public void linkSelected(LinkLabel aSource, Object aLinkData) {
62+
ActionUtil.invokeAction(myAction, CustomActionLink.this, myPlace, myEvent, onDone);
63+
}
64+
}, null);
65+
myAction = action;
66+
}
67+
68+
@Override
69+
public void doClick(InputEvent e) {
70+
myEvent = e;
71+
super.doClick();
72+
}
73+
74+
@Override
75+
protected Color getVisited() {
76+
return myVisitedColor == null ? super.getVisited() : myVisitedColor;
77+
}
78+
79+
public Color getActiveColor() {
80+
return myActiveColor == null ? super.getActive() : myActiveColor;
81+
}
82+
83+
@Override
84+
protected Color getTextColor() {
85+
return myUnderline ? getActiveColor() : getNormal();
86+
}
87+
88+
@Override
89+
protected Color getNormal() {
90+
return myNormalColor == null ? super.getNormal() : myNormalColor;
91+
}
92+
93+
public void setVisitedColor(Color visitedColor) {
94+
myVisitedColor = visitedColor;
95+
}
96+
97+
public void setActiveColor(Color activeColor) {
98+
myActiveColor = activeColor;
99+
}
100+
101+
public void setNormalColor(Color normalColor) {
102+
myNormalColor = normalColor;
103+
}
104+
105+
@Override
106+
public Object getData(@NotNull @NonNls String dataId) {
107+
if (PlatformDataKeys.DOMINANT_HINT_AREA_RECTANGLE.is(dataId)) {
108+
final Point p = SwingUtilities.getRoot(this).getLocationOnScreen();
109+
return new Rectangle(p.x, p.y + getHeight(), 0, 0);
110+
}
111+
if (PlatformDataKeys.CONTEXT_MENU_POINT.is(dataId)) {
112+
return SwingUtilities.convertPoint(this, 0, getHeight(), UIUtil.getRootPane(this));
113+
}
114+
return myAction instanceof DataProvider ? ((DataProvider)myAction).getData(dataId) : null;
115+
}
116+
117+
@TestOnly
118+
public AnAction getAction() {
119+
return myAction;
120+
}
121+
}
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,117 @@
1-
/*
2-
* Copyright (c) 2020 Tony Ho. Some rights reserved.
3-
*/
4-
1+
// 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.
52
package com.chuntung.plugin.gistsnippet.view;
63

4+
import com.intellij.icons.AllIcons;
5+
import com.intellij.openapi.ui.popup.IPopupChooserBuilder;
6+
import com.intellij.openapi.ui.popup.JBPopup;
77
import com.intellij.openapi.ui.popup.JBPopupFactory;
8-
import com.intellij.openapi.ui.popup.ListPopup;
9-
import com.intellij.openapi.ui.popup.PopupStep;
10-
import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
11-
import com.intellij.ui.JBColor;
128
import com.intellij.ui.awt.RelativePoint;
13-
import com.intellij.ui.components.JBComboBoxLabel;
14-
import com.intellij.util.SmartList;
9+
import com.intellij.ui.components.labels.LinkLabel;
10+
import com.intellij.util.Consumer;
11+
import com.intellij.util.containers.Convertor;
1512
import com.intellij.util.ui.JBUI;
13+
import org.jetbrains.annotations.NotNull;
14+
import org.jetbrains.annotations.Nullable;
1615

1716
import javax.swing.*;
18-
import javax.swing.border.EmptyBorder;
17+
import javax.swing.plaf.metal.MetalLabelUI;
1918
import java.awt.*;
20-
import java.awt.event.MouseAdapter;
21-
import java.awt.event.MouseEvent;
22-
import java.util.Arrays;
2319
import java.util.List;
24-
import java.util.function.Consumer;
25-
26-
/**
27-
* @deprecated use {@link com.chuntung.plugin.gistsnippet.action.CustomComboBoxAction} for toolbar
28-
*/
29-
class CustomDropDownLink extends JBComboBoxLabel {
30-
private final JLabel leftIcon = new JLabel("", null, SwingConstants.CENTER);
31-
private final List<String> items = new SmartList<>();
32-
private final List<Icon> icons = new SmartList<>();
33-
private String selectedItem;
34-
private Consumer consumer;
35-
36-
CustomDropDownLink(String selectedItem, String[] items, Icon[] icons, Consumer consumer) {
37-
this.selectedItem = selectedItem;
38-
this.consumer = consumer;
39-
this.items.addAll(Arrays.asList(items));
40-
if (icons != null) {
41-
this.icons.addAll(Arrays.asList(icons));
42-
}
4320

44-
// setForeground(JBColor.link());
21+
public class CustomDropDownLink<T> extends LinkLabel<Object> {
22+
private T chosenItem;
4523

46-
addMouseListener(new MouseAdapter() {
47-
@Override
48-
public void mousePressed(MouseEvent e) {
49-
showPopup();
50-
}
51-
});
24+
public CustomDropDownLink(@NotNull T value, @NotNull Runnable clickAction) {
25+
super(value.toString(), AllIcons.General.LinkDropTriangle, (s, d) -> clickAction.run());
26+
chosenItem = value;
27+
init();
28+
}
29+
30+
public CustomDropDownLink(@NotNull T value, @NotNull Convertor<? super CustomDropDownLink, ? extends JBPopup> popupBuilder) {
31+
super(value.toString(), AllIcons.General.LinkDropTriangle);
32+
chosenItem = value;
33+
34+
setListener((linkLabel, d) -> {
35+
JBPopup popup = popupBuilder.convert((CustomDropDownLink)linkLabel);
36+
Point showPoint = new Point(0, getHeight() + JBUI.scale(4));
37+
popup.show(new RelativePoint(this, showPoint));
38+
}, null);
5239

5340
init();
5441
}
5542

56-
private void init() {
57-
leftIcon.setBorder(new EmptyBorder(0, 4, 0, 4));
58-
add(leftIcon, BorderLayout.WEST);
43+
public CustomDropDownLink(@NotNull T initialItem, @NotNull List<T> items, @Nullable Consumer<? super T> itemChosenAction, boolean updateLabel) {
44+
this(initialItem, (linkLabel) -> {
45+
IPopupChooserBuilder<T> popupBuilder = JBPopupFactory.getInstance().createPopupChooserBuilder(items).
46+
setRenderer(new LinkCellRenderer<>(linkLabel)).
47+
setItemChosenCallback(t -> {
48+
linkLabel.chosenItem = t;
49+
if (updateLabel) {
50+
linkLabel.setText(t.toString());
51+
}
52+
53+
if (itemChosenAction != null) {
54+
itemChosenAction.consume(t);
55+
}
56+
});
57+
return popupBuilder.createPopup();
58+
});
59+
}
5960

60-
setText(selectedItem);
61+
private void init() {
62+
setIconTextGap(JBUI.scale(1));
63+
setHorizontalAlignment(SwingConstants.LEADING);
64+
setHorizontalTextPosition(SwingConstants.LEADING);
6165

62-
if (icons.size() > 0) {
63-
leftIcon.setIcon(icons.get(items.indexOf(selectedItem)));
64-
}
66+
setUI(new MetalLabelUI() {
67+
@Override
68+
protected String layoutCL(JLabel label, FontMetrics fontMetrics, String text, Icon icon,
69+
Rectangle viewR, Rectangle iconR, Rectangle textR) {
70+
String result = super.layoutCL(label, fontMetrics, text, icon, viewR, iconR, textR);
71+
iconR.y += JBUI.scale(1);
72+
return result;
73+
}
74+
});
6575
}
6676

67-
public String getSelectedItem() {
68-
return selectedItem;
77+
public T getChosenItem() {
78+
return chosenItem;
6979
}
7080

71-
/**
72-
* Only render selected item.
73-
*
74-
* @param selectedItem
75-
*/
76-
public void setSelectedItem(String selectedItem) {
77-
if (!items.contains(selectedItem)) {
78-
return;
81+
private static class LinkCellRenderer<T> extends JLabel implements ListCellRenderer<T> {
82+
private final JComponent owner;
83+
84+
private LinkCellRenderer(JComponent owner) {
85+
this.owner = owner;
86+
setBorder(JBUI.Borders.empty(0, 5, 0, 10));
7987
}
8088

81-
setText(selectedItem);
82-
if (icons.size() > 0) {
83-
leftIcon.setIcon(icons.get(items.indexOf(selectedItem)));
89+
@Override
90+
public Dimension getPreferredSize() {
91+
return recomputeSize(super.getPreferredSize());
8492
}
8593

86-
this.selectedItem = selectedItem;
87-
}
94+
@Override
95+
public Dimension getMinimumSize() {
96+
return recomputeSize(super.getMinimumSize());
97+
}
8898

89-
void showPopup() {
90-
if (!isEnabled()) return;
91-
final BaseListPopupStep<String> list = new BaseListPopupStep<String>(null, items, icons) {
92-
@Override
93-
public PopupStep onChosen(String selectedValue, boolean finalChoice) {
94-
if (consumer != null) {
95-
consumer.accept(selectedValue);
96-
}
97-
setSelectedItem(selectedValue);
98-
return super.onChosen(selectedValue, finalChoice);
99-
}
99+
private Dimension recomputeSize(@NotNull Dimension size) {
100+
size.height = Math.max(size.height, JBUI.scale(22));
101+
size.width = Math.max(size.width, owner.getPreferredSize().width);
102+
return size;
103+
}
100104

101-
@Override
102-
public int getDefaultOptionIndex() {
103-
return items.indexOf(selectedItem);
104-
}
105+
@Override
106+
public Component getListCellRendererComponent(JList<? extends T> list, T value, int index, boolean isSelected, boolean cellHasFocus) {
107+
setText(value.toString());
108+
setEnabled(list.isEnabled());
109+
setOpaque(true);
105110

106-
// @Override
107-
// public Color getForegroundFor(String val) {
108-
// return JBColor.link();
109-
// }
110-
};
111+
setBackground(isSelected ? list.getSelectionBackground() : UIManager.getColor("Label.background"));
112+
setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
111113

112-
final ListPopup popup = JBPopupFactory.getInstance().createListPopup(list);
113-
Point showPoint = new Point(0, getHeight() + JBUI.scale(4));
114-
popup.show(new RelativePoint(this, showPoint));
114+
return this;
115+
}
115116
}
116-
}
117+
}

0 commit comments

Comments
 (0)