| 1 | package be.nikiroo.fanfix.reader.ui; |
| 2 | |
| 3 | import java.awt.BorderLayout; |
| 4 | import java.awt.Color; |
| 5 | import java.awt.EventQueue; |
| 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.MouseEvent; |
| 12 | import java.io.File; |
| 13 | import java.io.IOException; |
| 14 | import java.lang.reflect.InvocationTargetException; |
| 15 | import java.net.URL; |
| 16 | import java.net.UnknownHostException; |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.List; |
| 19 | import java.util.Map; |
| 20 | import java.util.TreeMap; |
| 21 | |
| 22 | import javax.swing.BoxLayout; |
| 23 | import javax.swing.JFileChooser; |
| 24 | import javax.swing.JLabel; |
| 25 | import javax.swing.JMenuBar; |
| 26 | import javax.swing.JOptionPane; |
| 27 | import javax.swing.JPanel; |
| 28 | import javax.swing.JPopupMenu; |
| 29 | import javax.swing.JScrollPane; |
| 30 | import javax.swing.SwingConstants; |
| 31 | import javax.swing.SwingUtilities; |
| 32 | |
| 33 | import be.nikiroo.fanfix.Instance; |
| 34 | import be.nikiroo.fanfix.bundles.UiConfig; |
| 35 | import be.nikiroo.fanfix.data.MetaData; |
| 36 | import be.nikiroo.fanfix.data.Story; |
| 37 | import be.nikiroo.fanfix.library.BasicLibrary; |
| 38 | import be.nikiroo.fanfix.library.BasicLibrary.Status; |
| 39 | import be.nikiroo.fanfix.library.LocalLibrary; |
| 40 | import be.nikiroo.fanfix.reader.BasicReader; |
| 41 | import be.nikiroo.fanfix.reader.ui.GuiReaderBook.BookActionListener; |
| 42 | import be.nikiroo.utils.Progress; |
| 43 | import be.nikiroo.utils.ui.ProgressBar; |
| 44 | |
| 45 | /** |
| 46 | * A {@link Frame} that will show a {@link GuiReaderBook} item for each |
| 47 | * {@link Story} in the main cache ({@link Instance#getCache()}), and offer a |
| 48 | * way to copy them to the {@link GuiReader} cache ( |
| 49 | * {@link BasicReader#getLibrary()}), read them, delete them... |
| 50 | * |
| 51 | * @author niki |
| 52 | */ |
| 53 | class GuiReaderMainPanel extends JPanel { |
| 54 | private static final long serialVersionUID = 1L; |
| 55 | private FrameHelper helper; |
| 56 | private Map<String, GuiReaderGroup> books; |
| 57 | private GuiReaderGroup bookPane; // for more "All" |
| 58 | private JPanel pane; |
| 59 | private Color color; |
| 60 | private ProgressBar pgBar; |
| 61 | private JMenuBar bar; |
| 62 | private GuiReaderBook selectedBook; |
| 63 | private boolean words; // words or authors (secondary info on books) |
| 64 | private boolean currentType; // type/source or author mode (All and Listing) |
| 65 | |
| 66 | /** |
| 67 | * An object that offers some helper methods to access the frame that host |
| 68 | * it and the Fanfix-related functions. |
| 69 | * |
| 70 | * @author niki |
| 71 | */ |
| 72 | public interface FrameHelper { |
| 73 | /** |
| 74 | * Return the reader associated to this {@link FrameHelper}. |
| 75 | * |
| 76 | * @return the reader |
| 77 | */ |
| 78 | public GuiReader getReader(); |
| 79 | |
| 80 | /** |
| 81 | * Create the main menu bar. |
| 82 | * <p> |
| 83 | * Will invalidate the layout. |
| 84 | * |
| 85 | * @param libOk |
| 86 | * the library can be queried |
| 87 | */ |
| 88 | public void createMenu(boolean libOk); |
| 89 | |
| 90 | /** |
| 91 | * Create a popup menu for a {@link GuiReaderBook} that represents a |
| 92 | * story. |
| 93 | * |
| 94 | * @return the popup menu to display |
| 95 | */ |
| 96 | public JPopupMenu createBookPopup(); |
| 97 | |
| 98 | /** |
| 99 | * Create a popup menu for a {@link GuiReaderBook} that represents a |
| 100 | * source/type or an author. |
| 101 | * |
| 102 | * @return the popup menu to display |
| 103 | */ |
| 104 | public JPopupMenu createSourceAuthorPopup(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * A {@link Runnable} with a {@link Story} parameter. |
| 109 | * |
| 110 | * @author niki |
| 111 | */ |
| 112 | public interface StoryRunnable { |
| 113 | /** |
| 114 | * Run the action. |
| 115 | * |
| 116 | * @param story |
| 117 | * the story |
| 118 | */ |
| 119 | public void run(Story story); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Create a new {@link GuiReaderMainPanel}. |
| 124 | * |
| 125 | * @param parent |
| 126 | * the associated {@link FrameHelper} to forward some commands |
| 127 | * and access its {@link LocalLibrary} |
| 128 | * @param type |
| 129 | * the type of {@link Story} to load, or NULL for all types |
| 130 | */ |
| 131 | public GuiReaderMainPanel(FrameHelper parent, String type) { |
| 132 | super(new BorderLayout(), true); |
| 133 | |
| 134 | this.helper = parent; |
| 135 | |
| 136 | pane = new JPanel(); |
| 137 | pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS)); |
| 138 | |
| 139 | Integer icolor = Instance.getUiConfig().getColor( |
| 140 | UiConfig.BACKGROUND_COLOR); |
| 141 | if (icolor != null) { |
| 142 | color = new Color(icolor); |
| 143 | setBackground(color); |
| 144 | pane.setBackground(color); |
| 145 | } |
| 146 | |
| 147 | JScrollPane scroll = new JScrollPane(pane); |
| 148 | scroll.getVerticalScrollBar().setUnitIncrement(16); |
| 149 | add(scroll, BorderLayout.CENTER); |
| 150 | |
| 151 | String message = parent.getReader().getLibrary().getLibraryName(); |
| 152 | if (!message.isEmpty()) { |
| 153 | JLabel name = new JLabel(message, SwingConstants.CENTER); |
| 154 | add(name, BorderLayout.NORTH); |
| 155 | } |
| 156 | |
| 157 | pgBar = new ProgressBar(); |
| 158 | add(pgBar, BorderLayout.SOUTH); |
| 159 | |
| 160 | pgBar.addActionListener(new ActionListener() { |
| 161 | @Override |
| 162 | public void actionPerformed(ActionEvent e) { |
| 163 | pgBar.invalidate(); |
| 164 | pgBar.setProgress(null); |
| 165 | setEnabled(true); |
| 166 | validate(); |
| 167 | } |
| 168 | }); |
| 169 | |
| 170 | pgBar.addUpdateListener(new ActionListener() { |
| 171 | @Override |
| 172 | public void actionPerformed(ActionEvent e) { |
| 173 | pgBar.invalidate(); |
| 174 | validate(); |
| 175 | repaint(); |
| 176 | } |
| 177 | }); |
| 178 | |
| 179 | books = new TreeMap<String, GuiReaderGroup>(); |
| 180 | |
| 181 | pane.setVisible(false); |
| 182 | final Progress pg = new Progress(); |
| 183 | final String typeF = type; |
| 184 | outOfUi(pg, true, new Runnable() { |
| 185 | @Override |
| 186 | public void run() { |
| 187 | final BasicLibrary lib = helper.getReader().getLibrary(); |
| 188 | final Status status = lib.getStatus(); |
| 189 | |
| 190 | if (status == Status.READY) { |
| 191 | lib.refresh(pg); |
| 192 | } |
| 193 | |
| 194 | inUi(new Runnable() { |
| 195 | @Override |
| 196 | public void run() { |
| 197 | if (status == Status.READY) { |
| 198 | helper.createMenu(true); |
| 199 | if (typeF == null) { |
| 200 | addBookPane(true, false); |
| 201 | } else { |
| 202 | addBookPane(typeF, true); |
| 203 | } |
| 204 | pane.setVisible(true); |
| 205 | } else { |
| 206 | helper.createMenu(false); |
| 207 | validate(); |
| 208 | |
| 209 | String err = lib.getLibraryName() + "\n"; |
| 210 | switch (status) { |
| 211 | case INVALID: |
| 212 | err += "Library not valid"; |
| 213 | break; |
| 214 | |
| 215 | case UNAUTORIZED: |
| 216 | err += "You are not allowed to access this library"; |
| 217 | break; |
| 218 | |
| 219 | case UNAVAILABLE: |
| 220 | err += "Library currently unavailable"; |
| 221 | break; |
| 222 | |
| 223 | default: |
| 224 | err += "An error occured when contacting the library"; |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | error(err, "Library error", null); |
| 229 | } |
| 230 | } |
| 231 | }); |
| 232 | } |
| 233 | }); |
| 234 | } |
| 235 | |
| 236 | public boolean getCurrentType() { |
| 237 | return currentType; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Add a new {@link GuiReaderGroup} on the frame to display all the |
| 242 | * sources/types or all the authors, or a listing of all the books sorted |
| 243 | * either by source or author. |
| 244 | * <p> |
| 245 | * A display of all the sources/types or all the authors will show one icon |
| 246 | * per source/type or author. |
| 247 | * <p> |
| 248 | * A listing of all the books sorted by source/type or author will display |
| 249 | * all the books. |
| 250 | * |
| 251 | * @param type |
| 252 | * TRUE for type/source, FALSE for author |
| 253 | * @param listMode |
| 254 | * TRUE to get a listing of all the sources or authors, FALSE to |
| 255 | * get one icon per source or author |
| 256 | */ |
| 257 | public void addBookPane(boolean type, boolean listMode) { |
| 258 | this.currentType = type; |
| 259 | BasicLibrary lib = helper.getReader().getLibrary(); |
| 260 | if (type) { |
| 261 | if (!listMode) { |
| 262 | addListPane("Sources", lib.getSources(), type); |
| 263 | } else { |
| 264 | for (String tt : lib.getSources()) { |
| 265 | if (tt != null) { |
| 266 | addBookPane(tt, type); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } else { |
| 271 | if (!listMode) { |
| 272 | addListPane("Authors", lib.getAuthors(), type); |
| 273 | } else { |
| 274 | for (String tt : lib.getAuthors()) { |
| 275 | if (tt != null) { |
| 276 | addBookPane(tt, type); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Add a new {@link GuiReaderGroup} on the frame to display the books of the |
| 285 | * selected type or author. |
| 286 | * <p> |
| 287 | * Will invalidate the layout. |
| 288 | * |
| 289 | * @param value |
| 290 | * the author or the type, or NULL to get all the |
| 291 | * authors-or-types |
| 292 | * @param type |
| 293 | * TRUE for type/source, FALSE for author |
| 294 | */ |
| 295 | public void addBookPane(String value, boolean type) { |
| 296 | this.currentType = type; |
| 297 | |
| 298 | GuiReaderGroup bookPane = new GuiReaderGroup(helper.getReader(), value, |
| 299 | color); |
| 300 | |
| 301 | books.put(value, bookPane); |
| 302 | |
| 303 | pane.invalidate(); |
| 304 | pane.add(bookPane); |
| 305 | |
| 306 | bookPane.setActionListener(new BookActionListener() { |
| 307 | @Override |
| 308 | public void select(GuiReaderBook book) { |
| 309 | selectedBook = book; |
| 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public void popupRequested(GuiReaderBook book, MouseEvent e) { |
| 314 | JPopupMenu popup = helper.createBookPopup(); |
| 315 | popup.show(e.getComponent(), e.getX(), e.getY()); |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public void action(final GuiReaderBook book) { |
| 320 | openBook(book); |
| 321 | } |
| 322 | }); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Clear the pane from any book that may be present, usually prior to adding |
| 327 | * new ones. |
| 328 | * <p> |
| 329 | * Will invalidate the layout. |
| 330 | */ |
| 331 | public void removeBookPanes() { |
| 332 | books.clear(); |
| 333 | pane.invalidate(); |
| 334 | pane.removeAll(); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Refresh the list of {@link GuiReaderBook}s from disk. |
| 339 | * <p> |
| 340 | * Will validate the layout, as it is a "refresh" operation. |
| 341 | */ |
| 342 | public void refreshBooks() { |
| 343 | BasicLibrary lib = helper.getReader().getLibrary(); |
| 344 | for (String value : books.keySet()) { |
| 345 | List<GuiReaderBookInfo> infos = new ArrayList<GuiReaderBookInfo>(); |
| 346 | |
| 347 | List<MetaData> metas; |
| 348 | if (currentType) { |
| 349 | metas = lib.getListBySource(value); |
| 350 | } else { |
| 351 | metas = lib.getListByAuthor(value); |
| 352 | } |
| 353 | for (MetaData meta : metas) { |
| 354 | infos.add(GuiReaderBookInfo.fromMeta(meta)); |
| 355 | } |
| 356 | |
| 357 | books.get(value).refreshBooks(infos, words); |
| 358 | } |
| 359 | |
| 360 | if (bookPane != null) { |
| 361 | bookPane.refreshBooks(words); |
| 362 | } |
| 363 | |
| 364 | this.validate(); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Open a {@link GuiReaderBook} item. |
| 369 | * |
| 370 | * @param book |
| 371 | * the {@link GuiReaderBook} to open |
| 372 | */ |
| 373 | public void openBook(final GuiReaderBook book) { |
| 374 | final Progress pg = new Progress(); |
| 375 | outOfUi(pg, false, new Runnable() { |
| 376 | @Override |
| 377 | public void run() { |
| 378 | try { |
| 379 | helper.getReader().read(book.getInfo().getMeta().getLuid(), |
| 380 | false, pg); |
| 381 | SwingUtilities.invokeLater(new Runnable() { |
| 382 | @Override |
| 383 | public void run() { |
| 384 | book.setCached(true); |
| 385 | } |
| 386 | }); |
| 387 | } catch (IOException e) { |
| 388 | Instance.getTraceHandler().error(e); |
| 389 | error("Cannot open the selected book", "Error", e); |
| 390 | } |
| 391 | } |
| 392 | }); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Process the given action out of the Swing UI thread and link the given |
| 397 | * {@link ProgressBar} to the action. |
| 398 | * <p> |
| 399 | * The code will make sure that the {@link ProgressBar} (if not NULL) is set |
| 400 | * to done when the action is done. |
| 401 | * |
| 402 | * @param progress |
| 403 | * the {@link ProgressBar} or NULL |
| 404 | * @param refreshBooks |
| 405 | * TRUE to refresh the books after |
| 406 | * @param run |
| 407 | * the action to run |
| 408 | */ |
| 409 | public void outOfUi(Progress progress, final boolean refreshBooks, |
| 410 | final Runnable run) { |
| 411 | final Progress pg = new Progress(); |
| 412 | final Progress reload = new Progress("Reload books"); |
| 413 | |
| 414 | if (progress == null) { |
| 415 | progress = new Progress(); |
| 416 | } |
| 417 | |
| 418 | if (refreshBooks) { |
| 419 | pg.addProgress(progress, 100); |
| 420 | } else { |
| 421 | pg.addProgress(progress, 90); |
| 422 | pg.addProgress(reload, 10); |
| 423 | } |
| 424 | |
| 425 | invalidate(); |
| 426 | pgBar.setProgress(pg); |
| 427 | validate(); |
| 428 | setEnabled(false); |
| 429 | |
| 430 | new Thread(new Runnable() { |
| 431 | @Override |
| 432 | public void run() { |
| 433 | try { |
| 434 | run.run(); |
| 435 | if (refreshBooks) { |
| 436 | refreshBooks(); |
| 437 | } |
| 438 | } finally { |
| 439 | reload.done(); |
| 440 | if (!pg.isDone()) { |
| 441 | // will trigger pgBar ActionListener: |
| 442 | pg.done(); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | }, "outOfUi thread").start(); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Process the given action in the main Swing UI thread. |
| 451 | * <p> |
| 452 | * The code will make sure the current thread is the main UI thread and, if |
| 453 | * not, will switch to it before executing the runnable. |
| 454 | * <p> |
| 455 | * Synchronous operation. |
| 456 | * |
| 457 | * @param run |
| 458 | * the action to run |
| 459 | */ |
| 460 | public void inUi(final Runnable run) { |
| 461 | if (EventQueue.isDispatchThread()) { |
| 462 | run.run(); |
| 463 | } else { |
| 464 | try { |
| 465 | EventQueue.invokeAndWait(run); |
| 466 | } catch (InterruptedException e) { |
| 467 | Instance.getTraceHandler().error(e); |
| 468 | } catch (InvocationTargetException e) { |
| 469 | Instance.getTraceHandler().error(e); |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Import a {@link Story} into the main {@link LocalLibrary}. |
| 476 | * <p> |
| 477 | * Should be called inside the UI thread. |
| 478 | * |
| 479 | * @param askUrl |
| 480 | * TRUE for an {@link URL}, false for a {@link File} |
| 481 | */ |
| 482 | public void imprt(boolean askUrl) { |
| 483 | JFileChooser fc = new JFileChooser(); |
| 484 | |
| 485 | Object url; |
| 486 | if (askUrl) { |
| 487 | String clipboard = ""; |
| 488 | try { |
| 489 | clipboard = ("" + Toolkit.getDefaultToolkit() |
| 490 | .getSystemClipboard().getData(DataFlavor.stringFlavor)) |
| 491 | .trim(); |
| 492 | } catch (Exception e) { |
| 493 | // No data will be handled |
| 494 | } |
| 495 | |
| 496 | if (clipboard == null || !clipboard.startsWith("http")) { |
| 497 | clipboard = ""; |
| 498 | } |
| 499 | |
| 500 | url = JOptionPane.showInputDialog(GuiReaderMainPanel.this, |
| 501 | "url of the story to import?", "Importing from URL", |
| 502 | JOptionPane.QUESTION_MESSAGE, null, null, clipboard); |
| 503 | } else if (fc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION) { |
| 504 | url = fc.getSelectedFile().getAbsolutePath(); |
| 505 | } else { |
| 506 | url = null; |
| 507 | } |
| 508 | |
| 509 | if (url != null && !url.toString().isEmpty()) { |
| 510 | imprt(url.toString(), null, null); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * Actually import the {@link Story} into the main {@link LocalLibrary}. |
| 516 | * <p> |
| 517 | * Should be called inside the UI thread. |
| 518 | * |
| 519 | * @param url |
| 520 | * the {@link Story} to import by {@link URL} |
| 521 | * @param onSuccess |
| 522 | * Action to execute on success |
| 523 | * @param onSuccessPgName |
| 524 | * the name to use for the onSuccess progress bar |
| 525 | */ |
| 526 | public void imprt(final String url, final StoryRunnable onSuccess, |
| 527 | String onSuccessPgName) { |
| 528 | final Progress pg = new Progress(); |
| 529 | final Progress pgImprt = new Progress(); |
| 530 | final Progress pgOnSuccess = new Progress(onSuccessPgName); |
| 531 | pg.addProgress(pgImprt, 95); |
| 532 | pg.addProgress(pgOnSuccess, 5); |
| 533 | |
| 534 | outOfUi(pg, true, new Runnable() { |
| 535 | @Override |
| 536 | public void run() { |
| 537 | Exception ex = null; |
| 538 | Story story = null; |
| 539 | try { |
| 540 | story = helper.getReader().getLibrary() |
| 541 | .imprt(BasicReader.getUrl(url), pgImprt); |
| 542 | } catch (IOException e) { |
| 543 | ex = e; |
| 544 | } |
| 545 | |
| 546 | final Exception e = ex; |
| 547 | |
| 548 | final boolean ok = (e == null); |
| 549 | |
| 550 | pgOnSuccess.setProgress(0); |
| 551 | if (!ok) { |
| 552 | if (e instanceof UnknownHostException) { |
| 553 | error("URL not supported: " + url, "Cannot import URL", |
| 554 | null); |
| 555 | } else { |
| 556 | error("Failed to import " + url + ": \n" |
| 557 | + e.getMessage(), "Cannot import URL", e); |
| 558 | } |
| 559 | } else { |
| 560 | if (onSuccess != null) { |
| 561 | onSuccess.run(story); |
| 562 | } |
| 563 | } |
| 564 | pgOnSuccess.done(); |
| 565 | } |
| 566 | }); |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Enables or disables this component, depending on the value of the |
| 571 | * parameter <code>b</code>. An enabled component can respond to user input |
| 572 | * and generate events. Components are enabled initially by default. |
| 573 | * <p> |
| 574 | * Enabling or disabling <b>this</b> component will also affect its |
| 575 | * children. |
| 576 | * |
| 577 | * @param b |
| 578 | * If <code>true</code>, this component is enabled; otherwise |
| 579 | * this component is disabled |
| 580 | */ |
| 581 | @Override |
| 582 | public void setEnabled(boolean b) { |
| 583 | if (bar != null) { |
| 584 | bar.setEnabled(b); |
| 585 | } |
| 586 | |
| 587 | for (GuiReaderGroup group : books.values()) { |
| 588 | group.setEnabled(b); |
| 589 | } |
| 590 | super.setEnabled(b); |
| 591 | repaint(); |
| 592 | } |
| 593 | |
| 594 | public void setWords(boolean words) { |
| 595 | this.words = words; |
| 596 | } |
| 597 | |
| 598 | public GuiReaderBook getSelectedBook() { |
| 599 | return selectedBook; |
| 600 | } |
| 601 | |
| 602 | public void unsetSelectedBook() { |
| 603 | selectedBook = null; |
| 604 | } |
| 605 | |
| 606 | private void addListPane(String name, List<String> values, |
| 607 | final boolean type) { |
| 608 | GuiReader reader = helper.getReader(); |
| 609 | BasicLibrary lib = reader.getLibrary(); |
| 610 | |
| 611 | bookPane = new GuiReaderGroup(reader, name, color); |
| 612 | |
| 613 | List<GuiReaderBookInfo> infos = new ArrayList<GuiReaderBookInfo>(); |
| 614 | for (String value : values) { |
| 615 | if (type) { |
| 616 | infos.add(GuiReaderBookInfo.fromSource(lib, value)); |
| 617 | } else { |
| 618 | infos.add(GuiReaderBookInfo.fromAuthor(lib, value)); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | bookPane.refreshBooks(infos, words); |
| 623 | |
| 624 | this.invalidate(); |
| 625 | pane.invalidate(); |
| 626 | pane.add(bookPane); |
| 627 | pane.validate(); |
| 628 | this.validate(); |
| 629 | |
| 630 | bookPane.setActionListener(new BookActionListener() { |
| 631 | @Override |
| 632 | public void select(GuiReaderBook book) { |
| 633 | selectedBook = book; |
| 634 | } |
| 635 | |
| 636 | @Override |
| 637 | public void popupRequested(GuiReaderBook book, MouseEvent e) { |
| 638 | JPopupMenu popup = helper.createSourceAuthorPopup(); |
| 639 | popup.show(e.getComponent(), e.getX(), e.getY()); |
| 640 | } |
| 641 | |
| 642 | @Override |
| 643 | public void action(final GuiReaderBook book) { |
| 644 | removeBookPanes(); |
| 645 | addBookPane(book.getInfo().getMainInfo(), type); |
| 646 | refreshBooks(); |
| 647 | } |
| 648 | }); |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Display an error message and log the linked {@link Exception}. |
| 653 | * |
| 654 | * @param message |
| 655 | * the message |
| 656 | * @param title |
| 657 | * the title of the error message |
| 658 | * @param e |
| 659 | * the exception to log if any |
| 660 | */ |
| 661 | private void error(final String message, final String title, Exception e) { |
| 662 | Instance.getTraceHandler().error(title + ": " + message); |
| 663 | if (e != null) { |
| 664 | Instance.getTraceHandler().error(e); |
| 665 | } |
| 666 | |
| 667 | SwingUtilities.invokeLater(new Runnable() { |
| 668 | @Override |
| 669 | public void run() { |
| 670 | JOptionPane.showMessageDialog(GuiReaderMainPanel.this, message, |
| 671 | title, JOptionPane.ERROR_MESSAGE); |
| 672 | } |
| 673 | }); |
| 674 | } |
| 675 | } |