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