EasyPanel.java

  1. package edu.jiangxin.apktoolbox.swing.extend;

  2. import edu.jiangxin.apktoolbox.utils.Constants;
  3. import edu.jiangxin.apktoolbox.utils.FileUtils;
  4. import edu.jiangxin.apktoolbox.utils.Utils;
  5. import org.apache.commons.configuration2.Configuration;
  6. import org.apache.logging.log4j.LogManager;
  7. import org.apache.logging.log4j.Logger;

  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.io.File;
  11. import java.util.ResourceBundle;

  12. /**
  13.  * @author jiangxin
  14.  * @author 2019-04-12
  15.  *
  16.  */
  17. public class EasyPanel extends JPanel {

  18.     private static final long serialVersionUID = 1L;
  19.     protected Logger logger;
  20.     protected Configuration conf;
  21.     protected ResourceBundle bundle;

  22.     protected boolean isInited = false;
  23.    
  24.     public EasyPanel() throws HeadlessException {
  25.         super();
  26.         logger = LogManager.getLogger(this.getClass().getSimpleName());
  27.         conf = Utils.getConfiguration();
  28.         bundle = ResourceBundle.getBundle("apktoolbox");
  29.         logger.info("Panel start: " + this.getClass().getSimpleName());
  30.     }

  31.     public boolean isNeedPreChangeMenu() {
  32.         return false;
  33.     }

  34.     public void init() {
  35.         if (!isInited) {
  36.             initUI();
  37.             isInited = true;
  38.         }
  39.     }

  40.     public void initUI() {
  41.         // do nothing
  42.     }

  43.     protected EasyFrame getFrame() {
  44.         //Java/Swing: Obtain Window/JFrame from inside a JPanel:
  45.         // https://stackoverflow.com/questions/9650874/java-swing-obtain-window-jframe-from-inside-a-jpanel
  46.         Window window = SwingUtilities.getWindowAncestor(this);
  47.         if (window instanceof EasyFrame) {
  48.             return (EasyFrame) window;
  49.         }
  50.         return null;
  51.     }

  52.     protected String checkAndGetFileContent(JTextField textField, String key, String msg) {
  53.         File file = new File(textField.getText());
  54.         if (!file.exists() || !file.isFile()) {
  55.             logger.error(msg);
  56.             Toolkit.getDefaultToolkit().beep();
  57.             JOptionPane.showMessageDialog(this, msg, Constants.MESSAGE_DIALOG_TITLE,
  58.                     JOptionPane.ERROR_MESSAGE);
  59.             textField.requestFocus();
  60.             return null;
  61.         }
  62.         String path = FileUtils.getCanonicalPathQuiet(file);
  63.         if (path != null) {
  64.             conf.setProperty(key, path);
  65.         }
  66.         return path;
  67.     }

  68.     protected String checkAndGetNewFileContent(JTextField textField, String key, String msg) {
  69.         File file = new File(textField.getText());
  70.         File parentFile = file.getParentFile();
  71.         if (!parentFile.exists() || !parentFile.isDirectory()) {
  72.             logger.error(msg);
  73.             Toolkit.getDefaultToolkit().beep();
  74.             JOptionPane.showMessageDialog(this, msg, Constants.MESSAGE_DIALOG_TITLE,
  75.                     JOptionPane.ERROR_MESSAGE);
  76.             textField.requestFocus();
  77.             return null;
  78.         }
  79.         String path = FileUtils.getCanonicalPathQuiet(file);
  80.         if (path != null) {
  81.             conf.setProperty(key, path);
  82.         }
  83.         return path;
  84.     }

  85.     protected String checkAndGetDirContent(JTextField textField, String key, String msg) {
  86.         File file = new File(textField.getText());
  87.         if (!file.exists() || !file.isDirectory()) {
  88.             logger.error(msg);
  89.             Toolkit.getDefaultToolkit().beep();
  90.             JOptionPane.showMessageDialog(this, msg, Constants.MESSAGE_DIALOG_TITLE,
  91.                     JOptionPane.ERROR_MESSAGE);
  92.             textField.requestFocus();
  93.             return null;
  94.         }
  95.         String path = FileUtils.getCanonicalPathQuiet(file);
  96.         if (path != null) {
  97.             conf.setProperty(key, path);
  98.         }
  99.         return path;
  100.     }

  101.     protected String checkAndGetStringContent(JTextField textField, String key, String msg) {
  102.         String content = textField.getText();
  103.         if (content == null || content.isEmpty()) {
  104.             logger.error(msg);
  105.             Toolkit.getDefaultToolkit().beep();
  106.             JOptionPane.showMessageDialog(this, msg, Constants.MESSAGE_DIALOG_TITLE,
  107.                     JOptionPane.ERROR_MESSAGE);
  108.             textField.requestFocus();
  109.             return null;
  110.         }
  111.         conf.setProperty(key, content);
  112.         return content;
  113.     }
  114. }