• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.TimeUtils;
20 import ohos.devtools.views.trace.util.Utils;
21 
22 import javax.swing.JComponent;
23 import java.awt.AlphaComposite;
24 import java.awt.Graphics2D;
25 import java.awt.geom.Rectangle2D;
26 
27 /**
28  * The left part of the timeline display area
29  *
30  * @since 2021/04/22 12:25
31  */
32 public class LeftFragment extends AbstractFragment {
33     private String startTimeS = "0.0s";
34     private long startNS;
35     private int extendHeight;
36     private String logString;
37 
38     /**
39      * LeftFragment
40      *
41      * @param root Component
42      */
LeftFragment(final JComponent root)43     public LeftFragment(final JComponent root) {
44         this.setRoot(root);
45         final int width = 200;
46         final int height = 134;
47         getRect().setBounds(0, 0, width, height);
48     }
49 
50     /**
51      * Gets the value of startTimeS .
52      *
53      * @return the value of java.lang.String
54      */
getStartTimeS()55     public String getStartTimeS() {
56         return startTimeS;
57     }
58 
59     /**
60      * Sets the startTimeS .
61      * <p>You can use getStartTimeS() to get the value of startTimeS</p>
62      *
63      * @param time time
64      */
setStartTimeS(final String time)65     public void setStartTimeS(final String time) {
66         this.startTimeS = time;
67     }
68 
69     /**
70      * Gets the value of extendHeight .
71      *
72      * @return the value of int
73      */
getExtendHeight()74     public int getExtendHeight() {
75         return extendHeight;
76     }
77 
78     /**
79      * Sets the extendHeight .
80      * <p>You can use getExtendHeight() to get the value of extendHeight</p>
81      *
82      * @param height height
83      */
setExtendHeight(final int height)84     public void setExtendHeight(final int height) {
85         this.extendHeight = height;
86     }
87 
88     /**
89      * Gets the value of logString .
90      *
91      * @return the value of java.lang.String
92      */
getLogString()93     public String getLogString() {
94         return logString;
95     }
96 
97     /**
98      * Sets the logString .
99      * <p>You can use getLogString() to get the value of logString</p>
100      *
101      * @param log log
102      */
setLogString(final String log)103     public void setLogString(final String log) {
104         this.logString = log;
105     }
106 
107     /**
108      * Drawing method
109      *
110      * @param graphics graphics
111      */
112     @Override
draw(final Graphics2D graphics)113     public void draw(final Graphics2D graphics) {
114         final int yaxis = 94;
115         final float alpha50 = 0.5f;
116         graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha50));
117         graphics.setColor(getRoot().getForeground());
118         graphics.drawLine(0, yaxis, getRect().width, yaxis);
119         graphics.drawLine(getRect().width, 0, getRect().width, getRect().height + extendHeight);
120         final int pad = 4;
121         graphics.drawLine(0, getRect().height, getRoot().getWidth(), getRect().height);
122         graphics.drawLine(0, 0, 0, getRect().height + extendHeight);
123         graphics.drawRect(Utils.getX(getRect()), Utils.getY(getRect()), getRect().width, getRect().height);
124         graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
125         Rectangle2D rectangle2D = graphics.getFontMetrics().getStringBounds(startTimeS, graphics);
126         int strHeight = (int) rectangle2D.getHeight();
127         int strWidth = (int) rectangle2D.getWidth() + pad;
128         graphics.drawString(TimeUtils.getSecondFromNSecond(AnalystPanel.getDURATION()), 2, yaxis + strHeight);
129         graphics.drawString(startTimeS, getRect().width - strWidth, yaxis + strHeight);
130         if (logString != null && !logString.isEmpty()) {
131             int index = 0;
132             for (String str : logString.split(System.getProperty("line.separator"))) {
133                 graphics.drawString(str, 1, index * 12 + 10);
134                 index++;
135             }
136         }
137     }
138 
139     /**
140      * set Start Time
141      *
142      * @param leftNS leftNS
143      */
setStartTime(final long leftNS)144     public void setStartTime(final long leftNS) {
145         this.startNS = leftNS;
146         startTimeS = TimeUtils.getSecondFromNSecond(leftNS);
147         repaint();
148     }
149 
150     /**
151      * get Start Time
152      *
153      * @return long Starting time
154      */
getStartNS()155     public long getStartNS() {
156         return this.startNS;
157     }
158 
159     /**
160      * Sets the startNS .
161      * <p>You can use getStartNS() to get the value of startNS</p>
162      *
163      * @param ns Starting time
164      */
setStartNS(final long ns)165     public void setStartNS(final long ns) {
166         this.startNS = ns;
167     }
168 }
169