|
4 | 4 | import com.itextpdf.rups.view.icons.FrameIconUtil; |
5 | 5 |
|
6 | 6 | import java.awt.BorderLayout; |
| 7 | +import java.awt.Component; |
7 | 8 | import java.awt.GridBagConstraints; |
8 | 9 | import java.awt.GridBagLayout; |
| 10 | +import java.util.Locale; |
9 | 11 | import javax.swing.JButton; |
10 | 12 | import javax.swing.JCheckBox; |
11 | 13 | import javax.swing.JComboBox; |
| 14 | +import javax.swing.JDialog; |
12 | 15 | import javax.swing.JFileChooser; |
13 | | -import javax.swing.JFrame; |
14 | 16 | import javax.swing.JLabel; |
15 | 17 | import javax.swing.JOptionPane; |
16 | 18 | import javax.swing.JPanel; |
|
24 | 26 | */ |
25 | 27 | public class PreferencesWindow { |
26 | 28 |
|
27 | | - private JFrame jFrame; |
| 29 | + private JDialog jDialog; |
| 30 | + |
| 31 | + private GridBagLayout gridBagLayout; |
| 32 | + private GridBagConstraints left; |
| 33 | + private GridBagConstraints right; |
| 34 | + |
| 35 | + private JPanel visualPanel; |
| 36 | + private JScrollPane generalSettingsScrollPane; |
| 37 | + |
| 38 | + // Fields to reset |
| 39 | + private JCheckBox openDuplicateFiles; |
| 40 | + private JTextField pathField; |
28 | 41 |
|
29 | 42 | public PreferencesWindow() { |
30 | | - jFrame = new JFrame(); |
31 | | - jFrame.setTitle(Language.PREFERENCES.getString()); |
32 | | - jFrame.setIconImages(FrameIconUtil.loadFrameIcons()); |
33 | | - jFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
34 | | - jFrame.setLayout(new BorderLayout()); |
| 43 | + initializeJDialog(); |
| 44 | + initializeLayout(); |
| 45 | + |
| 46 | + createGeneralSettingsTab(); |
| 47 | + createVisualSettingsTab(); |
| 48 | + createTabbedPane(); |
| 49 | + createSaveCancelResetSection(); |
| 50 | + |
| 51 | + completeJDialogCreation(); |
| 52 | + } |
| 53 | + |
| 54 | + private void initializeJDialog() { |
| 55 | + this.jDialog = new JDialog(); |
| 56 | + |
| 57 | + this.jDialog.setTitle(Language.PREFERENCES.getString()); |
| 58 | + this.jDialog.setIconImages(FrameIconUtil.loadFrameIcons()); |
| 59 | + this.jDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
| 60 | + this.jDialog.setModal(true); |
| 61 | + this.jDialog.setLayout(new BorderLayout()); |
| 62 | + } |
| 63 | + |
| 64 | + private void initializeLayout() { |
| 65 | + this.gridBagLayout = new GridBagLayout(); |
| 66 | + |
| 67 | + this.left = new GridBagConstraints(); |
| 68 | + this.left.anchor = GridBagConstraints.EAST; |
35 | 69 |
|
36 | | - JTabbedPane tabbedPane = new JTabbedPane(); |
| 70 | + this.right = new GridBagConstraints(); |
| 71 | + this.right.weightx = 2.0; |
| 72 | + this.right.fill = GridBagConstraints.HORIZONTAL; |
| 73 | + this.right.gridwidth = GridBagConstraints.REMAINDER; |
| 74 | + } |
37 | 75 |
|
38 | | - JTextField pathField = new JTextField(RupsConfiguration.INSTANCE.getHomeFolder().getPath(), 30); |
| 76 | + private void createGeneralSettingsTab() { |
| 77 | + this.pathField = new JTextField(RupsConfiguration.INSTANCE.getHomeFolder().getPath(), 30); |
39 | 78 | JLabel pathLabel = new JLabel(Language.PREFERENCES_OPEN_FOLDER.getString()); |
40 | | - pathLabel.setLabelFor(pathField); |
| 79 | + pathLabel.setLabelFor(this.pathField); |
41 | 80 |
|
42 | 81 | JButton pathChooser = new JButton(Language.PREFERENCES_SELECT_NEW_DEFAULT_FOLDER.getString()); |
43 | 82 | pathChooser.addActionListener(e -> { |
44 | 83 | JFileChooser fileChooser = new JFileChooser(RupsConfiguration.INSTANCE.getHomeFolder()); |
45 | 84 | fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
46 | | - int choice = fileChooser.showOpenDialog(jFrame); |
| 85 | + int choice = fileChooser.showOpenDialog(jDialog); |
47 | 86 |
|
48 | 87 | if (choice == JFileChooser.APPROVE_OPTION) { |
49 | 88 | String path = fileChooser.getSelectedFile().getPath(); |
50 | | - pathField.setText(path); |
| 89 | + this.pathField.setText(path); |
51 | 90 | RupsConfiguration.INSTANCE.setHomeFolder(path); |
52 | 91 | } |
53 | 92 | }); |
54 | 93 |
|
55 | 94 | JPanel fieldsPanel = new JPanel(); |
56 | | - fieldsPanel.add(pathField); |
| 95 | + fieldsPanel.add(this.pathField); |
57 | 96 | fieldsPanel.add(pathChooser); |
58 | 97 |
|
59 | | - JCheckBox openDuplicateFiles = new JCheckBox("", RupsConfiguration.INSTANCE.canOpenDuplicateFiles()); |
60 | | - openDuplicateFiles.addActionListener( |
| 98 | + this.openDuplicateFiles = new JCheckBox("", RupsConfiguration.INSTANCE.canOpenDuplicateFiles()); |
| 99 | + this.openDuplicateFiles.addActionListener( |
61 | 100 | e -> RupsConfiguration.INSTANCE.setOpenDuplicateFiles(((JCheckBox) e.getSource()).isSelected()) |
62 | 101 | ); |
63 | 102 | JLabel openDuplicateFilesLabel = new JLabel(Language.PREFERENCES_ALLOW_DUPLICATE_FILES.getString()); |
64 | | - openDuplicateFilesLabel.setLabelFor(openDuplicateFiles); |
| 103 | + openDuplicateFilesLabel.setLabelFor(this.openDuplicateFiles); |
| 104 | + |
| 105 | + JPanel generalSettingsPanel = new JPanel(); |
| 106 | + generalSettingsPanel.setLayout(this.gridBagLayout); |
| 107 | + |
| 108 | + generalSettingsPanel.add(pathLabel, this.left); |
| 109 | + generalSettingsPanel.add(fieldsPanel, this.right); |
65 | 110 |
|
66 | | - JPanel outerPanel = new JPanel(); |
67 | | - GridBagLayout gridBagLayout = new GridBagLayout(); |
68 | | - outerPanel.setLayout(gridBagLayout); |
| 111 | + generalSettingsPanel.add(openDuplicateFilesLabel, this.left); |
| 112 | + generalSettingsPanel.add(this.openDuplicateFiles, this.right); |
69 | 113 |
|
70 | | - GridBagConstraints left = new GridBagConstraints(); |
71 | | - left.anchor = GridBagConstraints.EAST; |
72 | | - GridBagConstraints right = new GridBagConstraints(); |
73 | | - right.weightx = 2.0; |
74 | | - right.fill = GridBagConstraints.HORIZONTAL; |
75 | | - right.gridwidth = GridBagConstraints.REMAINDER; |
| 114 | + this.generalSettingsScrollPane = new JScrollPane(generalSettingsPanel); |
| 115 | + } |
76 | 116 |
|
77 | | - outerPanel.add(pathLabel, left); |
78 | | - outerPanel.add(fieldsPanel, right); |
| 117 | + private void createVisualSettingsTab() { |
| 118 | + final JComboBox<String> localeBox = new JComboBox<>(); |
| 119 | + localeBox.addItem("nl-NL"); |
| 120 | + localeBox.addItem("en-US"); |
| 121 | + localeBox.setSelectedItem(RupsConfiguration.INSTANCE.getUserLocale().toLanguageTag()); |
| 122 | + final JLabel localeLabel = new JLabel("Locale"); |
| 123 | + localeLabel.setLabelFor(localeBox); |
| 124 | + |
| 125 | + localeBox.addActionListener(e -> { |
| 126 | + Object selectedItem = localeBox.getSelectedItem(); |
| 127 | + String selectedString = (String) selectedItem; |
| 128 | + RupsConfiguration.INSTANCE.setUserLocale(Locale.forLanguageTag(selectedString)); |
| 129 | + }); |
79 | 130 |
|
80 | | - outerPanel.add(openDuplicateFilesLabel, left); |
81 | | - outerPanel.add(openDuplicateFiles, right); |
| 131 | + this.visualPanel = new JPanel(); |
| 132 | + this.visualPanel.setLayout(this.gridBagLayout); |
82 | 133 |
|
83 | | - JScrollPane scrollPane = new JScrollPane(outerPanel); |
| 134 | + this.visualPanel.add(localeLabel, this.left); |
| 135 | + this.visualPanel.add(localeBox, this.right); |
| 136 | + } |
84 | 137 |
|
85 | | - tabbedPane.add(Language.PREFERENCES_RUPS_SETTINGS.getString(), scrollPane); |
| 138 | + private void createTabbedPane() { |
| 139 | + final JTabbedPane tabbedPane = new JTabbedPane(); |
86 | 140 |
|
87 | | - jFrame.add(tabbedPane, BorderLayout.CENTER); |
| 141 | + tabbedPane.add(Language.PREFERENCES_RUPS_SETTINGS.getString(), this.generalSettingsScrollPane); |
| 142 | + tabbedPane.add(Language.PREFERENCES_VISUAL_SETTINGS.getString(), this.visualPanel); |
88 | 143 |
|
| 144 | + this.jDialog.add(tabbedPane, BorderLayout.CENTER); |
| 145 | + } |
| 146 | + |
| 147 | + private void createSaveCancelResetSection() { |
89 | 148 | JPanel buttons = new JPanel(); |
90 | 149 |
|
91 | 150 | JButton save = new JButton(Language.SAVE.getString()); |
92 | | - save.addActionListener(e -> RupsConfiguration.INSTANCE.saveConfiguration()); |
| 151 | + save.addActionListener(e -> { |
| 152 | + RupsConfiguration.INSTANCE.saveConfiguration(); |
| 153 | + this.jDialog.dispose(); |
| 154 | + }); |
93 | 155 | buttons.add(save); |
94 | 156 |
|
95 | 157 | JButton cancel = new JButton(Language.DIALOG_CANCEL.getString()); |
96 | 158 | cancel.addActionListener(e -> { |
97 | 159 | if (RupsConfiguration.INSTANCE.hasUnsavedChanges()) { |
98 | | - int choice = JOptionPane.showConfirmDialog(jFrame, |
| 160 | + int choice = JOptionPane.showConfirmDialog(jDialog, |
99 | 161 | Language.SAVE_UNSAVED_CHANGES.getString()); |
100 | 162 | if (choice == JOptionPane.OK_OPTION) { |
101 | 163 | RupsConfiguration.INSTANCE.cancelTemporaryChanges(); |
102 | | - jFrame.dispose(); |
| 164 | + this.jDialog.dispose(); |
103 | 165 | } |
104 | 166 | } else { |
105 | | - jFrame.dispose(); |
| 167 | + this.jDialog.dispose(); |
106 | 168 | } |
107 | 169 | }); |
108 | 170 | buttons.add(cancel); |
109 | 171 |
|
110 | 172 | JButton reset = new JButton(Language.PREFERENCES_RESET_TO_DEFAULTS.getString()); |
111 | 173 |
|
112 | 174 | reset.addActionListener(e -> { |
113 | | - int choice = JOptionPane.showConfirmDialog(jFrame, |
| 175 | + int choice = JOptionPane.showConfirmDialog(jDialog, |
114 | 176 | Language.PREFERENCES_RESET_TO_DEFAULTS_CONFIRM.getString()); |
115 | 177 | if (choice == JOptionPane.OK_OPTION) { |
116 | 178 | RupsConfiguration.INSTANCE.resetToDefaultProperties(); |
117 | 179 |
|
118 | | - resetView(pathField, openDuplicateFiles); |
| 180 | + resetView(); |
119 | 181 | } |
120 | 182 | }); |
121 | 183 | buttons.add(reset); |
122 | 184 |
|
123 | | - jFrame.add(buttons, BorderLayout.SOUTH); |
124 | | - jFrame.pack(); |
125 | | - jFrame.setResizable(false); |
| 185 | + jDialog.add(buttons, BorderLayout.SOUTH); |
| 186 | + } |
| 187 | + |
| 188 | + private void completeJDialogCreation() { |
| 189 | + this.jDialog.pack(); |
| 190 | + this.jDialog.setResizable(false); |
126 | 191 | } |
127 | 192 |
|
128 | | - private void resetView(JTextField pathField, JCheckBox openDuplicateFiles) { |
129 | | - pathField.setText(RupsConfiguration.INSTANCE.getHomeFolder().getPath()); |
130 | | - openDuplicateFiles.setSelected(RupsConfiguration.INSTANCE.canOpenDuplicateFiles()); |
| 193 | + private void resetView() { |
| 194 | + this.pathField.setText(RupsConfiguration.INSTANCE.getHomeFolder().getPath()); |
| 195 | + this.openDuplicateFiles.setSelected(RupsConfiguration.INSTANCE.canOpenDuplicateFiles()); |
131 | 196 | } |
132 | 197 |
|
133 | | - public void show() { |
134 | | - jFrame.setLocationRelativeTo(null); |
135 | | - jFrame.setVisible(true); |
| 198 | + public void show(Component component) { |
| 199 | + jDialog.setLocationRelativeTo(component); |
| 200 | + jDialog.setVisible(true); |
136 | 201 | } |
137 | 202 | } |
0 commit comments