make it subtree
[nikiroo-utils.git] / reader / ui / GuiReaderFrame.java
CommitLineData
16a81ef7 1package be.nikiroo.fanfix.reader.ui;
a6395bef 2
d3c84ac3 3import java.awt.BorderLayout;
4d205683 4import java.awt.Frame;
a6395bef
NR
5import java.awt.event.ActionEvent;
6import java.awt.event.ActionListener;
333f0e7b
NR
7import java.awt.event.KeyEvent;
8import java.awt.event.WindowEvent;
9import java.io.File;
a6395bef 10import java.io.IOException;
4d205683 11import java.util.HashMap;
a6395bef 12import java.util.List;
4d205683
NR
13import java.util.Map;
14import java.util.Map.Entry;
a6395bef 15
10d558d2 16import javax.swing.JFileChooser;
a6395bef 17import javax.swing.JFrame;
333f0e7b
NR
18import javax.swing.JMenu;
19import javax.swing.JMenuBar;
20import javax.swing.JMenuItem;
21import javax.swing.JOptionPane;
9843a5e5 22import javax.swing.JPopupMenu;
3b2b638f 23import javax.swing.SwingUtilities;
4d205683
NR
24import javax.swing.filechooser.FileFilter;
25import javax.swing.filechooser.FileNameExtensionFilter;
a6395bef
NR
26
27import be.nikiroo.fanfix.Instance;
e8eeea0a 28import be.nikiroo.fanfix.bundles.Config;
5bc9573b 29import be.nikiroo.fanfix.bundles.StringIdGui;
b4dc6ab5 30import be.nikiroo.fanfix.bundles.UiConfig;
a6395bef 31import be.nikiroo.fanfix.data.MetaData;
4d205683 32import be.nikiroo.fanfix.data.Story;
79a99506 33import be.nikiroo.fanfix.library.BasicLibrary;
0bb51c9c 34import be.nikiroo.fanfix.library.BasicLibrary.Status;
e42573a0 35import be.nikiroo.fanfix.library.LocalLibrary;
4d205683 36import be.nikiroo.fanfix.output.BasicOutput.OutputType;
16a81ef7 37import be.nikiroo.fanfix.reader.BasicReader;
14bb95fa 38import be.nikiroo.fanfix.reader.ui.GuiReaderMainPanel.FrameHelper;
b6b65795 39import be.nikiroo.fanfix.reader.ui.GuiReaderMainPanel.MetaDataRunnable;
99e1c5ec
NR
40import be.nikiroo.fanfix.searchable.BasicSearchable;
41import be.nikiroo.fanfix.supported.SupportType;
3b2b638f 42import be.nikiroo.utils.Progress;
39c3c689 43import be.nikiroo.utils.Version;
e8eeea0a 44import be.nikiroo.utils.ui.ConfigEditor;
a6395bef 45
4d205683 46/**
5dd985cf 47 * A {@link Frame} that will show a {@link GuiReaderBook} item for each
4d205683 48 * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a
5dd985cf
NR
49 * way to copy them to the {@link GuiReader} cache (
50 * {@link BasicReader#getLibrary()}), read them, delete them...
4d205683
NR
51 *
52 * @author niki
53 */
14bb95fa 54class GuiReaderFrame extends JFrame implements FrameHelper {
a6395bef 55 private static final long serialVersionUID = 1L;
5dd985cf 56 private GuiReader reader;
31e28683 57 private GuiReaderMainPanel mainPanel;
a6395bef 58
79a99506
NR
59 /**
60 * The different modification actions you can use on {@link Story} items.
61 *
62 * @author niki
63 */
64 private enum ChangeAction {
65 /** Change the source/type, that is, move it to another source. */
66 SOURCE,
67 /** Change its name. */
68 TITLE,
69 /** Change its author. */
70 AUTHOR
c8faa52a
NR
71 }
72
4d205683 73 /**
5dd985cf 74 * Create a new {@link GuiReaderFrame}.
4d205683
NR
75 *
76 * @param reader
5dd985cf
NR
77 * the associated {@link GuiReader} to forward some commands and
78 * access its {@link LocalLibrary}
4d205683
NR
79 * @param type
80 * the type of {@link Story} to load, or NULL for all types
81 */
5dd985cf 82 public GuiReaderFrame(GuiReader reader, String type) {
5bc9573b 83 super(getAppTitle(reader.getLibrary().getLibraryName()));
0b39fb9f 84
a6395bef
NR
85 this.reader = reader;
86
31e28683 87 mainPanel = new GuiReaderMainPanel(this, type);
4310bae9 88
14bb95fa
NR
89 setSize(800, 600);
90 setLayout(new BorderLayout());
5beb7cdc 91 add(mainPanel, BorderLayout.CENTER);
333f0e7b
NR
92 }
93
14bb95fa
NR
94 @Override
95 public JPopupMenu createBookPopup() {
0bb51c9c 96 Status status = reader.getLibrary().getStatus();
14bb95fa
NR
97 JPopupMenu popup = new JPopupMenu();
98 popup.add(createMenuItemOpenBook());
99 popup.addSeparator();
100 popup.add(createMenuItemExport());
0bb51c9c
NR
101 if (status.isWritable()) {
102 popup.add(createMenuItemMoveTo());
103 popup.add(createMenuItemSetCoverForSource());
104 popup.add(createMenuItemSetCoverForAuthor());
105 }
ca7d4e2f 106 popup.add(createMenuItemDownloadToCache());
14bb95fa 107 popup.add(createMenuItemClearCache());
0bb51c9c
NR
108 if (status.isWritable()) {
109 popup.add(createMenuItemRedownload());
110 popup.addSeparator();
111 popup.add(createMenuItemRename());
112 popup.add(createMenuItemSetAuthor());
113 popup.addSeparator();
114 popup.add(createMenuItemDelete());
115 }
14bb95fa
NR
116 popup.addSeparator();
117 popup.add(createMenuItemProperties());
118 return popup;
4310bae9
NR
119 }
120
14bb95fa 121 @Override
8590da19 122 public JPopupMenu createSourceAuthorPopup() {
14bb95fa
NR
123 JPopupMenu popup = new JPopupMenu();
124 popup.add(createMenuItemOpenBook());
125 return popup;
333f0e7b
NR
126 }
127
14bb95fa 128 @Override
0bb51c9c 129 public void createMenu(Status status) {
32224dda
N
130 invalidate();
131
14bb95fa 132 JMenuBar bar = new JMenuBar();
333f0e7b 133
5bc9573b 134 JMenu file = new JMenu(GuiReader.trans(StringIdGui.MENU_FILE));
10d558d2 135 file.setMnemonic(KeyEvent.VK_F);
333f0e7b 136
5bc9573b
NR
137 JMenuItem imprt = new JMenuItem(
138 GuiReader.trans(StringIdGui.MENU_FILE_IMPORT_URL),
139 KeyEvent.VK_U);
333f0e7b 140 imprt.addActionListener(new ActionListener() {
211f7ddb 141 @Override
333f0e7b 142 public void actionPerformed(ActionEvent e) {
31e28683 143 mainPanel.imprt(true);
10d558d2
NR
144 }
145 });
5bc9573b
NR
146 JMenuItem imprtF = new JMenuItem(
147 GuiReader.trans(StringIdGui.MENU_FILE_IMPORT_FILE),
148 KeyEvent.VK_F);
10d558d2 149 imprtF.addActionListener(new ActionListener() {
211f7ddb 150 @Override
10d558d2 151 public void actionPerformed(ActionEvent e) {
31e28683 152 mainPanel.imprt(false);
10d558d2
NR
153 }
154 });
5bc9573b
NR
155 JMenuItem exit = new JMenuItem(
156 GuiReader.trans(StringIdGui.MENU_FILE_EXIT), KeyEvent.VK_X);
10d558d2 157 exit.addActionListener(new ActionListener() {
211f7ddb 158 @Override
10d558d2 159 public void actionPerformed(ActionEvent e) {
5dd985cf
NR
160 GuiReaderFrame.this.dispatchEvent(new WindowEvent(
161 GuiReaderFrame.this, WindowEvent.WINDOW_CLOSING));
10d558d2
NR
162 }
163 });
3b2b638f 164
9843a5e5
NR
165 file.add(createMenuItemOpenBook());
166 file.add(createMenuItemExport());
0bb51c9c
NR
167 if (status.isWritable()) {
168 file.add(createMenuItemMoveTo());
169 file.addSeparator();
170 file.add(imprt);
171 file.add(imprtF);
172 file.addSeparator();
173 file.add(createMenuItemRename());
174 file.add(createMenuItemSetAuthor());
175 }
c8d48938 176 file.addSeparator();
5bc9573b
NR
177 file.add(createMenuItemProperties());
178 file.addSeparator();
10d558d2 179 file.add(exit);
3b2b638f 180
10d558d2
NR
181 bar.add(file);
182
5bc9573b 183 JMenu edit = new JMenu(GuiReader.trans(StringIdGui.MENU_EDIT));
10d558d2
NR
184 edit.setMnemonic(KeyEvent.VK_E);
185
5bc9573b
NR
186 edit.add(createMenuItemSetCoverForSource());
187 edit.add(createMenuItemSetCoverForAuthor());
ca7d4e2f 188 edit.add(createMenuItemDownloadToCache());
22848428
NR
189 edit.add(createMenuItemClearCache());
190 edit.add(createMenuItemRedownload());
9843a5e5
NR
191 edit.addSeparator();
192 edit.add(createMenuItemDelete());
193
194 bar.add(edit);
195
99e1c5ec
NR
196 JMenu search = new JMenu(GuiReader.trans(StringIdGui.MENU_SEARCH));
197 search.setMnemonic(KeyEvent.VK_H);
0b39fb9f 198 for (final SupportType type : SupportType.values()) {
99e1c5ec
NR
199 BasicSearchable searchable = BasicSearchable.getSearchable(type);
200 if (searchable != null) {
201 JMenuItem searchItem = new JMenuItem(type.getSourceName());
202 searchItem.addActionListener(new ActionListener() {
203 @Override
204 public void actionPerformed(ActionEvent e) {
cde97f46 205 reader.search(type, null, 1, 0, false);
99e1c5ec
NR
206 }
207 });
208 search.add(searchItem);
209 }
210 }
0b39fb9f 211
c5097604 212 bar.add(search);
0b39fb9f 213
5bc9573b 214 JMenu view = new JMenu(GuiReader.trans(StringIdGui.MENU_VIEW));
793f1071 215 view.setMnemonic(KeyEvent.VK_V);
5bc9573b
NR
216 JMenuItem vauthors = new JMenuItem(
217 GuiReader.trans(StringIdGui.MENU_VIEW_AUTHOR));
793f1071
NR
218 vauthors.setMnemonic(KeyEvent.VK_A);
219 vauthors.addActionListener(new ActionListener() {
211f7ddb 220 @Override
793f1071 221 public void actionPerformed(ActionEvent e) {
31e28683
NR
222 mainPanel.setWords(false);
223 mainPanel.refreshBooks();
793f1071
NR
224 }
225 });
226 view.add(vauthors);
5bc9573b
NR
227 JMenuItem vwords = new JMenuItem(
228 GuiReader.trans(StringIdGui.MENU_VIEW_WCOUNT));
793f1071
NR
229 vwords.setMnemonic(KeyEvent.VK_W);
230 vwords.addActionListener(new ActionListener() {
211f7ddb 231 @Override
793f1071 232 public void actionPerformed(ActionEvent e) {
31e28683
NR
233 mainPanel.setWords(true);
234 mainPanel.refreshBooks();
793f1071
NR
235 }
236 });
237 view.add(vwords);
238 bar.add(view);
239
97b36d32 240 Map<String, List<String>> groupedSources = new HashMap<String, List<String>>();
0bb51c9c
NR
241 if (status.isReady()) {
242 try {
243 groupedSources = reader.getLibrary().getSourcesGrouped();
244 } catch (IOException e) {
245 error(e.getLocalizedMessage(), "IOException", e);
246 }
97b36d32 247 }
5bc9573b 248 JMenu sources = new JMenu(GuiReader.trans(StringIdGui.MENU_SOURCES));
4310bae9 249 sources.setMnemonic(KeyEvent.VK_S);
97b36d32
NR
250 populateMenuSA(sources, groupedSources, true);
251 bar.add(sources);
9843a5e5 252
97b36d32 253 Map<String, List<String>> goupedAuthors = new HashMap<String, List<String>>();
0bb51c9c
NR
254 if (status.isReady()) {
255 try {
256 goupedAuthors = reader.getLibrary().getAuthorsGrouped();
257 } catch (IOException e) {
258 error(e.getLocalizedMessage(), "IOException", e);
259 }
e6249b0f 260 }
5bc9573b 261 JMenu authors = new JMenu(GuiReader.trans(StringIdGui.MENU_AUTHORS));
97b36d32
NR
262 authors.setMnemonic(KeyEvent.VK_A);
263 populateMenuSA(authors, goupedAuthors, false);
264 bar.add(authors);
e6249b0f 265
5bc9573b 266 JMenu options = new JMenu(GuiReader.trans(StringIdGui.MENU_OPTIONS));
97b36d32
NR
267 options.setMnemonic(KeyEvent.VK_O);
268 options.add(createMenuItemConfig());
269 options.add(createMenuItemUiConfig());
270 bar.add(options);
c1b93db3 271
14bb95fa 272 setJMenuBar(bar);
97b36d32
NR
273 }
274
275 // "" = [unknown]
276 private void populateMenuSA(JMenu menu,
277 Map<String, List<String>> groupedValues, boolean type) {
278
5bc9573b 279 // "All" and "Listing" special items first
c428e675
NR
280 JMenuItem item = new JMenuItem(
281 GuiReader.trans(StringIdGui.MENU_XXX_ALL_GROUPED));
97b36d32
NR
282 item.addActionListener(getActionOpenList(type, false));
283 menu.add(item);
5bc9573b 284 item = new JMenuItem(GuiReader.trans(StringIdGui.MENU_XXX_ALL_LISTING));
97b36d32
NR
285 item.addActionListener(getActionOpenList(type, true));
286 menu.add(item);
c428e675 287
97b36d32
NR
288 menu.addSeparator();
289
290 for (final String value : groupedValues.keySet()) {
291 List<String> list = groupedValues.get(value);
292 if (type && list.size() == 1 && list.get(0).isEmpty()) {
293 // leaf item source/type
c428e675
NR
294 item = new JMenuItem(
295 value.isEmpty() ? GuiReader
296 .trans(StringIdGui.MENU_AUTHORS_UNKNOWN)
297 : value);
97b36d32
NR
298 item.addActionListener(getActionOpen(value, type));
299 menu.add(item);
c1b93db3 300 } else {
97b36d32
NR
301 JMenu dir;
302 if (!type && groupedValues.size() == 1) {
303 // only one group of authors
304 dir = menu;
305 } else {
c428e675
NR
306 dir = new JMenu(
307 value.isEmpty() ? GuiReader
308 .trans(StringIdGui.MENU_AUTHORS_UNKNOWN)
309 : value);
97b36d32
NR
310 }
311
c1b93db3
NR
312 for (String sub : list) {
313 // " " instead of "" for the visual height
116904b8
NR
314 String itemName = sub;
315 if (itemName.isEmpty()) {
c428e675
NR
316 itemName = type ? " " : GuiReader
317 .trans(StringIdGui.MENU_AUTHORS_UNKNOWN);
116904b8 318 }
97b36d32 319
116904b8 320 String actualValue = value;
97b36d32
NR
321 if (type) {
322 if (!sub.isEmpty()) {
323 actualValue += "/" + sub;
324 }
325 } else {
326 actualValue = sub;
c1b93db3 327 }
9843a5e5 328
c1b93db3 329 item = new JMenuItem(itemName);
97b36d32 330 item.addActionListener(getActionOpen(actualValue, type));
c1b93db3
NR
331 dir.add(item);
332 }
5f42f329 333
97b36d32
NR
334 if (menu != dir) {
335 menu.add(dir);
336 }
5f42f329 337 }
4310bae9 338 }
9843a5e5
NR
339 }
340
c1b93db3
NR
341 /**
342 * Return an {@link ActionListener} that will set the given source (type) as
343 * the selected/displayed one.
344 *
345 * @param type
97b36d32 346 * the type (source) to select, cannot be NULL
c1b93db3
NR
347 *
348 * @return the {@link ActionListener}
349 */
97b36d32 350 private ActionListener getActionOpen(final String source, final boolean type) {
c1b93db3
NR
351 return new ActionListener() {
352 @Override
353 public void actionPerformed(ActionEvent e) {
31e28683
NR
354 mainPanel.removeBookPanes();
355 mainPanel.addBookPane(source, type);
356 mainPanel.refreshBooks();
c1b93db3
NR
357 }
358 };
359 }
360
97b36d32
NR
361 private ActionListener getActionOpenList(final boolean type,
362 final boolean listMode) {
363 return new ActionListener() {
364 @Override
0bb51c9c 365 public void actionPerformed(ActionEvent ae) {
31e28683 366 mainPanel.removeBookPanes();
0bb51c9c
NR
367 try {
368 mainPanel.addBookPane(type, listMode);
369 } catch (IOException e) {
370 error(e.getLocalizedMessage(), "IOException", e);
371 }
31e28683 372 mainPanel.refreshBooks();
5f42f329 373 }
97b36d32 374 };
5f42f329
NR
375 }
376
e8eeea0a
NR
377 /**
378 * Create the Fanfix Configuration menu item.
379 *
380 * @return the item
381 */
382 private JMenuItem createMenuItemConfig() {
5bc9573b 383 final String title = GuiReader.trans(StringIdGui.TITLE_CONFIG);
e8eeea0a
NR
384 JMenuItem item = new JMenuItem(title);
385 item.setMnemonic(KeyEvent.VK_F);
386
387 item.addActionListener(new ActionListener() {
211f7ddb 388 @Override
e8eeea0a
NR
389 public void actionPerformed(ActionEvent e) {
390 ConfigEditor<Config> ed = new ConfigEditor<Config>(
c428e675
NR
391 Config.class, Instance.getConfig(), GuiReader
392 .trans(StringIdGui.SUBTITLE_CONFIG));
e8eeea0a
NR
393 JFrame frame = new JFrame(title);
394 frame.add(ed);
459b4c28 395 frame.setSize(850, 600);
e8eeea0a
NR
396 frame.setVisible(true);
397 }
398 });
399
400 return item;
401 }
402
403 /**
404 * Create the UI Configuration menu item.
405 *
406 * @return the item
407 */
408 private JMenuItem createMenuItemUiConfig() {
5bc9573b 409 final String title = GuiReader.trans(StringIdGui.TITLE_CONFIG_UI);
e8eeea0a
NR
410 JMenuItem item = new JMenuItem(title);
411 item.setMnemonic(KeyEvent.VK_U);
412
413 item.addActionListener(new ActionListener() {
211f7ddb 414 @Override
e8eeea0a
NR
415 public void actionPerformed(ActionEvent e) {
416 ConfigEditor<UiConfig> ed = new ConfigEditor<UiConfig>(
c428e675
NR
417 UiConfig.class, Instance.getUiConfig(), GuiReader
418 .trans(StringIdGui.SUBTITLE_CONFIG_UI));
e8eeea0a
NR
419 JFrame frame = new JFrame(title);
420 frame.add(ed);
421 frame.setSize(800, 600);
422 frame.setVisible(true);
423 }
424 });
425
426 return item;
427 }
428
4d205683
NR
429 /**
430 * Create the export menu item.
431 *
432 * @return the item
433 */
9843a5e5 434 private JMenuItem createMenuItemExport() {
4d205683
NR
435 final JFileChooser fc = new JFileChooser();
436 fc.setAcceptAllFileFilterUsed(false);
437
c428e675
NR
438 // Add the "ALL" filters first, then the others
439 final Map<FileFilter, OutputType> otherFilters = new HashMap<FileFilter, OutputType>();
4d205683
NR
440 for (OutputType type : OutputType.values()) {
441 String ext = type.getDefaultExtension(false);
442 String desc = type.getDesc(false);
b2612f9d 443
4d205683 444 if (ext == null || ext.isEmpty()) {
c428e675 445 fc.addChoosableFileFilter(createAllFilter(desc));
4d205683 446 } else {
c428e675 447 otherFilters.put(new FileNameExtensionFilter(desc, ext), type);
4d205683
NR
448 }
449 }
450
c428e675
NR
451 for (Entry<FileFilter, OutputType> entry : otherFilters.entrySet()) {
452 fc.addChoosableFileFilter(entry.getKey());
4d205683
NR
453 }
454 //
9843a5e5 455
c428e675
NR
456 JMenuItem export = new JMenuItem(
457 GuiReader.trans(StringIdGui.MENU_FILE_EXPORT), KeyEvent.VK_S);
10d558d2 458 export.addActionListener(new ActionListener() {
211f7ddb 459 @Override
10d558d2 460 public void actionPerformed(ActionEvent e) {
31e28683 461 final GuiReaderBook selectedBook = mainPanel.getSelectedBook();
4d205683 462 if (selectedBook != null) {
c428e675
NR
463 fc.showDialog(GuiReaderFrame.this,
464 GuiReader.trans(StringIdGui.TITLE_SAVE));
b2612f9d 465 if (fc.getSelectedFile() != null) {
0b39fb9f
NR
466 final OutputType type = otherFilters.get(fc
467 .getFileFilter());
b2612f9d
NR
468 final String path = fc.getSelectedFile()
469 .getAbsolutePath()
470 + type.getDefaultExtension(false);
471 final Progress pg = new Progress();
2d56db73 472 mainPanel.outOfUi(pg, false, new Runnable() {
211f7ddb 473 @Override
b2612f9d
NR
474 public void run() {
475 try {
e42573a0 476 reader.getLibrary().export(
79a99506
NR
477 selectedBook.getInfo().getMeta()
478 .getLuid(), type, path, pg);
b2612f9d 479 } catch (IOException e) {
62c63b07 480 Instance.getTraceHandler().error(e);
b2612f9d 481 }
4d205683 482 }
b2612f9d
NR
483 });
484 }
4d205683 485 }
10d558d2
NR
486 }
487 });
488
9843a5e5
NR
489 return export;
490 }
491
edd46289
NR
492 /**
493 * Create a {@link FileFilter} that accepts all files and return the given
494 * description.
495 *
496 * @param desc
497 * the description
498 *
499 * @return the filter
500 */
4d205683
NR
501 private FileFilter createAllFilter(final String desc) {
502 return new FileFilter() {
503 @Override
504 public String getDescription() {
505 return desc;
506 }
507
508 @Override
509 public boolean accept(File f) {
510 return true;
511 }
512 };
513 }
514
515 /**
516 * Create the refresh (delete cache) menu item.
517 *
518 * @return the item
519 */
22848428 520 private JMenuItem createMenuItemClearCache() {
c428e675
NR
521 JMenuItem refresh = new JMenuItem(
522 GuiReader.trans(StringIdGui.MENU_EDIT_CLEAR_CACHE),
523 KeyEvent.VK_C);
10d558d2 524 refresh.addActionListener(new ActionListener() {
211f7ddb 525 @Override
10d558d2 526 public void actionPerformed(ActionEvent e) {
31e28683 527 final GuiReaderBook selectedBook = mainPanel.getSelectedBook();
10d558d2 528 if (selectedBook != null) {
2d56db73 529 mainPanel.outOfUi(null, false, new Runnable() {
211f7ddb 530 @Override
10d558d2 531 public void run() {
79a99506
NR
532 reader.clearLocalReaderCache(selectedBook.getInfo()
533 .getMeta().getLuid());
10d558d2 534 selectedBook.setCached(false);
df6e2d88 535 GuiReaderCoverImager.clearIcon(selectedBook
79a99506 536 .getInfo());
3b2b638f 537 SwingUtilities.invokeLater(new Runnable() {
211f7ddb 538 @Override
3b2b638f 539 public void run() {
10d558d2 540 selectedBook.repaint();
3b2b638f
NR
541 }
542 });
543 }
544 });
333f0e7b
NR
545 }
546 }
547 });
10d558d2 548
9843a5e5
NR
549 return refresh;
550 }
551
70c9b112 552 /**
c8d48938 553 * Create the "move to" menu item.
70c9b112
NR
554 *
555 * @return the item
556 */
0bb51c9c 557 private JMenuItem createMenuItemMoveTo() {
c428e675
NR
558 JMenu changeTo = new JMenu(
559 GuiReader.trans(StringIdGui.MENU_FILE_MOVE_TO));
c8d48938 560 changeTo.setMnemonic(KeyEvent.VK_M);
70c9b112 561
c1b93db3 562 Map<String, List<String>> groupedSources = new HashMap<String, List<String>>();
0bb51c9c 563 try {
c1b93db3 564 groupedSources = reader.getLibrary().getSourcesGrouped();
0bb51c9c
NR
565 } catch (IOException e) {
566 error(e.getLocalizedMessage(), "IOException", e);
e6249b0f 567 }
70c9b112 568
c428e675
NR
569 JMenuItem item = new JMenuItem(
570 GuiReader.trans(StringIdGui.MENU_FILE_MOVE_TO_NEW_TYPE));
79a99506 571 item.addActionListener(createMoveAction(ChangeAction.SOURCE, null));
c1b93db3
NR
572 changeTo.add(item);
573 changeTo.addSeparator();
70c9b112 574
c1b93db3
NR
575 for (final String type : groupedSources.keySet()) {
576 List<String> list = groupedSources.get(type);
577 if (list.size() == 1 && list.get(0).isEmpty()) {
578 item = new JMenuItem(type);
79a99506
NR
579 item.addActionListener(createMoveAction(ChangeAction.SOURCE,
580 type));
c1b93db3
NR
581 changeTo.add(item);
582 } else {
583 JMenu dir = new JMenu(type);
584 for (String sub : list) {
585 // " " instead of "" for the visual height
586 String itemName = sub.isEmpty() ? " " : sub;
587 String actualType = type;
588 if (!sub.isEmpty()) {
589 actualType += "/" + sub;
590 }
70c9b112 591
c1b93db3 592 item = new JMenuItem(itemName);
79a99506
NR
593 item.addActionListener(createMoveAction(
594 ChangeAction.SOURCE, actualType));
c1b93db3
NR
595 dir.add(item);
596 }
597 changeTo.add(dir);
c8d48938
NR
598 }
599 }
211f7ddb 600
c8d48938
NR
601 return changeTo;
602 }
70c9b112 603
c8d48938
NR
604 /**
605 * Create the "set author" menu item.
606 *
c8d48938
NR
607 * @return the item
608 */
0bb51c9c 609 private JMenuItem createMenuItemSetAuthor() {
c428e675
NR
610 JMenu changeTo = new JMenu(
611 GuiReader.trans(StringIdGui.MENU_FILE_SET_AUTHOR));
c8d48938 612 changeTo.setMnemonic(KeyEvent.VK_A);
70c9b112 613
c8d48938 614 // New author
c428e675
NR
615 JMenuItem newItem = new JMenuItem(
616 GuiReader.trans(StringIdGui.MENU_FILE_MOVE_TO_NEW_AUTHOR));
c8d48938
NR
617 changeTo.add(newItem);
618 changeTo.addSeparator();
79a99506 619 newItem.addActionListener(createMoveAction(ChangeAction.AUTHOR, null));
70c9b112 620
c8d48938 621 // Existing authors
0bb51c9c
NR
622 Map<String, List<String>> groupedAuthors;
623
624 try {
625 groupedAuthors = reader.getLibrary().getAuthorsGrouped();
626 } catch (IOException e) {
627 error(e.getLocalizedMessage(), "IOException", e);
628 groupedAuthors = new HashMap<String, List<String>>();
629
630 }
631
632 if (groupedAuthors.size() > 1) {
633 for (String key : groupedAuthors.keySet()) {
634 JMenu group = new JMenu(key);
635 for (String value : groupedAuthors.get(key)) {
116904b8 636 JMenuItem item = new JMenuItem(
c428e675
NR
637 value.isEmpty() ? GuiReader
638 .trans(StringIdGui.MENU_AUTHORS_UNKNOWN)
639 : value);
79a99506
NR
640 item.addActionListener(createMoveAction(
641 ChangeAction.AUTHOR, value));
0bb51c9c 642 group.add(item);
c8d48938 643 }
0bb51c9c
NR
644 changeTo.add(group);
645 }
646 } else if (groupedAuthors.size() == 1) {
647 for (String value : groupedAuthors.values().iterator().next()) {
648 JMenuItem item = new JMenuItem(
649 value.isEmpty() ? GuiReader
650 .trans(StringIdGui.MENU_AUTHORS_UNKNOWN)
651 : value);
652 item.addActionListener(createMoveAction(ChangeAction.AUTHOR,
653 value));
654 changeTo.add(item);
c8d48938 655 }
70c9b112
NR
656 }
657
c8d48938
NR
658 return changeTo;
659 }
660
661 /**
662 * Create the "rename" menu item.
663 *
c8d48938
NR
664 * @return the item
665 */
0bb51c9c 666 private JMenuItem createMenuItemRename() {
c428e675
NR
667 JMenuItem changeTo = new JMenuItem(
668 GuiReader.trans(StringIdGui.MENU_FILE_RENAME));
c8d48938 669 changeTo.setMnemonic(KeyEvent.VK_R);
79a99506 670 changeTo.addActionListener(createMoveAction(ChangeAction.TITLE, null));
c8d48938
NR
671 return changeTo;
672 }
673
79a99506 674 private ActionListener createMoveAction(final ChangeAction what,
14bb95fa 675 final String type) {
c8d48938
NR
676 return new ActionListener() {
677 @Override
678 public void actionPerformed(ActionEvent e) {
31e28683 679 final GuiReaderBook selectedBook = mainPanel.getSelectedBook();
c8d48938 680 if (selectedBook != null) {
c349fd48
NR
681 boolean refreshRequired = false;
682
683 if (what == ChangeAction.SOURCE) {
684 refreshRequired = mainPanel.getCurrentType();
685 } else if (what == ChangeAction.TITLE) {
686 refreshRequired = false;
687 } else if (what == ChangeAction.AUTHOR) {
688 refreshRequired = !mainPanel.getCurrentType();
689 }
690
c8d48938
NR
691 String changeTo = type;
692 if (type == null) {
79a99506 693 MetaData meta = selectedBook.getInfo().getMeta();
c8d48938 694 String init = "";
79a99506
NR
695 if (what == ChangeAction.SOURCE) {
696 init = meta.getSource();
697 } else if (what == ChangeAction.TITLE) {
698 init = meta.getTitle();
699 } else if (what == ChangeAction.AUTHOR) {
700 init = meta.getAuthor();
c8d48938
NR
701 }
702
703 Object rep = JOptionPane.showInputDialog(
c428e675
NR
704 GuiReaderFrame.this,
705 GuiReader.trans(StringIdGui.SUBTITLE_MOVE_TO),
706 GuiReader.trans(StringIdGui.TITLE_MOVE_TO),
707 JOptionPane.QUESTION_MESSAGE, null, null, init);
c8d48938
NR
708
709 if (rep == null) {
710 return;
711 }
712
713 changeTo = rep.toString();
714 }
715
716 final String fChangeTo = changeTo;
c349fd48 717 mainPanel.outOfUi(null, refreshRequired, new Runnable() {
c8d48938
NR
718 @Override
719 public void run() {
79a99506
NR
720 String luid = selectedBook.getInfo().getMeta()
721 .getLuid();
722 if (what == ChangeAction.SOURCE) {
723 reader.changeSource(luid, fChangeTo);
724 } else if (what == ChangeAction.TITLE) {
725 reader.changeTitle(luid, fChangeTo);
726 } else if (what == ChangeAction.AUTHOR) {
727 reader.changeAuthor(luid, fChangeTo);
c8d48938
NR
728 }
729
c349fd48 730 mainPanel.getSelectedBook().repaint();
31e28683 731 mainPanel.unsetSelectedBook();
c8d48938
NR
732
733 SwingUtilities.invokeLater(new Runnable() {
734 @Override
735 public void run() {
0bb51c9c 736 createMenu(reader.getLibrary().getStatus());
c8d48938
NR
737 }
738 });
739 }
740 });
741 }
742 }
743 };
70c9b112
NR
744 }
745
22848428 746 /**
79a99506 747 * Create the re-download (then delete original) menu item.
22848428
NR
748 *
749 * @return the item
750 */
751 private JMenuItem createMenuItemRedownload() {
c428e675
NR
752 JMenuItem refresh = new JMenuItem(
753 GuiReader.trans(StringIdGui.MENU_EDIT_REDOWNLOAD),
754 KeyEvent.VK_R);
22848428 755 refresh.addActionListener(new ActionListener() {
211f7ddb 756 @Override
22848428 757 public void actionPerformed(ActionEvent e) {
31e28683 758 final GuiReaderBook selectedBook = mainPanel.getSelectedBook();
22848428 759 if (selectedBook != null) {
79a99506 760 final MetaData meta = selectedBook.getInfo().getMeta();
b6b65795 761 mainPanel.imprt(meta.getUrl(), new MetaDataRunnable() {
0b39fb9f 762 @Override
b6b65795 763 public void run(MetaData newMeta) {
0b39fb9f
NR
764 if (!newMeta.getSource().equals(meta.getSource())) {
765 reader.changeSource(newMeta.getLuid(),
766 meta.getSource());
767 }
768 }
769 }, GuiReader.trans(StringIdGui.PROGRESS_CHANGE_SOURCE));
22848428
NR
770 }
771 }
772 });
773
774 return refresh;
775 }
ca7d4e2f
NR
776
777 /**
778 * Create the download to cache menu item.
779 *
780 * @return the item
781 */
782 private JMenuItem createMenuItemDownloadToCache() {
783 JMenuItem refresh = new JMenuItem(
784 GuiReader.trans(StringIdGui.MENU_EDIT_DOWNLOAD_TO_CACHE),
785 KeyEvent.VK_T);
786 refresh.addActionListener(new ActionListener() {
787 @Override
788 public void actionPerformed(ActionEvent e) {
789 final GuiReaderBook selectedBook = mainPanel.getSelectedBook();
790 if (selectedBook != null) {
791 mainPanel.prefetchBook(selectedBook);
792 }
793 }
794 });
795
796 return refresh;
797 }
798
22848428 799
4d205683
NR
800 /**
801 * Create the delete menu item.
802 *
803 * @return the item
804 */
9843a5e5 805 private JMenuItem createMenuItemDelete() {
c428e675
NR
806 JMenuItem delete = new JMenuItem(
807 GuiReader.trans(StringIdGui.MENU_EDIT_DELETE), KeyEvent.VK_D);
10d558d2 808 delete.addActionListener(new ActionListener() {
211f7ddb 809 @Override
10d558d2 810 public void actionPerformed(ActionEvent e) {
31e28683 811 final GuiReaderBook selectedBook = mainPanel.getSelectedBook();
c349fd48
NR
812 if (selectedBook != null
813 && selectedBook.getInfo().getMeta() != null) {
814
815 final MetaData meta = selectedBook.getInfo().getMeta();
816 int rep = JOptionPane.showConfirmDialog(
817 GuiReaderFrame.this,
c428e675
NR
818 GuiReader.trans(StringIdGui.SUBTITLE_DELETE,
819 meta.getLuid(), meta.getTitle()),
820 GuiReader.trans(StringIdGui.TITLE_DELETE),
c349fd48
NR
821 JOptionPane.OK_CANCEL_OPTION);
822
823 if (rep == JOptionPane.OK_OPTION) {
824 mainPanel.outOfUi(null, true, new Runnable() {
825 @Override
826 public void run() {
827 reader.delete(meta.getLuid());
828 mainPanel.unsetSelectedBook();
829 }
830 });
831 }
10d558d2
NR
832 }
833 }
834 });
835
9843a5e5
NR
836 return delete;
837 }
10d558d2 838
df6e2d88
NR
839 /**
840 * Create the properties menu item.
841 *
842 * @return the item
843 */
844 private JMenuItem createMenuItemProperties() {
c428e675
NR
845 JMenuItem delete = new JMenuItem(
846 GuiReader.trans(StringIdGui.MENU_FILE_PROPERTIES),
847 KeyEvent.VK_P);
df6e2d88
NR
848 delete.addActionListener(new ActionListener() {
849 @Override
850 public void actionPerformed(ActionEvent e) {
31e28683 851 final GuiReaderBook selectedBook = mainPanel.getSelectedBook();
df6e2d88 852 if (selectedBook != null) {
2d56db73 853 mainPanel.outOfUi(null, false, new Runnable() {
df6e2d88
NR
854 @Override
855 public void run() {
5beb7cdc
NR
856 new GuiReaderPropertiesFrame(reader.getLibrary(),
857 selectedBook.getInfo().getMeta())
858 .setVisible(true);
df6e2d88
NR
859 }
860 });
861 }
862 }
863 });
864
865 return delete;
866 }
867
4d205683 868 /**
32224dda 869 * Create the open menu item for a book, a source/type or an author.
4d205683
NR
870 *
871 * @return the item
872 */
14bb95fa 873 public JMenuItem createMenuItemOpenBook() {
c428e675
NR
874 JMenuItem open = new JMenuItem(
875 GuiReader.trans(StringIdGui.MENU_FILE_OPEN), KeyEvent.VK_O);
9843a5e5 876 open.addActionListener(new ActionListener() {
211f7ddb 877 @Override
9843a5e5 878 public void actionPerformed(ActionEvent e) {
31e28683 879 final GuiReaderBook selectedBook = mainPanel.getSelectedBook();
9843a5e5 880 if (selectedBook != null) {
8590da19 881 if (selectedBook.getInfo().getMeta() == null) {
31e28683 882 mainPanel.removeBookPanes();
5beb7cdc
NR
883 mainPanel.addBookPane(selectedBook.getInfo()
884 .getMainInfo(), mainPanel.getCurrentType());
31e28683 885 mainPanel.refreshBooks();
14b57448 886 } else {
31e28683 887 mainPanel.openBook(selectedBook);
14b57448
NR
888 }
889 }
890 }
891 });
892
893 return open;
894 }
895
896 /**
897 * Create the SetCover menu item for a book to change the linked source
898 * cover.
899 *
900 * @return the item
901 */
79a99506 902 private JMenuItem createMenuItemSetCoverForSource() {
c428e675
NR
903 JMenuItem open = new JMenuItem(
904 GuiReader.trans(StringIdGui.MENU_EDIT_SET_COVER_FOR_SOURCE),
905 KeyEvent.VK_C);
14b57448
NR
906 open.addActionListener(new ActionListener() {
907 @Override
0bb51c9c 908 public void actionPerformed(ActionEvent ae) {
31e28683 909 final GuiReaderBook selectedBook = mainPanel.getSelectedBook();
14b57448 910 if (selectedBook != null) {
79a99506
NR
911 BasicLibrary lib = reader.getLibrary();
912 String luid = selectedBook.getInfo().getMeta().getLuid();
913 String source = selectedBook.getInfo().getMeta()
914 .getSource();
915
0bb51c9c
NR
916 try {
917 lib.setSourceCover(source, luid);
918 } catch (IOException e) {
919 error(e.getLocalizedMessage(), "IOException", e);
920 }
79a99506
NR
921
922 GuiReaderBookInfo sourceInfo = GuiReaderBookInfo
923 .fromSource(lib, source);
924 GuiReaderCoverImager.clearIcon(sourceInfo);
925 }
926 }
927 });
928
929 return open;
930 }
931
932 /**
933 * Create the SetCover menu item for a book to change the linked source
934 * cover.
935 *
936 * @return the item
937 */
938 private JMenuItem createMenuItemSetCoverForAuthor() {
c428e675
NR
939 JMenuItem open = new JMenuItem(
940 GuiReader.trans(StringIdGui.MENU_EDIT_SET_COVER_FOR_AUTHOR),
941 KeyEvent.VK_A);
79a99506
NR
942 open.addActionListener(new ActionListener() {
943 @Override
0bb51c9c 944 public void actionPerformed(ActionEvent ae) {
79a99506
NR
945 final GuiReaderBook selectedBook = mainPanel.getSelectedBook();
946 if (selectedBook != null) {
947 BasicLibrary lib = reader.getLibrary();
948 String luid = selectedBook.getInfo().getMeta().getLuid();
949 String author = selectedBook.getInfo().getMeta()
950 .getAuthor();
951
0bb51c9c
NR
952 try {
953 lib.setAuthorCover(author, luid);
954 } catch (IOException e) {
955 error(e.getLocalizedMessage(), "IOException", e);
956 }
79a99506
NR
957
958 GuiReaderBookInfo authorInfo = GuiReaderBookInfo
959 .fromAuthor(lib, author);
960 GuiReaderCoverImager.clearIcon(authorInfo);
9843a5e5
NR
961 }
962 }
963 });
10d558d2 964
9843a5e5
NR
965 return open;
966 }
10d558d2 967
e6249b0f
NR
968 /**
969 * Display an error message and log the linked {@link Exception}.
970 *
971 * @param message
972 * the message
973 * @param title
974 * the title of the error message
975 * @param e
976 * the exception to log if any
977 */
14bb95fa 978 public void error(final String message, final String title, Exception e) {
e6249b0f
NR
979 Instance.getTraceHandler().error(title + ": " + message);
980 if (e != null) {
981 Instance.getTraceHandler().error(e);
982 }
983
984 SwingUtilities.invokeLater(new Runnable() {
985 @Override
986 public void run() {
987 JOptionPane.showMessageDialog(GuiReaderFrame.this, message,
988 title, JOptionPane.ERROR_MESSAGE);
989 }
990 });
991 }
14bb95fa
NR
992
993 @Override
994 public GuiReader getReader() {
995 return reader;
996 }
5bc9573b
NR
997
998 /**
999 * Return the title of the application.
1000 *
1001 * @param libraryName
1002 * the name of the associated {@link BasicLibrary}, which can be
1003 * EMPTY
1004 *
1005 * @return the title
1006 */
1007 static private String getAppTitle(String libraryName) {
1008 if (!libraryName.isEmpty()) {
1009 return GuiReader.trans(StringIdGui.TITLE_LIBRARY_WITH_NAME, Version
1010 .getCurrentVersion().toString(), libraryName);
1011 }
1012
c428e675
NR
1013 return GuiReader.trans(StringIdGui.TITLE_LIBRARY, Version
1014 .getCurrentVersion().toString());
5bc9573b 1015 }
a6395bef 1016}