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 ohos.devtools.views.trace.util.Utils; 20 21 import javax.swing.SwingUtilities; 22 import java.awt.Graphics; 23 import java.awt.Graphics2D; 24 import java.awt.Point; 25 import java.util.Arrays; 26 import java.util.List; 27 import java.util.Objects; 28 import java.util.concurrent.CompletableFuture; 29 import java.util.function.Supplier; 30 31 /** 32 * TraceSimpleRow 33 * 34 * @since 2021/5/18 13:56 35 */ 36 public class TraceSimpleRow<T extends AbstractNode> extends AbstractRow { 37 private IRender render; 38 private List<T> data; 39 private T currentData; 40 private Supplier<List<T>> supplier; 41 42 /** 43 * structure 44 * 45 * @param name name 46 */ TraceSimpleRow(String name)47 public TraceSimpleRow(String name) { 48 super(name, false, true); 49 loadData(); 50 } 51 52 /** 53 * setRender 54 * 55 * @param render set current render 56 */ setRender(IRender<T> render)57 public void setRender(IRender<T> render) { 58 this.render = render; 59 } 60 61 /** 62 * setSupplier 63 * 64 * @param supplier set current supplier 65 */ setSupplier(Supplier<List<T>> supplier)66 public void setSupplier(Supplier<List<T>> supplier) { 67 this.supplier = supplier; 68 } 69 70 @Override contentPaint(Graphics graphics)71 public void contentPaint(Graphics graphics) { 72 if (render != null) { 73 if (Objects.isNull(data)) { 74 graphics.setColor(JBColor.foreground()); 75 graphics.drawString("Loading...", 10, 10 + 12); 76 loadData(); 77 } else { 78 if (graphics instanceof Graphics2D) { 79 render.paint((Graphics2D) graphics, data); 80 } 81 } 82 } 83 } 84 85 @Override mouseMoveHandler(Point point)86 public void mouseMoveHandler(Point point) { 87 super.mouseMoveHandler(point); 88 if (Objects.nonNull(data)) { 89 if (data.stream().filter(it -> contains(it)).anyMatch(it -> it.getRect().contains(point))) { 90 data.stream().filter(it -> contains(it) && it.getRect().contains(point)).forEach(it -> { 91 List<String> stringList = it.getStringList(getTimeByX(Utils.getX(point))); 92 Tip.getInstance().display(content, point, stringList); 93 if (Objects.nonNull(currentData)) { 94 currentData.moveOut(point, content); 95 } 96 it.moveIn(point, content); 97 currentData = it; 98 }); 99 } else { 100 if (Objects.nonNull(currentData)) { 101 currentData.moveOut(point, content); 102 } 103 Tip.getInstance().display(content, point, Arrays.asList(getTimeByX(Utils.getX(point)))); 104 } 105 } 106 } 107 108 @Override loadData()109 public void loadData() { 110 if (!isLoading.get()) { 111 isLoading.set(true); 112 CompletableFuture.runAsync(() -> { 113 if (supplier != null) { 114 data = supplier.get(); 115 } 116 SwingUtilities.invokeLater(() -> { 117 isLoading.set(false); 118 content.repaint(); 119 }); 120 }, Utils.getPool()).whenComplete((unused, throwable) -> { 121 if (Objects.nonNull(throwable)) { 122 throwable.printStackTrace(); 123 } 124 }); 125 } 126 } 127 128 /** 129 * reload current row 130 */ reload()131 public void reload() { 132 data = null; 133 repaint(); 134 } 135 136 /** 137 * interface IRender 138 */ 139 public interface IRender<T> { 140 /** 141 * paint 142 * 143 * @param g2 Graphics2D 144 * @param data data 145 */ paint(Graphics2D g2, List<T> data)146 void paint(Graphics2D g2, List<T> data); 147 } 148 } 149