|
| 1 | +package qz.ui; |
| 2 | + |
| 3 | +import qz.common.Constants; |
| 4 | +import qz.ui.component.IconCache; |
| 5 | + |
| 6 | +import javax.swing.*; |
| 7 | +import javax.swing.border.*; |
| 8 | +import java.awt.*; |
| 9 | +import java.awt.event.*; |
| 10 | +import java.util.Arrays; |
| 11 | + |
| 12 | +/** |
| 13 | + * Created by Tres on 11/16/2022 |
| 14 | + * A container for all the System Tray menu items |
| 15 | + */ |
| 16 | +public class ControlDialog extends BasicDialog implements Themeable { |
| 17 | + public enum Category { |
| 18 | + STATUS, |
| 19 | + GENERAL, |
| 20 | + ADVANCED, |
| 21 | + DIAGNOSTIC |
| 22 | + } |
| 23 | + |
| 24 | + private boolean persistent = false; |
| 25 | + |
| 26 | + private JPanel statusPanel; |
| 27 | + private JPanel generalPanel; |
| 28 | + private JPanel advancedPanel; |
| 29 | + private JPanel diagnosticPanel; |
| 30 | + |
| 31 | + public ControlDialog(IconCache iconCache) { |
| 32 | + super(Constants.ABOUT_TITLE, iconCache); |
| 33 | + initComponents(); |
| 34 | + } |
| 35 | + |
| 36 | + public void add(Component component, Category category) { |
| 37 | + add(component, null, category); |
| 38 | + } |
| 39 | + |
| 40 | + public void add(Component component, String text, Category category) { |
| 41 | + JComponent toAdd = null; |
| 42 | + if(component instanceof JCheckBoxMenuItem) { |
| 43 | + JCheckBoxMenuItem checkBoxMenuItem = (JCheckBoxMenuItem)component; |
| 44 | + JCheckBox checkBox = new JCheckBox(text == null ? checkBoxMenuItem.getText() : text, checkBoxMenuItem.getIcon(), checkBoxMenuItem.getState()); |
| 45 | + Arrays.stream(checkBoxMenuItem.getActionListeners()).forEach(checkBox::addActionListener); |
| 46 | + |
| 47 | + // Keep original in sync |
| 48 | + checkBox.addChangeListener(e -> { |
| 49 | + checkBoxMenuItem.setState(checkBox.isSelected()); |
| 50 | + }); |
| 51 | + |
| 52 | + // Add change listener on the original too |
| 53 | + checkBoxMenuItem.addChangeListener(e -> checkBox.setSelected(checkBoxMenuItem.getState())); |
| 54 | + |
| 55 | + toAdd = checkBox; |
| 56 | + } else if(component instanceof JMenuItem) { |
| 57 | + JMenuItem menuItem = (JMenuItem)component; |
| 58 | + JButton button = new JButton(text == null ? menuItem.getText() : text, menuItem.getIcon()); |
| 59 | + Arrays.stream(menuItem.getActionListeners()).forEach(button::addActionListener); |
| 60 | + toAdd = button; |
| 61 | + } else if(component instanceof JSeparator){ |
| 62 | + toAdd = (JSeparator)component; |
| 63 | + } |
| 64 | + if(toAdd != null) { |
| 65 | + switch(category) { |
| 66 | + case ADVANCED: |
| 67 | + advancedPanel.add(toAdd); |
| 68 | + break; |
| 69 | + case DIAGNOSTIC: |
| 70 | + diagnosticPanel.add(toAdd); |
| 71 | + break; |
| 72 | + case STATUS: |
| 73 | + statusPanel.add(toAdd); |
| 74 | + break; |
| 75 | + case GENERAL: |
| 76 | + default: |
| 77 | + generalPanel.add(toAdd); |
| 78 | + } |
| 79 | + } |
| 80 | + pack(); |
| 81 | + } |
| 82 | + |
| 83 | + private void initComponents() { |
| 84 | + |
| 85 | + JPanel mainPanel = new JPanel(); |
| 86 | + mainPanel.setLayout(new BorderLayout()); |
| 87 | + mainPanel.setBorder(createPaddedLineBorder(5, SwingConstants.SOUTH)); |
| 88 | + |
| 89 | + statusPanel = new JPanel(new FlowLayout()); |
| 90 | + |
| 91 | + Font headingFont = new JLabel().getFont().deriveFont(16f).deriveFont(Font.BOLD); |
| 92 | + |
| 93 | + generalPanel = new JPanel(); |
| 94 | + generalPanel.setLayout(new BoxLayout(generalPanel, BoxLayout.Y_AXIS)); |
| 95 | + generalPanel.setBorder(createPaddedLineBorder(5, SwingConstants.EAST)); |
| 96 | + JLabel generalLabel = new JLabel("General"); |
| 97 | + generalLabel.setFont(headingFont); |
| 98 | + generalPanel.add(generalLabel); |
| 99 | + |
| 100 | + advancedPanel = new JPanel(); |
| 101 | + advancedPanel.setLayout(new BoxLayout(advancedPanel, BoxLayout.Y_AXIS)); |
| 102 | + advancedPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); |
| 103 | + JLabel advancedLabel = new JLabel("Advanced"); |
| 104 | + advancedLabel.setFont(headingFont); |
| 105 | + advancedPanel.add(advancedLabel); |
| 106 | + |
| 107 | + diagnosticPanel = new JPanel(); |
| 108 | + diagnosticPanel.setLayout(new BoxLayout(diagnosticPanel, BoxLayout.Y_AXIS)); |
| 109 | + diagnosticPanel.setBorder(createPaddedLineBorder(5, SwingConstants.WEST)); |
| 110 | + JLabel diagnosticLabel = new JLabel("Diagnostic"); |
| 111 | + diagnosticLabel.setFont(headingFont); |
| 112 | + diagnosticPanel.add(diagnosticLabel); |
| 113 | + |
| 114 | + setHeader(statusPanel); |
| 115 | + statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED)); |
| 116 | + mainPanel.add(generalPanel, BorderLayout.LINE_START); |
| 117 | + mainPanel.add(advancedPanel, BorderLayout.CENTER); |
| 118 | + mainPanel.add(diagnosticPanel, BorderLayout.LINE_END); |
| 119 | + setContent(mainPanel, false); |
| 120 | + |
| 121 | + if(persistent) { |
| 122 | + addWindowListener(new WindowAdapter() { |
| 123 | + @Override |
| 124 | + public void windowClosing(WindowEvent e) { |
| 125 | + setExtendedState(JFrame.ICONIFIED); |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + setResizable(false); |
| 131 | + pack(); |
| 132 | + |
| 133 | + setLocationRelativeTo(null); // center on main display |
| 134 | + } |
| 135 | + |
| 136 | + private static Border createPaddedLineBorder(int padding, int position) { |
| 137 | + Color borderColor = new JSeparator().getForeground(); |
| 138 | + Border margins = new EmptyBorder(padding, padding, padding, padding); |
| 139 | + Border border; |
| 140 | + |
| 141 | + switch(position) { |
| 142 | + case SwingConstants.NORTH: |
| 143 | + border = new MatteBorder(1, 0, 0, 0, borderColor); |
| 144 | + break; |
| 145 | + case SwingConstants.WEST: |
| 146 | + border = new MatteBorder(0, 1, 0, 0, borderColor); |
| 147 | + break; |
| 148 | + case SwingConstants.SOUTH: |
| 149 | + border = new MatteBorder(0, 0, 1, 0, borderColor); |
| 150 | + break; |
| 151 | + default: |
| 152 | + case SwingConstants.EAST: |
| 153 | + border = new MatteBorder(0, 0, 0, 1, borderColor); |
| 154 | + } |
| 155 | + return new CompoundBorder(border, margins); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void setVisible(boolean b) { |
| 160 | + // Prevent closing if persistent mode is enabled |
| 161 | + if(!b && persistent) { |
| 162 | + setExtendedState(JFrame.ICONIFIED); |
| 163 | + } else { |
| 164 | + super.setVisible(b); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void refresh() { |
| 170 | + ThemeUtilities.refreshAll(this); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Sets persistent mode, window can't be closed unless shutdown |
| 175 | + */ |
| 176 | + public void setPersistent(boolean persistent) { |
| 177 | + this.persistent = persistent; |
| 178 | + } |
| 179 | +} |
| 180 | + |
0 commit comments