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.
52package 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 ;
77import 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 ;
128import 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 ;
1512import com .intellij .util .ui .JBUI ;
13+ import org .jetbrains .annotations .NotNull ;
14+ import org .jetbrains .annotations .Nullable ;
1615
1716import javax .swing .*;
18- import javax .swing .border . EmptyBorder ;
17+ import javax .swing .plaf . metal . MetalLabelUI ;
1918import java .awt .*;
20- import java .awt .event .MouseAdapter ;
21- import java .awt .event .MouseEvent ;
22- import java .util .Arrays ;
2319import 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