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.Component; |
19 | import java.awt.event.ActionEvent; |
20 | import java.awt.event.ActionListener; |
21 | import java.awt.event.WindowEvent; |
22 | import java.awt.event.WindowListener; |
23 | import java.io.File; |
24 | import java.io.IOException; |
25 | import java.util.LinkedList; |
26 | import java.util.List; |
27 | |
28 | import javax.help.HelpBroker; |
29 | import javax.swing.AbstractAction; |
30 | import javax.swing.Action; |
31 | import javax.swing.JFileChooser; |
32 | import javax.swing.SwingUtilities; |
33 | import javax.swing.WindowConstants; |
34 | |
35 | import net.roydesign.event.ApplicationEvent; |
36 | import net.roydesign.mac.MRJAdapter; |
37 | import net.sf.jomic.comic.ComicCache; |
38 | import net.sf.jomic.comic.ComicChooserFileFilter; |
39 | import net.sf.jomic.comic.ComicFileFilter; |
40 | import net.sf.jomic.common.JomicHelpTools; |
41 | import net.sf.jomic.common.JomicStartup; |
42 | import net.sf.jomic.common.JomicTools; |
43 | import net.sf.jomic.common.PropertyConstants; |
44 | import net.sf.jomic.common.Settings; |
45 | import net.sf.jomic.tools.LocaleTools; |
46 | import net.sf.jomic.tools.StringTools; |
47 | import net.sf.jomic.tools.UiTools; |
48 | |
49 | import org.apache.commons.logging.Log; |
50 | import org.apache.commons.logging.LogFactory; |
51 | |
52 | /** |
53 | * Jomic application that manages all open comics and processes comic independent commands like |
54 | * "about", "help", or "quit". |
55 | * |
56 | * @author Thomas Aglassinger |
57 | */ |
58 | public final class JomicApplication implements ActionListener, WindowListener |
59 | { |
60 | private static JomicApplication instance; |
61 | |
62 | private AboutFrame aboutFrame; |
63 | private ChangeBlurSettingsDialog changeBlurSettingsDialog; |
64 | private ComicFileFilter comicFileFilter; |
65 | private ComicChooserFileFilter comicFilter; |
66 | private ConvertDialog convertDialog; |
67 | private CreateComicDialog createComicDialog; |
68 | private FramelessJomicMenuBar framelessMenuBar; |
69 | private Runnable fullScreenViewerRunner; |
70 | private boolean hasFramelessMenuBar; |
71 | private JomicStartup jomicStartup; |
72 | private JomicTools jomicTools; |
73 | private LocaleTools localeTools; |
74 | private Log logger; |
75 | private JFileChooser openComicChooser; |
76 | private List openComicWindows; |
77 | private Settings settings; |
78 | private StringTools stringTools; |
79 | private SystemInfoFrame systemInfoFrame; |
80 | private UiTools uiTools; |
81 | |
82 | private JomicApplication() { |
83 | logger = LogFactory.getLog(JomicApplication.class); |
84 | jomicTools = JomicTools.instance(); |
85 | localeTools = LocaleTools.instance(); |
86 | stringTools = StringTools.instance(); |
87 | uiTools = UiTools.instance(); |
88 | jomicStartup = JomicStartup.instance(); |
89 | settings = Settings.instance(); |
90 | openComicWindows = new LinkedList(); |
91 | comicFileFilter = new ComicFileFilter(); |
92 | comicFilter = new ComicChooserFileFilter(); |
93 | |
94 | jomicTools.setFullScreenCancelabel(FullScreenViewer.instance()); |
95 | } |
96 | |
97 | public void setUpFramelessMenuBar() { |
98 | // Under Mac OS X, the following lines create a hidden frame with a |
99 | // menu bar where most items are disabled. This keeps Jomic running |
100 | // even when no comic is open. (Don't ask. This is a Mac OS UI |
101 | // convention.) |
102 | if (MRJAdapter.isSwingUsingScreenMenuBar()) { |
103 | String showMenuBarName = |
104 | PropertyConstants.SYSTEM_PROPERTY_PREFIX + PropertyConstants.SHOW_FRAMELESS_MENU_BAR; |
105 | |
106 | hasFramelessMenuBar = Boolean.getBoolean(showMenuBarName); |
107 | if (hasFramelessMenuBar) { |
108 | framelessMenuBar = new FramelessJomicMenuBar(); |
109 | framelessMenuBar.addActionListener(this); |
110 | MRJAdapter.setFramelessJMenuBar(framelessMenuBar); |
111 | } |
112 | } |
113 | } |
114 | |
115 | public Action getChangeBlurSettingsAction() { |
116 | return new ChangeBlurSettingsAction(); |
117 | } |
118 | |
119 | private JomicFrame getLastActiveWindow() { |
120 | JomicFrame result; |
121 | |
122 | synchronized (openComicWindows) { |
123 | int lastActiveWindowIndex = openComicWindows.size() - 1; |
124 | |
125 | assert lastActiveWindowIndex >= 0; |
126 | result = (JomicFrame) openComicWindows.get(lastActiveWindowIndex); |
127 | } |
128 | return result; |
129 | } |
130 | |
131 | public static synchronized JomicApplication instance() { |
132 | if (instance == null) { |
133 | instance = new JomicApplication(); |
134 | } |
135 | return instance; |
136 | } |
137 | |
138 | public void actionPerformed(ActionEvent event) { |
139 | ApplicationEvent appEvent = null; |
140 | String command = null; |
141 | |
142 | // Find out if we received an Mac OS ApplicationEvent or just a |
143 | // "normal" command. |
144 | if (event instanceof ApplicationEvent) { |
145 | appEvent = (ApplicationEvent) event; |
146 | if (logger.isInfoEnabled()) { |
147 | logger.info("handle application event: " + appEvent); |
148 | } |
149 | } else { |
150 | command = event.getActionCommand(); |
151 | if (logger.isInfoEnabled()) { |
152 | logger.info("handle command: " + command); |
153 | } |
154 | } |
155 | |
156 | try { |
157 | if (appEvent != null) { |
158 | performApplicationEvent(appEvent); |
159 | } else if (command.equals(Commands.ABOUT)) { |
160 | performAbout(); |
161 | } else if (command.equals(Commands.CLEAR_RECENT)) { |
162 | settings.clearRecentFiles(); |
163 | } else if (command.equals(Commands.CONVERT)) { |
164 | performConvert(); |
165 | } else if (command.equals(Commands.NEW_COMIC)) { |
166 | performCreate(); |
167 | } else if (command.equals(Commands.FULL_SCREEN)) { |
168 | performToggleFullScreen(); |
169 | } else if (command.equals(Commands.HELP)) { |
170 | performHelp(); |
171 | } else if (command.equals(Commands.OPEN)) { |
172 | synchronized (openComicWindows) { |
173 | int lastWindowIndex = openComicWindows.size(); |
174 | JomicFrame mostRecentlyActivatedWindow; |
175 | |
176 | if (lastWindowIndex == 0) { |
177 | mostRecentlyActivatedWindow = null; |
178 | } else { |
179 | mostRecentlyActivatedWindow = (JomicFrame) openComicWindows.get(lastWindowIndex - 1); |
180 | } |
181 | open(mostRecentlyActivatedWindow, null); |
182 | } |
183 | } else if (command.equals(Commands.OPEN_IN_NEW_WINDOW)) { |
184 | openInNewWindow(); |
185 | } else if (command.equals(Commands.OPEN_RECENT)) { |
186 | performOpenRecent(event); |
187 | } else if (command.equals(Commands.QUIT)) { |
188 | quit(); |
189 | } else if (command.equals(Commands.SYSTEM_INFO)) { |
190 | performSystemInfo(); |
191 | } else if (command.equals(Commands.TOGGLE_ADJUST_ARCHIVE_SUFFIX)) { |
192 | performToggleAdjustArchiveSuffix(); |
193 | } else if (command.equals(Commands.TOGGLE_OPEN_IN_FULL_SCREEN)) { |
194 | performToggleOpenInFullScreen(); |
195 | } else { |
196 | String message = localeTools.getMessage("errors.cannotProcessUnknownCommand", command); |
197 | |
198 | throw new IllegalArgumentException(message); |
199 | } |
200 | } catch (Throwable error) { |
201 | showInternalCommandErrorMessage(command, error); |
202 | } |
203 | } |
204 | |
205 | /** |
206 | * <p> |
207 | * |
208 | * Open in window <code>jomicFrameToOpenIn</code> the comic <code>comicToOpen</code>.</p> If |
209 | * <code>jomicFrameToOpenIn</code> is <code>null</code>, open a new window first. If <code>comicToOpen</code> |
210 | * is <code>null</code>, let the user choose one. |
211 | */ |
212 | public void open(JomicFrame jomicFrameToOpenIn, File comicToOpen) { |
213 | JomicFrame jomicFrame; |
214 | boolean useNewFrame = jomicFrameToOpenIn == null; |
215 | |
216 | if (useNewFrame) { |
217 | jomicFrame = createJomicFrame(); |
218 | } else { |
219 | jomicFrame = jomicFrameToOpenIn; |
220 | } |
221 | |
222 | File comicFile = comicToOpen; |
223 | |
224 | if (comicFile == null) { |
225 | prepareOpenDialog(); |
226 | |
227 | // Let the user select the file, and open it. |
228 | int selection = openComicChooser.showOpenDialog(jomicFrame); |
229 | |
230 | if (selection == JFileChooser.APPROVE_OPTION) { |
231 | comicFile = openComicChooser.getSelectedFile(); |
232 | } |
233 | } |
234 | |
235 | if (comicFile != null) { |
236 | jomicFrame.open(comicFile); |
237 | } else if (useNewFrame) { |
238 | boolean frameWasVisible = jomicFrame.isVisible(); |
239 | |
240 | // We just created a new window for this comic, but now do not need |
241 | // it because the user canceled the "Open comic" dialog. |
242 | if (!frameWasVisible) { |
243 | removeOpenWindow(jomicFrame); |
244 | } |
245 | jomicFrame.performClose(); |
246 | if (!frameWasVisible) { |
247 | ifAllWindowsClosedThenQuit(); |
248 | } |
249 | } |
250 | } |
251 | |
252 | /** |
253 | * Open dialog to select comic, then open it in new window. |
254 | */ |
255 | public void openInNewWindow() { |
256 | open(null, null); |
257 | } |
258 | |
259 | /** |
260 | * Open comic in new window. |
261 | */ |
262 | public void openInNewWindow(File comicFile) { |
263 | assert comicFile != null; |
264 | createJomicFrame().open(comicFile); |
265 | } |
266 | |
267 | /** |
268 | * Change view of current comic to full screen. |
269 | */ |
270 | public void performToggleFullScreen() { |
271 | FullScreenViewer fullScreen = FullScreenViewer.instance(); |
272 | JomicFrame controller = getLastActiveWindow(); |
273 | |
274 | if (fullScreen.isVisible()) { |
275 | fullScreen.performCancel(); |
276 | } else { |
277 | fullScreenViewerRunner = new FullScreenViewerRunner(controller); |
278 | |
279 | SwingUtilities.invokeLater(fullScreenViewerRunner); |
280 | } |
281 | } |
282 | |
283 | /** |
284 | * Cleanup, write settings and exit application. |
285 | * |
286 | * @see JomicStartup#exit(int) |
287 | */ |
288 | public void quit() { |
289 | ComicCache.instance().dispose(); |
290 | |
291 | if (logger.isInfoEnabled()) { |
292 | logger.info("write settings"); |
293 | } |
294 | |
295 | File settingsFile = settings.getSettingsFile(); |
296 | |
297 | try { |
298 | settings.write(settingsFile); |
299 | } catch (IOException error) { |
300 | logger.warn("cannot write settings to \"" + settingsFile + "\"", error); |
301 | } |
302 | if (framelessMenuBar != null) { |
303 | framelessMenuBar.removeActionListener(this); |
304 | } |
305 | jomicStartup.exit(0); |
306 | } |
307 | |
308 | public void windowActivated(WindowEvent event) { |
309 | assert event != null; |
310 | |
311 | synchronized (openComicWindows) { |
312 | // Move window to end of list |
313 | JomicFrame jomicFrame = (JomicFrame) event.getWindow(); |
314 | |
315 | removeOpenWindow(jomicFrame); |
316 | addOpenWindow(jomicFrame); |
317 | } |
318 | } |
319 | |
320 | public void windowClosed(WindowEvent event) { |
321 | // Do nothing. |
322 | } |
323 | |
324 | public void windowClosing(WindowEvent event) { |
325 | assert event != null; |
326 | |
327 | synchronized (openComicWindows) { |
328 | JomicFrame jomicFrame = (JomicFrame) event.getWindow(); |
329 | |
330 | if (logger.isInfoEnabled()) { |
331 | logger.info("closing comic frame: " + stringTools.sourced(jomicFrame.getTitle())); |
332 | } |
333 | removeOpenWindow(jomicFrame); |
334 | jomicFrame.removeWindowListener(this); |
335 | jomicFrame.dispose(); |
336 | settings.setComicWindow(jomicFrame); |
337 | |
338 | ifAllWindowsClosedThenQuit(); |
339 | } |
340 | } |
341 | |
342 | public void windowDeactivated(WindowEvent event) { |
343 | // Do nothing. |
344 | } |
345 | |
346 | public void windowDeiconified(WindowEvent event) { |
347 | // Do nothing. |
348 | } |
349 | |
350 | public void windowIconified(WindowEvent event) { |
351 | // Do nothing. |
352 | } |
353 | |
354 | public void windowOpened(WindowEvent event) { |
355 | // Do nothing. |
356 | } |
357 | |
358 | synchronized void prepareChangeBlurSettings() { |
359 | if (changeBlurSettingsDialog == null) { |
360 | changeBlurSettingsDialog = new ChangeBlurSettingsDialog(null); |
361 | settings.applyComponentAreaProperty(PropertyConstants.SET_BLUR_DIALOG_WINDOW, changeBlurSettingsDialog); |
362 | } |
363 | } |
364 | |
365 | synchronized void prepareConvertDialog() { |
366 | if (convertDialog == null) { |
367 | convertDialog = new ConvertDialog(); |
368 | convertDialog.pack(); |
369 | settings.applyComponentAreaProperty(PropertyConstants.CONVERT_DIALOG_WINDOW, convertDialog); |
370 | } |
371 | } |
372 | |
373 | synchronized void prepareCreateComicDialog() { |
374 | if (createComicDialog == null) { |
375 | createComicDialog = new CreateComicDialog(); |
376 | createComicDialog.pack(); |
377 | settings.applyComponentAreaProperty(PropertyConstants.NEW_COMIC_DIALOG_WINDOW, createComicDialog); |
378 | } |
379 | } |
380 | |
381 | /** |
382 | * Prepare <code>openComicChooser</code> by possibly creating it first, and setting the |
383 | * selected file name to the most recently opened file. |
384 | */ |
385 | void prepareOpenDialog() { |
386 | // Create the dialog if necessary. |
387 | if (openComicChooser == null) { |
388 | String title = localeTools.getMessage("dialogs.open.title"); |
389 | OpenComicFileChooserAccessory accessory = new OpenComicFileChooserAccessory(); |
390 | |
391 | openComicChooser = new SnapableJFileChooser(settings, PropertyConstants.OPEN_DIALOG_WINDOW); |
392 | openComicChooser.setDialogTitle(title); |
393 | openComicChooser.setFileFilter(comicFilter); |
394 | openComicChooser.setAccessory(accessory); |
395 | openComicChooser.addPropertyChangeListener(accessory); |
396 | } |
397 | |
398 | // Set the selected file to the most recent one. |
399 | File mostRecentFile = settings.getMostRecentFile(); |
400 | |
401 | if (mostRecentFile != null) { |
402 | String mostRecentName = mostRecentFile.getName(); |
403 | |
404 | if (comicFileFilter.accept(null, mostRecentName)) { |
405 | openComicChooser.setSelectedFile(mostRecentFile); |
406 | } |
407 | } |
408 | } |
409 | |
410 | private void addOpenWindow(JomicFrame openWindow) { |
411 | synchronized (openComicWindows) { |
412 | assert openWindow != null; |
413 | assert !openComicWindows.contains(openWindow); |
414 | openComicWindows.add(openWindow); |
415 | } |
416 | } |
417 | |
418 | private JomicFrame createJomicFrame() { |
419 | JomicFrame result; |
420 | |
421 | synchronized (openComicWindows) { |
422 | if (openComicWindows.size() > 0) { |
423 | result = (JomicFrame) openComicWindows.get(0); |
424 | } else { |
425 | result = new JomicFrame(); |
426 | result.addWindowListener(this); |
427 | result.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); |
428 | result.pack(); |
429 | settings.applyComicWindow(result); |
430 | if (logger.isInfoEnabled()) { |
431 | logger.info("create new comic window"); |
432 | } |
433 | addOpenWindow(result); |
434 | } |
435 | } |
436 | return result; |
437 | } |
438 | |
439 | private void ifAllWindowsClosedThenQuit() { |
440 | // If this was the last window, then quit (unless we are running |
441 | // under Mac OS and using a frameless menu). |
442 | boolean allWindowsClosed = (openComicWindows.size() == 0); |
443 | |
444 | if (allWindowsClosed && !hasFramelessMenuBar) { |
445 | SwingUtilities.invokeLater(new QuitRunner()); |
446 | } |
447 | } |
448 | |
449 | private synchronized void performAbout() { |
450 | if (aboutFrame == null) { |
451 | aboutFrame = new AboutFrame(); |
452 | aboutFrame.pack(); |
453 | } |
454 | aboutFrame.setVisible(true); |
455 | } |
456 | |
457 | /** |
458 | * Process Mac OS application event. |
459 | */ |
460 | private void performApplicationEvent(ApplicationEvent appEvent) { |
461 | int type = appEvent.getType(); |
462 | |
463 | if (type == ApplicationEvent.ABOUT) { |
464 | performAbout(); |
465 | } else if (type == ApplicationEvent.QUIT_APPLICATION) { |
466 | quit(); |
467 | } else { |
468 | logger.warn("ignoring unknown application event: " + appEvent); |
469 | } |
470 | } |
471 | |
472 | private void performConvert() { |
473 | prepareConvertDialog(); |
474 | convertDialog.setVisible(true); |
475 | settings.setComponentAreaProperty(PropertyConstants.CONVERT_DIALOG_WINDOW, convertDialog); |
476 | |
477 | int selection = convertDialog.getSelection(); |
478 | |
479 | if (selection == JFileChooser.APPROVE_OPTION) { |
480 | ConvertWorker convertWorker = new ConvertWorker( |
481 | convertDialog.getTargetDir(), |
482 | convertDialog.getSelectedFiles(), |
483 | convertDialog.getConversion()); |
484 | |
485 | convertWorker.start(); |
486 | } |
487 | } |
488 | |
489 | private void performCreate() { |
490 | prepareCreateComicDialog(); |
491 | createComicDialog.setVisible(true); |
492 | settings.setComponentAreaProperty(PropertyConstants.NEW_COMIC_DIALOG_WINDOW, createComicDialog); |
493 | |
494 | int selection = createComicDialog.getSelection(); |
495 | |
496 | if (selection == JFileChooser.APPROVE_OPTION) { |
497 | File targetDir = createComicDialog.getTargetDir(); |
498 | File sourceDir = createComicDialog.getSourceDir(); |
499 | boolean isCreateComicForEachFolder = createComicDialog.isCreateComicForEachFolder(); |
500 | boolean isOpenAfterwards = createComicDialog.isOpenAfterwards(); |
501 | CreateComicWorker createComicWorker = new CreateComicWorker(sourceDir, targetDir, |
502 | createComicDialog.getConversion(), isCreateComicForEachFolder, isOpenAfterwards); |
503 | |
504 | createComicWorker.start(); |
505 | } |
506 | } |
507 | |
508 | private void performHelp() { |
509 | JomicHelpTools helpTools = JomicHelpTools.instance(); |
510 | HelpBroker helpBroker = helpTools.getHelpBroker(); |
511 | |
512 | helpBroker.setDisplayed(true); |
513 | } |
514 | |
515 | private void performOpenRecent(ActionEvent event) { |
516 | OpenRecentFileEvent orfEvent = (OpenRecentFileEvent) event; |
517 | |
518 | openInNewWindow(orfEvent.getFile()); |
519 | } |
520 | |
521 | private synchronized void performSystemInfo() { |
522 | if (systemInfoFrame == null) { |
523 | systemInfoFrame = new SystemInfoFrame(); |
524 | systemInfoFrame.pack(); |
525 | uiTools.centerUp(systemInfoFrame); |
526 | } |
527 | systemInfoFrame.setVisible(true); |
528 | } |
529 | |
530 | |
531 | private void performToggleAdjustArchiveSuffix() { |
532 | boolean newValue = !settings.getAdjustArchiveSuffix(); |
533 | |
534 | settings.setAdjustArchiveSuffix(newValue); |
535 | } |
536 | |
537 | private void performToggleOpenInFullScreen() { |
538 | boolean newValue = !settings.getOpenInFullScreen(); |
539 | |
540 | settings.setOpenInFullScreen(newValue); |
541 | } |
542 | |
543 | private void removeOpenWindow(Component frame) { |
544 | synchronized (openComicWindows) { |
545 | assert frame != null : "frame to remove must not be null"; |
546 | assert openComicWindows.contains(frame) || MRJAdapter.isSwingUsingScreenMenuBar() : |
547 | "frame must be open: " + frame; |
548 | openComicWindows.remove(frame); |
549 | if (hasFramelessMenuBar) { |
550 | framelessMenuBar.updateFileOpenRecent(); |
551 | } |
552 | } |
553 | } |
554 | |
555 | private void showError(String key, Object option, Throwable error) { |
556 | jomicTools.showError(null, key, option, error); |
557 | } |
558 | |
559 | private void showInternalCommandErrorMessage(String command, Throwable error) { |
560 | showError("errors.cannotProcessInternalCommand", command, error); |
561 | } |
562 | |
563 | /** |
564 | * Action to open ChangeBlurSettingsDialog. |
565 | * |
566 | * @see ChangeBlurSettingsDialog |
567 | * @author Thomas Aglassinger |
568 | */ |
569 | private final class ChangeBlurSettingsAction extends AbstractAction |
570 | { |
571 | private ChangeBlurSettingsAction() { |
572 | super(localeTools.getMenuItemLabel(LocaleTools.MENU_VIEW, "changeBlurSettings")); |
573 | } |
574 | |
575 | public void actionPerformed(ActionEvent event) { |
576 | prepareChangeBlurSettings(); |
577 | changeBlurSettingsDialog.setVisible(true); |
578 | } |
579 | } |
580 | |
581 | /** |
582 | * Runnable to quit application. |
583 | */ |
584 | private final class QuitRunner implements Runnable |
585 | { |
586 | private QuitRunner() { |
587 | super(); |
588 | } |
589 | |
590 | public void run() { |
591 | quit(); |
592 | } |
593 | } |
594 | } |