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