• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Guangzhou Digitalchina Information Technology Co., Ltd.
3  * All rights reserved.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.sk.dialog;
17 
18 import com.intellij.openapi.diagnostic.Logger;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.ui.DialogWrapper;
21 import com.intellij.openapi.ui.ValidationInfo;
22 import org.jetbrains.annotations.NotNull;
23 import org.jetbrains.annotations.Nullable;
24 
25 import javax.swing.Action;
26 import javax.swing.JComponent;
27 import java.awt.Desktop;
28 import java.awt.event.ActionEvent;
29 import java.io.IOException;
30 import java.net.URI;
31 import java.net.URISyntaxException;
32 
33 /**
34  * 主界面对话框Wrapper
35  *
36  * @author: xudong
37  * @see: tool conversion plug-in
38  * @version: v1.0.0
39  * @since 2022-05-27
40  */
41 public class GenerateDialog extends DialogWrapper {
42     private static final Logger LOG = Logger.getInstance(GenerateDialog.class);
43     private static final String FRAME_TITLE = "Generate Napi Frame";
44     private static final String CODE_URL = "https://gitee.com/openharmony/napi_generator";
45 
46     private final GenerateDialogPane genDiag;
47 
48     /**
49      * 构造函数
50      *
51      * @param project       projectId
52      * @param destPath      目录文件
53      * @param directoryPath 文件夹目录
54      * @param fileName      文件名
55      */
GenerateDialog(Project project, String destPath, String directoryPath, String fileName)56     public GenerateDialog(Project project, String destPath, String directoryPath, String fileName) {
57         super(true);
58         this.setResizable(false);
59         setTitle(FRAME_TITLE);
60         setModal(true);
61         genDiag = new GenerateDialogPane(project, destPath, directoryPath, fileName);
62         init();
63     }
64 
65     /**
66      * 创建视图
67      *
68      * @return 组件内容
69      */
70     @Nullable
71     @Override
createCenterPanel()72     protected JComponent createCenterPanel() {
73         return genDiag.getContentPanel();
74     }
75 
76 
77     /**
78      * 校验数据
79      *
80      * @return 检测文本框架是否有目录。
81      */
82     @Nullable
83     @Override
doValidate()84     protected ValidationInfo doValidate() {
85         return genDiag.validationInfo();
86     }
87 
88     /**
89      * ok/cancel按钮
90      *
91      * @return Action[] buttos list
92      */
93     @NotNull
94     @Override
createActions()95     protected Action[] createActions() {
96         DialogWrapperExitAction exitAction = new DialogWrapperExitAction("Cancel", CANCEL_EXIT_CODE);
97         CustomOKAction okAction = new CustomOKAction();
98 
99         // 设置默认的焦点按钮
100         okAction.putValue(DialogWrapper.DEFAULT_ACTION, true);
101         return new Action[]{exitAction, okAction};
102     }
103 
104     @NotNull
105     @Override
createLeftSideActions()106     protected Action[] createLeftSideActions() {
107         CustomHelpAction helpAction = new CustomHelpAction();
108         return new Action[]{helpAction};
109     }
110 
111     /**
112      * 自定义 ok Action
113      */
114     protected class CustomOKAction extends DialogWrapperAction {
115 
CustomOKAction()116         protected CustomOKAction() {
117             super("OK");
118         }
119 
120         @Override
doAction(ActionEvent actionEvent)121         protected void doAction(ActionEvent actionEvent) {
122 
123 
124             ValidationInfo validationInfo = doValidate();
125             if (validationInfo != null) {
126                 LOG.info(validationInfo.message);
127             } else {
128                 if (genDiag.getSelectedIndex() == 0) {
129                     if (genDiag.runFun()) {
130                         close(CANCEL_EXIT_CODE);
131                     }
132                 } else {
133                     if (genDiag.runFunH2ts()) {
134                         close(CANCEL_EXIT_CODE);
135                     }
136                 }
137             }
138         }
139     }
140 
141     /**
142      * 自定义 help Action
143      */
144     protected class CustomHelpAction extends DialogWrapperAction {
145 
CustomHelpAction()146         protected CustomHelpAction() {
147             super("Help");
148         }
149 
150         @Override
doAction(ActionEvent actionEvent)151         protected void doAction(ActionEvent actionEvent) {
152             try {
153                 Desktop.getDesktop().browse(new URI(CODE_URL));
154             } catch (URISyntaxException | IOException e) {
155                 LOG.error("Open help error:" + e);
156             }
157         }
158     }
159 }
160