1 /* 2 * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 package com.sk.action; 16 17 import com.intellij.notification.NotificationType; 18 import com.intellij.openapi.project.Project; 19 import com.sk.utils.FileUtil; 20 import com.sk.utils.GenNotification; 21 import org.apache.http.util.TextUtils; 22 23 import javax.swing.JButton; 24 import javax.swing.JTextField; 25 import javax.swing.JFileChooser; 26 import javax.swing.filechooser.FileNameExtensionFilter; 27 import java.awt.event.ActionEvent; 28 import java.awt.event.ActionListener; 29 import java.io.File; 30 import java.util.prefs.Preferences; 31 32 /** 33 * 接口文件选择框。 34 * 35 * @author: xudong 36 * @see: select file 37 * @version: v1.0.0 38 * @since 2022-02-21 39 */ 40 public class BrowseAction implements ActionListener { 41 private final JButton button; 42 private final JTextField interField; 43 private final JTextField genField; 44 private final JTextField scriptField; 45 private final Project project; 46 47 BrowseAction(Project project, JButton button, JTextField interField, JTextField geField, JTextField scriptField)48 public BrowseAction(Project project, JButton button, JTextField interField, 49 JTextField geField, JTextField scriptField) { 50 this.project = project; 51 this.button = button; 52 this.interField = interField; 53 this.genField = geField; 54 this.scriptField = scriptField; 55 } 56 57 @Override actionPerformed(ActionEvent actionEvent)58 public void actionPerformed(ActionEvent actionEvent) { 59 if (actionEvent.getSource().equals(button)) { 60 Preferences preferences = Preferences.userRoot(); 61 // 弹窗默认路径为上次选中的文件/目录路径 62 String tsFilePath = interField.getText().split(",")[0]; 63 if (tsFilePath.isBlank()) { 64 // 如果上次选中路径为空,则取历史记录中上次打开的路径 65 tsFilePath = preferences.get("interPathRecord", ""); 66 } 67 JFileChooser fcDlg = new JFileChooser(tsFilePath); 68 fcDlg.setDialogTitle("请选择接口文件..."); 69 fcDlg.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 70 FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件(*.ts)", "ts"); 71 fcDlg.setMultiSelectionEnabled(true); 72 fcDlg.setFileFilter(filter); 73 int returnVal = fcDlg.showOpenDialog(null); 74 if (returnVal == JFileChooser.APPROVE_OPTION) { 75 String upPath = fcDlg.getSelectedFile().getParent(); 76 File[] files = fcDlg.getSelectedFiles(); 77 String interFile = setSelectFile(files); 78 if (TextUtils.isBlank(interFile)) { 79 return; 80 } 81 82 // 设置默认打开路径; 83 84 preferences.put("interPathRecord", upPath); 85 interField.setText(interFile.substring(0, interFile.length() - 1)); 86 genField.setText(upPath); 87 scriptField.setText(upPath); 88 } 89 } 90 } 91 setSelectFile(File[] files)92 private String setSelectFile(File[] files) { 93 StringBuilder interFile = new StringBuilder(); 94 boolean existFile = false; 95 boolean existDir = false; 96 for (File file : files) { 97 if (file.isDirectory()) { 98 if (!existDir) { 99 existDir = true; 100 interFile.append(file.getPath()).append(","); 101 } else { 102 GenNotification.notifyMessage(project, 103 "目前只支持单个文件夹转换", 104 "选择不符合要求", 105 NotificationType.WARNING); 106 interField.setText(""); 107 return ""; 108 } 109 } else { 110 if (!FileUtil.patternFileName(file.getName())) { 111 GenNotification.notifyMessage(project, 112 file.getPath(), 113 file.getName() + "文件名不符合", 114 NotificationType.WARNING); 115 return ""; 116 } 117 existFile = true; 118 interFile.append(file.getPath()).append(","); 119 } 120 } 121 if (existDir && existFile) { 122 GenNotification.notifyMessage(project, 123 "不能同时转换文件和文件夹", 124 "选择不符合要求", 125 NotificationType.WARNING); 126 interField.setText(""); 127 return ""; 128 } 129 return interFile.toString(); 130 } 131 } 132