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