1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | package net.sf.jomic.ui; |
17 | |
18 | import java.awt.BorderLayout; |
19 | import java.awt.GridBagConstraints; |
20 | import java.awt.GridBagLayout; |
21 | import java.awt.Insets; |
22 | import java.awt.event.ActionEvent; |
23 | import java.awt.event.ActionListener; |
24 | import java.awt.event.WindowEvent; |
25 | import java.awt.event.WindowListener; |
26 | import java.io.File; |
27 | |
28 | import javax.swing.Box; |
29 | import javax.swing.BoxLayout; |
30 | import javax.swing.JButton; |
31 | import javax.swing.JDialog; |
32 | import javax.swing.JFileChooser; |
33 | import javax.swing.JOptionPane; |
34 | import javax.swing.JPanel; |
35 | import javax.swing.JTextField; |
36 | import javax.swing.event.DocumentEvent; |
37 | import javax.swing.event.DocumentListener; |
38 | |
39 | import net.sf.jomic.comic.Conversion; |
40 | import net.sf.jomic.comic.ConversionBean; |
41 | import net.sf.jomic.common.JomicTools; |
42 | import net.sf.jomic.common.PropertyConstants; |
43 | import net.sf.jomic.common.Settings; |
44 | import net.sf.jomic.tools.LocaleTools; |
45 | import net.sf.jomic.tools.UiTools; |
46 | |
47 | import org.apache.commons.logging.Log; |
48 | import org.apache.commons.logging.LogFactory; |
49 | |
50 | /** |
51 | * Dialog to specify how to create new comics. |
52 | * |
53 | * @author Thomas Aglassinger |
54 | */ |
55 | public class CreateComicDialog extends JDialog implements ActionListener, DocumentListener, WindowListener |
56 | { |
57 | private static final int INSET = 4; |
58 | private JButton cancelButton; |
59 | private ConversionBean conversionBean; |
60 | private JButton createButton; |
61 | private BooleanSettingCheckBox createComicForEachFolderCheckBox; |
62 | private JomicTools jomicTools; |
63 | private LocaleTools localeTools; |
64 | private Log logger; |
65 | private BooleanSettingCheckBox openAfterwardsCheckBox; |
66 | private int selection; |
67 | private Settings settings; |
68 | private JTextField sourceDirField; |
69 | private JTextField targetDirField; |
70 | private UiTools uiTools; |
71 | |
72 | public CreateComicDialog() { |
73 | super(); |
74 | logger = LogFactory.getLog(CreateComicDialog.class); |
75 | settings = Settings.instance(); |
76 | jomicTools = JomicTools.instance(); |
77 | localeTools = LocaleTools.instance(); |
78 | uiTools = UiTools.instance(); |
79 | |
80 | setTitle(localeTools.getMessage("dialogs.createComic.title")); |
81 | setModal(true); |
82 | |
83 | JPanel mainPane = uiTools.createDialogMainPanel(); |
84 | |
85 | conversionBean = new ConversionBean(); |
86 | mainPane.add(conversionBean, BorderLayout.LINE_END); |
87 | mainPane.add(createFolderPanel(), BorderLayout.CENTER); |
88 | mainPane.add(createButtonPanel(), BorderLayout.PAGE_END); |
89 | getContentPane().add(mainPane); |
90 | getRootPane().setDefaultButton(createButton); |
91 | addWindowListener(this); |
92 | } |
93 | |
94 | private void setSelectionAndClose(int newSelection) { |
95 | selection = newSelection; |
96 | setVisible(false); |
97 | } |
98 | |
99 | public Conversion getConversion() { |
100 | return conversionBean.getConversion(); |
101 | } |
102 | |
103 | /** |
104 | * Get selected button. |
105 | * |
106 | * @see JOptionPane#CANCEL_OPTION |
107 | * @see JOptionPane#OK_OPTION |
108 | */ |
109 | public int getSelection() { |
110 | return selection; |
111 | } |
112 | |
113 | public File getSourceDir() { |
114 | return new File(sourceDirField.getText()); |
115 | } |
116 | |
117 | public File getTargetDir() { |
118 | return new File(targetDirField.getText()); |
119 | } |
120 | |
121 | public boolean isCreateComicForEachFolder() { |
122 | return createComicForEachFolderCheckBox.isSelected(); |
123 | } |
124 | |
125 | public boolean isOpenAfterwards() { |
126 | return openAfterwardsCheckBox.isSelected(); |
127 | } |
128 | |
129 | public void actionPerformed(ActionEvent event) { |
130 | try { |
131 | assert event != null; |
132 | |
133 | String command = event.getActionCommand(); |
134 | |
135 | assert command != null; |
136 | if (command.equals(Commands.CANCEL)) { |
137 | setSelectionAndClose(JOptionPane.CANCEL_OPTION); |
138 | logger.info("canceled"); |
139 | } else if (command.equals(Commands.NEW_COMIC)) { |
140 | settings.setLastCreateComicSourceDir(getSourceDir()); |
141 | settings.setLastCreateComicTargetDir(getTargetDir()); |
142 | setSelectionAndClose(JOptionPane.OK_OPTION); |
143 | } else { |
144 | assert false : "command=" + command; |
145 | } |
146 | } catch (Throwable error) { |
147 | jomicTools.showError(event, error); |
148 | } |
149 | } |
150 | |
151 | public void changedUpdate(DocumentEvent event) { |
152 | updateEnabledButtons(); |
153 | } |
154 | |
155 | public void dispose() { |
156 | removeWindowListener(this); |
157 | uiTools.attemptToRemoveDocumentListener(targetDirField, this); |
158 | uiTools.attemptToRemoveDocumentListener(sourceDirField, this); |
159 | uiTools.attemptToRemoveActionListener(cancelButton, this); |
160 | uiTools.attemptToRemoveActionListener(createButton, this); |
161 | super.dispose(); |
162 | } |
163 | |
164 | public void insertUpdate(DocumentEvent event) { |
165 | updateEnabledButtons(); |
166 | } |
167 | |
168 | public void removeUpdate(DocumentEvent event) { |
169 | updateEnabledButtons(); |
170 | } |
171 | |
172 | public void windowActivated(WindowEvent event) { |
173 | // Do nothing. |
174 | } |
175 | |
176 | public void windowClosed(WindowEvent event) { |
177 | // Do nothing. |
178 | } |
179 | |
180 | public void windowClosing(WindowEvent event) { |
181 | // Do nothing. |
182 | } |
183 | |
184 | public void windowDeactivated(WindowEvent event) { |
185 | // Do nothing. |
186 | } |
187 | |
188 | public void windowDeiconified(WindowEvent event) { |
189 | // Do nothing. |
190 | } |
191 | |
192 | public void windowIconified(WindowEvent event) { |
193 | // Do nothing. |
194 | } |
195 | |
196 | public void windowOpened(WindowEvent event) { |
197 | // Set to cancel in case dialog is closed without clicking any buttons. |
198 | selection = JOptionPane.CANCEL_OPTION; |
199 | updateEnabledButtons(); |
200 | } |
201 | |
202 | private JButton createButton(String localeKeySuffix, String command) { |
203 | assert localeKeySuffix != null; |
204 | assert command != null; |
205 | JButton result = localeTools.createButton("dialogs.createComic." + localeKeySuffix); |
206 | |
207 | result.setActionCommand(command); |
208 | result.addActionListener(this); |
209 | return result; |
210 | } |
211 | |
212 | private JPanel createButtonPanel() { |
213 | JPanel result = new JPanel(); |
214 | |
215 | createButton = createButton("create", Commands.NEW_COMIC); |
216 | cancelButton = localeTools.createCancelButton(); |
217 | cancelButton.setActionCommand(Commands.CANCEL); |
218 | cancelButton.addActionListener(this); |
219 | result.setLayout(new BoxLayout(result, BoxLayout.LINE_AXIS)); |
220 | result.add(Box.createHorizontalGlue()); |
221 | result.add(cancelButton); |
222 | result.add(uiTools.createRigidAreaBetweenButtons()); |
223 | result.add(createButton); |
224 | return result; |
225 | } |
226 | |
227 | private JPanel createFolderPanel() { |
228 | JPanel result = new JPanel(); |
229 | GridBagConstraints constraints = new GridBagConstraints(); |
230 | JFileChooser sourceImageDirChooser = new SnapableJFileChooser( |
231 | settings, PropertyConstants.SET_CREATE_COMIC_SOURCE_DIR_DIALOG_WINDOW); |
232 | String setButtonText = localeTools.getMessage("buttons.set"); |
233 | String sourceChooserTitle = localeTools.getMessage("dialogs.setCreateComicSourceDir.title"); |
234 | String targetChooserTitle = localeTools.getMessage("dialogs.setCreateComicTargetDir.title"); |
235 | JFileChooser targetDirChooser = new SnapableJFileChooser( |
236 | settings, PropertyConstants.SET_CREATE_COMIC_TARGET_DIR_DIALOG_WINDOW); |
237 | FileFieldAdder sourceDirPane; |
238 | FileFieldAdder targetDirPane; |
239 | |
240 | result.setLayout(new GridBagLayout()); |
241 | constraints.insets = new Insets(INSET, INSET, INSET / 2, INSET / 2); |
242 | constraints.gridx = 0; |
243 | constraints.gridy = 0; |
244 | sourceDirPane = new FileFieldAdder(result, constraints, sourceImageDirChooser); |
245 | constraints.gridx = 0; |
246 | constraints.gridy += 1; |
247 | targetDirPane = new FileFieldAdder(result, constraints, targetDirChooser); |
248 | |
249 | sourceImageDirChooser.setApproveButtonText(setButtonText); |
250 | sourceImageDirChooser.setDialogTitle(sourceChooserTitle); |
251 | sourceImageDirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
252 | targetDirChooser.setApproveButtonText(setButtonText); |
253 | targetDirChooser.setDialogTitle(targetChooserTitle); |
254 | targetDirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
255 | |
256 | sourceDirField = sourceDirPane.getField(); |
257 | sourceDirField.setText(settings.getLastCreateComicSourceDir().getAbsolutePath()); |
258 | sourceDirField.getDocument().addDocumentListener(this); |
259 | sourceDirPane.getChooser().setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
260 | sourceDirPane.getLabel().setText(localeTools.getMessage("dialogs.createComic.sourceDir")); |
261 | |
262 | targetDirField = targetDirPane.getField(); |
263 | targetDirField.setText(settings.getLastCreateComicTargetDir().getAbsolutePath()); |
264 | targetDirField.getDocument().addDocumentListener(this); |
265 | targetDirPane.getLabel().setText(localeTools.getMessage("dialogs.createComic.targetDir")); |
266 | |
267 | openAfterwardsCheckBox = new BooleanSettingCheckBox( |
268 | localeTools.getMessage("dialogs.createComic.openNewComic"), |
269 | PropertyConstants.OPEN_NEW_COMIC, Commands.TOGGLE_OPEN_NEW_COMIC); |
270 | constraints.anchor = GridBagConstraints.LINE_START; |
271 | constraints.gridwidth = GridBagConstraints.REMAINDER; |
272 | constraints.gridx = 0; |
273 | constraints.gridy += 1; |
274 | result.add(openAfterwardsCheckBox, constraints); |
275 | |
276 | createComicForEachFolderCheckBox = new BooleanSettingCheckBox( |
277 | localeTools.getMessage("dialogs.createComic.createComicForEachSubFolder"), |
278 | PropertyConstants.CREATE_COMIC_FOR_EACH_SUB_FOLDER, |
279 | Commands.TOGGLE_CREATE_COMIC_FOR_EACH_SUB_FOLDER); |
280 | constraints.anchor = GridBagConstraints.LINE_START; |
281 | constraints.gridx = 0; |
282 | constraints.gridy += 1; |
283 | result.add(createComicForEachFolderCheckBox, constraints); |
284 | |
285 | return result; |
286 | } |
287 | |
288 | private void updateEnabledButtons() { |
289 | boolean hasSourceImages = (sourceDirField.getDocument().getLength() > 0); |
290 | boolean hasTargetComicDir = (targetDirField.getDocument().getLength() > 0); |
291 | boolean ready = hasSourceImages && hasTargetComicDir; |
292 | |
293 | createButton.setEnabled(ready); |
294 | } |
295 | } |