• 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.layout.utils;
17 
18 import com.intellij.openapi.ui.DialogWrapper;
19 import com.intellij.openapi.ui.ValidationInfo;
20 import com.intellij.ui.ColoredListCellRenderer;
21 import com.intellij.ui.JBColor;
22 import com.intellij.ui.components.JBList;
23 import com.intellij.ui.components.JBPanel;
24 import ohos.devtools.datasources.transport.hdc.HdcWrapper;
25 import ohos.devtools.datasources.utils.common.util.CommonUtil;
26 import ohos.devtools.datasources.utils.device.entity.DeviceProcessInfo;
27 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager;
28 import ohos.devtools.datasources.utils.session.service.SessionManager;
29 import ohos.devtools.services.hiperf.HiperfParse;
30 import ohos.devtools.services.hiperf.ParsePerf;
31 import ohos.devtools.services.hiperf.PerfDAO;
32 import ohos.devtools.views.applicationtrace.AppTracePanel;
33 import ohos.devtools.views.common.LayoutConstants;
34 import ohos.devtools.views.common.UtConstant;
35 import ohos.devtools.views.common.customcomp.CustomJLabel;
36 import ohos.devtools.views.common.customcomp.CustomProgressBar;
37 import ohos.devtools.views.layout.HomePanel;
38 import ohos.devtools.views.layout.SystemPanel;
39 import ohos.devtools.views.layout.TaskPanel;
40 import ohos.devtools.views.layout.chartview.TaskScenePanelChart;
41 import ohos.devtools.views.layout.dialog.ImportFileChooserDialog;
42 import ohos.devtools.views.layout.dialog.SampleDialog;
43 import ohos.devtools.views.perftrace.PerfTracePanel;
44 import ohos.devtools.views.trace.component.SysAnalystPanel;
45 import org.apache.logging.log4j.LogManager;
46 import org.apache.logging.log4j.Logger;
47 import org.jetbrains.annotations.NotNull;
48 import org.jetbrains.annotations.Nullable;
49 
50 import javax.swing.DefaultListModel;
51 import javax.swing.JComponent;
52 import javax.swing.JList;
53 import javax.swing.SwingWorker;
54 import java.awt.BorderLayout;
55 import java.awt.Component;
56 import java.awt.Dimension;
57 import java.awt.Rectangle;
58 import java.awt.event.ComponentAdapter;
59 import java.awt.event.ComponentEvent;
60 import java.io.BufferedReader;
61 import java.io.File;
62 import java.io.FileInputStream;
63 import java.io.FileNotFoundException;
64 import java.io.IOException;
65 import java.io.InputStreamReader;
66 import java.io.RandomAccessFile;
67 import java.io.UnsupportedEncodingException;
68 import java.text.SimpleDateFormat;
69 import java.util.ArrayList;
70 import java.util.Date;
71 import java.util.List;
72 import java.util.Optional;
73 import java.util.concurrent.ExecutionException;
74 
75 import static ohos.devtools.datasources.transport.hdc.HdcCmdList.TRACE_STREAMER_LOAD;
76 import static ohos.devtools.datasources.transport.hdc.HdcWrapper.conversionCommand;
77 import static ohos.devtools.datasources.utils.common.Constant.DEVICE_STAT_OFFLINE;
78 
79 /**
80  * OpenFileDialogUtil
81  *
82  * @since : 2021/10/25
83  */
84 public class OpenFileDialogUtils {
85     private static OpenFileDialogUtils instance = new OpenFileDialogUtils();
86     private static TaskPanel taskPanel;
87     private static CustomProgressBar progressBar;
88     private static final Logger LOGGER = LogManager.getLogger(OpenFileDialogUtils.class);
89     private static final String HI_PERF_TYPE_VALUE = "HIPERF";
90     private static final String BYTRACE_TYPE_VALUE = "TRACE";
91     private static final String BYTRACE_TYPE_FLAG_VALUE = "tracer";
92     private static final String HTRACE_TYPE_VALUE = "OHOSPROF";
93     private static final String TRACE_FILE = "TraceFileInfo";
94 
95     private Boolean traceAnalysisResult = true;
96     private String path = "";
97 
OpenFileDialogUtils()98     private OpenFileDialogUtils() {
99     }
100 
101     /**
102      * Get the current OpenFileDialogUtils object
103      *
104      * @return Db
105      */
getInstance()106     public static OpenFileDialogUtils getInstance() {
107         if (instance == null) {
108             instance = new OpenFileDialogUtils();
109         }
110         return instance;
111     }
112 
113     /**
114      * showFileOpenDialog
115      *
116      * @param tabItem tabItem
117      * @param contentPanel contentPanel
118      */
showFileOpenDialog(JBPanel tabItem, TaskPanel contentPanel)119     public void showFileOpenDialog(JBPanel tabItem, TaskPanel contentPanel) {
120         ImportFileChooserDialog fileChooserDialogWrapper = new ImportFileChooserDialog("Open File");
121         boolean showAndGet = fileChooserDialogWrapper.showAndGet();
122         if (showAndGet) {
123             taskPanel = contentPanel;
124             progressBar = new CustomProgressBar(tabItem);
125             tabItem.remove(taskPanel.getBtnPanel());
126             // JProgressBar Listener
127             tabItem.addComponentListener(new ComponentAdapter() {
128                 @Override
129                 public void componentResized(ComponentEvent event) {
130                     super.componentResized(event);
131                     progressBar.setBounds(LayoutConstants.TEN, tabItem.getHeight() - LayoutConstants.FORTY,
132                         tabItem.getWidth() - LayoutConstants.TWENTY, LayoutConstants.THIRTY);
133                 }
134             });
135             BufferedReader bufferedReader = null;
136             try {
137                 String fileName = fileChooserDialogWrapper.getImportFilePath();
138                 bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "GBK"));
139                 String readLineStr = null;
140                 while ((readLineStr = bufferedReader.readLine()) != null) {
141                     break;
142                 }
143                 File selectedFile = new File(fileName);
144                 if (readLineStr == null) {
145                     showWarningDialog(tabItem);
146                     return;
147                 }
148                 loadOfflineFiles(tabItem, readLineStr, selectedFile);
149             } catch (UnsupportedEncodingException unsupportedEncodingException) {
150                 LOGGER.error(unsupportedEncodingException.getMessage());
151             } catch (FileNotFoundException fileNotFoundException) {
152                 LOGGER.error(fileNotFoundException.getMessage());
153             } catch (IOException ioException) {
154                 LOGGER.error(ioException.getMessage());
155             } finally {
156                 if (bufferedReader != null) {
157                     try {
158                         bufferedReader.close();
159                     } catch (IOException ioException) {
160                         LOGGER.error(ioException.getMessage());
161                     }
162                 }
163             }
164         } else {
165             tabItem.requestFocus(false);
166         }
167     }
168 
169     /**
170      * load offline files
171      *
172      * @param tabItem tabItem
173      * @param readLineStr readLineStr
174      * @param selectedFile selectedFile
175      */
loadOfflineFiles(JBPanel tabItem, String readLineStr, File selectedFile)176     private void loadOfflineFiles(JBPanel tabItem, String readLineStr, File selectedFile) {
177         if (readLineStr.contains(HI_PERF_TYPE_VALUE)) {
178             if (HomePanel.isTaskIsOpen()) {
179                 TaskScenePanelChart taskScenePanelChart = getTaskScenePanelChart();
180                 CustomJLabel hosJLabel = new CustomJLabel();
181                 hosJLabel.setProcessName(getTime() + ";" + selectedFile.getName() + ";" + "Perf");
182                 long sessionId = CommonUtil.getLocalSessionId();
183                 hosJLabel.setSessionId(sessionId);
184                 hosJLabel.setConnectType(DEVICE_STAT_OFFLINE);
185                 taskScenePanelChart.createImportFileSessionList(readLineStr, hosJLabel, selectedFile);
186             } else {
187                 loadPerf(selectedFile, taskPanel.getTabContainer(), readLineStr);
188             }
189         } else if (readLineStr.contains(BYTRACE_TYPE_VALUE) || readLineStr.contains(HTRACE_TYPE_VALUE) || readLineStr
190             .contains(BYTRACE_TYPE_FLAG_VALUE)) {
191             loadOfflineFile(tabItem, taskPanel, selectedFile, taskPanel.getTabContainer());
192         } else if (readLineStr.contains(TRACE_FILE)) {
193             if (HomePanel.isTaskIsOpen()) {
194                 TaskScenePanelChart taskScenePanelChart = getTaskScenePanelChart();
195                 CustomJLabel hosJLabel = new CustomJLabel();
196                 hosJLabel.setProcessName(getTime() + ";" + selectedFile.getName() + ";" + "trace");
197                 long sessionId = CommonUtil.getLocalSessionId();
198                 hosJLabel.setSessionId(sessionId);
199                 hosJLabel.setConnectType(DEVICE_STAT_OFFLINE);
200                 taskScenePanelChart.createImportFileSessionList(UtConstant.FILE_TYPE_TRACE, hosJLabel, selectedFile);
201             } else {
202                 loadOfflineFile(tabItem, taskPanel, selectedFile, taskPanel.getTabContainer());
203             }
204         } else if (readLineStr.contains("malloc") || readLineStr.contains("free")) {
205             loadOfflineFileNativeHook(selectedFile, taskPanel, taskPanel.getTabContainer());
206         } else {
207             showWarningDialog(tabItem);
208         }
209     }
210 
211     /**
212      * loadOfflineFileHprof
213      *
214      * @param selectedFile selectedFile
215      * @param taskPanel taskPanel
216      * @param optionJPanel optionJPanel
217      */
loadOfflineFileHprof(File selectedFile, TaskPanel taskPanel, JBPanel optionJPanel)218     public void loadOfflineFileHprof(File selectedFile, TaskPanel taskPanel, JBPanel optionJPanel) {
219         SwingWorker<String, Object> task = new SwingWorker<>() {
220             TaskScenePanelChart chart = null;
221 
222             /**
223              * doInBackground
224              *
225              * @return String
226              */
227             @Override
228             protected String doInBackground() {
229                 if (HomePanel.isTaskIsOpen()) {
230                     chart = getTaskScenePanelChart();
231                 } else {
232                     progressBar.setValue(LayoutConstants.DEFAULT_NUMBER);
233                     Dimension dimension = progressBar.getSize();
234                     Rectangle rect = new Rectangle(0, 0, dimension.width, dimension.height);
235                     progressBar.setValue(LayoutConstants.HUNDRED);
236                     progressBar.paintImmediately(rect);
237                 }
238                 return getTime() + ";" + selectedFile.getName() + ";" + "Heap Dump";
239             }
240 
241             /**
242              * done
243              */
244             @Override
245             protected void done() {
246                 try {
247                     long sessionId = CommonUtil.getLocalSessionId();
248                     String fileName = get();
249                     if (fileName != null) {
250                         CustomJLabel hosJLabel = new CustomJLabel();
251                         hosJLabel.setProcessName(fileName);
252                         hosJLabel.setDeviceName("");
253                         hosJLabel.setOnline(false);
254                         hosJLabel.setConnectType(DEVICE_STAT_OFFLINE);
255                         hosJLabel.setSessionId(sessionId);
256                         hosJLabel.setFileType(UtConstant.FILE_TYPE_HPROF);
257                         ArrayList<CustomJLabel> hosJLabels = new ArrayList<CustomJLabel>();
258                         hosJLabels.add(hosJLabel);
259                         if (HomePanel.isTaskIsOpen()) {
260                             // filter TaskScenePanelChart
261                             chart.createImportFileSessionList(LayoutConstants.HEAP_DUMP, hosJLabel, selectedFile);
262                         } else {
263                             optionJPanel.removeAll();
264                             TaskScenePanelChart taskScenePanelChart = new TaskScenePanelChart(taskPanel, hosJLabels);
265                             taskPanel.getTabContainer().setBackground(JBColor.background());
266                             taskPanel.setLocalSessionId(sessionId);
267                             optionJPanel.add(taskScenePanelChart);
268                         }
269                     }
270                 } catch (InterruptedException | ExecutionException exception) {
271                     exception.printStackTrace();
272                 }
273             }
274         };
275         task.execute();
276     }
277 
getTime()278     private String getTime() {
279         SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
280         Date currentTime = new Date();
281         return formatter.format(currentTime);
282     }
283 
284     /**
285      * loadOfflineFileNativeHook
286      *
287      * @param selectedFile selectedFile
288      * @param taskPanel taskPanel
289      * @param optionJPanel optionJPanel
290      */
loadOfflineFileNativeHook(File selectedFile, TaskPanel taskPanel, JBPanel optionJPanel)291     public void loadOfflineFileNativeHook(File selectedFile, TaskPanel taskPanel, JBPanel optionJPanel) {
292         String absolutePath = null;
293         try {
294             absolutePath = selectedFile.getCanonicalPath();
295         } catch (IOException ioException) {
296             if (ProfilerLogManager.isErrorEnabled()) {
297                 LOGGER.error("load NativeHook file failed {}", ioException.getMessage());
298             }
299         }
300         long sessionId = CommonUtil.getLocalSessionId();
301         CustomJLabel hosJLabel = new CustomJLabel();
302         hosJLabel.setProcessName(getTime() + ";" + selectedFile.getName() + ";" + "NativeHeap");
303         hosJLabel.setDeviceName("");
304         hosJLabel.setOnline(false);
305         hosJLabel.setConnectType(DEVICE_STAT_OFFLINE);
306         hosJLabel.setSessionId(sessionId);
307         hosJLabel.setFileType("nativeHeap");
308         hosJLabel.setMessage(absolutePath);
309         ArrayList<CustomJLabel> hosJLabels = new ArrayList<CustomJLabel>();
310         hosJLabels.add(hosJLabel);
311         if (HomePanel.isTaskIsOpen()) {
312             // filter TaskScenePanelChart
313             TaskScenePanelChart chart = getTaskScenePanelChart();
314             chart.createImportFileSessionList("NativeHeap", hosJLabel, selectedFile);
315         } else {
316             optionJPanel.removeAll();
317             TaskScenePanelChart taskScenePanelChart = new TaskScenePanelChart(taskPanel, hosJLabels);
318             taskPanel.getTabContainer().setBackground(JBColor.background());
319             taskPanel.setLocalSessionId(sessionId);
320             optionJPanel.add(taskScenePanelChart);
321         }
322     }
323 
getTaskScenePanelChart()324     private TaskScenePanelChart getTaskScenePanelChart() {
325         TaskScenePanelChart chart = null;
326         Component[] innerComponents = taskPanel.getTabContainer().getComponents();
327         for (Component component : innerComponents) {
328             // filter TaskScenePanelChart
329             if (component instanceof TaskScenePanelChart) {
330                 chart = (TaskScenePanelChart) component;
331             }
332         }
333         return chart;
334     }
335 
336     /**
337      * loadOfflineFile
338      *
339      * @param optionJPanelContent optionJPanelContent
340      * @param taskPanel taskPanel
341      * @param selectedFile selectedFile
342      * @param optionJPanel optionJPanel
343      */
loadOfflineFile(JBPanel optionJPanelContent, TaskPanel taskPanel, File selectedFile, JBPanel optionJPanel)344     private void loadOfflineFile(JBPanel optionJPanelContent, TaskPanel taskPanel, File selectedFile,
345         JBPanel optionJPanel) {
346         SwingWorker<Optional<DeviceProcessInfo>, Object> task = new SwingWorker<Optional<DeviceProcessInfo>, Object>() {
347             /**
348              * doInBackground
349              *
350              * @return Optional<DeviceProcessInfo>
351              */
352             @Override
353             protected Optional<DeviceProcessInfo> doInBackground() {
354                 path = selectedFile.getPath();
355                 return SessionManager.getInstance().localSessionDataFromFile(progressBar, selectedFile);
356             }
357 
358             /**
359              * done
360              */
361             @Override
362             protected void done() {
363                 try {
364                     Optional<DeviceProcessInfo> deviceProcessInfo = get();
365                     if (deviceProcessInfo.isPresent()) {
366                         DeviceProcessInfo deviceInfo = deviceProcessInfo.get();
367                         CustomJLabel hosJLabel = getCustomJLabel(deviceInfo, selectedFile);
368                         List<CustomJLabel> hosJLabels = new ArrayList<CustomJLabel>();
369                         hosJLabels.add(hosJLabel);
370                         if (HomePanel.isTaskIsOpen()) {
371                             TaskScenePanelChart taskScenePanelChart = getTaskScenePanelChart();
372                             taskScenePanelChart
373                                 .createImportFileSessionList(UtConstant.FILE_TYPE_TRACE, hosJLabel, null);
374                         } else {
375                             optionJPanel.removeAll();
376                             TaskScenePanelChart taskScenePanelChart = new TaskScenePanelChart(taskPanel, hosJLabels);
377                             taskPanel.getTabContainer().setBackground(JBColor.background());
378                             taskPanel.setLocalSessionId(deviceInfo.getLocalSessionId());
379                             optionJPanel.add(taskScenePanelChart);
380                         }
381                     } else {
382                         ChooseTraceTypeDialogWrapper chooseTraceTypeDialogWrapper =
383                             new ChooseTraceTypeDialogWrapper(optionJPanelContent, selectedFile, optionJPanel);
384                         boolean result = chooseTraceTypeDialogWrapper.showAndGet();
385                         if (!result) {
386                             optionJPanelContent.remove(progressBar);
387                             optionJPanelContent.add(taskPanel.getBtnPanel());
388                             optionJPanelContent.repaint();
389                         }
390                     }
391                 } catch (InterruptedException | ExecutionException exception) {
392                     exception.printStackTrace();
393                 }
394             }
395         };
396         task.execute();
397     }
398 
399     @NotNull
getCustomJLabel(DeviceProcessInfo deviceInfo, File selectedFile)400     private CustomJLabel getCustomJLabel(DeviceProcessInfo deviceInfo, File selectedFile) {
401         CustomJLabel hosJLabel = new CustomJLabel();
402         hosJLabel.setProcessName(deviceInfo.getProcessName() + ";" + selectedFile.getName());
403         hosJLabel.setSessionId(deviceInfo.getLocalSessionId());
404         hosJLabel.setDeviceName(deviceInfo.getDeviceName());
405         hosJLabel.setOnline(false);
406         hosJLabel.setFileType(UtConstant.FILE_TYPE_TRACE);
407         hosJLabel.setStartTime(deviceInfo.getStartTime());
408         hosJLabel.setEndTime(deviceInfo.getEndTime());
409         return hosJLabel;
410     }
411 
412     /**
413      * loadHiPerf
414      *
415      * @param selectedFile selectedFile
416      * @param optionJPanel optionJPanel
417      * @param readLineStr readLineStr
418      */
loadPerf(File selectedFile, JBPanel optionJPanel, String readLineStr)419     public void loadPerf(File selectedFile, JBPanel optionJPanel, String readLineStr) {
420         SwingWorker<String, Object> task = new SwingWorker<>() {
421             @Override
422             protected String doInBackground() {
423                 progressBar.setValue(30);
424                 ParsePerf traceParser = new HiperfParse();
425                 try {
426                     traceParser.parseFile(selectedFile);
427                     PerfDAO.getInstance().createTable(null);
428                     traceParser.insertSample();
429                 } catch (IOException exception) {
430                     exception.printStackTrace();
431                 }
432                 progressBar.setValue(LayoutConstants.HUNDRED);
433                 return SessionManager.getInstance().tempPath() + "perf.db";
434             }
435 
436             @Override
437             protected void done() {
438                 try {
439                     String dbPath = get();
440                     if (dbPath != null) {
441                         optionJPanel.removeAll();
442                         long sessionId = CommonUtil.getLocalSessionId();
443                         CustomJLabel hosJLabel = new CustomJLabel();
444                         hosJLabel.setProcessName(getTime() + ";" + selectedFile.getName() + ";" + "Perf");
445                         hosJLabel.setDeviceName("");
446                         hosJLabel.setOnline(false);
447                         hosJLabel.setConnectType(DEVICE_STAT_OFFLINE);
448                         hosJLabel.setSessionId(sessionId);
449                         hosJLabel.setFileType(BYTRACE_TYPE_VALUE);
450                         ArrayList<CustomJLabel> hosJLabels = new ArrayList<CustomJLabel>();
451                         String cardName = "Trace" + sessionId;
452                         hosJLabel.setCardName(cardName);
453                         hosJLabels.add(hosJLabel);
454                         TaskScenePanelChart taskScenePanelChart = new TaskScenePanelChart(taskPanel, hosJLabels);
455                         taskPanel.getTabContainer().setBackground(JBColor.background());
456                         taskPanel.setLocalSessionId(sessionId);
457                         optionJPanel.add(taskScenePanelChart);
458                         taskPanel.getTabContainer().setBackground(JBColor.background());
459                         PerfTracePanel component = new PerfTracePanel();
460                         component.load(dbPath, null, true);
461                         progressBar.setValue(LayoutConstants.HUNDRED);
462                         // optionJPanel.add(component);
463                         taskScenePanelChart.getCards().add(component, cardName);
464                         taskScenePanelChart.getCardLayout().show(taskScenePanelChart.getCards(), cardName);
465                     }
466                 } catch (InterruptedException | ExecutionException exception) {
467                     exception.printStackTrace();
468                 }
469             }
470         };
471         task.execute();
472     }
473 
474     /**
475      * loadTrace
476      *
477      * @param optionJPanelContent optionJPanelContent
478      * @param selectedFile selectedFile
479      * @param optionJPanel optionJPanel
480      * @param isAppTrace isAppTrace
481      */
loadTrace(JBPanel optionJPanelContent, File selectedFile, JBPanel optionJPanel, boolean isAppTrace)482     public void loadTrace(JBPanel optionJPanelContent, File selectedFile, JBPanel optionJPanel, boolean isAppTrace) {
483         SwingWorker<String, Object> task = new SwingWorker<String, Object>() {
484             @Override
485             protected String doInBackground() throws Exception {
486                 traceAnalysisResult = true;
487                 String logPath = TraceStreamerUtils.getInstance().getLogPath("trace_streamer.db");
488                 File logFile = new File(logPath);
489                 if (logFile.exists()) {
490                     logFile.delete();
491                 }
492                 String baseDir = TraceStreamerUtils.getInstance().getBaseDir();
493                 String dbPath = TraceStreamerUtils.getInstance().getDbPath();
494                 HdcWrapper.getInstance().getHdcStringResult(conversionCommand(TRACE_STREAMER_LOAD,
495                     baseDir + TraceStreamerUtils.getInstance().getTraceStreamerApp(), selectedFile.getPath(), dbPath));
496                 randomFile(logFile);
497                 progressBar.setValue(LayoutConstants.FIFTY);
498                 return dbPath;
499             }
500 
501             /**
502              * done
503              */
504             @Override
505             protected void done() {
506                 if (!traceAnalysisResult) {
507                     if (progressBar != null) {
508                         optionJPanelContent.remove(progressBar);
509                     }
510                     if (taskPanel != null) {
511                         optionJPanelContent.add(taskPanel.getBtnPanel());
512                     }
513                     optionJPanelContent.repaint();
514                     new SampleDialog("Warring",
515                         "The system cannot parse the file properly. Please import the legal file.").show();
516                 }
517                 try {
518                     if (traceAnalysisResult) {
519                         String dbPath = get();
520                         addOptionJPanel(dbPath, optionJPanel, isAppTrace, selectedFile);
521                     }
522                 } catch (InterruptedException interruptedException) {
523                     LOGGER.error(interruptedException.getMessage());
524                 } catch (ExecutionException executionException) {
525                     LOGGER.error(executionException.getMessage());
526                 }
527             }
528         };
529         task.execute();
530     }
531 
532     /**
533      * addOptionJPanel
534      *
535      * @param dbPath dbPath
536      * @param optionJPanel optionJPanel
537      * @param isAppTrace isAppTrace
538      * @param selectedFile selectedFile
539      */
addOptionJPanel(String dbPath, JBPanel optionJPanel, boolean isAppTrace, File selectedFile)540     private void addOptionJPanel(String dbPath, JBPanel optionJPanel, boolean isAppTrace, File selectedFile) {
541         if (!HomePanel.isTaskIsOpen()) {
542             optionJPanel.removeAll();
543         }
544         long sessionId = CommonUtil.getLocalSessionId();
545         CustomJLabel hosJLabel = new CustomJLabel();
546         hosJLabel.setProcessName(getTime() + ";" + selectedFile.getName() + ";" + BYTRACE_TYPE_VALUE);
547         hosJLabel.setDeviceName("");
548         hosJLabel.setOnline(false);
549         hosJLabel.setConnectType(DEVICE_STAT_OFFLINE);
550         hosJLabel.setSessionId(sessionId);
551         hosJLabel.setFileType(BYTRACE_TYPE_VALUE);
552         ArrayList<CustomJLabel> hosJLabels = new ArrayList<CustomJLabel>();
553         String cardName = "Trace" + sessionId;
554         hosJLabel.setCardName(cardName);
555         hosJLabels.add(hosJLabel);
556         TaskScenePanelChart taskScenePanelChart = new TaskScenePanelChart(taskPanel, hosJLabels);
557         taskPanel.getTabContainer().setBackground(JBColor.background());
558         taskPanel.setLocalSessionId(sessionId);
559         optionJPanel.add(taskScenePanelChart);
560         if (isAppTrace) {
561             AppTracePanel component = new AppTracePanel();
562             component.load(dbPath, null, null, true);
563             // taskPanel.getTabContainer().setBackground(JBColor.background());
564             progressBar.setValue(LayoutConstants.HUNDRED);
565             // optionJPanel.add(component);
566             taskScenePanelChart.getCards().add(component, cardName);
567         } else {
568             SysAnalystPanel component = new SysAnalystPanel();
569             component.load(dbPath, true);
570             // taskPanel.getTabContainer().setBackground(JBColor.background());
571             progressBar.setValue(LayoutConstants.HUNDRED);
572             JBPanel tabContainer = new JBPanel(new BorderLayout());
573             SystemPanel systemTuningPanel = new SystemPanel(tabContainer, component);
574             tabContainer.add(systemTuningPanel, BorderLayout.NORTH);
575             tabContainer.add(component, BorderLayout.CENTER);
576             component.getAnalystPanel()
577                 .setPreferredSize(new Dimension(optionJPanel.getWidth() - 20, optionJPanel.getHeight()));
578             taskScenePanelChart.getCards().add(tabContainer, cardName);
579         }
580         taskScenePanelChart.getCardLayout().show(taskScenePanelChart.getCards(), cardName);
581     }
582 
583     /**
584      * random File
585      *
586      * @param logFile log File
587      * @throws IOException IOException
588      */
randomFile(File logFile)589     private void randomFile(File logFile) throws IOException {
590         RandomAccessFile randomFile = null;
591         try {
592             if (logFile.exists()) {
593                 randomFile = new RandomAccessFile(logFile, "r");
594                 String tmp = null;
595                 while ((tmp = randomFile.readLine()) != null) {
596                     if (tmp.startsWith("last")) {
597                         continue;
598                     }
599                     if (Integer.valueOf(tmp.split(":")[1]) != 0) {
600                         traceAnalysisResult = false;
601                     }
602                 }
603             }
604         } catch (FileNotFoundException fileNotFoundException) {
605             LOGGER.error("randomFile exception:{}", fileNotFoundException.getMessage());
606         } catch (IOException iOException) {
607             LOGGER.error("randomFile exception:{}", iOException.getMessage());
608         } finally {
609             if (randomFile != null) {
610                 randomFile.close();
611             }
612         }
613     }
614 
showWarningDialog(JBPanel tabItem)615     private void showWarningDialog(JBPanel tabItem) {
616         tabItem.remove(progressBar);
617         tabItem.add(taskPanel.getBtnPanel());
618         tabItem.repaint();
619         new SampleDialog("prompt", "Please select the right file!").show();
620     }
621 
622     /**
623      * ChooseTraceTypeDialogWrapper
624      */
625     private class ChooseTraceTypeDialogWrapper extends DialogWrapper {
626         private static final String DIALOG_TITLE = "Please Choose the type";
627         private static final String SYSTEM_TYPE = "1.System Trace";
628         private static final String APPLICATION_TYPE = "2.Application Trace";
629 
630         private JBPanel optionJPanelContent;
631         private JBPanel optionJPanel;
632         private File selectedFile;
633         private JBList<String> typeList;
634         private DefaultListModel<String> listModel;
635 
636         /**
637          * ChooseTraceTypeDialogWrapper
638          *
639          * @param optionJPanelContent optionJPanelContent
640          * @param selectedFile selectedFile
641          * @param optionJPanel optionJPanel
642          */
ChooseTraceTypeDialogWrapper(JBPanel optionJPanelContent, File selectedFile, JBPanel optionJPanel)643         ChooseTraceTypeDialogWrapper(JBPanel optionJPanelContent, File selectedFile, JBPanel optionJPanel) {
644             super(true);
645             this.optionJPanelContent = optionJPanelContent;
646             this.selectedFile = selectedFile;
647             this.optionJPanel = optionJPanel;
648             init();
649             setTitle(DIALOG_TITLE);
650         }
651 
652         @Nullable
653         @Override
createCenterPanel()654         protected JComponent createCenterPanel() {
655             listModel = new DefaultListModel<>();
656             listModel.add(0, SYSTEM_TYPE);
657             listModel.add(1, APPLICATION_TYPE);
658             typeList = new JBList<>(listModel);
659             ColoredListCellRenderer<String> listCellRenderer = new ColoredListCellRenderer<String>() {
660                 @Override
661                 protected void customizeCellRenderer(@NotNull JList<? extends String> jList, String value, int index,
662                     boolean selected, boolean hasFocus) {
663                     append(value);
664                 }
665             };
666             typeList.setCellRenderer(listCellRenderer);
667             typeList.setSelectedIndex(0);
668             return typeList;
669         }
670 
671         @Nullable
672         @Override
doValidate()673         protected ValidationInfo doValidate() {
674             String value = typeList.getSelectedValue();
675             if (HomePanel.isTaskIsOpen()) {
676                 TaskScenePanelChart taskScenePanelChart = getTaskScenePanelChart();
677                 CustomJLabel hosJLabel = new CustomJLabel();
678                 hosJLabel.setProcessName(getTime() + ";" + selectedFile.getName() + ";" + BYTRACE_TYPE_VALUE);
679                 long sessionId = CommonUtil.getLocalSessionId();
680                 hosJLabel.setSessionId(sessionId);
681                 hosJLabel.setConnectType(DEVICE_STAT_OFFLINE);
682                 hosJLabel.setMessage(value);
683                 taskScenePanelChart.createImportFileSessionList(BYTRACE_TYPE_VALUE, hosJLabel, selectedFile);
684             } else {
685                 if (value != null) {
686                     if (value.equals(SYSTEM_TYPE)) {
687                         loadTrace(optionJPanelContent, selectedFile, optionJPanel, false);
688                     }
689                     if (value.equals(APPLICATION_TYPE)) {
690                         loadTrace(optionJPanelContent, selectedFile, optionJPanel, true);
691                     }
692                 }
693             }
694             return null;
695         }
696     }
697 }
698