| 1 | package be.nikiroo.fanfix.reader.ui; |
| 2 | |
| 3 | import java.awt.BorderLayout; |
| 4 | import java.awt.Color; |
| 5 | import java.awt.Font; |
| 6 | import java.awt.Frame; |
| 7 | import java.awt.Toolkit; |
| 8 | import java.awt.datatransfer.DataFlavor; |
| 9 | import java.awt.event.ActionEvent; |
| 10 | import java.awt.event.ActionListener; |
| 11 | import java.awt.event.KeyEvent; |
| 12 | import java.awt.event.MouseEvent; |
| 13 | import java.awt.event.WindowEvent; |
| 14 | import java.io.File; |
| 15 | import java.io.IOException; |
| 16 | import java.net.URL; |
| 17 | import java.net.UnknownHostException; |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.List; |
| 21 | import java.util.Map; |
| 22 | import java.util.Map.Entry; |
| 23 | |
| 24 | import javax.swing.BorderFactory; |
| 25 | import javax.swing.BoxLayout; |
| 26 | import javax.swing.ImageIcon; |
| 27 | import javax.swing.JFileChooser; |
| 28 | import javax.swing.JFrame; |
| 29 | import javax.swing.JLabel; |
| 30 | import javax.swing.JMenu; |
| 31 | import javax.swing.JMenuBar; |
| 32 | import javax.swing.JMenuItem; |
| 33 | import javax.swing.JOptionPane; |
| 34 | import javax.swing.JPanel; |
| 35 | import javax.swing.JPopupMenu; |
| 36 | import javax.swing.JScrollPane; |
| 37 | import javax.swing.JTextArea; |
| 38 | import javax.swing.SwingConstants; |
| 39 | import javax.swing.SwingUtilities; |
| 40 | import javax.swing.filechooser.FileFilter; |
| 41 | import javax.swing.filechooser.FileNameExtensionFilter; |
| 42 | |
| 43 | import be.nikiroo.fanfix.Instance; |
| 44 | import be.nikiroo.fanfix.bundles.Config; |
| 45 | import be.nikiroo.fanfix.bundles.UiConfig; |
| 46 | import be.nikiroo.fanfix.data.MetaData; |
| 47 | import be.nikiroo.fanfix.data.Story; |
| 48 | import be.nikiroo.fanfix.library.BasicLibrary; |
| 49 | import be.nikiroo.fanfix.library.BasicLibrary.Status; |
| 50 | import be.nikiroo.fanfix.library.LocalLibrary; |
| 51 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; |
| 52 | import be.nikiroo.fanfix.reader.BasicReader; |
| 53 | import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener; |
| 54 | import be.nikiroo.utils.Progress; |
| 55 | import be.nikiroo.utils.Version; |
| 56 | import be.nikiroo.utils.ui.ConfigEditor; |
| 57 | import be.nikiroo.utils.ui.ProgressBar; |
| 58 | |
| 59 | /** |
| 60 | * A {@link Frame} that will show a {@link GuiReaderBook} item for each |
| 61 | * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a |
| 62 | * way to copy them to the {@link GuiReader} cache ( |
| 63 | * {@link BasicReader#getLibrary()}), read them, delete them... |
| 64 | * |
| 65 | * @author niki |
| 66 | */ |
| 67 | class GuiReaderFrame extends JFrame { |
| 68 | private static final long serialVersionUID = 1L; |
| 69 | private GuiReader reader; |
| 70 | private Map<GuiReaderGroup, String> booksByType; |
| 71 | private Map<GuiReaderGroup, String> booksByAuthor; |
| 72 | private JPanel pane; |
| 73 | private Color color; |
| 74 | private ProgressBar pgBar; |
| 75 | private JMenuBar bar; |
| 76 | private GuiReaderBook selectedBook; |
| 77 | private boolean words; // words or authors (secondary info on books) |
| 78 | |
| 79 | /** |
| 80 | * A {@link Runnable} with a {@link Story} parameter. |
| 81 | * |
| 82 | * @author niki |
| 83 | */ |
| 84 | private interface StoryRunnable { |
| 85 | /** |
| 86 | * Run the action. |
| 87 | * |
| 88 | * @param story |
| 89 | * the story |
| 90 | */ |
| 91 | public void run(Story story); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Create a new {@link GuiReaderFrame}. |
| 96 | * |
| 97 | * @param reader |
| 98 | * the associated {@link GuiReader} to forward some commands and |
| 99 | * access its {@link LocalLibrary} |
| 100 | * @param type |
| 101 | * the type of {@link Story} to load, or NULL for all types |
| 102 | */ |
| 103 | public GuiReaderFrame(GuiReader reader, String type) { |
| 104 | super(String.format("Fanfix %s Library", Version.getCurrentVersion())); |
| 105 | |
| 106 | this.reader = reader; |
| 107 | |
| 108 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 109 | setSize(800, 600); |
| 110 | setLayout(new BorderLayout()); |
| 111 | |
| 112 | pane = new JPanel(); |
| 113 | pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS)); |
| 114 | |
| 115 | Integer icolor = Instance.getUiConfig().getColor( |
| 116 | UiConfig.BACKGROUND_COLOR); |
| 117 | if (icolor != null) { |
| 118 | color = new Color(icolor); |
| 119 | setBackground(color); |
| 120 | pane.setBackground(color); |
| 121 | } |
| 122 | |
| 123 | JScrollPane scroll = new JScrollPane(pane); |
| 124 | scroll.getVerticalScrollBar().setUnitIncrement(16); |
| 125 | add(scroll, BorderLayout.CENTER); |
| 126 | |
| 127 | String message = reader.getLibrary().getLibraryName(); |
| 128 | if (!message.isEmpty()) { |
| 129 | JLabel name = new JLabel(message, SwingConstants.CENTER); |
| 130 | add(name, BorderLayout.NORTH); |
| 131 | } |
| 132 | |
| 133 | pgBar = new ProgressBar(); |
| 134 | add(pgBar, BorderLayout.SOUTH); |
| 135 | |
| 136 | pgBar.addActionListener(new ActionListener() { |
| 137 | @Override |
| 138 | public void actionPerformed(ActionEvent e) { |
| 139 | invalidate(); |
| 140 | pgBar.setProgress(null); |
| 141 | validate(); |
| 142 | setEnabled(true); |
| 143 | } |
| 144 | }); |
| 145 | |
| 146 | pgBar.addUpdateListener(new ActionListener() { |
| 147 | @Override |
| 148 | public void actionPerformed(ActionEvent e) { |
| 149 | invalidate(); |
| 150 | validate(); |
| 151 | repaint(); |
| 152 | } |
| 153 | }); |
| 154 | |
| 155 | booksByType = new HashMap<GuiReaderGroup, String>(); |
| 156 | booksByAuthor = new HashMap<GuiReaderGroup, String>(); |
| 157 | |
| 158 | pane.setVisible(false); |
| 159 | final Progress pg = new Progress(); |
| 160 | final String typeF = type; |
| 161 | outOfUi(pg, new Runnable() { |
| 162 | @Override |
| 163 | public void run() { |
| 164 | BasicLibrary lib = GuiReaderFrame.this.reader.getLibrary(); |
| 165 | Status status = lib.getStatus(); |
| 166 | |
| 167 | if (status == Status.READY) { |
| 168 | lib.refresh(pg); |
| 169 | invalidate(); |
| 170 | setJMenuBar(createMenu(true)); |
| 171 | if (typeF == null) { |
| 172 | addBookPane(true, false); |
| 173 | } else { |
| 174 | addBookPane(typeF, true); |
| 175 | } |
| 176 | refreshBooks(); |
| 177 | validate(); |
| 178 | pane.setVisible(true); |
| 179 | } else { |
| 180 | invalidate(); |
| 181 | setJMenuBar(createMenu(false)); |
| 182 | validate(); |
| 183 | |
| 184 | String err = lib.getLibraryName() + "\n"; |
| 185 | switch (status) { |
| 186 | case INVALID: |
| 187 | err += "Library not valid"; |
| 188 | break; |
| 189 | |
| 190 | case UNAUTORIZED: |
| 191 | err += "You are not allowed to access this library"; |
| 192 | break; |
| 193 | |
| 194 | case UNAVAILABLE: |
| 195 | err += "Library currently unavailable"; |
| 196 | break; |
| 197 | |
| 198 | default: |
| 199 | err += "An error occured when contacting the library"; |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | error(err, "Library error", null); |
| 204 | } |
| 205 | } |
| 206 | }); |
| 207 | |
| 208 | setVisible(true); |
| 209 | } |
| 210 | |
| 211 | private void addListPane(String name, List<String> values, |
| 212 | final boolean type) { |
| 213 | // Sources -> i18n |
| 214 | GuiReaderGroup bookPane = new GuiReaderGroup(reader, name, color); |
| 215 | |
| 216 | List<MetaData> metas = new ArrayList<MetaData>(); |
| 217 | for (String source : values) { |
| 218 | MetaData mSource = new MetaData(); |
| 219 | mSource.setLuid(null); |
| 220 | mSource.setTitle(source); |
| 221 | mSource.setSource(source); |
| 222 | metas.add(mSource); |
| 223 | } |
| 224 | |
| 225 | bookPane.refreshBooks(metas, false); |
| 226 | |
| 227 | this.invalidate(); |
| 228 | pane.invalidate(); |
| 229 | pane.add(bookPane); |
| 230 | pane.validate(); |
| 231 | this.validate(); |
| 232 | |
| 233 | bookPane.setActionListener(new BookActionListener() { |
| 234 | @Override |
| 235 | public void select(GuiReaderBook book) { |
| 236 | selectedBook = book; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public void popupRequested(GuiReaderBook book, MouseEvent e) { |
| 241 | JPopupMenu popup = new JPopupMenu(); |
| 242 | popup.add(createMenuItemOpenBook()); |
| 243 | popup.show(e.getComponent(), e.getX(), e.getY()); |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public void action(final GuiReaderBook book) { |
| 248 | removeBookPanes(); |
| 249 | addBookPane(book.getMeta().getSource(), type); |
| 250 | refreshBooks(); |
| 251 | } |
| 252 | }); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Add a new {@link GuiReaderGroup} on the frame to display all the |
| 257 | * sources/types or all the authors, or a listing of all the books sorted |
| 258 | * either by source or author. |
| 259 | * <p> |
| 260 | * A display of all the sources/types or all the authors will show one icon |
| 261 | * per source/type or author. |
| 262 | * <p> |
| 263 | * A listing of all the books sorted by source/type or author will display |
| 264 | * all the books. |
| 265 | * |
| 266 | * @param type |
| 267 | * TRUE for type/source, FALSE for author |
| 268 | * @param listMode |
| 269 | * TRUE to get a listing of all the sources or authors, FALSE to |
| 270 | * get one icon per source or author |
| 271 | */ |
| 272 | private void addBookPane(boolean type, boolean listMode) { |
| 273 | if (type) { |
| 274 | if (!listMode) { |
| 275 | addListPane("Sources", reader.getLibrary().getSources(), type); |
| 276 | } else { |
| 277 | for (String tt : reader.getLibrary().getSources()) { |
| 278 | if (tt != null) { |
| 279 | addBookPane(tt, type); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | } else { |
| 284 | if (!listMode) { |
| 285 | addListPane("Authors", reader.getLibrary().getAuthors(), type); |
| 286 | } else { |
| 287 | for (String tt : reader.getLibrary().getAuthors()) { |
| 288 | if (tt != null) { |
| 289 | addBookPane(tt, type); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Add a new {@link GuiReaderGroup} on the frame to display the books of the |
| 298 | * selected type or author. |
| 299 | * |
| 300 | * @param value |
| 301 | * the author or the type, or NULL to get all the |
| 302 | * authors-or-types |
| 303 | * @param type |
| 304 | * TRUE for type/source, FALSE for author |
| 305 | * |
| 306 | */ |
| 307 | private void addBookPane(String value, boolean type) { |
| 308 | GuiReaderGroup bookPane = new GuiReaderGroup(reader, value, color); |
| 309 | if (type) { |
| 310 | booksByType.put(bookPane, value); |
| 311 | } else { |
| 312 | booksByAuthor.put(bookPane, value); |
| 313 | } |
| 314 | |
| 315 | this.invalidate(); |
| 316 | pane.invalidate(); |
| 317 | pane.add(bookPane); |
| 318 | pane.validate(); |
| 319 | this.validate(); |
| 320 | |
| 321 | bookPane.setActionListener(new BookActionListener() { |
| 322 | @Override |
| 323 | public void select(GuiReaderBook book) { |
| 324 | selectedBook = book; |
| 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public void popupRequested(GuiReaderBook book, MouseEvent e) { |
| 329 | JPopupMenu popup = new JPopupMenu(); |
| 330 | popup.add(createMenuItemOpenBook()); |
| 331 | popup.addSeparator(); |
| 332 | popup.add(createMenuItemExport()); |
| 333 | popup.add(createMenuItemMoveTo(true)); |
| 334 | popup.add(createMenuItemSetCover()); |
| 335 | popup.add(createMenuItemClearCache()); |
| 336 | popup.add(createMenuItemRedownload()); |
| 337 | popup.addSeparator(); |
| 338 | popup.add(createMenuItemRename(true)); |
| 339 | popup.add(createMenuItemSetAuthor(true)); |
| 340 | popup.addSeparator(); |
| 341 | popup.add(createMenuItemDelete()); |
| 342 | popup.addSeparator(); |
| 343 | popup.add(createMenuItemProperties()); |
| 344 | popup.show(e.getComponent(), e.getX(), e.getY()); |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public void action(final GuiReaderBook book) { |
| 349 | openBook(book); |
| 350 | } |
| 351 | }); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Clear the pane from any book that may be present, usually prior to adding |
| 356 | * new ones. |
| 357 | */ |
| 358 | private void removeBookPanes() { |
| 359 | booksByType.clear(); |
| 360 | booksByAuthor.clear(); |
| 361 | pane.invalidate(); |
| 362 | this.invalidate(); |
| 363 | pane.removeAll(); |
| 364 | pane.validate(); |
| 365 | this.validate(); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Refresh the list of {@link GuiReaderBook}s from disk. |
| 370 | */ |
| 371 | private void refreshBooks() { |
| 372 | for (GuiReaderGroup group : booksByType.keySet()) { |
| 373 | List<MetaData> stories = reader.getLibrary().getListBySource( |
| 374 | booksByType.get(group)); |
| 375 | group.refreshBooks(stories, words); |
| 376 | } |
| 377 | |
| 378 | for (GuiReaderGroup group : booksByAuthor.keySet()) { |
| 379 | List<MetaData> stories = reader.getLibrary().getListByAuthor( |
| 380 | booksByAuthor.get(group)); |
| 381 | group.refreshBooks(stories, words); |
| 382 | } |
| 383 | |
| 384 | pane.repaint(); |
| 385 | this.repaint(); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Create the main menu bar. |
| 390 | * |
| 391 | * @param libOk |
| 392 | * the library can be queried |
| 393 | * |
| 394 | * @return the bar |
| 395 | */ |
| 396 | private JMenuBar createMenu(boolean libOk) { |
| 397 | bar = new JMenuBar(); |
| 398 | |
| 399 | JMenu file = new JMenu("File"); |
| 400 | file.setMnemonic(KeyEvent.VK_F); |
| 401 | |
| 402 | JMenuItem imprt = new JMenuItem("Import URL...", KeyEvent.VK_U); |
| 403 | imprt.addActionListener(new ActionListener() { |
| 404 | @Override |
| 405 | public void actionPerformed(ActionEvent e) { |
| 406 | imprt(true); |
| 407 | } |
| 408 | }); |
| 409 | JMenuItem imprtF = new JMenuItem("Import File...", KeyEvent.VK_F); |
| 410 | imprtF.addActionListener(new ActionListener() { |
| 411 | @Override |
| 412 | public void actionPerformed(ActionEvent e) { |
| 413 | imprt(false); |
| 414 | } |
| 415 | }); |
| 416 | JMenuItem exit = new JMenuItem("Exit", KeyEvent.VK_X); |
| 417 | exit.addActionListener(new ActionListener() { |
| 418 | @Override |
| 419 | public void actionPerformed(ActionEvent e) { |
| 420 | GuiReaderFrame.this.dispatchEvent(new WindowEvent( |
| 421 | GuiReaderFrame.this, WindowEvent.WINDOW_CLOSING)); |
| 422 | } |
| 423 | }); |
| 424 | |
| 425 | file.add(createMenuItemOpenBook()); |
| 426 | file.add(createMenuItemExport()); |
| 427 | file.add(createMenuItemMoveTo(libOk)); |
| 428 | file.addSeparator(); |
| 429 | file.add(imprt); |
| 430 | file.add(imprtF); |
| 431 | file.addSeparator(); |
| 432 | file.add(createMenuItemRename(libOk)); |
| 433 | file.add(createMenuItemSetAuthor(libOk)); |
| 434 | file.addSeparator(); |
| 435 | file.add(exit); |
| 436 | |
| 437 | bar.add(file); |
| 438 | |
| 439 | JMenu edit = new JMenu("Edit"); |
| 440 | edit.setMnemonic(KeyEvent.VK_E); |
| 441 | |
| 442 | edit.add(createMenuItemClearCache()); |
| 443 | edit.add(createMenuItemRedownload()); |
| 444 | edit.addSeparator(); |
| 445 | edit.add(createMenuItemDelete()); |
| 446 | |
| 447 | bar.add(edit); |
| 448 | |
| 449 | JMenu view = new JMenu("View"); |
| 450 | view.setMnemonic(KeyEvent.VK_V); |
| 451 | JMenuItem vauthors = new JMenuItem("Author"); |
| 452 | vauthors.setMnemonic(KeyEvent.VK_A); |
| 453 | vauthors.addActionListener(new ActionListener() { |
| 454 | @Override |
| 455 | public void actionPerformed(ActionEvent e) { |
| 456 | words = false; |
| 457 | refreshBooks(); |
| 458 | } |
| 459 | }); |
| 460 | view.add(vauthors); |
| 461 | JMenuItem vwords = new JMenuItem("Word count"); |
| 462 | vwords.setMnemonic(KeyEvent.VK_W); |
| 463 | vwords.addActionListener(new ActionListener() { |
| 464 | @Override |
| 465 | public void actionPerformed(ActionEvent e) { |
| 466 | words = true; |
| 467 | refreshBooks(); |
| 468 | } |
| 469 | }); |
| 470 | view.add(vwords); |
| 471 | bar.add(view); |
| 472 | |
| 473 | Map<String, List<String>> groupedSources = new HashMap<String, List<String>>(); |
| 474 | if (libOk) { |
| 475 | groupedSources = reader.getLibrary().getSourcesGrouped(); |
| 476 | } |
| 477 | JMenu sources = new JMenu("Sources"); |
| 478 | sources.setMnemonic(KeyEvent.VK_S); |
| 479 | populateMenuSA(sources, groupedSources, true); |
| 480 | bar.add(sources); |
| 481 | |
| 482 | Map<String, List<String>> goupedAuthors = new HashMap<String, List<String>>(); |
| 483 | if (libOk) { |
| 484 | goupedAuthors = reader.getLibrary().getAuthorsGrouped(); |
| 485 | } |
| 486 | JMenu authors = new JMenu("Authors"); |
| 487 | authors.setMnemonic(KeyEvent.VK_A); |
| 488 | populateMenuSA(authors, goupedAuthors, false); |
| 489 | bar.add(authors); |
| 490 | |
| 491 | JMenu options = new JMenu("Options"); |
| 492 | options.setMnemonic(KeyEvent.VK_O); |
| 493 | options.add(createMenuItemConfig()); |
| 494 | options.add(createMenuItemUiConfig()); |
| 495 | bar.add(options); |
| 496 | |
| 497 | return bar; |
| 498 | } |
| 499 | |
| 500 | // "" = [unknown] |
| 501 | private void populateMenuSA(JMenu menu, |
| 502 | Map<String, List<String>> groupedValues, boolean type) { |
| 503 | |
| 504 | // "All" and "Listing" special items |
| 505 | JMenuItem item = new JMenuItem("All"); |
| 506 | item.addActionListener(getActionOpenList(type, false)); |
| 507 | menu.add(item); |
| 508 | item = new JMenuItem("Listing"); |
| 509 | item.addActionListener(getActionOpenList(type, true)); |
| 510 | menu.add(item); |
| 511 | menu.addSeparator(); |
| 512 | |
| 513 | for (final String value : groupedValues.keySet()) { |
| 514 | List<String> list = groupedValues.get(value); |
| 515 | if (type && list.size() == 1 && list.get(0).isEmpty()) { |
| 516 | // leaf item source/type |
| 517 | item = new JMenuItem(value.isEmpty() ? "[unknown]" : value); |
| 518 | item.addActionListener(getActionOpen(value, type)); |
| 519 | menu.add(item); |
| 520 | } else { |
| 521 | JMenu dir; |
| 522 | if (!type && groupedValues.size() == 1) { |
| 523 | // only one group of authors |
| 524 | dir = menu; |
| 525 | } else { |
| 526 | dir = new JMenu(value.isEmpty() ? "[unknown]" : value); |
| 527 | } |
| 528 | |
| 529 | for (String sub : list) { |
| 530 | // " " instead of "" for the visual height |
| 531 | String itemName = sub.isEmpty() ? " " : sub; |
| 532 | String actualValue = value; |
| 533 | |
| 534 | if (type) { |
| 535 | if (!sub.isEmpty()) { |
| 536 | actualValue += "/" + sub; |
| 537 | } |
| 538 | } else { |
| 539 | actualValue = sub; |
| 540 | } |
| 541 | |
| 542 | item = new JMenuItem(itemName); |
| 543 | item.addActionListener(getActionOpen(actualValue, type)); |
| 544 | dir.add(item); |
| 545 | } |
| 546 | |
| 547 | if (menu != dir) { |
| 548 | menu.add(dir); |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Return an {@link ActionListener} that will set the given source (type) as |
| 556 | * the selected/displayed one. |
| 557 | * |
| 558 | * @param type |
| 559 | * the type (source) to select, cannot be NULL |
| 560 | * |
| 561 | * @return the {@link ActionListener} |
| 562 | */ |
| 563 | private ActionListener getActionOpen(final String source, final boolean type) { |
| 564 | return new ActionListener() { |
| 565 | @Override |
| 566 | public void actionPerformed(ActionEvent e) { |
| 567 | removeBookPanes(); |
| 568 | addBookPane(source, type); |
| 569 | refreshBooks(); |
| 570 | } |
| 571 | }; |
| 572 | } |
| 573 | |
| 574 | private ActionListener getActionOpenList(final boolean type, |
| 575 | final boolean listMode) { |
| 576 | return new ActionListener() { |
| 577 | @Override |
| 578 | public void actionPerformed(ActionEvent e) { |
| 579 | removeBookPanes(); |
| 580 | addBookPane(type, listMode); |
| 581 | refreshBooks(); |
| 582 | } |
| 583 | }; |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Create the Fanfix Configuration menu item. |
| 588 | * |
| 589 | * @return the item |
| 590 | */ |
| 591 | private JMenuItem createMenuItemConfig() { |
| 592 | final String title = "Fanfix Configuration"; |
| 593 | JMenuItem item = new JMenuItem(title); |
| 594 | item.setMnemonic(KeyEvent.VK_F); |
| 595 | |
| 596 | item.addActionListener(new ActionListener() { |
| 597 | @Override |
| 598 | public void actionPerformed(ActionEvent e) { |
| 599 | ConfigEditor<Config> ed = new ConfigEditor<Config>( |
| 600 | Config.class, Instance.getConfig(), |
| 601 | "This is where you configure the options of the program."); |
| 602 | JFrame frame = new JFrame(title); |
| 603 | frame.add(ed); |
| 604 | frame.setSize(800, 600); |
| 605 | frame.setVisible(true); |
| 606 | } |
| 607 | }); |
| 608 | |
| 609 | return item; |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * Create the UI Configuration menu item. |
| 614 | * |
| 615 | * @return the item |
| 616 | */ |
| 617 | private JMenuItem createMenuItemUiConfig() { |
| 618 | final String title = "UI Configuration"; |
| 619 | JMenuItem item = new JMenuItem(title); |
| 620 | item.setMnemonic(KeyEvent.VK_U); |
| 621 | |
| 622 | item.addActionListener(new ActionListener() { |
| 623 | @Override |
| 624 | public void actionPerformed(ActionEvent e) { |
| 625 | ConfigEditor<UiConfig> ed = new ConfigEditor<UiConfig>( |
| 626 | UiConfig.class, Instance.getUiConfig(), |
| 627 | "This is where you configure the graphical appearence of the program."); |
| 628 | JFrame frame = new JFrame(title); |
| 629 | frame.add(ed); |
| 630 | frame.setSize(800, 600); |
| 631 | frame.setVisible(true); |
| 632 | } |
| 633 | }); |
| 634 | |
| 635 | return item; |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * Create the export menu item. |
| 640 | * |
| 641 | * @return the item |
| 642 | */ |
| 643 | private JMenuItem createMenuItemExport() { |
| 644 | final JFileChooser fc = new JFileChooser(); |
| 645 | fc.setAcceptAllFileFilterUsed(false); |
| 646 | |
| 647 | final Map<FileFilter, OutputType> filters = new HashMap<FileFilter, OutputType>(); |
| 648 | for (OutputType type : OutputType.values()) { |
| 649 | String ext = type.getDefaultExtension(false); |
| 650 | String desc = type.getDesc(false); |
| 651 | |
| 652 | if (ext == null || ext.isEmpty()) { |
| 653 | filters.put(createAllFilter(desc), type); |
| 654 | } else { |
| 655 | filters.put(new FileNameExtensionFilter(desc, ext), type); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | // First the "ALL" filters, then, the extension filters |
| 660 | for (Entry<FileFilter, OutputType> entry : filters.entrySet()) { |
| 661 | if (!(entry.getKey() instanceof FileNameExtensionFilter)) { |
| 662 | fc.addChoosableFileFilter(entry.getKey()); |
| 663 | } |
| 664 | } |
| 665 | for (Entry<FileFilter, OutputType> entry : filters.entrySet()) { |
| 666 | if (entry.getKey() instanceof FileNameExtensionFilter) { |
| 667 | fc.addChoosableFileFilter(entry.getKey()); |
| 668 | } |
| 669 | } |
| 670 | // |
| 671 | |
| 672 | JMenuItem export = new JMenuItem("Save as...", KeyEvent.VK_S); |
| 673 | export.addActionListener(new ActionListener() { |
| 674 | @Override |
| 675 | public void actionPerformed(ActionEvent e) { |
| 676 | if (selectedBook != null) { |
| 677 | fc.showDialog(GuiReaderFrame.this, "Save"); |
| 678 | if (fc.getSelectedFile() != null) { |
| 679 | final OutputType type = filters.get(fc.getFileFilter()); |
| 680 | final String path = fc.getSelectedFile() |
| 681 | .getAbsolutePath() |
| 682 | + type.getDefaultExtension(false); |
| 683 | final Progress pg = new Progress(); |
| 684 | outOfUi(pg, new Runnable() { |
| 685 | @Override |
| 686 | public void run() { |
| 687 | try { |
| 688 | reader.getLibrary().export( |
| 689 | selectedBook.getMeta().getLuid(), |
| 690 | type, path, pg); |
| 691 | } catch (IOException e) { |
| 692 | Instance.getTraceHandler().error(e); |
| 693 | } |
| 694 | } |
| 695 | }); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | }); |
| 700 | |
| 701 | return export; |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * Create a {@link FileFilter} that accepts all files and return the given |
| 706 | * description. |
| 707 | * |
| 708 | * @param desc |
| 709 | * the description |
| 710 | * |
| 711 | * @return the filter |
| 712 | */ |
| 713 | private FileFilter createAllFilter(final String desc) { |
| 714 | return new FileFilter() { |
| 715 | @Override |
| 716 | public String getDescription() { |
| 717 | return desc; |
| 718 | } |
| 719 | |
| 720 | @Override |
| 721 | public boolean accept(File f) { |
| 722 | return true; |
| 723 | } |
| 724 | }; |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * Create the refresh (delete cache) menu item. |
| 729 | * |
| 730 | * @return the item |
| 731 | */ |
| 732 | private JMenuItem createMenuItemClearCache() { |
| 733 | JMenuItem refresh = new JMenuItem("Clear cache", KeyEvent.VK_C); |
| 734 | refresh.addActionListener(new ActionListener() { |
| 735 | @Override |
| 736 | public void actionPerformed(ActionEvent e) { |
| 737 | if (selectedBook != null) { |
| 738 | outOfUi(null, new Runnable() { |
| 739 | @Override |
| 740 | public void run() { |
| 741 | reader.clearLocalReaderCache(selectedBook.getMeta() |
| 742 | .getLuid()); |
| 743 | selectedBook.setCached(false); |
| 744 | GuiReaderCoverImager.clearIcon(selectedBook |
| 745 | .getMeta()); |
| 746 | SwingUtilities.invokeLater(new Runnable() { |
| 747 | @Override |
| 748 | public void run() { |
| 749 | selectedBook.repaint(); |
| 750 | } |
| 751 | }); |
| 752 | } |
| 753 | }); |
| 754 | } |
| 755 | } |
| 756 | }); |
| 757 | |
| 758 | return refresh; |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * Create the "move to" menu item. |
| 763 | * |
| 764 | * @param libOk |
| 765 | * the library can be queried |
| 766 | * |
| 767 | * @return the item |
| 768 | */ |
| 769 | private JMenuItem createMenuItemMoveTo(boolean libOk) { |
| 770 | JMenu changeTo = new JMenu("Move to"); |
| 771 | changeTo.setMnemonic(KeyEvent.VK_M); |
| 772 | |
| 773 | Map<String, List<String>> groupedSources = new HashMap<String, List<String>>(); |
| 774 | if (libOk) { |
| 775 | groupedSources = reader.getLibrary().getSourcesGrouped(); |
| 776 | } |
| 777 | |
| 778 | JMenuItem item = new JMenuItem("New type..."); |
| 779 | item.addActionListener(createMoveAction("SOURCE", null)); |
| 780 | changeTo.add(item); |
| 781 | changeTo.addSeparator(); |
| 782 | |
| 783 | for (final String type : groupedSources.keySet()) { |
| 784 | List<String> list = groupedSources.get(type); |
| 785 | if (list.size() == 1 && list.get(0).isEmpty()) { |
| 786 | item = new JMenuItem(type); |
| 787 | item.addActionListener(createMoveAction("SOURCE", type)); |
| 788 | changeTo.add(item); |
| 789 | } else { |
| 790 | JMenu dir = new JMenu(type); |
| 791 | for (String sub : list) { |
| 792 | // " " instead of "" for the visual height |
| 793 | String itemName = sub.isEmpty() ? " " : sub; |
| 794 | String actualType = type; |
| 795 | if (!sub.isEmpty()) { |
| 796 | actualType += "/" + sub; |
| 797 | } |
| 798 | |
| 799 | item = new JMenuItem(itemName); |
| 800 | item.addActionListener(createMoveAction("SOURCE", |
| 801 | actualType)); |
| 802 | dir.add(item); |
| 803 | } |
| 804 | changeTo.add(dir); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | return changeTo; |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * Create the "set author" menu item. |
| 813 | * |
| 814 | * @param libOk |
| 815 | * the library can be queried |
| 816 | * |
| 817 | * @return the item |
| 818 | */ |
| 819 | private JMenuItem createMenuItemSetAuthor(boolean libOk) { |
| 820 | JMenu changeTo = new JMenu("Set author"); |
| 821 | changeTo.setMnemonic(KeyEvent.VK_A); |
| 822 | |
| 823 | // New author |
| 824 | JMenuItem newItem = new JMenuItem("New author..."); |
| 825 | changeTo.add(newItem); |
| 826 | changeTo.addSeparator(); |
| 827 | newItem.addActionListener(createMoveAction("AUTHOR", null)); |
| 828 | |
| 829 | // Existing authors |
| 830 | if (libOk) { |
| 831 | Map<String, List<String>> groupedAuthors = reader.getLibrary() |
| 832 | .getAuthorsGrouped(); |
| 833 | |
| 834 | if (groupedAuthors.size() > 1) { |
| 835 | for (String key : groupedAuthors.keySet()) { |
| 836 | JMenu group = new JMenu(key); |
| 837 | for (String value : groupedAuthors.get(key)) { |
| 838 | JMenuItem item = new JMenuItem(value); |
| 839 | item.addActionListener(createMoveAction("AUTHOR", value)); |
| 840 | group.add(item); |
| 841 | } |
| 842 | changeTo.add(group); |
| 843 | } |
| 844 | } else if (groupedAuthors.size() == 1) { |
| 845 | for (String value : groupedAuthors.values().iterator().next()) { |
| 846 | JMenuItem item = new JMenuItem(value); |
| 847 | item.addActionListener(createMoveAction("AUTHOR", value)); |
| 848 | changeTo.add(item); |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | return changeTo; |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * Create the "rename" menu item. |
| 858 | * |
| 859 | * @param libOk |
| 860 | * the library can be queried |
| 861 | * |
| 862 | * @return the item |
| 863 | */ |
| 864 | private JMenuItem createMenuItemRename( |
| 865 | @SuppressWarnings("unused") boolean libOk) { |
| 866 | JMenuItem changeTo = new JMenuItem("Rename..."); |
| 867 | changeTo.setMnemonic(KeyEvent.VK_R); |
| 868 | changeTo.addActionListener(createMoveAction("TITLE", null)); |
| 869 | return changeTo; |
| 870 | } |
| 871 | |
| 872 | private ActionListener createMoveAction(final String what, final String type) { |
| 873 | return new ActionListener() { |
| 874 | @Override |
| 875 | public void actionPerformed(ActionEvent e) { |
| 876 | if (selectedBook != null) { |
| 877 | String changeTo = type; |
| 878 | if (type == null) { |
| 879 | String init = ""; |
| 880 | if (what.equals("SOURCE")) { |
| 881 | init = selectedBook.getMeta().getSource(); |
| 882 | } else if (what.equals("TITLE")) { |
| 883 | init = selectedBook.getMeta().getTitle(); |
| 884 | } else if (what.equals("AUTHOR")) { |
| 885 | init = selectedBook.getMeta().getAuthor(); |
| 886 | } |
| 887 | |
| 888 | Object rep = JOptionPane.showInputDialog( |
| 889 | GuiReaderFrame.this, "Move to:", |
| 890 | "Moving story", JOptionPane.QUESTION_MESSAGE, |
| 891 | null, null, init); |
| 892 | |
| 893 | if (rep == null) { |
| 894 | return; |
| 895 | } |
| 896 | |
| 897 | changeTo = rep.toString(); |
| 898 | } |
| 899 | |
| 900 | final String fChangeTo = changeTo; |
| 901 | outOfUi(null, new Runnable() { |
| 902 | @Override |
| 903 | public void run() { |
| 904 | if (what.equals("SOURCE")) { |
| 905 | reader.changeSource(selectedBook.getMeta() |
| 906 | .getLuid(), fChangeTo); |
| 907 | } else if (what.equals("TITLE")) { |
| 908 | reader.changeTitle(selectedBook.getMeta() |
| 909 | .getLuid(), fChangeTo); |
| 910 | } else if (what.equals("AUTHOR")) { |
| 911 | reader.changeAuthor(selectedBook.getMeta() |
| 912 | .getLuid(), fChangeTo); |
| 913 | } |
| 914 | |
| 915 | selectedBook = null; |
| 916 | |
| 917 | SwingUtilities.invokeLater(new Runnable() { |
| 918 | @Override |
| 919 | public void run() { |
| 920 | setJMenuBar(createMenu(true)); |
| 921 | } |
| 922 | }); |
| 923 | } |
| 924 | }); |
| 925 | } |
| 926 | } |
| 927 | }; |
| 928 | } |
| 929 | |
| 930 | /** |
| 931 | * Create the redownload (then delete original) menu item. |
| 932 | * |
| 933 | * @return the item |
| 934 | */ |
| 935 | private JMenuItem createMenuItemRedownload() { |
| 936 | JMenuItem refresh = new JMenuItem("Redownload", KeyEvent.VK_R); |
| 937 | refresh.addActionListener(new ActionListener() { |
| 938 | @Override |
| 939 | public void actionPerformed(ActionEvent e) { |
| 940 | if (selectedBook != null) { |
| 941 | final MetaData meta = selectedBook.getMeta(); |
| 942 | imprt(meta.getUrl(), new StoryRunnable() { |
| 943 | @Override |
| 944 | public void run(Story story) { |
| 945 | reader.delete(meta.getLuid()); |
| 946 | GuiReaderFrame.this.selectedBook = null; |
| 947 | MetaData newMeta = story.getMeta(); |
| 948 | if (!newMeta.getSource().equals(meta.getSource())) { |
| 949 | reader.changeSource(newMeta.getLuid(), |
| 950 | meta.getSource()); |
| 951 | } |
| 952 | } |
| 953 | }, "Removing old copy"); |
| 954 | } |
| 955 | } |
| 956 | }); |
| 957 | |
| 958 | return refresh; |
| 959 | } |
| 960 | |
| 961 | /** |
| 962 | * Create the delete menu item. |
| 963 | * |
| 964 | * @return the item |
| 965 | */ |
| 966 | private JMenuItem createMenuItemDelete() { |
| 967 | JMenuItem delete = new JMenuItem("Delete", KeyEvent.VK_D); |
| 968 | delete.addActionListener(new ActionListener() { |
| 969 | @Override |
| 970 | public void actionPerformed(ActionEvent e) { |
| 971 | if (selectedBook != null) { |
| 972 | outOfUi(null, new Runnable() { |
| 973 | @Override |
| 974 | public void run() { |
| 975 | reader.delete(selectedBook.getMeta().getLuid()); |
| 976 | selectedBook = null; |
| 977 | } |
| 978 | }); |
| 979 | } |
| 980 | } |
| 981 | }); |
| 982 | |
| 983 | return delete; |
| 984 | } |
| 985 | |
| 986 | /** |
| 987 | * Create the properties menu item. |
| 988 | * |
| 989 | * @return the item |
| 990 | */ |
| 991 | private JMenuItem createMenuItemProperties() { |
| 992 | JMenuItem delete = new JMenuItem("Properties", KeyEvent.VK_P); |
| 993 | delete.addActionListener(new ActionListener() { |
| 994 | @Override |
| 995 | public void actionPerformed(ActionEvent e) { |
| 996 | if (selectedBook != null) { |
| 997 | outOfUi(null, new Runnable() { |
| 998 | @Override |
| 999 | public void run() { |
| 1000 | final MetaData meta = selectedBook.getMeta(); |
| 1001 | new JFrame() { |
| 1002 | private static final long serialVersionUID = 1L; |
| 1003 | @SuppressWarnings("unused") |
| 1004 | private Object init = init(); |
| 1005 | |
| 1006 | private Object init() { |
| 1007 | // Borders |
| 1008 | int top = 20; |
| 1009 | int space = 10; |
| 1010 | |
| 1011 | // Image |
| 1012 | ImageIcon img = GuiReaderCoverImager |
| 1013 | .generateCoverIcon( |
| 1014 | reader.getLibrary(), meta); |
| 1015 | |
| 1016 | // frame |
| 1017 | setTitle(meta.getLuid() + ": " |
| 1018 | + meta.getTitle()); |
| 1019 | |
| 1020 | setSize(800, img.getIconHeight() + 2 * top); |
| 1021 | setLayout(new BorderLayout()); |
| 1022 | |
| 1023 | // Main panel |
| 1024 | JPanel mainPanel = new JPanel( |
| 1025 | new BorderLayout()); |
| 1026 | JPanel mainPanelKeys = new JPanel(); |
| 1027 | mainPanelKeys.setLayout(new BoxLayout( |
| 1028 | mainPanelKeys, BoxLayout.Y_AXIS)); |
| 1029 | JPanel mainPanelValues = new JPanel(); |
| 1030 | mainPanelValues.setLayout(new BoxLayout( |
| 1031 | mainPanelValues, BoxLayout.Y_AXIS)); |
| 1032 | |
| 1033 | mainPanel.add(mainPanelKeys, |
| 1034 | BorderLayout.WEST); |
| 1035 | mainPanel.add(mainPanelValues, |
| 1036 | BorderLayout.CENTER); |
| 1037 | |
| 1038 | List<Entry<String, String>> infos = BasicReader |
| 1039 | .getMetaDesc(meta); |
| 1040 | |
| 1041 | Color trans = new Color(0, 0, 0, 1); |
| 1042 | for (Entry<String, String> info : infos) { |
| 1043 | JTextArea key = new JTextArea(info |
| 1044 | .getKey()); |
| 1045 | key.setFont(new Font(key.getFont() |
| 1046 | .getFontName(), Font.BOLD, key |
| 1047 | .getFont().getSize())); |
| 1048 | key.setEditable(false); |
| 1049 | key.setLineWrap(false); |
| 1050 | key.setBackground(trans); |
| 1051 | mainPanelKeys.add(key); |
| 1052 | |
| 1053 | JTextArea value = new JTextArea(info |
| 1054 | .getValue()); |
| 1055 | value.setEditable(false); |
| 1056 | value.setLineWrap(false); |
| 1057 | value.setBackground(trans); |
| 1058 | mainPanelValues.add(value); |
| 1059 | } |
| 1060 | |
| 1061 | // Image |
| 1062 | JLabel imgLabel = new JLabel(img); |
| 1063 | imgLabel.setVerticalAlignment(JLabel.TOP); |
| 1064 | |
| 1065 | // Borders |
| 1066 | mainPanelKeys.setBorder(BorderFactory |
| 1067 | .createEmptyBorder(top, space, 0, 0)); |
| 1068 | mainPanelValues.setBorder(BorderFactory |
| 1069 | .createEmptyBorder(top, space, 0, 0)); |
| 1070 | imgLabel.setBorder(BorderFactory |
| 1071 | .createEmptyBorder(0, space, 0, 0)); |
| 1072 | |
| 1073 | // Add all |
| 1074 | add(imgLabel, BorderLayout.WEST); |
| 1075 | add(mainPanel, BorderLayout.CENTER); |
| 1076 | |
| 1077 | return null; |
| 1078 | } |
| 1079 | |
| 1080 | }.setVisible(true); |
| 1081 | } |
| 1082 | }); |
| 1083 | } |
| 1084 | } |
| 1085 | }); |
| 1086 | |
| 1087 | return delete; |
| 1088 | } |
| 1089 | |
| 1090 | /** |
| 1091 | * Create the open menu item for a book or a source (no LUID). |
| 1092 | * |
| 1093 | * @return the item |
| 1094 | */ |
| 1095 | private JMenuItem createMenuItemOpenBook() { |
| 1096 | JMenuItem open = new JMenuItem("Open", KeyEvent.VK_O); |
| 1097 | open.addActionListener(new ActionListener() { |
| 1098 | @Override |
| 1099 | public void actionPerformed(ActionEvent e) { |
| 1100 | if (selectedBook != null) { |
| 1101 | if (selectedBook.getMeta().getLuid() == null) { |
| 1102 | removeBookPanes(); |
| 1103 | addBookPane(selectedBook.getMeta().getSource(), true); |
| 1104 | refreshBooks(); |
| 1105 | } else { |
| 1106 | openBook(selectedBook); |
| 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | }); |
| 1111 | |
| 1112 | return open; |
| 1113 | } |
| 1114 | |
| 1115 | /** |
| 1116 | * Create the SetCover menu item for a book to change the linked source |
| 1117 | * cover. |
| 1118 | * |
| 1119 | * @return the item |
| 1120 | */ |
| 1121 | private JMenuItem createMenuItemSetCover() { |
| 1122 | JMenuItem open = new JMenuItem("Set as cover for source", KeyEvent.VK_C); |
| 1123 | open.addActionListener(new ActionListener() { |
| 1124 | @Override |
| 1125 | public void actionPerformed(ActionEvent e) { |
| 1126 | if (selectedBook != null) { |
| 1127 | reader.getLibrary().setSourceCover( |
| 1128 | selectedBook.getMeta().getSource(), |
| 1129 | selectedBook.getMeta().getLuid()); |
| 1130 | MetaData source = selectedBook.getMeta().clone(); |
| 1131 | source.setLuid(null); |
| 1132 | GuiReaderCoverImager.clearIcon(source); |
| 1133 | } |
| 1134 | } |
| 1135 | }); |
| 1136 | |
| 1137 | return open; |
| 1138 | } |
| 1139 | |
| 1140 | /** |
| 1141 | * Open a {@link GuiReaderBook} item. |
| 1142 | * |
| 1143 | * @param book |
| 1144 | * the {@link GuiReaderBook} to open |
| 1145 | */ |
| 1146 | private void openBook(final GuiReaderBook book) { |
| 1147 | final Progress pg = new Progress(); |
| 1148 | outOfUi(pg, new Runnable() { |
| 1149 | @Override |
| 1150 | public void run() { |
| 1151 | try { |
| 1152 | reader.read(book.getMeta().getLuid(), false, pg); |
| 1153 | SwingUtilities.invokeLater(new Runnable() { |
| 1154 | @Override |
| 1155 | public void run() { |
| 1156 | book.setCached(true); |
| 1157 | } |
| 1158 | }); |
| 1159 | } catch (IOException e) { |
| 1160 | // TODO: error message? |
| 1161 | Instance.getTraceHandler().error(e); |
| 1162 | } |
| 1163 | } |
| 1164 | }); |
| 1165 | } |
| 1166 | |
| 1167 | /** |
| 1168 | * Process the given action out of the Swing UI thread and link the given |
| 1169 | * {@link ProgressBar} to the action. |
| 1170 | * <p> |
| 1171 | * The code will make sure that the {@link ProgressBar} (if not NULL) is set |
| 1172 | * to done when the action is done. |
| 1173 | * |
| 1174 | * @param progress |
| 1175 | * the {@link ProgressBar} or NULL |
| 1176 | * @param run |
| 1177 | * the action to run |
| 1178 | */ |
| 1179 | private void outOfUi(Progress progress, final Runnable run) { |
| 1180 | final Progress pg = new Progress(); |
| 1181 | final Progress reload = new Progress("Reload books"); |
| 1182 | if (progress == null) { |
| 1183 | progress = new Progress(); |
| 1184 | } |
| 1185 | |
| 1186 | pg.addProgress(progress, 90); |
| 1187 | pg.addProgress(reload, 10); |
| 1188 | |
| 1189 | invalidate(); |
| 1190 | pgBar.setProgress(pg); |
| 1191 | validate(); |
| 1192 | setEnabled(false); |
| 1193 | |
| 1194 | new Thread(new Runnable() { |
| 1195 | @Override |
| 1196 | public void run() { |
| 1197 | try { |
| 1198 | run.run(); |
| 1199 | refreshBooks(); |
| 1200 | } finally { |
| 1201 | reload.done(); |
| 1202 | if (!pg.isDone()) { |
| 1203 | // will trigger pgBar ActionListener: |
| 1204 | pg.done(); |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | }, "outOfUi thread").start(); |
| 1209 | } |
| 1210 | |
| 1211 | /** |
| 1212 | * Import a {@link Story} into the main {@link LocalLibrary}. |
| 1213 | * <p> |
| 1214 | * Should be called inside the UI thread. |
| 1215 | * |
| 1216 | * @param askUrl |
| 1217 | * TRUE for an {@link URL}, false for a {@link File} |
| 1218 | */ |
| 1219 | private void imprt(boolean askUrl) { |
| 1220 | JFileChooser fc = new JFileChooser(); |
| 1221 | |
| 1222 | Object url; |
| 1223 | if (askUrl) { |
| 1224 | String clipboard = ""; |
| 1225 | try { |
| 1226 | clipboard = ("" + Toolkit.getDefaultToolkit() |
| 1227 | .getSystemClipboard().getData(DataFlavor.stringFlavor)) |
| 1228 | .trim(); |
| 1229 | } catch (Exception e) { |
| 1230 | // No data will be handled |
| 1231 | } |
| 1232 | |
| 1233 | if (clipboard == null || !clipboard.startsWith("http")) { |
| 1234 | clipboard = ""; |
| 1235 | } |
| 1236 | |
| 1237 | url = JOptionPane.showInputDialog(GuiReaderFrame.this, |
| 1238 | "url of the story to import?", "Importing from URL", |
| 1239 | JOptionPane.QUESTION_MESSAGE, null, null, clipboard); |
| 1240 | } else if (fc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION) { |
| 1241 | url = fc.getSelectedFile().getAbsolutePath(); |
| 1242 | } else { |
| 1243 | url = null; |
| 1244 | } |
| 1245 | |
| 1246 | if (url != null && !url.toString().isEmpty()) { |
| 1247 | imprt(url.toString(), null, null); |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | /** |
| 1252 | * Actually import the {@link Story} into the main {@link LocalLibrary}. |
| 1253 | * <p> |
| 1254 | * Should be called inside the UI thread. |
| 1255 | * |
| 1256 | * @param url |
| 1257 | * the {@link Story} to import by {@link URL} |
| 1258 | * @param onSuccess |
| 1259 | * Action to execute on success |
| 1260 | */ |
| 1261 | private void imprt(final String url, final StoryRunnable onSuccess, |
| 1262 | String onSuccessPgName) { |
| 1263 | final Progress pg = new Progress(); |
| 1264 | final Progress pgImprt = new Progress(); |
| 1265 | final Progress pgOnSuccess = new Progress(onSuccessPgName); |
| 1266 | pg.addProgress(pgImprt, 95); |
| 1267 | pg.addProgress(pgOnSuccess, 5); |
| 1268 | |
| 1269 | outOfUi(pg, new Runnable() { |
| 1270 | @Override |
| 1271 | public void run() { |
| 1272 | Exception ex = null; |
| 1273 | Story story = null; |
| 1274 | try { |
| 1275 | story = reader.getLibrary().imprt(BasicReader.getUrl(url), |
| 1276 | pgImprt); |
| 1277 | } catch (IOException e) { |
| 1278 | ex = e; |
| 1279 | } |
| 1280 | |
| 1281 | final Exception e = ex; |
| 1282 | |
| 1283 | final boolean ok = (e == null); |
| 1284 | |
| 1285 | pgOnSuccess.setProgress(0); |
| 1286 | if (!ok) { |
| 1287 | if (e instanceof UnknownHostException) { |
| 1288 | error("URL not supported: " + url, "Cannot import URL", |
| 1289 | null); |
| 1290 | } else { |
| 1291 | error("Failed to import " + url + ": \n" |
| 1292 | + e.getMessage(), "Cannot import URL", e); |
| 1293 | } |
| 1294 | } else { |
| 1295 | if (onSuccess != null) { |
| 1296 | onSuccess.run(story); |
| 1297 | } |
| 1298 | } |
| 1299 | pgOnSuccess.done(); |
| 1300 | } |
| 1301 | }); |
| 1302 | } |
| 1303 | |
| 1304 | /** |
| 1305 | * Enables or disables this component, depending on the value of the |
| 1306 | * parameter <code>b</code>. An enabled component can respond to user input |
| 1307 | * and generate events. Components are enabled initially by default. |
| 1308 | * <p> |
| 1309 | * Disabling this component will also affect its children. |
| 1310 | * |
| 1311 | * @param b |
| 1312 | * If <code>true</code>, this component is enabled; otherwise |
| 1313 | * this component is disabled |
| 1314 | */ |
| 1315 | @Override |
| 1316 | public void setEnabled(boolean b) { |
| 1317 | if (bar != null) { |
| 1318 | bar.setEnabled(b); |
| 1319 | } |
| 1320 | |
| 1321 | for (GuiReaderGroup group : booksByType.keySet()) { |
| 1322 | group.setEnabled(b); |
| 1323 | } |
| 1324 | for (GuiReaderGroup group : booksByAuthor.keySet()) { |
| 1325 | group.setEnabled(b); |
| 1326 | } |
| 1327 | super.setEnabled(b); |
| 1328 | repaint(); |
| 1329 | } |
| 1330 | |
| 1331 | /** |
| 1332 | * Display an error message and log the linked {@link Exception}. |
| 1333 | * |
| 1334 | * @param message |
| 1335 | * the message |
| 1336 | * @param title |
| 1337 | * the title of the error message |
| 1338 | * @param e |
| 1339 | * the exception to log if any |
| 1340 | */ |
| 1341 | private void error(final String message, final String title, Exception e) { |
| 1342 | Instance.getTraceHandler().error(title + ": " + message); |
| 1343 | if (e != null) { |
| 1344 | Instance.getTraceHandler().error(e); |
| 1345 | } |
| 1346 | |
| 1347 | SwingUtilities.invokeLater(new Runnable() { |
| 1348 | @Override |
| 1349 | public void run() { |
| 1350 | JOptionPane.showMessageDialog(GuiReaderFrame.this, message, |
| 1351 | title, JOptionPane.ERROR_MESSAGE); |
| 1352 | } |
| 1353 | }); |
| 1354 | } |
| 1355 | } |