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.Container; |
20 | import java.awt.event.ActionEvent; |
21 | import java.awt.event.ActionListener; |
22 | import java.awt.event.WindowEvent; |
23 | import java.awt.event.WindowListener; |
24 | import java.io.File; |
25 | |
26 | import javax.swing.BorderFactory; |
27 | import javax.swing.Box; |
28 | import javax.swing.BoxLayout; |
29 | import javax.swing.DefaultListModel; |
30 | import javax.swing.JButton; |
31 | import javax.swing.JDialog; |
32 | import javax.swing.JFileChooser; |
33 | import javax.swing.JLabel; |
34 | import javax.swing.JList; |
35 | import javax.swing.JOptionPane; |
36 | import javax.swing.JPanel; |
37 | import javax.swing.JScrollPane; |
38 | import javax.swing.JTextField; |
39 | import javax.swing.border.Border; |
40 | import javax.swing.event.DocumentEvent; |
41 | import javax.swing.event.DocumentListener; |
42 | import javax.swing.event.ListDataEvent; |
43 | import javax.swing.event.ListDataListener; |
44 | import javax.swing.event.ListSelectionEvent; |
45 | import javax.swing.event.ListSelectionListener; |
46 | import javax.swing.filechooser.FileFilter; |
47 | import javax.swing.text.Document; |
48 | |
49 | import net.sf.jomic.comic.ComicChooserFileFilter; |
50 | import net.sf.jomic.comic.Conversion; |
51 | import net.sf.jomic.comic.ConversionBean; |
52 | import net.sf.jomic.common.JomicTools; |
53 | import net.sf.jomic.common.PropertyConstants; |
54 | import net.sf.jomic.common.Settings; |
55 | import net.sf.jomic.tools.LocaleTools; |
56 | import net.sf.jomic.tools.StringTools; |
57 | import net.sf.jomic.tools.UiTools; |
58 | |
59 | /** |
60 | * Modal JDialog to specify parameters, source, and target of a conversion. |
61 | * |
62 | * @author Thomas Aglassinger |
63 | * @see net.sf.jomic.comic.Conversion |
64 | */ |
65 | public class ConvertDialog extends JDialog |
66 | implements ActionListener, DocumentListener, ListDataListener, ListSelectionListener, WindowListener |
67 | { |
68 | private static final String COMMAND_ADD_COMICS = "addComics"; |
69 | private static final String COMMAND_CLEAR_COMICS = "clearComics"; |
70 | private static final String COMMAND_CONVERT = "convert"; |
71 | private static final String COMMAND_REMOVE_COMICS = "removeComics"; |
72 | private static final String COMMAND_SET_TARGET_DIR = "setTargetDir"; |
73 | private static final int TARGET_DIR_PREFERRED_COLUMNS = 35; |
74 | |
75 | private JButton addComicsButton; |
76 | private JFileChooser addComicsToConvertChooser; |
77 | private JButton cancelButton; |
78 | private JButton chooseTargetDirButton; |
79 | private JButton clearComicsButton; |
80 | private FileFilter comicFileFilter; |
81 | private JList comicFileList; |
82 | private DefaultListModel comicListModel; |
83 | private ConversionBean conversionBean; |
84 | private JButton convertButton; |
85 | private JomicTools jomicTools; |
86 | private LocaleTools localeTools; |
87 | private JButton removeComicsButton; |
88 | private int selection; |
89 | private Settings settings; |
90 | private StringTools stringTools; |
91 | private JFileChooser targetDirChooser; |
92 | private JTextField targetDirField; |
93 | private UiTools uiTools; |
94 | |
95 | public ConvertDialog() { |
96 | super(); |
97 | setModal(true); |
98 | jomicTools = JomicTools.instance(); |
99 | localeTools = LocaleTools.instance(); |
100 | stringTools = StringTools.instance(); |
101 | uiTools = UiTools.instance(); |
102 | settings = Settings.instance(); |
103 | |
104 | String title = localeTools.getMessage("dialogs.convert.title"); |
105 | |
106 | setTitle(title); |
107 | |
108 | conversionBean = new ConversionBean(); |
109 | addComicsToConvertChooser = createAddComicsToConvertChooser(); |
110 | targetDirChooser = createTargetDirChooser(); |
111 | |
112 | JPanel buttonPane = createButtonPanel(); |
113 | JPanel controlPane = createControlPanel(); |
114 | Container contentPane = getContentPane(); |
115 | JPanel mainPane = uiTools.createDialogMainPanel(); |
116 | |
117 | mainPane.add(controlPane, BorderLayout.CENTER); |
118 | mainPane.add(buttonPane, BorderLayout.PAGE_END); |
119 | contentPane.add(mainPane); |
120 | getRootPane().setDefaultButton(convertButton); |
121 | comicFileFilter = new ComicChooserFileFilter(); |
122 | addWindowListener(this); |
123 | updateEnabledButtons(); |
124 | } |
125 | |
126 | /** |
127 | * Set directory where converted files end up. |
128 | */ |
129 | public void setTargetDir(File newTargetDir) { |
130 | targetDirField.setText(newTargetDir.getAbsolutePath()); |
131 | } |
132 | |
133 | private void setSelectionAndClose(int newSelection) { |
134 | selection = newSelection; |
135 | setVisible(false); |
136 | } |
137 | |
138 | public Conversion getConversion() { |
139 | return conversionBean.getConversion(); |
140 | } |
141 | |
142 | /** |
143 | * Get the selected comic files and directories. |
144 | */ |
145 | public File[] getSelectedFiles() { |
146 | File[] result; |
147 | |
148 | synchronized (comicListModel) { |
149 | int fileCount = comicListModel.getSize(); |
150 | |
151 | result = new File[fileCount]; |
152 | for (int i = 0; i < fileCount; i += 1) { |
153 | result[i] = (File) comicListModel.get(i); |
154 | } |
155 | } |
156 | |
157 | return result; |
158 | } |
159 | |
160 | /** |
161 | * Get selected button. |
162 | * |
163 | * @see JOptionPane#CANCEL_OPTION |
164 | * @see JOptionPane#OK_OPTION |
165 | */ |
166 | public int getSelection() { |
167 | return selection; |
168 | } |
169 | |
170 | public File getTargetDir() { |
171 | File result; |
172 | String targetDirText = targetDirField.getText(); |
173 | |
174 | if (stringTools.isNullOrEmpty(targetDirText)) { |
175 | result = null; |
176 | } else { |
177 | result = new File(targetDirText); |
178 | } |
179 | return result; |
180 | } |
181 | |
182 | /** |
183 | * Get button to start conversion. |
184 | */ |
185 | JButton getConvertButton() { |
186 | return convertButton; |
187 | } |
188 | |
189 | public void actionPerformed(ActionEvent event) { |
190 | try { |
191 | assert event != null; |
192 | |
193 | String command = event.getActionCommand(); |
194 | |
195 | assert command != null; |
196 | if (command.equals(COMMAND_ADD_COMICS)) { |
197 | performAddComicsToConvert(); |
198 | } else if (command.equals(Commands.CANCEL)) { |
199 | setSelectionAndClose(JOptionPane.CANCEL_OPTION); |
200 | } else if (command.equals(COMMAND_SET_TARGET_DIR)) { |
201 | performChooseTargetDir(); |
202 | } else if (command.equals(COMMAND_CLEAR_COMICS)) { |
203 | comicListModel.clear(); |
204 | } else if (command.equals(COMMAND_CONVERT)) { |
205 | setSelectionAndClose(JOptionPane.OK_OPTION); |
206 | } else if (command.equals(COMMAND_REMOVE_COMICS)) { |
207 | performRemoveComicsToConvert(); |
208 | } else { |
209 | assert false : "command=" + command; |
210 | } |
211 | } catch (Throwable error) { |
212 | jomicTools.showError(event, error); |
213 | } |
214 | } |
215 | |
216 | public void changedUpdate(DocumentEvent event) { |
217 | assert false : "assumption that targetDirField is the only source and is a PlainDocument must be restored: " |
218 | + event; |
219 | } |
220 | |
221 | public void contentsChanged(ListDataEvent event) { |
222 | updateEnabledButtons(); |
223 | } |
224 | |
225 | public void dispose() { |
226 | super.dispose(); |
227 | |
228 | uiTools.attemptToRemoveActionListener(convertButton, this); |
229 | uiTools.attemptToRemoveActionListener(cancelButton, this); |
230 | uiTools.attemptToRemoveActionListener(chooseTargetDirButton, this); |
231 | uiTools.attemptToRemoveActionListener(addComicsButton, this); |
232 | uiTools.attemptToRemoveActionListener(clearComicsButton, this); |
233 | uiTools.attemptToRemoveActionListener(removeComicsButton, this); |
234 | if (targetDirField != null) { |
235 | Document targetDirDocument = targetDirField.getDocument(); |
236 | |
237 | if (targetDirDocument != null) { |
238 | targetDirDocument.removeDocumentListener(this); |
239 | } |
240 | } |
241 | if (comicListModel != null) { |
242 | comicListModel.removeListDataListener(this); |
243 | } |
244 | if (comicFileList != null) { |
245 | comicFileList.removeListSelectionListener(this); |
246 | } |
247 | removeWindowListener(this); |
248 | } |
249 | |
250 | public void insertUpdate(DocumentEvent event) { |
251 | updateEnabledButtons(); |
252 | } |
253 | |
254 | public void intervalAdded(ListDataEvent e) { |
255 | // Do nothing. |
256 | } |
257 | |
258 | public void intervalRemoved(ListDataEvent e) { |
259 | updateEnabledButtons(); |
260 | } |
261 | |
262 | public void removeUpdate(DocumentEvent event) { |
263 | updateEnabledButtons(); |
264 | } |
265 | |
266 | public void valueChanged(ListSelectionEvent event) { |
267 | if (!event.getValueIsAdjusting()) { |
268 | updateEnabledButtons(); |
269 | } |
270 | } |
271 | |
272 | public void windowActivated(WindowEvent e) { |
273 | // Do nothing. |
274 | } |
275 | |
276 | public void windowClosed(WindowEvent e) { |
277 | // Do nothing. |
278 | } |
279 | |
280 | public void windowClosing(WindowEvent e) { |
281 | // Do nothing. |
282 | } |
283 | |
284 | public void windowDeactivated(WindowEvent e) { |
285 | // Do nothing. |
286 | } |
287 | |
288 | public void windowDeiconified(WindowEvent e) { |
289 | // Do nothing. |
290 | } |
291 | |
292 | public void windowIconified(WindowEvent e) { |
293 | // Do nothing. |
294 | } |
295 | |
296 | public void windowOpened(WindowEvent e) { |
297 | // Set to cancel in case dialog is closed without clicking any buttons. |
298 | selection = JOptionPane.CANCEL_OPTION; |
299 | } |
300 | |
301 | void addComicToConvert(File comicFile) { |
302 | assert comicFile != null; |
303 | synchronized (comicListModel) { |
304 | if (!comicListModel.contains(comicFile)) { |
305 | comicListModel.addElement(comicFile); |
306 | updateEnabledButtons(); |
307 | } |
308 | } |
309 | } |
310 | |
311 | private JFileChooser createAddComicsToConvertChooser() { |
312 | JFileChooser result = new SnapableJFileChooser( |
313 | settings, PropertyConstants.ADD_COMICS_TO_CONVERT_DIALOG_WINDOW); |
314 | String title = localeTools.getMessage("dialogs.addComicsToConvert.title"); |
315 | String addButtonText = localeTools.getMessage("dialogs.addComicsToConvert.add"); |
316 | |
317 | result.setApproveButtonText(addButtonText); |
318 | result.setDialogTitle(title); |
319 | result.setFileFilter(comicFileFilter); |
320 | result.setMultiSelectionEnabled(true); |
321 | result.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); |
322 | return result; |
323 | } |
324 | |
325 | private JButton createButton(String localeKey, String command) { |
326 | assert localeKey != null; |
327 | assert command != null; |
328 | JButton result = localeTools.createButton("dialogs.convert." + localeKey); |
329 | |
330 | result.setActionCommand(command); |
331 | result.addActionListener(this); |
332 | return result; |
333 | } |
334 | |
335 | private JPanel createButtonPanel() { |
336 | JPanel result = new JPanel(); |
337 | BoxLayout layout = new BoxLayout(result, BoxLayout.LINE_AXIS); |
338 | |
339 | result.setLayout(layout); |
340 | convertButton = createButton("convert", COMMAND_CONVERT); |
341 | cancelButton = localeTools.createCancelButton(); |
342 | cancelButton.setActionCommand(Commands.CANCEL); |
343 | cancelButton.addActionListener(this); |
344 | |
345 | result.add(Box.createHorizontalGlue()); |
346 | result.add(cancelButton); |
347 | result.add(uiTools.createRigidAreaBetweenButtons()); |
348 | result.add(convertButton); |
349 | return result; |
350 | } |
351 | |
352 | private JPanel createControlPanel() { |
353 | assert conversionBean != null; |
354 | JPanel result = new JPanel(new BorderLayout()); |
355 | JPanel filesAndConversionPane = new JPanel(new BorderLayout()); |
356 | JPanel filePane = createFilePanel(); |
357 | JPanel targetDirPane = createTargetDirPanel(); |
358 | |
359 | filesAndConversionPane.setBorder(null); |
360 | filesAndConversionPane.add(filePane, BorderLayout.CENTER); |
361 | filesAndConversionPane.add(conversionBean, BorderLayout.LINE_END); |
362 | |
363 | result.setBorder(null); |
364 | result.add(filesAndConversionPane, BorderLayout.CENTER); |
365 | result.add(targetDirPane, BorderLayout.PAGE_END); |
366 | return result; |
367 | } |
368 | |
369 | private JPanel createFilePanel() { |
370 | JPanel result = new JPanel(new BorderLayout()); |
371 | JPanel comicListButtonPane = new JPanel(); |
372 | BoxLayout comicListButtonLayout = new BoxLayout(comicListButtonPane, BoxLayout.LINE_AXIS); |
373 | |
374 | comicListButtonPane.setLayout(comicListButtonLayout); |
375 | comicListModel = new DefaultListModel(); |
376 | comicFileList = new JList(comicListModel); |
377 | comicFileList.setCellRenderer(new FileIconRenderer()); |
378 | comicListModel.addListDataListener(this); |
379 | comicFileList.addListSelectionListener(this); |
380 | addComicsButton = createButton("addComics", COMMAND_ADD_COMICS); |
381 | removeComicsButton = createButton("removeComics", COMMAND_REMOVE_COMICS); |
382 | clearComicsButton = createButton("clearComics", COMMAND_CLEAR_COMICS); |
383 | |
384 | comicListButtonPane.add(addComicsButton); |
385 | comicListButtonPane.add(uiTools.createRigidAreaBetweenButtons()); |
386 | comicListButtonPane.add(removeComicsButton); |
387 | comicListButtonPane.add(uiTools.createRigidAreaBetweenButtons()); |
388 | comicListButtonPane.add(clearComicsButton); |
389 | |
390 | result.add(new JScrollPane(comicFileList), BorderLayout.CENTER); |
391 | result.add(comicListButtonPane, BorderLayout.PAGE_END); |
392 | |
393 | String comicsToConvertText = localeTools.getMessage("dialogs.convert.comicsToConvert"); |
394 | Border comicsToConvertBorder = BorderFactory.createTitledBorder(null, comicsToConvertText); |
395 | |
396 | result.setBorder(comicsToConvertBorder); |
397 | |
398 | return result; |
399 | } |
400 | |
401 | private JFileChooser createTargetDirChooser() { |
402 | JFileChooser result = new SnapableJFileChooser( |
403 | settings, PropertyConstants.ADD_COMICS_TO_CONVERT_DIALOG_WINDOW); |
404 | String title = localeTools.getMessage("dialogs.setConversionTargetDir.title"); |
405 | String addButtonText = localeTools.getMessage("buttons.set"); |
406 | |
407 | result.setApproveButtonText(addButtonText); |
408 | result.setDialogTitle(title); |
409 | result.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
410 | return result; |
411 | } |
412 | |
413 | private JPanel createTargetDirPanel() { |
414 | JPanel result = new JPanel(); |
415 | JLabel targetDirLabel = new JLabel(localeTools.getMessage("dialogs.convert.targetDir")); |
416 | |
417 | targetDirField = new JTextField(TARGET_DIR_PREFERRED_COLUMNS); |
418 | targetDirField.getDocument().addDocumentListener(this); |
419 | |
420 | chooseTargetDirButton = localeTools.createButton("buttons.chooseFile"); |
421 | chooseTargetDirButton.setActionCommand(COMMAND_SET_TARGET_DIR); |
422 | chooseTargetDirButton.addActionListener(this); |
423 | |
424 | result.setBorder(null); |
425 | result.setLayout(new BoxLayout(result, BoxLayout.LINE_AXIS)); |
426 | result.add(targetDirLabel); |
427 | result.add(targetDirField); |
428 | result.add(chooseTargetDirButton); |
429 | return result; |
430 | } |
431 | |
432 | private void performAddComicsToConvert() { |
433 | int added = addComicsToConvertChooser.showDialog(this, null); |
434 | |
435 | if (added == JFileChooser.APPROVE_OPTION) { |
436 | File[] selectedComics = addComicsToConvertChooser.getSelectedFiles(); |
437 | |
438 | for (int i = 0; i < selectedComics.length; i += 1) { |
439 | File comicFile = selectedComics[i]; |
440 | |
441 | addComicToConvert(comicFile); |
442 | } |
443 | updateEnabledButtons(); |
444 | } |
445 | } |
446 | |
447 | private void performChooseTargetDir() { |
448 | int set = targetDirChooser.showDialog(this, null); |
449 | |
450 | if (set == JFileChooser.APPROVE_OPTION) { |
451 | File targetDir = targetDirChooser.getSelectedFile(); |
452 | |
453 | targetDirField.setText(targetDir.getAbsolutePath()); |
454 | updateEnabledButtons(); |
455 | } |
456 | } |
457 | |
458 | private void performRemoveComicsToConvert() { |
459 | int[] selectedIndices = comicFileList.getSelectedIndices(); |
460 | int selectedIndexCount = selectedIndices.length; |
461 | |
462 | assert selectedIndexCount > 0; |
463 | for (int i = 0; i < selectedIndexCount; i += 1) { |
464 | comicListModel.remove(selectedIndices[i]); |
465 | } |
466 | updateEnabledButtons(); |
467 | } |
468 | |
469 | private void updateEnabledButtons() { |
470 | boolean hasTargetDir = targetDirField.getDocument().getLength() > 0; |
471 | boolean hasComics = comicListModel.getSize() != 0; |
472 | |
473 | clearComicsButton.setEnabled(hasComics); |
474 | convertButton.setEnabled(hasComics && hasTargetDir); |
475 | removeComicsButton.setEnabled(comicFileList.getSelectedIndex() != -1); |
476 | } |
477 | } |