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