• 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.trace;
17 
18 import com.intellij.icons.AllIcons;
19 import com.intellij.ui.JBColor;
20 import com.intellij.ui.components.JBLabel;
21 import com.intellij.ui.components.JBPanel;
22 import net.miginfocom.swing.MigLayout;
23 
24 import javax.swing.Icon;
25 import javax.swing.JButton;
26 import javax.swing.JComponent;
27 import java.awt.Color;
28 import java.awt.Component;
29 import java.awt.Dimension;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 
33 /**
34  * ExpandPanel
35  *
36  * @since 2021/5/19 16:39
37  */
38 public class ExpandPanel extends JBPanel {
39     private JComponent myContent;
40     private Icon myExpandIcon;
41     private Icon myCollapseIcon;
42     private JButton myToggleCollapseButton;
43     private JBLabel myTitleLabel;
44     private boolean myIsInitialized;
45     private boolean myIsCollapsed;
46 
47     /**
48      * Constructor
49      *
50      * @param title title
51      */
ExpandPanel(String title)52     public ExpandPanel(String title) {
53         super(new MigLayout("insets 0", "0[grow,fill]0", "0[grow,fill]0"));
54         myContent = new JBPanel();
55         myContent.setLayout(new MigLayout("insets 0,wrap 1", "0[]0", "0[]0"));
56         myExpandIcon = AllIcons.General.ArrowRight;
57         myCollapseIcon = AllIcons.General.ArrowDown;
58         setBackground(JBColor.background().brighter());
59         myToggleCollapseButton = new JButton();
60         myToggleCollapseButton.setBorderPainted(false);
61         myToggleCollapseButton.setContentAreaFilled(false);
62         myToggleCollapseButton.setBackground(new Color(0, 0, 0, 0));
63         myToggleCollapseButton.setMaximumSize(new Dimension(myExpandIcon.getIconWidth(), myExpandIcon.getIconHeight()));
64         myToggleCollapseButton.setFocusable(true);
65         add(myToggleCollapseButton, "split 2 ,w 5!,gapleft 5,gapright 5");
66         if (title != null) {
67             myTitleLabel = new JBLabel(title);
68             myTitleLabel.setToolTipText(title);
69             add(myTitleLabel, "h 30!,align center,wrap");
70         }
71         myToggleCollapseButton.addActionListener(new ActionListener() {
72             @Override
73             public void actionPerformed(ActionEvent event) {
74                 setCollapsed(!myIsCollapsed);
75             }
76         });
77         setCollapsed(false);
78     }
79 
80     /**
81      * Constructor
82      *
83      * @param title title
84      * @param labelWidth labelWidth
85      */
ExpandPanel(String title, int labelWidth)86     public ExpandPanel(String title, int labelWidth) {
87         super(new MigLayout("insets 0", "0[" + labelWidth + "!]0[grow,fill]0", "0[grow,fill]0"));
88         myContent = new JBPanel();
89         myContent.setLayout(new MigLayout("insets 0,wrap 1", "0[]0", "0[]0"));
90         myExpandIcon = AllIcons.General.ArrowRight;
91         myCollapseIcon = AllIcons.General.ArrowDown;
92         myToggleCollapseButton = new JButton();
93         myToggleCollapseButton.setBorderPainted(false);
94         myToggleCollapseButton.setContentAreaFilled(false);
95         myToggleCollapseButton.setBackground(new Color(0, 0, 0, 0));
96         myToggleCollapseButton.setMaximumSize(new Dimension(myExpandIcon.getIconWidth(), myExpandIcon.getIconHeight()));
97         myToggleCollapseButton.setFocusable(true);
98         add(myToggleCollapseButton, "split 2,w 5!,gapleft 5,gapright 5");
99         if (title != null) {
100             myTitleLabel = new JBLabel(title);
101             JBPanel rPanel = new JBPanel();
102             add(myTitleLabel, "h 30!,w " + (labelWidth - 15) + "!,align left");
103             add(rPanel, "h 30!,pushx,growx,wrap");
104             rPanel.setBackground(JBColor.background().darker());
105         }
106         myToggleCollapseButton.addActionListener(new ActionListener() {
107             @Override
108             public void actionPerformed(ActionEvent event) {
109                 setCollapsed(!myIsCollapsed);
110             }
111         });
112         setCollapsed(false);
113     }
114 
115     /**
116      * Constructor
117      *
118      * @param content content
119      * @param isCollapsed isCollapsed
120      * @param collapseIcon collapseIcon
121      * @param expandIcon expandIcon
122      * @param title title
123      */
ExpandPanel(JComponent content, boolean isCollapsed, Icon collapseIcon, Icon expandIcon, String title)124     public ExpandPanel(JComponent content, boolean isCollapsed, Icon collapseIcon, Icon expandIcon, String title) {
125         super(new MigLayout("insets 0", "0[grow,fill]0", "0[grow,fill]0"));
126         myContent = content;
127         myExpandIcon = expandIcon;
128         myCollapseIcon = collapseIcon;
129         setBackground(JBColor.background().brighter());
130         myToggleCollapseButton = new JButton();
131         myToggleCollapseButton.setBorderPainted(false);
132         myToggleCollapseButton.setContentAreaFilled(false);
133         myToggleCollapseButton.setBackground(new Color(0, 0, 0, 0));
134         myToggleCollapseButton.setMaximumSize(new Dimension(myExpandIcon.getIconWidth(), myExpandIcon.getIconHeight()));
135         myToggleCollapseButton.setFocusable(true);
136         add(myToggleCollapseButton, "split 2,w 5!,gapleft 5,gapright 5");
137         if (title != null) {
138             myTitleLabel = new JBLabel(title);
139             add(myTitleLabel, "h 30!,align center,wrap");
140         }
141         myToggleCollapseButton.addActionListener(new ActionListener() {
142             @Override
143             public void actionPerformed(ActionEvent actionEvent) {
144                 setCollapsed(!myIsCollapsed);
145             }
146         });
147         setCollapsed(isCollapsed);
148     }
149 
150     /**
151      * get title
152      *
153      * @return title text
154      */
getTitle()155     public String getTitle() {
156         return myTitleLabel.getText();
157     }
158 
159     /**
160      * set title
161      *
162      * @param title title
163      */
setTitle(String title)164     public void setTitle(String title) {
165         myTitleLabel.setText(title);
166     }
167 
168     /**
169      * add TraceRow
170      *
171      * @param row row
172      */
addTraceRow(AbstractRow row)173     public void addTraceRow(AbstractRow row) {
174         myContent.add(row, "pushx,growx,h 30::");
175     }
176 
177     /**
178      * add TraceRow
179      *
180      * @param row row
181      * @param constraints constraints
182      */
addTraceRow(AbstractRow row, String constraints)183     public void addTraceRow(AbstractRow row, String constraints) {
184         myContent.add(row, constraints);
185     }
186 
187     /**
188      * add component
189      *
190      * @param component component
191      * @param constraints constraints
192      */
addRow(JComponent component, String constraints)193     public void addRow(JComponent component, String constraints) {
194         myContent.add(component, constraints);
195     }
196 
197     /**
198      * get myContent
199      *
200      * @return myContent
201      */
getContent()202     public JComponent getContent() {
203         return myContent;
204     }
205 
206     /**
207      * fresh myContent
208      *
209      * @param startNS startNS
210      * @param endNS endNS
211      */
refresh(long startNS, long endNS)212     public void refresh(long startNS, long endNS) {
213         if (!myIsCollapsed) {
214             for (Component component : myContent.getComponents()) {
215                 if (component instanceof AbstractRow) {
216                     ((AbstractRow) component).refresh(startNS, endNS);
217                 }
218             }
219         }
220     }
221 
222     /**
223      * is collapsed
224      *
225      * @return isCollapsed
226      */
isCollapsed()227     public boolean isCollapsed() {
228         return myIsCollapsed;
229     }
230 
setCollapsed(boolean isCollapsed)231     private void setCollapsed(boolean isCollapsed) {
232         try {
233             if (isCollapsed) {
234                 if (myIsInitialized) {
235                     remove(myContent);
236                 }
237             } else {
238                 add(myContent, "span 2 1,pushx,growx,wrap");
239             }
240             myIsCollapsed = isCollapsed;
241             Icon icon = myIsCollapsed ? myExpandIcon : myCollapseIcon;
242             if (icon != null) {
243                 myToggleCollapseButton.setIcon(icon);
244                 myToggleCollapseButton.setBorder(null);
245                 myToggleCollapseButton.setBorderPainted(false);
246             }
247             if (isCollapsed) {
248                 myToggleCollapseButton.requestFocusInWindow();
249                 myToggleCollapseButton.setSelected(true);
250             } else {
251                 myContent.requestFocusInWindow();
252             }
253             revalidate();
254             repaint();
255         } finally {
256             myIsInitialized = true;
257         }
258     }
259 }
260