1 package edu.jiangxin.apktoolbox.help.settings;
2
3 import edu.jiangxin.apktoolbox.swing.extend.EasyChildTabbedPanel;
4 import edu.jiangxin.apktoolbox.utils.Constants;
5 import org.apache.commons.lang3.ArrayUtils;
6 import org.apache.commons.lang3.StringUtils;
7
8 import javax.swing.*;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11
12 public class LookAndFeelPanel extends EasyChildTabbedPanel {
13
14 private JPanel optionPanel;
15
16 private JComboBox<String> typeComboBox;
17
18 private JPanel operationPanel;
19
20 static {
21
22 UIManager.installLookAndFeel("Flat Light", "com.formdev.flatlaf.FlatLightLaf");
23 UIManager.installLookAndFeel("Flat Dark", "com.formdev.flatlaf.FlatDarkLaf");
24 UIManager.installLookAndFeel("Flat IntelliJ", "com.formdev.flatlaf.FlatIntelliJLaf");
25 UIManager.installLookAndFeel("Flat Darcula", "com.formdev.flatlaf.FlatDarculaLaf");
26 }
27
28 @Override
29 public void createUI() {
30 BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
31 setLayout(boxLayout);
32
33 createOptionPanel();
34 add(optionPanel);
35
36 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
37
38 createOperationPanel();
39 add(operationPanel);
40
41 add(Box.createVerticalStrut(15 * Constants.DEFAULT_Y_BORDER));
42 }
43
44 private void createOptionPanel() {
45 optionPanel = new JPanel();
46 optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.X_AXIS));
47
48 JLabel typeLabel = new JLabel("Type:");
49 typeComboBox = new JComboBox<>();
50
51 UIManager.LookAndFeelInfo[] lookAndFeelInfos = UIManager.getInstalledLookAndFeels();
52 if (ArrayUtils.isEmpty(lookAndFeelInfos)) {
53 typeComboBox.setEnabled(false);
54 } else {
55 typeComboBox.setEnabled(true);
56 String className = conf.getString("look.and.feel.class.name");
57 for (UIManager.LookAndFeelInfo info : lookAndFeelInfos) {
58 typeComboBox.addItem(info.getName());
59 if (StringUtils.equals(className, info.getClassName())) {
60 typeComboBox.setSelectedItem(info.getName());
61 }
62 }
63 }
64
65 optionPanel.add(typeLabel);
66 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
67 optionPanel.add(typeComboBox);
68 }
69
70 private void createOperationPanel() {
71 operationPanel = new JPanel();
72 operationPanel.setLayout(new BoxLayout(operationPanel, BoxLayout.X_AXIS));
73
74 JButton applyButton = new JButton("Apply");
75 applyButton.addActionListener(new ApplyButtonActionListener());
76
77 operationPanel.add(applyButton);
78 }
79
80 private final class ApplyButtonActionListener implements ActionListener {
81 @Override
82 public void actionPerformed(ActionEvent actionEvent) {
83 String name = (String) typeComboBox.getSelectedItem();
84 String className = getLookAndFeelClassNameFromName(name);
85 if (className == null) {
86 logger.warn("className is null");
87 return;
88 }
89 conf.setProperty("look.and.feel.class.name", className);
90 try {
91 UIManager.setLookAndFeel(className);
92 } catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
93 logger.error("setLookAndFeel failed, use default instead", e);
94 }
95 SwingUtilities.updateComponentTreeUI(getFrame());
96 getFrame().refreshSizeAndLocation();
97 }
98 }
99
100 private String getLookAndFeelClassNameFromName(String name) {
101 UIManager.LookAndFeelInfo[] lookAndFeelInfos = UIManager.getInstalledLookAndFeels();
102 for (UIManager.LookAndFeelInfo info : lookAndFeelInfos) {
103 if (StringUtils.equals(name, info.getName())) {
104 return info.getClassName();
105 }
106 }
107 return null;
108 }
109 }