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.ui.JBColor; 19 import com.intellij.ui.components.JBPanel; 20 import net.miginfocom.swing.MigLayout; 21 import ohos.devtools.views.trace.util.Utils; 22 23 import javax.swing.JLayeredPane; 24 import javax.swing.SwingUtilities; 25 import javax.swing.border.LineBorder; 26 import java.awt.Component; 27 import java.awt.Graphics; 28 import java.awt.Graphics2D; 29 import java.awt.Point; 30 import java.awt.RenderingHints; 31 import java.awt.event.MouseEvent; 32 import java.util.Arrays; 33 import java.util.Comparator; 34 import java.util.List; 35 import java.util.Objects; 36 37 /** 38 * Tip 39 * 40 * @since 2021/5/28 19:32 41 */ 42 public class Tip extends JBPanel { 43 private static final int LEFT_PADDING = 10; 44 private static final int RIGHT_PADDING = 10; 45 private static final int BOTTOM_PADDING = 20; 46 private static final int TOP_PADDING = 20; 47 private static final int ROW_HEIGHT = 20; 48 private static final int MEGA_BYTES = 10241024; 49 private static Tip tip = new Tip(); 50 51 private JLayeredPane layeredPane; 52 private List<String> stringList; 53 Tip()54 private Tip() { 55 setLayout(new MigLayout("")); 56 setBorder(new LineBorder(JBColor.background().darker())); 57 setBackground(JBColor.background().brighter()); 58 } 59 60 /** 61 * get tip Instance 62 * 63 * @return Tip 64 */ getInstance()65 public static Tip getInstance() { 66 return tip; 67 } 68 69 @Override paint(Graphics graphics)70 public void paint(Graphics graphics) { 71 super.paint(graphics); 72 if (graphics instanceof Graphics2D) { 73 Graphics2D g2 = (Graphics2D) graphics; 74 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 75 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // Set anti-aliasing 76 if (Objects.nonNull(stringList)) { 77 int startY = TOP_PADDING; 78 for (String str : stringList) { 79 g2.setPaint(JBColor.foreground()); 80 g2.drawString(str, LEFT_PADDING, startY); 81 g2.setPaint(JBColor.foreground()); 82 g2.drawString(str, LEFT_PADDING, startY); 83 startY += ROW_HEIGHT; 84 } 85 } 86 } 87 } 88 89 /** 90 * setJLayeredPane 91 * 92 * @param layeredPane layeredPane 93 */ setJLayeredPane(JLayeredPane layeredPane)94 public void setJLayeredPane(JLayeredPane layeredPane) { 95 this.layeredPane = layeredPane; 96 if (Arrays.stream(layeredPane.getComponents()).allMatch(component -> component.getClass() != Tip.class)) { 97 layeredPane.add(this); 98 } 99 } 100 101 /** 102 * setJLayeredPane 103 * 104 * @param source source 105 * @param point point 106 * @return Point 107 */ getPoint(Component source, Point point)108 public Point getPoint(Component source, Point point) { 109 return SwingUtilities.convertPoint(source, point, layeredPane); 110 } 111 112 /** 113 * display current tip 114 * 115 * @param source source 116 * @param point point 117 * @param stringList stringList 118 */ display(Component source, Point point, List<String> stringList)119 public void display(Component source, Point point, List<String> stringList) { 120 if (Objects.isNull(stringList)) { 121 return; 122 } 123 if (Objects.nonNull(layeredPane)) { 124 this.stringList = stringList; 125 this.setVisible(true); 126 Point point1 = SwingUtilities.convertPoint(source, point, layeredPane); 127 String maxString = stringList.stream().max(Comparator.comparingInt(String::length)).orElse(""); 128 int maxWidth = SwingUtilities.computeStringWidth(getFontMetrics(getFont()), maxString); 129 setBounds(Utils.getX(point1), Utils.getY(point1), maxWidth + LEFT_PADDING + RIGHT_PADDING, 130 stringList.size() * ROW_HEIGHT + BOTTOM_PADDING); 131 layeredPane.setLayer(this, 1000); 132 } 133 } 134 135 /** 136 * display current tip 137 * 138 * @param event event 139 * @param stringList stringList 140 */ display(MouseEvent event, List<String> stringList)141 public void display(MouseEvent event, List<String> stringList) { 142 display(event.getComponent(), event.getPoint(), stringList); 143 } 144 145 /** 146 * display current tip 147 */ hidden()148 public void hidden() { 149 if (Objects.nonNull(layeredPane)) { 150 this.setVisible(false); 151 layeredPane.setLayer(this, -1); 152 } 153 } 154 } 155