• 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.dialog;
17 
18 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
19 import com.intellij.openapi.ui.DialogWrapper;
20 import com.intellij.openapi.ui.TextBrowseFolderListener;
21 import com.intellij.openapi.ui.TextFieldWithBrowseButton;
22 import com.intellij.openapi.ui.ValidationInfo;
23 import com.intellij.openapi.vfs.LocalFileSystem;
24 import com.intellij.openapi.vfs.VirtualFile;
25 import com.intellij.ui.components.JBPanel;
26 import ohos.devtools.datasources.utils.device.entity.DeviceIPPortInfo;
27 import ohos.devtools.datasources.utils.device.entity.DeviceProcessInfo;
28 import ohos.devtools.datasources.utils.session.service.SessionManager;
29 import ohos.devtools.views.common.Constant;
30 import ohos.devtools.views.common.LayoutConstants;
31 import ohos.devtools.views.common.UtConstant;
32 import ohos.devtools.views.common.customcomp.CustomJButton;
33 import org.apache.commons.lang3.StringUtils;
34 import org.jetbrains.annotations.Nullable;
35 
36 import javax.swing.JComboBox;
37 import javax.swing.JComponent;
38 import javax.swing.JLabel;
39 import javax.swing.JTextField;
40 import java.awt.Dimension;
41 import java.io.File;
42 
43 /**
44  * ExportFileChooserDialog
45  *
46  * @since 2021/10/25
47  */
48 public class ExportFileChooserDialog extends DialogWrapper {
49     private TextFieldWithBrowseButton textFieldWithBrowseButton;
50 
51     private JTextField jNameTextField;
52 
53     private String exportFileName;
54 
55     private String exportFilePath;
56 
57     private String fileType;
58 
59     private JComboBox<String> fileTypeBox = new JComboBox<String>();
60 
61     /**
62      * constructor
63      *
64      * @param title title
65      * @param fileType fileType
66      */
ExportFileChooserDialog(String title, String fileType)67     public ExportFileChooserDialog(String title, String fileType) {
68         super(true);
69         init();
70         setTitle(title);
71         this.fileType = fileType;
72         fileTypeBox.addItem(fileType);
73     }
74 
75     @Nullable
76     @Override
createCenterPanel()77     protected JComponent createCenterPanel() {
78         JBPanel panel = new JBPanel(null);
79         JLabel taskNameLabel = new JLabel("File Name");
80         taskNameLabel.setBounds(LayoutConstants.MARGIN_LEFT, LayoutConstants.TWENTY, LayoutConstants.HUNDRED_FIFTY,
81             LayoutConstants.THIRTY);
82         panel.add(taskNameLabel);
83         jNameTextField = new JTextField(LayoutConstants.THIRTY);
84         jNameTextField.setBounds(LayoutConstants.MARGIN_LEFT, LayoutConstants.FIFTY, LayoutConstants.SCROPNUM,
85             LayoutConstants.THIRTY);
86         jNameTextField.setName(UtConstant.UT_EXPORT_FILE_FILE_NAME);
87         panel.add(jNameTextField);
88         JLabel filePathLocation = new JLabel("File Location");
89         filePathLocation.setBounds(LayoutConstants.MARGIN_LEFT, LayoutConstants.NINETY, LayoutConstants.HUNDRED_FIFTY,
90             LayoutConstants.THIRTY);
91         panel.add(filePathLocation);
92         textFieldWithBrowseButton = new TextFieldWithBrowseButton();
93         FileChooserDescriptor chooserDescriptor = new FileChooserDescriptor(true, true, true, true, true, true);
94         TextBrowseFolderListener listener = new TextBrowseFolderListener(chooserDescriptor);
95         textFieldWithBrowseButton.addBrowseFolderListener(listener);
96         textFieldWithBrowseButton.setText(System.getProperty("user.dir"));
97         textFieldWithBrowseButton
98             .setBounds(LayoutConstants.MARGIN_LEFT, LayoutConstants.FILE_SELECT_Y, LayoutConstants.SCROPNUM,
99                 LayoutConstants.THIRTY);
100         panel.add(textFieldWithBrowseButton);
101         JLabel fileTypes = new JLabel("File Type");
102         fileTypes.setBounds(LayoutConstants.MARGIN_LEFT, LayoutConstants.FILE_TYPE_Y, LayoutConstants.HUNDRED_FIFTY,
103             LayoutConstants.THIRTY);
104         panel.add(fileTypes);
105         fileTypeBox.setBounds(LayoutConstants.MARGIN_LEFT, LayoutConstants.FILE_TYPE_BOX_Y, LayoutConstants.SCROPNUM,
106             LayoutConstants.THIRTY);
107         panel.add(fileTypeBox);
108         panel.setPreferredSize(new Dimension(LayoutConstants.DIALOG_WIDTH, LayoutConstants.DIALOG_HEIGHT));
109         return panel;
110     }
111 
112     @Nullable
113     @Override
doValidate()114     protected ValidationInfo doValidate() {
115         exportFilePath = textFieldWithBrowseButton.getText();
116         exportFileName = jNameTextField.getText().trim();
117         if (StringUtils.isBlank(exportFileName)) {
118             return new ValidationInfo("Please input the file name !", jNameTextField);
119         }
120         if (!exportFileName.matches("^[A-Za-z0-9]+$")) {
121             return new ValidationInfo("The file name can only contain numbers and letters !", jNameTextField);
122         }
123         VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(exportFilePath);
124         if (virtualFile == null || exportFilePath.isEmpty()) {
125             return new ValidationInfo("Illegal path", textFieldWithBrowseButton);
126         }
127         return null;
128     }
129 
130     /**
131      * save as trace file
132      *
133      * @param jButton jButton
134      */
saveDataToFile(CustomJButton jButton)135     public void saveDataToFile(CustomJButton jButton) {
136         // 查询数据保存到file
137         String pathName = exportFilePath + File.separator + exportFileName + Constant.TRACE_SUFFIX;
138         DeviceIPPortInfo deviceIPPortInfo =
139             SessionManager.getInstance().getDeviceInfoBySessionId(jButton.getSessionId());
140         DeviceProcessInfo deviceProcessInfo = new DeviceProcessInfo();
141         deviceProcessInfo.setDeviceName(jButton.getDeviceName());
142         deviceProcessInfo.setProcessName(jButton.getProcessName());
143         deviceProcessInfo.setLocalSessionId(jButton.getSessionId());
144         deviceProcessInfo.setDeviceType(deviceIPPortInfo.getDeviceType().toString());
145         boolean saveResult =
146             SessionManager.getInstance().saveSessionDataToFile(jButton.getSessionId(), deviceProcessInfo, pathName);
147         if (saveResult) {
148             new SampleDialog("prompt", "Save Successfully !").show();
149         } else {
150             new SampleDialog("prompt", "Save failure !").show();
151         }
152     }
153 
154     /**
155      * get ExportFileName
156      *
157      * @return String FileName
158      */
getExportFileName()159     public String getExportFileName() {
160         return exportFileName;
161     }
162 
163     /**
164      * get exportFilePath
165      *
166      * @return String FilePath
167      */
getExportFilePath()168     public String getExportFilePath() {
169         return exportFilePath;
170     }
171 
172     /**
173      * getFileType
174      *
175      * @return String
176      */
getFileType()177     public String getFileType() {
178         return fileType;
179     }
180 }
181