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 ohos.devtools.views.trace.util.TimeUtils; 21 22 import java.awt.Graphics; 23 import java.awt.Graphics2D; 24 import java.awt.RenderingHints; 25 26 /** 27 * PrefSample from db file 28 * 29 * @since 2021/5/12 16:34 30 */ 31 public class Ruler extends JBPanel { 32 private long duration; 33 private int degreeCount = 55; 34 35 /** 36 * Ruler constructor 37 * 38 * @param duration duration 39 */ Ruler(long duration)40 public Ruler(long duration) { 41 this.duration = duration; 42 } 43 getDegreeText(long ns)44 private String[] getDegreeText(long ns) { 45 String[] dtArr = new String[6]; 46 dtArr[0] = "0"; 47 for (int index = 1; index < 6; index++) { 48 dtArr[index] = TimeUtils.getSecondFromNSecond(index * 10 * ns); 49 } 50 return dtArr; 51 } 52 53 /** 54 * refresh time ruler 55 * 56 * @param duration time 57 */ refreshTimeRuler(long duration)58 public void refreshTimeRuler(long duration) { 59 this.duration = duration; 60 repaint(); 61 } 62 63 @Override paint(final Graphics graphics)64 public void paint(final Graphics graphics) { 65 int width = getWidth(); 66 int height = getHeight(); 67 int range = width / degreeCount; 68 String[] dtArr = getDegreeText((duration / width) * range); 69 if (graphics instanceof Graphics2D) { 70 Graphics2D g2d = (Graphics2D) graphics; 71 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 72 g2d.setFont(getFont().deriveFont(11.0f)); 73 int xx = 0; 74 g2d.setColor(JBColor.background().darker()); 75 g2d.drawLine(xx, 0, xx, height); 76 g2d.setColor(getForeground()); 77 g2d.drawString(dtArr[0], xx + 3, 15); 78 int model = width % degreeCount; 79 for (int index = 1; index <= degreeCount; index++) { 80 xx = index < model ? xx + range + 1 : xx + range; 81 if ((index % 10) == 0) { 82 g2d.setColor(JBColor.background().darker()); 83 g2d.drawLine(xx, 0, xx, height); 84 g2d.setColor(getForeground()); 85 g2d.drawString(dtArr[index / 10], xx + 3, 15); 86 } else { 87 if (xx <= width) { 88 g2d.setColor(JBColor.background().darker()); 89 g2d.drawLine(xx, 0, xx, 5); 90 } 91 } 92 } 93 g2d.setColor(JBColor.background().darker()); 94 g2d.drawLine(0, 0, getWidth(), 0); 95 } 96 } 97 } 98