• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package ohos.devtools.views.common.customcomp;
17 
18 import com.intellij.icons.AllIcons;
19 import com.intellij.util.ui.components.JBComponent;
20 
21 import javax.swing.BorderFactory;
22 import javax.swing.JButton;
23 import javax.swing.JPanel;
24 import javax.swing.ListCellRenderer;
25 import javax.swing.UIManager;
26 import javax.swing.plaf.ComponentUI;
27 import javax.swing.plaf.basic.BasicComboBoxUI;
28 import java.awt.Component;
29 import java.awt.Graphics;
30 import java.awt.Rectangle;
31 
32 /**
33  * Hos JBComboBox
34  *
35  * @since 2021/5/19 16:39
36  */
37 public class CustomJBComboBoxUI extends BasicComboBoxUI {
38 
39     /**
40      * createUI
41      *
42      * @param component JComponent
43      * @return ComponentUI
44      */
createUI(JBComponent component)45     public static ComponentUI createUI(JBComponent component) {
46         return new CustomJBComboBoxUI();
47     }
48 
49     /**
50      * createArrowButton
51      *
52      * @return JButton
53      */
54     @Override
createArrowButton()55     protected JButton createArrowButton() {
56         JButton dropDownButton = new JButton();
57         dropDownButton.setContentAreaFilled(false);
58         dropDownButton.setFocusPainted(false);
59         dropDownButton.setBorder(BorderFactory.createEmptyBorder());
60         dropDownButton.setOpaque(true);
61         dropDownButton.setIcon(AllIcons.Actions.FindAndShowNextMatchesSmall);
62         return dropDownButton;
63     }
64 
65     @Override
paintCurrentValue(Graphics graphics, Rectangle bounds, boolean hasFocus)66     public void paintCurrentValue(Graphics graphics, Rectangle bounds, boolean hasFocus) {
67         ListCellRenderer<Object> renderer = comboBox.getRenderer();
68         Component component;
69 
70         if (hasFocus && !isPopupVisible(comboBox)) {
71             component = renderer.getListCellRendererComponent(listBox, comboBox.getSelectedItem(), -1, true, false);
72         } else {
73             component = renderer.getListCellRendererComponent(listBox, comboBox.getSelectedItem(), -1, false, false);
74             component.setBackground(UIManager.getColor("ComboBox.background"));
75         }
76         component.setFont(comboBox.getFont());
77         if (hasFocus && !isPopupVisible(comboBox)) {
78             component.setBackground(UIManager.getColor("ComboBox.background"));
79         } else {
80             if (comboBox.isEnabled()) {
81                 component.setBackground(UIManager.getColor("ComboBox.background"));
82             } else {
83                 component.setBackground(UIManager.getColor("ComboBox.background"));
84             }
85         }
86         boolean shouldValidate = false;
87         if (component instanceof JPanel) {
88             shouldValidate = true;
89         }
90         int boundX = (int) bounds.getX();
91         int boundY = (int) bounds.getY();
92         int boundW = bounds.width;
93         int boundH = bounds.height;
94         if (padding != null) {
95             boundX = (int) (bounds.getX() + padding.left);
96             boundY = (int) (bounds.getY() + padding.top);
97             boundW = bounds.width - (padding.left + padding.right);
98             boundH = bounds.height - (padding.top + padding.bottom);
99         }
100         currentValuePane.paintComponent(graphics, component, comboBox, boundX, boundY, boundW, boundH, shouldValidate);
101     }
102 }
103