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.component; 17 18 import com.intellij.ui.JBSplitter; 19 import com.intellij.ui.components.JBTabbedPane; 20 import ohos.devtools.views.trace.util.Final; 21 import ohos.devtools.views.trace.util.Utils; 22 23 import javax.imageio.ImageIO; 24 import java.awt.Graphics; 25 import java.awt.Image; 26 import java.awt.Rectangle; 27 import java.awt.event.MouseAdapter; 28 import java.awt.event.MouseEvent; 29 import java.io.IOException; 30 31 /** 32 * tab component 33 * 34 * @since 2021/04/20 12:12 35 */ 36 public class TabPanel extends JBTabbedPane { 37 private static int barHeight; 38 39 private final int iconWH = 20; 40 private Rectangle topRect; 41 private Rectangle bottomRect; 42 private Image topImage = null; 43 private Image bottomImage = null; 44 private JBSplitter splitter; 45 46 /** 47 * structure function 48 */ TabPanel()49 public TabPanel() { 50 setFont(Final.NORMAL_FONT); 51 try { 52 topImage = ImageIO.read(getClass().getResourceAsStream("/assets/top.png")); 53 bottomImage = ImageIO.read(getClass().getResourceAsStream("/assets/bottom.png")); 54 } catch (IOException ioException) { 55 ioException.printStackTrace(); 56 } 57 addMouseListener(new MouseAdapter() { 58 @Override 59 public void mouseClicked(final MouseEvent event) { 60 super.mouseClicked(event); 61 clickTop(event); 62 clickBottom(event); 63 } 64 }); 65 } 66 67 @Override paint(Graphics graphics)68 public void paint(Graphics graphics) { 69 super.paint(graphics); 70 if (getTabCount() > 0) { 71 Rectangle tabBounds = getUI().getTabBounds(this, 0); 72 barHeight = tabBounds.height; 73 } 74 topRect = new Rectangle(getWidth() - 65, (barHeight - iconWH) / 2, iconWH, iconWH); 75 bottomRect = new Rectangle(getWidth() - 35, (barHeight - iconWH) / 2, iconWH, iconWH); 76 if (topImage != null) { 77 graphics 78 .drawImage(topImage, Utils.getX(topRect) + 2, Utils.getY(topRect) + 2, iconWH - 5, iconWH - 5, null); 79 } 80 if (bottomImage != null) { 81 graphics 82 .drawImage(bottomImage, Utils.getX(bottomRect) + 2, Utils.getY(bottomRect) + 2, iconWH - 5, iconWH - 5, 83 null); 84 } 85 } 86 87 /** 88 * Click to top 89 * 90 * @param event Mouse event 91 */ clickTop(final MouseEvent event)92 private void clickTop(final MouseEvent event) { 93 if (topRect.contains(event.getPoint())) { 94 if (SysAnalystPanel.getSplitter() != null) { 95 this.setVisible(true); 96 SysAnalystPanel.getSplitter().setProportion(0.05f); 97 } 98 } 99 } 100 101 /** 102 * Click to bottom 103 * 104 * @param event Mouse event 105 */ clickBottom(final MouseEvent event)106 private void clickBottom(final MouseEvent event) { 107 if (bottomRect.contains(event.getPoint())) { 108 hideInBottom(); 109 } 110 } 111 112 /** 113 * Minimize the bottom tab 114 */ hideInBottom()115 public void hideInBottom() { 116 if (SysAnalystPanel.getSplitter() != null) { 117 this.setVisible(true); 118 SysAnalystPanel.getSplitter().setProportion(0.95f); 119 } 120 } 121 122 /** 123 * hide bottom tab 124 */ hidden()125 public void hidden() { 126 if (SysAnalystPanel.getSplitter() != null) { 127 this.setVisible(false); 128 SysAnalystPanel.getSplitter().setProportion(1.0f); 129 } 130 } 131 132 /** 133 * display current panel 134 */ display()135 public void display() { 136 if (SysAnalystPanel.getSplitter() != null) { 137 this.setVisible(true); 138 SysAnalystPanel.getSplitter().setProportion(0.6f); 139 } 140 } 141 142 /** 143 * display current panel 144 * 145 * @param proportion proportion 146 */ display(float proportion)147 public void display(float proportion) { 148 if (SysAnalystPanel.getSplitter() != null) { 149 this.setVisible(true); 150 SysAnalystPanel.getSplitter().setProportion(proportion); 151 } 152 } 153 154 } 155