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.dialog; 16 17 import com.intellij.openapi.diagnostic.Logger; 18 19 import javax.swing.JButton; 20 import javax.swing.JComponent; 21 import javax.swing.JDialog; 22 import javax.swing.JPanel; 23 import javax.swing.JTextArea; 24 import javax.swing.KeyStroke; 25 import java.awt.event.KeyEvent; 26 import java.awt.event.WindowAdapter; 27 import java.awt.event.WindowEvent; 28 import java.io.IOException; 29 30 /** 31 * ErrorDialog错误对话框 32 * 33 * @author: xudong 34 * @see: generator error dialog 35 * @version: v1.0.0 36 * @since 2022-02-21 37 */ 38 public class ErrorDialog extends JDialog { 39 private static final Logger LOG = Logger.getInstance(ErrorDialog.class); 40 private static final String URL = 41 "rundll32 url.dll,FileProtocolHandler" + " https://gitee" + ".com/openharmony" + "-sig/napi_generator"; 42 43 private JPanel contentPane; 44 private JButton buttonOK; 45 private JButton buttonHelp; 46 private JTextArea textAreaError; 47 private String errorMessage; 48 ErrorDialog(String sErrorMessage)49 public ErrorDialog(String sErrorMessage) { 50 errorMessage = sErrorMessage; 51 } 52 53 /** 54 * 初始化 55 */ initDialog()56 public void initDialog() { 57 setContentPane(contentPane); 58 setModal(true); 59 getRootPane().setDefaultButton(buttonOK); 60 setTitle("执行失败"); 61 textAreaError.setText(errorMessage); 62 buttonOK.addActionListener(actionEvent -> onOK()); 63 64 buttonHelp.addActionListener(actionEvent -> onCancel()); 65 66 // call onCancel() when cross is clicked 67 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 68 addWindowListener(new WindowAdapter() { 69 /** 70 * close dialog 71 * @param windowEvent WindowEvent 72 */ 73 @Override 74 public void windowClosing(WindowEvent windowEvent) { 75 onCancel(); 76 } 77 }); 78 79 // call onCancel() on ESCAPE 80 contentPane.registerKeyboardAction(actionEvent -> onCancel(), 81 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 82 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 83 } 84 onOK()85 private void onOK() { 86 dispose(); 87 } 88 onCancel()89 private void onCancel() { 90 try { 91 Runtime.getRuntime().exec(URL); 92 } catch (IOException ioException) { 93 LOG.error("exec command help error" + ioException); 94 } 95 dispose(); 96 } 97 }