View Javadoc
1   package edu.jiangxin.apktoolbox.swing.extend;
2   
3   import java.awt.*;
4   import java.awt.event.WindowAdapter;
5   import java.awt.event.WindowEvent;
6   import java.util.ResourceBundle;
7   
8   import javax.swing.JFrame;
9   
10  import org.apache.commons.configuration2.Configuration;
11  import org.apache.logging.log4j.LogManager;
12  import org.apache.logging.log4j.Logger;
13  
14  import edu.jiangxin.apktoolbox.utils.Utils;
15  
16  /**
17   * @author jiangxin
18   * @author 2018-09-09
19   *
20   */
21  public class EasyFrame extends JFrame {
22      private static final long serialVersionUID = 1L;
23      protected transient Logger logger;
24      protected transient Configuration conf;
25      protected transient ResourceBundle bundle;
26      protected transient Image image;
27  
28      public EasyFrame() throws HeadlessException {
29          super();
30          logger = LogManager.getLogger(this.getClass().getSimpleName());
31          conf = Utils.getConfiguration();
32          bundle = ResourceBundle.getBundle("apktoolbox");
33      }
34  
35      // in case of escape of "this"
36      public void initialize() {
37          addWindowListener(new WindowAdapter() {
38              @Override
39              public void windowClosing(WindowEvent e) {
40                  super.windowClosing(e);
41                  onWindowClosing(e);
42                  Utils.saveConfiguration();
43                  logger.info("windowClosing: " + EasyFrame.this.getClass().getSimpleName());
44              }
45              @Override
46              public void windowIconified(WindowEvent e) {
47                  super.windowIconified(e);
48                  onWindowIconified(e);
49                  setVisible(false);
50                  dispose();
51                  Utils.saveConfiguration();
52                  logger.info("windowIconified: " + EasyFrame.this.getClass().getSimpleName());
53              }
54          });
55          Toolkit tk = Toolkit.getDefaultToolkit();
56          image = tk.createImage(this.getClass().getResource("/icon.jpg"));
57          setIconImage(image);
58          logger.info("Frame start: " + this.getClass().getSimpleName());
59      }
60  
61      public void refreshSizeAndLocation() {
62          // use pack to resize the child component
63          pack();
64          setMinimumSize(new Dimension(800, 100));
65          setResizable(false);
66  
67          // relocation this JFrame
68          int windowWidth = getWidth();
69          int windowHeight = getHeight();
70          Toolkit kit = Toolkit.getDefaultToolkit();
71          if (kit == null) {
72              logger.error("kit is null");
73              return;
74          }
75          Dimension screenSize = kit.getScreenSize();
76          if (screenSize == null) {
77              logger.error("screenSize is null");
78              return;
79          }
80          int screenWidth = screenSize.width;
81          int screenHeight = screenSize.height;
82          setLocation(screenWidth / 2 - windowWidth / 2, screenHeight / 2 - windowHeight / 2);
83      }
84  
85      protected void onWindowClosing(WindowEvent e) {}
86  
87      protected void onWindowIconified(WindowEvent e) {}
88  }