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.fragment; 17 18 import ohos.devtools.views.trace.bean.FunctionBean; 19 import ohos.devtools.views.trace.bean.ThreadData; 20 import ohos.devtools.views.trace.component.AnalystPanel; 21 import ohos.devtools.views.trace.util.Utils; 22 23 import javax.swing.JComponent; 24 import java.awt.Graphics2D; 25 import java.awt.event.KeyEvent; 26 import java.awt.event.MouseEvent; 27 import java.awt.geom.Rectangle2D; 28 import java.util.ArrayList; 29 30 /** 31 * Method call data row 32 * 33 * @since 2021/04/22 12:25 34 */ 35 public class FunctionDataFragment extends AbstractDataFragment<FunctionBean> implements FunctionBean.IEventListener { 36 /** 37 * graph event callback 38 */ 39 protected static FunctionBean currentSelectedFunctionData; 40 41 /** 42 * Thread object 43 */ 44 public ThreadData thread; 45 46 /** 47 * structure 48 * 49 * @param contentPanel contentPanel 50 * @param functionBeans functionBeans 51 */ FunctionDataFragment(JComponent contentPanel, ArrayList<FunctionBean> functionBeans)52 public FunctionDataFragment(JComponent contentPanel, ArrayList<FunctionBean> functionBeans) { 53 super(contentPanel, true, false); 54 this.setRoot(contentPanel); 55 this.data = functionBeans; 56 } 57 58 /** 59 * Drawing method 60 * 61 * @param graphics graphics 62 */ 63 @Override draw(Graphics2D graphics)64 public void draw(Graphics2D graphics) { 65 super.draw(graphics); 66 // Supplement the information on the left 67 graphics.setColor(getRoot().getForeground()); 68 String name = thread.getThreadName() + " " + thread.getTid(); 69 Rectangle2D bounds = graphics.getFontMetrics().getStringBounds(name, graphics); 70 double wordWidth = bounds.getWidth() / name.length(); // Width per character 71 double wordNum = (getDescRect().width - 40) / wordWidth; // How many characters can be displayed on each line 72 if (bounds.getWidth() < getDescRect().width - 40) { // Direct line display 73 graphics.drawString(name, Utils.getX(getDescRect()) + 10, 74 (int) (Utils.getY(getDescRect()) + bounds.getHeight() + 10)); 75 } else { 76 String substring = name.substring((int) wordNum); 77 if (substring.length() < wordNum) { 78 graphics.drawString(name.substring(0, (int) wordNum), Utils.getX(getDescRect()) + 10, 79 (int) (Utils.getY(getDescRect()) + bounds.getHeight() + 8)); 80 graphics.drawString(substring, Utils.getX(getDescRect()) + 10, 81 (int) (Utils.getY(getDescRect()) + bounds.getHeight() * 2 + 8)); 82 } else { 83 graphics.drawString(name.substring(0, (int) wordNum), Utils.getX(getDescRect()) + 10, 84 (int) (Utils.getY(getDescRect()) + bounds.getHeight() + 2)); 85 graphics.drawString(substring.substring(0, (int) wordNum), Utils.getX(getDescRect()) + 10, 86 (int) (Utils.getY(getDescRect()) + bounds.getHeight() * 2 + 2)); 87 graphics.drawString(substring.substring((int) wordNum), Utils.getX(getDescRect()) + 10, 88 (int) (Utils.getY(getDescRect()) + bounds.getHeight() * 3 + 2)); 89 } 90 } 91 if (data != null) { 92 data.stream().filter(it -> it.getStartTime() + it.getDuration() > startNS && it.getStartTime() < endNS) 93 .forEach(bean -> { 94 int x1; 95 int x2; 96 if (bean.getStartTime() < startNS) { 97 x1 = getX(startNS); 98 } else { 99 x1 = getX(bean.getStartTime()); 100 } 101 if (bean.getStartTime() + bean.getDuration() > endNS) { 102 x2 = getX(endNS); 103 } else { 104 x2 = getX(bean.getStartTime() + bean.getDuration()); 105 } 106 bean.setRect(x1 + Utils.getX(getDataRect()), Utils.getY(getDataRect()) + 10 + 20 * bean.getDepth(), 107 x2 - x1 <= 0 ? 1 : x2 - x1, 20); 108 bean.root = getRoot(); 109 bean.setEventListener(this); 110 bean.draw(graphics); 111 }); 112 } 113 } 114 115 /** 116 * Mouse click event 117 * 118 * @param event event 119 */ 120 @Override mouseClicked(MouseEvent event)121 public void mouseClicked(MouseEvent event) { 122 super.mouseClicked(event); 123 if (data != null) { 124 data.stream() 125 .filter(bean -> bean.getStartTime() + bean.getDuration() > startNS && bean.getStartTime() < endNS) 126 .filter(bean -> bean.edgeInspect(event)).findFirst().ifPresent(bean -> { 127 bean.onClick(event); 128 }); 129 } 130 } 131 132 /** 133 * Mouse pressed event 134 * 135 * @param event event 136 */ 137 @Override mousePressed(MouseEvent event)138 public void mousePressed(MouseEvent event) { 139 } 140 141 /** 142 * Mouse exited event 143 * 144 * @param event event 145 */ 146 @Override mouseExited(MouseEvent event)147 public void mouseExited(MouseEvent event) { 148 } 149 150 /** 151 * Mouse entered event 152 * 153 * @param event event 154 */ 155 @Override mouseEntered(MouseEvent event)156 public void mouseEntered(MouseEvent event) { 157 } 158 159 /** 160 * Mouse moved event 161 * 162 * @param evt event 163 */ 164 @Override mouseMoved(MouseEvent evt)165 public void mouseMoved(MouseEvent evt) { 166 MouseEvent event = getRealMouseEvent(evt); 167 super.mouseMoved(event); 168 clearFocus(event); 169 } 170 171 /** 172 * Mouse released event 173 * 174 * @param event event 175 */ 176 @Override mouseReleased(MouseEvent event)177 public void mouseReleased(MouseEvent event) { 178 } 179 180 /** 181 * key released event 182 * 183 * @param event event 184 */ 185 @Override keyReleased(KeyEvent event)186 public void keyReleased(KeyEvent event) { 187 } 188 189 /** 190 * Click event 191 * 192 * @param event event 193 * @param data data 194 */ 195 @Override click(MouseEvent event, FunctionBean data)196 public void click(MouseEvent event, FunctionBean data) { 197 clearSelected(); 198 data.setSelected(true); 199 data.repaint(); 200 currentSelectedFunctionData = data; 201 if (AnalystPanel.iFunctionDataClick != null) { 202 AnalystPanel.iFunctionDataClick.click(data); 203 } 204 } 205 206 /** 207 * Loss of focus event 208 * 209 * @param event event 210 * @param data data 211 */ 212 @Override blur(MouseEvent event, FunctionBean data)213 public void blur(MouseEvent event, FunctionBean data) { 214 } 215 216 /** 217 * Get focus event 218 * 219 * @param event event 220 * @param data data 221 */ 222 @Override focus(MouseEvent event, FunctionBean data)223 public void focus(MouseEvent event, FunctionBean data) { 224 } 225 226 /** 227 * Mouse move event 228 * 229 * @param event event 230 * @param data data 231 */ 232 @Override mouseMove(MouseEvent event, FunctionBean data)233 public void mouseMove(MouseEvent event, FunctionBean data) { 234 } 235 236 // function After loading together, there is no need to redraw drawFrame according to the time interval 237 @Override drawFrame()238 public void drawFrame() { 239 } 240 } 241