Commit | Line | Data |
---|---|---|
a6395bef NR |
1 | package be.nikiroo.fanfix.reader; |
2 | ||
d3c84ac3 | 3 | import java.awt.BorderLayout; |
b4dc6ab5 | 4 | import java.awt.Color; |
4d205683 | 5 | import java.awt.Frame; |
5130ce84 NR |
6 | import java.awt.Toolkit; |
7 | import java.awt.datatransfer.DataFlavor; | |
a6395bef NR |
8 | import java.awt.event.ActionEvent; |
9 | import java.awt.event.ActionListener; | |
333f0e7b | 10 | import java.awt.event.KeyEvent; |
9843a5e5 | 11 | import java.awt.event.MouseEvent; |
333f0e7b NR |
12 | import java.awt.event.WindowEvent; |
13 | import java.io.File; | |
a6395bef | 14 | import java.io.IOException; |
4d205683 | 15 | import java.net.URL; |
4d205683 | 16 | import java.util.HashMap; |
a6395bef | 17 | import java.util.List; |
4d205683 NR |
18 | import java.util.Map; |
19 | import java.util.Map.Entry; | |
a6395bef | 20 | |
4310bae9 | 21 | import javax.swing.BoxLayout; |
10d558d2 | 22 | import javax.swing.JFileChooser; |
a6395bef | 23 | import javax.swing.JFrame; |
333f0e7b NR |
24 | import javax.swing.JMenu; |
25 | import javax.swing.JMenuBar; | |
26 | import javax.swing.JMenuItem; | |
27 | import javax.swing.JOptionPane; | |
28 | import javax.swing.JPanel; | |
9843a5e5 | 29 | import javax.swing.JPopupMenu; |
d3c84ac3 | 30 | import javax.swing.JScrollPane; |
3b2b638f | 31 | import javax.swing.SwingUtilities; |
4d205683 NR |
32 | import javax.swing.filechooser.FileFilter; |
33 | import javax.swing.filechooser.FileNameExtensionFilter; | |
a6395bef NR |
34 | |
35 | import be.nikiroo.fanfix.Instance; | |
4d205683 | 36 | import be.nikiroo.fanfix.Library; |
b4dc6ab5 | 37 | import be.nikiroo.fanfix.bundles.UiConfig; |
a6395bef | 38 | import be.nikiroo.fanfix.data.MetaData; |
4d205683 NR |
39 | import be.nikiroo.fanfix.data.Story; |
40 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; | |
92fb0719 | 41 | import be.nikiroo.fanfix.reader.LocalReaderBook.BookActionListener; |
3b2b638f | 42 | import be.nikiroo.utils.Progress; |
39c3c689 | 43 | import be.nikiroo.utils.Version; |
3b2b638f | 44 | import be.nikiroo.utils.ui.ProgressBar; |
a6395bef | 45 | |
4d205683 NR |
46 | /** |
47 | * A {@link Frame} that will show a {@link LocalReaderBook} item for each | |
48 | * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a | |
49 | * way to copy them to the {@link LocalReader} cache ({@link LocalReader#lib}), | |
50 | * read them, delete them... | |
51 | * | |
52 | * @author niki | |
53 | */ | |
a6395bef NR |
54 | class LocalReaderFrame extends JFrame { |
55 | private static final long serialVersionUID = 1L; | |
56 | private LocalReader reader; | |
4310bae9 NR |
57 | private Map<LocalReaderGroup, String> booksByType; |
58 | private Map<LocalReaderGroup, String> booksByAuthor; | |
59 | private JPanel pane; | |
b4dc6ab5 | 60 | private Color color; |
3b2b638f NR |
61 | private ProgressBar pgBar; |
62 | private JMenuBar bar; | |
10d558d2 | 63 | private LocalReaderBook selectedBook; |
793f1071 | 64 | private boolean words; // words or authors (secondary info on books) |
a6395bef | 65 | |
4d205683 NR |
66 | /** |
67 | * Create a new {@link LocalReaderFrame}. | |
68 | * | |
69 | * @param reader | |
70 | * the associated {@link LocalReader} to forward some commands | |
71 | * and access its {@link Library} | |
72 | * @param type | |
73 | * the type of {@link Story} to load, or NULL for all types | |
74 | */ | |
333f0e7b | 75 | public LocalReaderFrame(LocalReader reader, String type) { |
39c3c689 | 76 | super(String.format("Fanfix %s Library", Version.getCurrentVersion())); |
a6395bef NR |
77 | |
78 | this.reader = reader; | |
79 | ||
80 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
81 | setSize(800, 600); | |
d3c84ac3 | 82 | setLayout(new BorderLayout()); |
a6395bef | 83 | |
4310bae9 NR |
84 | pane = new JPanel(); |
85 | pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS)); | |
b4dc6ab5 | 86 | |
3b2b638f | 87 | color = Instance.getUiConfig().getColor(UiConfig.BACKGROUND_COLOR); |
b4dc6ab5 NR |
88 | if (color != null) { |
89 | setBackground(color); | |
4310bae9 | 90 | pane.setBackground(color); |
b4dc6ab5 | 91 | } |
d3c84ac3 | 92 | |
4310bae9 | 93 | JScrollPane scroll = new JScrollPane(pane); |
b4dc6ab5 NR |
94 | scroll.getVerticalScrollBar().setUnitIncrement(16); |
95 | add(scroll, BorderLayout.CENTER); | |
a6395bef | 96 | |
3b2b638f NR |
97 | pgBar = new ProgressBar(); |
98 | add(pgBar, BorderLayout.SOUTH); | |
99 | ||
333f0e7b NR |
100 | setJMenuBar(createMenu()); |
101 | ||
4310bae9 NR |
102 | booksByType = new HashMap<LocalReaderGroup, String>(); |
103 | booksByAuthor = new HashMap<LocalReaderGroup, String>(); | |
104 | ||
105 | addBookPane(type, true); | |
106 | refreshBooks(); | |
107 | ||
333f0e7b NR |
108 | setVisible(true); |
109 | } | |
110 | ||
4d205683 | 111 | /** |
4310bae9 NR |
112 | * Add a new {@link LocalReaderGroup} on the frame to display the books of |
113 | * the selected type or author. | |
4d205683 | 114 | * |
4310bae9 NR |
115 | * @param value |
116 | * the author or the type | |
4d205683 | 117 | * @param type |
4310bae9 | 118 | * TRUE for type, FALSE for author |
4d205683 | 119 | */ |
4310bae9 NR |
120 | private void addBookPane(String value, boolean type) { |
121 | if (value == null) { | |
122 | if (type) { | |
123 | for (String tt : Instance.getLibrary().getTypes()) { | |
124 | if (tt != null) { | |
125 | addBookPane(tt, type); | |
126 | } | |
127 | } | |
128 | } else { | |
129 | for (String tt : Instance.getLibrary().getAuthors()) { | |
130 | if (tt != null) { | |
131 | addBookPane(tt, type); | |
132 | } | |
133 | } | |
b4dc6ab5 NR |
134 | } |
135 | ||
4310bae9 NR |
136 | return; |
137 | } | |
4d205683 | 138 | |
4310bae9 NR |
139 | LocalReaderGroup bookPane = new LocalReaderGroup(reader, value, color); |
140 | if (type) { | |
141 | booksByType.put(bookPane, value); | |
142 | } else { | |
143 | booksByAuthor.put(bookPane, value); | |
144 | } | |
333f0e7b | 145 | |
4310bae9 NR |
146 | this.invalidate(); |
147 | pane.invalidate(); | |
148 | pane.add(bookPane); | |
149 | pane.validate(); | |
150 | this.validate(); | |
9843a5e5 | 151 | |
4310bae9 NR |
152 | bookPane.setActionListener(new BookActionListener() { |
153 | public void select(LocalReaderBook book) { | |
154 | selectedBook = book; | |
155 | } | |
156 | ||
157 | public void popupRequested(LocalReaderBook book, MouseEvent e) { | |
158 | JPopupMenu popup = new JPopupMenu(); | |
159 | popup.add(createMenuItemOpenBook()); | |
160 | popup.addSeparator(); | |
161 | popup.add(createMenuItemExport()); | |
162 | popup.add(createMenuItemClearCache()); | |
163 | popup.add(createMenuItemRedownload()); | |
164 | popup.addSeparator(); | |
165 | popup.add(createMenuItemDelete()); | |
166 | popup.show(e.getComponent(), e.getX(), e.getY()); | |
167 | } | |
168 | ||
169 | public void action(final LocalReaderBook book) { | |
170 | openBook(book); | |
171 | } | |
172 | }); | |
173 | } | |
a6395bef | 174 | |
4310bae9 NR |
175 | private void removeBookPanes() { |
176 | booksByType.clear(); | |
177 | booksByAuthor.clear(); | |
178 | pane.invalidate(); | |
179 | this.invalidate(); | |
180 | pane.removeAll(); | |
181 | pane.validate(); | |
182 | this.validate(); | |
183 | } | |
184 | ||
185 | /** | |
186 | * Refresh the list of {@link LocalReaderBook}s from disk. | |
187 | * | |
188 | * @param type | |
189 | * the type of {@link Story} to load, or NULL for all types | |
190 | */ | |
191 | private void refreshBooks() { | |
192 | for (LocalReaderGroup group : booksByType.keySet()) { | |
193 | List<MetaData> stories = Instance.getLibrary().getListByType( | |
194 | booksByType.get(group)); | |
793f1071 | 195 | group.refreshBooks(stories, words); |
4310bae9 NR |
196 | } |
197 | ||
198 | for (LocalReaderGroup group : booksByAuthor.keySet()) { | |
199 | List<MetaData> stories = Instance.getLibrary().getListByAuthor( | |
200 | booksByAuthor.get(group)); | |
793f1071 | 201 | group.refreshBooks(stories, words); |
a6395bef NR |
202 | } |
203 | ||
4310bae9 NR |
204 | pane.repaint(); |
205 | this.repaint(); | |
333f0e7b NR |
206 | } |
207 | ||
4d205683 NR |
208 | /** |
209 | * Create the main menu bar. | |
210 | * | |
211 | * @return the bar | |
212 | */ | |
333f0e7b | 213 | private JMenuBar createMenu() { |
3b2b638f | 214 | bar = new JMenuBar(); |
333f0e7b NR |
215 | |
216 | JMenu file = new JMenu("File"); | |
10d558d2 | 217 | file.setMnemonic(KeyEvent.VK_F); |
333f0e7b | 218 | |
22848428 | 219 | JMenuItem imprt = new JMenuItem("Import URL...", KeyEvent.VK_U); |
333f0e7b NR |
220 | imprt.addActionListener(new ActionListener() { |
221 | public void actionPerformed(ActionEvent e) { | |
10d558d2 NR |
222 | imprt(true); |
223 | } | |
224 | }); | |
22848428 | 225 | JMenuItem imprtF = new JMenuItem("Import File...", KeyEvent.VK_F); |
10d558d2 NR |
226 | imprtF.addActionListener(new ActionListener() { |
227 | public void actionPerformed(ActionEvent e) { | |
228 | imprt(false); | |
229 | } | |
230 | }); | |
231 | JMenuItem exit = new JMenuItem("Exit", KeyEvent.VK_X); | |
232 | exit.addActionListener(new ActionListener() { | |
233 | public void actionPerformed(ActionEvent e) { | |
234 | LocalReaderFrame.this.dispatchEvent(new WindowEvent( | |
235 | LocalReaderFrame.this, WindowEvent.WINDOW_CLOSING)); | |
236 | } | |
237 | }); | |
3b2b638f | 238 | |
9843a5e5 NR |
239 | file.add(createMenuItemOpenBook()); |
240 | file.add(createMenuItemExport()); | |
241 | file.addSeparator(); | |
10d558d2 NR |
242 | file.add(imprt); |
243 | file.add(imprtF); | |
244 | file.addSeparator(); | |
245 | file.add(exit); | |
3b2b638f | 246 | |
10d558d2 NR |
247 | bar.add(file); |
248 | ||
249 | JMenu edit = new JMenu("Edit"); | |
250 | edit.setMnemonic(KeyEvent.VK_E); | |
251 | ||
22848428 NR |
252 | edit.add(createMenuItemClearCache()); |
253 | edit.add(createMenuItemRedownload()); | |
9843a5e5 NR |
254 | edit.addSeparator(); |
255 | edit.add(createMenuItemDelete()); | |
256 | ||
257 | bar.add(edit); | |
258 | ||
793f1071 NR |
259 | JMenu view = new JMenu("View"); |
260 | view.setMnemonic(KeyEvent.VK_V); | |
261 | JMenuItem vauthors = new JMenuItem("Author"); | |
262 | vauthors.setMnemonic(KeyEvent.VK_A); | |
263 | vauthors.addActionListener(new ActionListener() { | |
264 | public void actionPerformed(ActionEvent e) { | |
265 | words = false; | |
266 | refreshBooks(); | |
267 | } | |
268 | }); | |
269 | view.add(vauthors); | |
270 | JMenuItem vwords = new JMenuItem("Word count"); | |
271 | vwords.setMnemonic(KeyEvent.VK_W); | |
272 | vwords.addActionListener(new ActionListener() { | |
273 | public void actionPerformed(ActionEvent e) { | |
274 | words = true; | |
275 | refreshBooks(); | |
276 | } | |
277 | }); | |
278 | view.add(vwords); | |
279 | bar.add(view); | |
280 | ||
4310bae9 NR |
281 | JMenu sources = new JMenu("Sources"); |
282 | sources.setMnemonic(KeyEvent.VK_S); | |
9843a5e5 NR |
283 | |
284 | List<String> tt = Instance.getLibrary().getTypes(); | |
285 | tt.add(0, null); | |
286 | for (final String type : tt) { | |
4310bae9 | 287 | JMenuItem item = new JMenuItem(type == null ? "All" : type); |
9843a5e5 NR |
288 | item.addActionListener(new ActionListener() { |
289 | public void actionPerformed(ActionEvent e) { | |
4310bae9 NR |
290 | removeBookPanes(); |
291 | addBookPane(type, true); | |
292 | refreshBooks(); | |
9843a5e5 NR |
293 | } |
294 | }); | |
4310bae9 | 295 | sources.add(item); |
9843a5e5 NR |
296 | |
297 | if (type == null) { | |
4310bae9 | 298 | sources.addSeparator(); |
9843a5e5 NR |
299 | } |
300 | } | |
10d558d2 | 301 | |
4310bae9 NR |
302 | bar.add(sources); |
303 | ||
304 | JMenu authors = new JMenu("Authors"); | |
305 | authors.setMnemonic(KeyEvent.VK_A); | |
306 | ||
307 | List<String> aa = Instance.getLibrary().getAuthors(); | |
308 | aa.add(0, null); | |
309 | for (final String author : aa) { | |
310 | JMenuItem item = new JMenuItem(author == null ? "All" | |
311 | : author.isEmpty() ? "[unknown]" : author); | |
312 | item.addActionListener(new ActionListener() { | |
313 | public void actionPerformed(ActionEvent e) { | |
314 | removeBookPanes(); | |
315 | addBookPane(author, false); | |
316 | refreshBooks(); | |
317 | } | |
318 | }); | |
319 | authors.add(item); | |
320 | ||
321 | if (author == null || author.isEmpty()) { | |
322 | authors.addSeparator(); | |
323 | } | |
324 | } | |
325 | ||
326 | bar.add(authors); | |
9843a5e5 NR |
327 | |
328 | return bar; | |
329 | } | |
330 | ||
4d205683 NR |
331 | /** |
332 | * Create the export menu item. | |
333 | * | |
334 | * @return the item | |
335 | */ | |
9843a5e5 | 336 | private JMenuItem createMenuItemExport() { |
4d205683 NR |
337 | final JFileChooser fc = new JFileChooser(); |
338 | fc.setAcceptAllFileFilterUsed(false); | |
339 | ||
340 | final Map<FileFilter, OutputType> filters = new HashMap<FileFilter, OutputType>(); | |
341 | for (OutputType type : OutputType.values()) { | |
342 | String ext = type.getDefaultExtension(false); | |
343 | String desc = type.getDesc(false); | |
b2612f9d | 344 | |
4d205683 NR |
345 | if (ext == null || ext.isEmpty()) { |
346 | filters.put(createAllFilter(desc), type); | |
347 | } else { | |
348 | filters.put(new FileNameExtensionFilter(desc, ext), type); | |
349 | } | |
350 | } | |
351 | ||
352 | // First the "ALL" filters, then, the extension filters | |
353 | for (Entry<FileFilter, OutputType> entry : filters.entrySet()) { | |
354 | if (!(entry.getKey() instanceof FileNameExtensionFilter)) { | |
355 | fc.addChoosableFileFilter(entry.getKey()); | |
356 | } | |
357 | } | |
358 | for (Entry<FileFilter, OutputType> entry : filters.entrySet()) { | |
359 | if (entry.getKey() instanceof FileNameExtensionFilter) { | |
360 | fc.addChoosableFileFilter(entry.getKey()); | |
361 | } | |
362 | } | |
363 | // | |
9843a5e5 | 364 | |
4d205683 | 365 | JMenuItem export = new JMenuItem("Save as...", KeyEvent.VK_S); |
10d558d2 NR |
366 | export.addActionListener(new ActionListener() { |
367 | public void actionPerformed(ActionEvent e) { | |
4d205683 NR |
368 | if (selectedBook != null) { |
369 | fc.showDialog(LocalReaderFrame.this, "Save"); | |
b2612f9d NR |
370 | if (fc.getSelectedFile() != null) { |
371 | final OutputType type = filters.get(fc.getFileFilter()); | |
372 | final String path = fc.getSelectedFile() | |
373 | .getAbsolutePath() | |
374 | + type.getDefaultExtension(false); | |
375 | final Progress pg = new Progress(); | |
376 | outOfUi(pg, new Runnable() { | |
377 | public void run() { | |
378 | try { | |
379 | Instance.getLibrary().export( | |
380 | selectedBook.getMeta().getLuid(), | |
381 | type, path, pg); | |
382 | } catch (IOException e) { | |
383 | Instance.syserr(e); | |
384 | } | |
4d205683 | 385 | } |
b2612f9d NR |
386 | }); |
387 | } | |
4d205683 | 388 | } |
10d558d2 NR |
389 | } |
390 | }); | |
391 | ||
9843a5e5 NR |
392 | return export; |
393 | } | |
394 | ||
edd46289 NR |
395 | /** |
396 | * Create a {@link FileFilter} that accepts all files and return the given | |
397 | * description. | |
398 | * | |
399 | * @param desc | |
400 | * the description | |
401 | * | |
402 | * @return the filter | |
403 | */ | |
4d205683 NR |
404 | private FileFilter createAllFilter(final String desc) { |
405 | return new FileFilter() { | |
406 | @Override | |
407 | public String getDescription() { | |
408 | return desc; | |
409 | } | |
410 | ||
411 | @Override | |
412 | public boolean accept(File f) { | |
413 | return true; | |
414 | } | |
415 | }; | |
416 | } | |
417 | ||
418 | /** | |
419 | * Create the refresh (delete cache) menu item. | |
420 | * | |
421 | * @return the item | |
422 | */ | |
22848428 | 423 | private JMenuItem createMenuItemClearCache() { |
4d205683 | 424 | JMenuItem refresh = new JMenuItem("Clear cache", KeyEvent.VK_C); |
10d558d2 NR |
425 | refresh.addActionListener(new ActionListener() { |
426 | public void actionPerformed(ActionEvent e) { | |
427 | if (selectedBook != null) { | |
428 | outOfUi(null, new Runnable() { | |
429 | public void run() { | |
22848428 | 430 | reader.refresh(selectedBook.getMeta().getLuid()); |
10d558d2 | 431 | selectedBook.setCached(false); |
3b2b638f NR |
432 | SwingUtilities.invokeLater(new Runnable() { |
433 | public void run() { | |
10d558d2 | 434 | selectedBook.repaint(); |
3b2b638f NR |
435 | } |
436 | }); | |
437 | } | |
438 | }); | |
333f0e7b NR |
439 | } |
440 | } | |
441 | }); | |
10d558d2 | 442 | |
9843a5e5 NR |
443 | return refresh; |
444 | } | |
445 | ||
22848428 NR |
446 | /** |
447 | * Create the redownload (then delete original) menu item. | |
448 | * | |
449 | * @return the item | |
450 | */ | |
451 | private JMenuItem createMenuItemRedownload() { | |
452 | JMenuItem refresh = new JMenuItem("Redownload", KeyEvent.VK_R); | |
453 | refresh.addActionListener(new ActionListener() { | |
454 | public void actionPerformed(ActionEvent e) { | |
455 | if (selectedBook != null) { | |
456 | imprt(selectedBook.getMeta().getUrl(), new Runnable() { | |
457 | public void run() { | |
458 | reader.delete(selectedBook.getMeta().getLuid()); | |
459 | selectedBook = null; | |
460 | } | |
461 | }); | |
462 | } | |
463 | } | |
464 | }); | |
465 | ||
466 | return refresh; | |
467 | } | |
468 | ||
4d205683 NR |
469 | /** |
470 | * Create the delete menu item. | |
471 | * | |
472 | * @return the item | |
473 | */ | |
9843a5e5 | 474 | private JMenuItem createMenuItemDelete() { |
10d558d2 NR |
475 | JMenuItem delete = new JMenuItem("Delete", KeyEvent.VK_D); |
476 | delete.addActionListener(new ActionListener() { | |
477 | public void actionPerformed(ActionEvent e) { | |
478 | if (selectedBook != null) { | |
479 | outOfUi(null, new Runnable() { | |
480 | public void run() { | |
22848428 | 481 | reader.delete(selectedBook.getMeta().getLuid()); |
10d558d2 NR |
482 | selectedBook = null; |
483 | SwingUtilities.invokeLater(new Runnable() { | |
484 | public void run() { | |
4310bae9 | 485 | refreshBooks(); |
10d558d2 NR |
486 | } |
487 | }); | |
488 | } | |
489 | }); | |
490 | } | |
491 | } | |
492 | }); | |
493 | ||
9843a5e5 NR |
494 | return delete; |
495 | } | |
10d558d2 | 496 | |
4d205683 NR |
497 | /** |
498 | * Create the open menu item. | |
499 | * | |
500 | * @return the item | |
501 | */ | |
9843a5e5 NR |
502 | private JMenuItem createMenuItemOpenBook() { |
503 | JMenuItem open = new JMenuItem("Open", KeyEvent.VK_O); | |
504 | open.addActionListener(new ActionListener() { | |
505 | public void actionPerformed(ActionEvent e) { | |
506 | if (selectedBook != null) { | |
507 | openBook(selectedBook); | |
508 | } | |
509 | } | |
510 | }); | |
10d558d2 | 511 | |
9843a5e5 NR |
512 | return open; |
513 | } | |
10d558d2 | 514 | |
4d205683 NR |
515 | /** |
516 | * Open a {@link LocalReaderBook} item. | |
517 | * | |
518 | * @param book | |
519 | * the {@link LocalReaderBook} to open | |
520 | */ | |
9843a5e5 NR |
521 | private void openBook(final LocalReaderBook book) { |
522 | final Progress pg = new Progress(); | |
523 | outOfUi(pg, new Runnable() { | |
524 | public void run() { | |
525 | try { | |
22848428 | 526 | reader.open(book.getMeta().getLuid(), pg); |
edd46289 NR |
527 | SwingUtilities.invokeLater(new Runnable() { |
528 | public void run() { | |
529 | book.setCached(true); | |
9843a5e5 | 530 | } |
edd46289 | 531 | }); |
9843a5e5 | 532 | } catch (IOException e) { |
edd46289 | 533 | // TODO: error message? |
9843a5e5 | 534 | Instance.syserr(e); |
333f0e7b | 535 | } |
10d558d2 | 536 | } |
9843a5e5 | 537 | }); |
a6395bef | 538 | } |
3b2b638f | 539 | |
4d205683 NR |
540 | /** |
541 | * Process the given action out of the Swing UI thread and link the given | |
542 | * {@link ProgressBar} to the action. | |
543 | * <p> | |
544 | * The code will make sure that the {@link ProgressBar} (if not NULL) is set | |
545 | * to done when the action is done. | |
546 | * | |
547 | * @param pg | |
548 | * the {@link ProgressBar} or NULL | |
549 | * @param run | |
550 | * the action to run | |
551 | */ | |
3b2b638f NR |
552 | private void outOfUi(final Progress pg, final Runnable run) { |
553 | pgBar.setProgress(pg); | |
554 | ||
a4143cd7 NR |
555 | setEnabled(false); |
556 | pgBar.addActioListener(new ActionListener() { | |
557 | public void actionPerformed(ActionEvent e) { | |
558 | pgBar.setProgress(null); | |
559 | setEnabled(true); | |
3b2b638f NR |
560 | } |
561 | }); | |
562 | ||
563 | new Thread(new Runnable() { | |
564 | public void run() { | |
565 | run.run(); | |
10d558d2 NR |
566 | if (pg == null) { |
567 | SwingUtilities.invokeLater(new Runnable() { | |
568 | public void run() { | |
4d205683 | 569 | setEnabled(true); |
10d558d2 NR |
570 | } |
571 | }); | |
572 | } else if (!pg.isDone()) { | |
3b2b638f NR |
573 | pg.setProgress(pg.getMax()); |
574 | } | |
575 | } | |
576 | }).start(); | |
577 | } | |
578 | ||
4d205683 NR |
579 | /** |
580 | * Import a {@link Story} into the main {@link Library}. | |
22848428 NR |
581 | * <p> |
582 | * Should be called inside the UI thread. | |
4d205683 NR |
583 | * |
584 | * @param askUrl | |
585 | * TRUE for an {@link URL}, false for a {@link File} | |
586 | */ | |
10d558d2 NR |
587 | private void imprt(boolean askUrl) { |
588 | JFileChooser fc = new JFileChooser(); | |
589 | ||
5130ce84 | 590 | Object url; |
10d558d2 | 591 | if (askUrl) { |
5130ce84 NR |
592 | String clipboard = ""; |
593 | try { | |
594 | clipboard = ("" + Toolkit.getDefaultToolkit() | |
595 | .getSystemClipboard().getData(DataFlavor.stringFlavor)) | |
596 | .trim(); | |
597 | } catch (Exception e) { | |
598 | // No data will be handled | |
599 | } | |
600 | ||
601 | if (clipboard == null || !clipboard.startsWith("http")) { | |
602 | clipboard = ""; | |
603 | } | |
604 | ||
10d558d2 NR |
605 | url = JOptionPane.showInputDialog(LocalReaderFrame.this, |
606 | "url of the story to import?", "Importing from URL", | |
5130ce84 | 607 | JOptionPane.QUESTION_MESSAGE, null, null, clipboard); |
10d558d2 NR |
608 | } else if (fc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION) { |
609 | url = fc.getSelectedFile().getAbsolutePath(); | |
610 | } else { | |
611 | url = null; | |
612 | } | |
613 | ||
5130ce84 NR |
614 | if (url != null && !url.toString().isEmpty()) { |
615 | imprt(url.toString(), null); | |
22848428 NR |
616 | } |
617 | } | |
10d558d2 | 618 | |
22848428 NR |
619 | /** |
620 | * Actually import the {@link Story} into the main {@link Library}. | |
621 | * <p> | |
622 | * Should be called inside the UI thread. | |
623 | * | |
624 | * @param url | |
625 | * the {@link Story} to import by {@link URL} | |
626 | * @param onSuccess | |
627 | * Action to execute on success | |
628 | */ | |
629 | private void imprt(final String url, final Runnable onSuccess) { | |
630 | final Progress pg = new Progress("Importing " + url); | |
631 | outOfUi(pg, new Runnable() { | |
632 | public void run() { | |
633 | Exception ex = null; | |
634 | try { | |
635 | Instance.getLibrary().imprt(BasicReader.getUrl(url), pg); | |
636 | } catch (IOException e) { | |
637 | ex = e; | |
638 | } | |
10d558d2 | 639 | |
22848428 NR |
640 | final Exception e = ex; |
641 | ||
642 | final boolean ok = (e == null); | |
643 | SwingUtilities.invokeLater(new Runnable() { | |
644 | public void run() { | |
645 | if (!ok) { | |
a4143cd7 | 646 | Instance.syserr(e); |
22848428 NR |
647 | JOptionPane.showMessageDialog( |
648 | LocalReaderFrame.this, "Cannot import: " | |
649 | + url, e.getMessage(), | |
650 | JOptionPane.ERROR_MESSAGE); | |
651 | ||
652 | setEnabled(true); | |
653 | } else { | |
4310bae9 | 654 | refreshBooks(); |
22848428 NR |
655 | if (onSuccess != null) { |
656 | onSuccess.run(); | |
4310bae9 | 657 | refreshBooks(); |
10d558d2 NR |
658 | } |
659 | } | |
22848428 NR |
660 | } |
661 | }); | |
662 | } | |
663 | }); | |
10d558d2 NR |
664 | } |
665 | ||
4d205683 NR |
666 | /** |
667 | * Enables or disables this component, depending on the value of the | |
668 | * parameter <code>b</code>. An enabled component can respond to user input | |
669 | * and generate events. Components are enabled initially by default. | |
670 | * <p> | |
671 | * Disabling this component will also affect its children. | |
672 | * | |
673 | * @param b | |
674 | * If <code>true</code>, this component is enabled; otherwise | |
675 | * this component is disabled | |
676 | */ | |
677 | @Override | |
678 | public void setEnabled(boolean b) { | |
4d205683 | 679 | bar.setEnabled(b); |
4310bae9 NR |
680 | for (LocalReaderGroup group : booksByType.keySet()) { |
681 | group.setEnabled(b); | |
682 | } | |
683 | for (LocalReaderGroup group : booksByAuthor.keySet()) { | |
684 | group.setEnabled(b); | |
685 | } | |
4d205683 | 686 | super.setEnabled(b); |
3b2b638f NR |
687 | repaint(); |
688 | } | |
a6395bef | 689 | } |