View Javadoc
1   package edu.jiangxin.apktoolbox.swing.extend;
2   
3   import edu.jiangxin.apktoolbox.utils.Constants;
4   import edu.jiangxin.apktoolbox.utils.FileUtils;
5   import edu.jiangxin.apktoolbox.utils.Utils;
6   import org.apache.commons.configuration2.Configuration;
7   import org.apache.logging.log4j.LogManager;
8   import org.apache.logging.log4j.Logger;
9   
10  import javax.swing.*;
11  import java.awt.*;
12  import java.io.File;
13  import java.util.ResourceBundle;
14  
15  /**
16   * @author jiangxin
17   * @author 2019-04-12
18   *
19   */
20  public class EasyPanel extends JPanel {
21  
22      private static final long serialVersionUID = 1L;
23      protected Logger logger;
24      protected Configuration conf;
25      protected ResourceBundle bundle;
26  
27      protected boolean isInited = false;
28      
29      public EasyPanel() throws HeadlessException {
30          super();
31          logger = LogManager.getLogger(this.getClass().getSimpleName());
32          conf = Utils.getConfiguration();
33          bundle = ResourceBundle.getBundle("apktoolbox");
34          logger.info("Panel start: " + this.getClass().getSimpleName());
35      }
36  
37      public boolean isNeedPreChangeMenu() {
38          return false;
39      }
40  
41      public void init() {
42          if (!isInited) {
43              initUI();
44              isInited = true;
45          }
46      }
47  
48      public void initUI() {
49          // do nothing
50      }
51  
52      protected EasyFrame getFrame() {
53          //Java/Swing: Obtain Window/JFrame from inside a JPanel:
54          // https://stackoverflow.com/questions/9650874/java-swing-obtain-window-jframe-from-inside-a-jpanel
55          Window window = SwingUtilities.getWindowAncestor(this);
56          if (window instanceof EasyFrame) {
57              return (EasyFrame) window;
58          }
59          return null;
60      }
61  
62      protected String checkAndGetFileContent(JTextField textField, String key, String msg) {
63          File file = new File(textField.getText());
64          if (!file.exists() || !file.isFile()) {
65              logger.error(msg);
66              Toolkit.getDefaultToolkit().beep();
67              JOptionPane.showMessageDialog(this, msg, Constants.MESSAGE_DIALOG_TITLE,
68                      JOptionPane.ERROR_MESSAGE);
69              textField.requestFocus();
70              return null;
71          }
72          String path = FileUtils.getCanonicalPathQuiet(file);
73          if (path != null) {
74              conf.setProperty(key, path);
75          }
76          return path;
77      }
78  
79      protected String checkAndGetNewFileContent(JTextField textField, String key, String msg) {
80          File file = new File(textField.getText());
81          File parentFile = file.getParentFile();
82          if (!parentFile.exists() || !parentFile.isDirectory()) {
83              logger.error(msg);
84              Toolkit.getDefaultToolkit().beep();
85              JOptionPane.showMessageDialog(this, msg, Constants.MESSAGE_DIALOG_TITLE,
86                      JOptionPane.ERROR_MESSAGE);
87              textField.requestFocus();
88              return null;
89          }
90          String path = FileUtils.getCanonicalPathQuiet(file);
91          if (path != null) {
92              conf.setProperty(key, path);
93          }
94          return path;
95      }
96  
97      protected String checkAndGetDirContent(JTextField textField, String key, String msg) {
98          File file = new File(textField.getText());
99          if (!file.exists() || !file.isDirectory()) {
100             logger.error(msg);
101             Toolkit.getDefaultToolkit().beep();
102             JOptionPane.showMessageDialog(this, msg, Constants.MESSAGE_DIALOG_TITLE,
103                     JOptionPane.ERROR_MESSAGE);
104             textField.requestFocus();
105             return null;
106         }
107         String path = FileUtils.getCanonicalPathQuiet(file);
108         if (path != null) {
109             conf.setProperty(key, path);
110         }
111         return path;
112     }
113 
114     protected String checkAndGetStringContent(JTextField textField, String key, String msg) {
115         String content = textField.getText();
116         if (content == null || content.isEmpty()) {
117             logger.error(msg);
118             Toolkit.getDefaultToolkit().beep();
119             JOptionPane.showMessageDialog(this, msg, Constants.MESSAGE_DIALOG_TITLE,
120                     JOptionPane.ERROR_MESSAGE);
121             textField.requestFocus();
122             return null;
123         }
124         conf.setProperty(key, content);
125         return content;
126     }
127 }