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.ruler; 17 18 import ohos.devtools.views.trace.component.AnalystPanel; 19 import ohos.devtools.views.trace.util.Utils; 20 21 import javax.swing.JComponent; 22 import java.awt.Color; 23 import java.awt.Rectangle; 24 import java.awt.event.MouseEvent; 25 26 /** 27 * basic data 28 * 29 * @since 2021/04/22 12:25 30 */ 31 public abstract class AbstractFragment extends AbstractNode implements IFragment { 32 private final Color lineColor = new Color(255, 255, 255, 80); 33 private final Color textColor = new Color(0x999999); 34 private Rectangle rect = new Rectangle(0, 0, 0, 0); 35 private Rectangle descRect = new Rectangle(0, 0, 0, 0); 36 private Rectangle dataRect = new Rectangle(0, 0, 0, 0); 37 private JComponent root; 38 39 /** 40 * Gets the value of rect . 41 * 42 * @return the value of java.awt.Rectangle 43 */ getRect()44 public Rectangle getRect() { 45 return rect; 46 } 47 48 /** 49 * Sets the rect .You can use getRect() to get the value of rect 50 * 51 * @param rect rect 52 */ setRect(final Rectangle rect)53 public void setRect(final Rectangle rect) { 54 this.rect = rect; 55 } 56 57 /** 58 * Gets the value of descRect . 59 * 60 * @return the value of java.awt.Rectangle 61 */ getDescRect()62 public Rectangle getDescRect() { 63 return descRect; 64 } 65 66 /** 67 * Sets the descRect . 68 * <p>You can use getDescRect() to get the value of descRect</p> 69 * 70 * @param descRect descRect 71 */ setDescRect(final Rectangle descRect)72 public void setDescRect(final Rectangle descRect) { 73 this.descRect = descRect; 74 } 75 76 /** 77 * Gets the value of dataRect . 78 * 79 * @return the value of java.awt.Rectangle 80 */ getDataRect()81 public Rectangle getDataRect() { 82 return dataRect; 83 } 84 85 /** 86 * Sets the dataRect . 87 * <p>You can use getDataRect() to get the value of dataRect</p> 88 * 89 * @param dataRect dataRect 90 */ setDataRect(final Rectangle dataRect)91 public void setDataRect(final Rectangle dataRect) { 92 this.dataRect = dataRect; 93 } 94 95 /** 96 * Gets the value of root . 97 * 98 * @return the value of javax.swing.JComponent 99 */ getRoot()100 public JComponent getRoot() { 101 return root; 102 } 103 104 /** 105 * Sets the root . 106 * <p>You can use getRoot() to get the value of root</p> 107 * 108 * @param jComponent component 109 */ setRoot(final JComponent jComponent)110 public void setRoot(final JComponent jComponent) { 111 this.root = jComponent; 112 } 113 114 /** 115 * Gets the value of lineColor . 116 * 117 * @return the value of java.awt.Color 118 */ getLineColor()119 public Color getLineColor() { 120 return lineColor; 121 } 122 123 /** 124 * Gets the value of textColor . 125 * 126 * @return the value of java.awt.Color 127 */ getTextColor()128 public Color getTextColor() { 129 return textColor; 130 } 131 132 /** 133 * repaint. 134 */ repaint()135 public void repaint() { 136 if (root != null) { 137 root.repaint(rect); 138 } 139 } 140 141 /** 142 * x to time. 143 * 144 * @param xValue x 145 * @return long time 146 */ x2ns(final int xValue)147 public long x2ns(final int xValue) { 148 return (xValue - Utils.getX(rect)) * AnalystPanel.getDURATION() / (root.getWidth() - Utils.getX(rect)); 149 } 150 151 /** 152 * time to x. 153 * 154 * @param ns time 155 * @return int x 156 */ ns2x(final long ns)157 public int ns2x(final long ns) { 158 long xValue = ns * (root.getWidth() - Utils.getX(rect)) / AnalystPanel.getDURATION(); 159 return (int) xValue + Utils.getX(rect); 160 } 161 162 /** 163 * edge inspect 164 * 165 * @param event event 166 * @return boolean boolean 167 */ edgeInspect(final MouseEvent event)168 public boolean edgeInspect(final MouseEvent event) { 169 return event.getX() >= Utils.getX(rect) && event.getX() <= Utils.getX(rect) + rect.width 170 && event.getY() >= Utils.getY(rect) && event.getY() <= Utils.getY(rect) + rect.height; 171 } 172 173 /** 174 * edge inspect 175 * 176 * @param rectangle rectangle 177 * @param event event 178 * @return boolean boolean 179 */ edgeInspectRect(final Rectangle rectangle, final MouseEvent event)180 public boolean edgeInspectRect(final Rectangle rectangle, final MouseEvent event) { 181 return event.getX() >= Utils.getX(rectangle) && event.getX() <= Utils.getX(rectangle) + rectangle.width 182 && event.getY() >= Utils.getY(rectangle) && event.getY() <= Utils.getY(rectangle) + rectangle.height; 183 } 184 185 } 186