1 package be
.nikiroo
.fanfix
.reader
.ui
;
3 import java
.awt
.Desktop
;
4 import java
.awt
.EventQueue
;
5 import java
.awt
.event
.WindowAdapter
;
6 import java
.awt
.event
.WindowEvent
;
8 import java
.io
.IOException
;
9 import java
.net
.URISyntaxException
;
11 import javax
.swing
.JEditorPane
;
12 import javax
.swing
.JFrame
;
13 import javax
.swing
.JLabel
;
14 import javax
.swing
.JOptionPane
;
15 import javax
.swing
.event
.HyperlinkEvent
;
16 import javax
.swing
.event
.HyperlinkListener
;
18 import be
.nikiroo
.fanfix
.Instance
;
19 import be
.nikiroo
.fanfix
.VersionCheck
;
20 import be
.nikiroo
.fanfix
.bundles
.StringIdGui
;
21 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
22 import be
.nikiroo
.fanfix
.data
.MetaData
;
23 import be
.nikiroo
.fanfix
.data
.Story
;
24 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
25 import be
.nikiroo
.fanfix
.library
.CacheLibrary
;
26 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
27 import be
.nikiroo
.fanfix
.reader
.Reader
;
28 import be
.nikiroo
.fanfix
.supported
.SupportType
;
29 import be
.nikiroo
.utils
.Progress
;
30 import be
.nikiroo
.utils
.Version
;
31 import be
.nikiroo
.utils
.ui
.UIUtils
;
34 * This class implements a graphical {@link Reader} using the Swing library from
37 * It can thus be themed to look native-like, or metal-like, or use any other
38 * theme you may want to try.
40 * We actually try to enable native look-alike mode on start.
44 class GuiReader
extends BasicReader
{
45 static private boolean nativeLookLoaded
;
47 private CacheLibrary cacheLib
;
49 private File cacheDir
;
52 * Create a new graphical {@link Reader}.
55 * in case of I/O errors
57 public GuiReader() throws IOException
{
58 // TODO: allow different themes?
59 if (!nativeLookLoaded
) {
60 UIUtils
.setLookAndFeel();
61 nativeLookLoaded
= true;
64 cacheDir
= Instance
.getReaderDir();
66 if (!cacheDir
.exists()) {
67 throw new IOException(
68 "Cannote create cache directory for local reader: "
74 public synchronized BasicLibrary
getLibrary() {
75 if (cacheLib
== null) {
76 BasicLibrary lib
= super.getLibrary();
77 if (lib
instanceof CacheLibrary
) {
78 cacheLib
= (CacheLibrary
) lib
;
80 cacheLib
= new CacheLibrary(cacheDir
, lib
);
88 public void read(boolean sync
) throws IOException
{
89 MetaData meta
= getMeta();
92 throw new IOException("No story to read");
95 read(meta
.getLuid(), sync
, null);
99 * Check if the {@link Story} denoted by this Library UID is present in the
100 * {@link GuiReader} cache.
105 * @return TRUE if it is
107 public boolean isCached(String luid
) {
108 return cacheLib
.isCached(luid
);
112 public void browse(String type
) {
113 final Boolean
[] done
= new Boolean
[] { false };
115 // TODO: improve presentation of update message
116 final VersionCheck updates
= VersionCheck
.check();
117 StringBuilder builder
= new StringBuilder();
119 final JEditorPane updateMessage
= new JEditorPane("text/html", "");
120 if (updates
.isNewVersionAvailable()) {
121 builder
.append(trans(StringIdGui
.NEW_VERSION_AVAILABLE
,
122 "<span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>"));
123 builder
.append("<br>");
124 builder
.append("<br>");
125 for (Version v
: updates
.getNewer()) {
126 builder
.append("\t<b>"
127 + trans(StringIdGui
.NEW_VERSION_VERSION
, v
.toString())
129 builder
.append("<br>");
130 builder
.append("<ul>");
131 for (String item
: updates
.getChanges().get(v
)) {
132 builder
.append("<li>" + item
+ "</li>");
134 builder
.append("</ul>");
138 updateMessage
.setText("<html><body>" //
142 // handle link events
143 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
145 public void hyperlinkUpdate(HyperlinkEvent e
) {
146 if (e
.getEventType().equals(
147 HyperlinkEvent
.EventType
.ACTIVATED
))
149 Desktop
.getDesktop().browse(e
.getURL().toURI());
150 } catch (IOException ee
) {
151 Instance
.getTraceHandler().error(ee
);
152 } catch (URISyntaxException ee
) {
153 Instance
.getTraceHandler().error(ee
);
157 updateMessage
.setEditable(false);
158 updateMessage
.setBackground(new JLabel().getBackground());
161 final String typeFinal
= type
;
162 EventQueue
.invokeLater(new Runnable() {
165 if (updates
.isNewVersionAvailable()) {
166 int rep
= JOptionPane
.showConfirmDialog(null,
168 trans(StringIdGui
.NEW_VERSION_TITLE
),
169 JOptionPane
.OK_CANCEL_OPTION
);
170 if (rep
== JOptionPane
.OK_OPTION
) {
177 new Thread(new Runnable() {
181 GuiReaderFrame gui
= new GuiReaderFrame(
182 GuiReader
.this, typeFinal
);
184 } catch (Exception e
) {
185 Instance
.getTraceHandler().error(e
);
195 // This action must be synchronous, so wait until the frame is closed
199 } catch (InterruptedException e
) {
205 public void start(File target
, String program
, boolean sync
)
208 boolean handled
= false;
209 if (program
== null && !sync
) {
211 Desktop
.getDesktop().browse(target
.toURI());
213 } catch (UnsupportedOperationException e
) {
218 super.start(target
, program
, sync
);
223 public void search(SupportType searchOn
, String keywords
, int page
, int item
) {
225 throw new java
.lang
.IllegalStateException("Not implemented yet.");
229 public void searchTag(SupportType searchOn
, int page
, int item
, String
... tags
) {
231 throw new java
.lang
.IllegalStateException("Not implemented yet.");
235 * Delete the {@link Story} from the cache if it is present, but <b>NOT</b>
236 * from the main library.
238 * The next time we try to retrieve the {@link Story}, it may be required to
242 * the luid of the {@link Story}
244 void clearLocalReaderCache(String luid
) {
246 cacheLib
.clearFromCache(luid
);
247 } catch (IOException e
) {
248 Instance
.getTraceHandler().error(e
);
253 * Forward the delete operation to the main library.
255 * The {@link Story} will be deleted from the main library as well as the
259 * the {@link Story} to delete
261 void delete(String luid
) {
263 cacheLib
.delete(luid
);
264 } catch (IOException e
) {
265 Instance
.getTraceHandler().error(e
);
270 * "Open" the given {@link Story}. It usually involves starting an external
271 * program adapted to the given file type.
273 * Asynchronous method.
276 * the luid of the {@link Story} to open
278 * execute the process synchronously (wait until it is terminated
281 * the optional progress (we may need to prepare the
282 * {@link Story} for reading
284 * @throws IOException
285 * in case of I/O errors
287 void read(String luid
, boolean sync
, Progress pg
) throws IOException
{
288 MetaData meta
= cacheLib
.getInfo(luid
);
290 boolean textInternal
= Instance
.getUiConfig().getBoolean(
291 UiConfig
.NON_IMAGES_DOCUMENT_USE_INTERNAL_READER
, true);
292 boolean imageInternal
= Instance
.getUiConfig().getBoolean(
293 UiConfig
.IMAGES_DOCUMENT_USE_INTERNAL_READER
, true);
295 boolean useInternalViewer
= true;
296 if (meta
.isImageDocument() && !imageInternal
) {
297 useInternalViewer
= false;
299 if (!meta
.isImageDocument() && !textInternal
) {
300 useInternalViewer
= false;
303 if (useInternalViewer
) {
304 GuiReaderViewer viewer
= new GuiReaderViewer(cacheLib
,
305 cacheLib
.getStory(luid
, null));
309 viewer
.setVisible(true);
312 File file
= cacheLib
.getFile(luid
, pg
);
313 openExternal(meta
, file
, sync
);
318 * Change the source of the given {@link Story} (the source is the main
319 * information used to group the stories together).
321 * In other words, <b>move</b> the {@link Story} into other source.
323 * The source can be a new one, it needs not exist before hand.
326 * the luid of the {@link Story} to move
330 void changeSource(String luid
, String newSource
) {
332 cacheLib
.changeSource(luid
, newSource
, null);
333 } catch (IOException e
) {
334 Instance
.getTraceHandler().error(e
);
339 * Change the title of the given {@link Story}.
342 * the luid of the {@link Story} to change
346 void changeTitle(String luid
, String newTitle
) {
348 cacheLib
.changeTitle(luid
, newTitle
, null);
349 } catch (IOException e
) {
350 Instance
.getTraceHandler().error(e
);
355 * Change the author of the given {@link Story}.
357 * The author can be a new one, it needs not exist before hand.
360 * the luid of the {@link Story} to change
364 void changeAuthor(String luid
, String newAuthor
) {
366 cacheLib
.changeAuthor(luid
, newAuthor
, null);
367 } catch (IOException e
) {
368 Instance
.getTraceHandler().error(e
);
373 * Simple shortcut method to call {link Instance#getTransGui()#getString()}.
376 * the ID to translate
378 * @return the translated result
380 static String
trans(StringIdGui id
, Object
... params
) {
381 return Instance
.getTransGui().getString(id
, params
);
385 * Start a frame and wait until it is closed before returning.
390 static private void sync(final JFrame frame
) {
391 if (EventQueue
.isDispatchThread()) {
392 throw new IllegalStateException(
393 "Cannot call a sync method in the dispatch thread");
396 final Boolean
[] done
= new Boolean
[] { false };
398 Runnable run
= new Runnable() {
402 frame
.addWindowListener(new WindowAdapter() {
404 public void windowClosing(WindowEvent e
) {
405 super.windowClosing(e
);
410 frame
.setVisible(true);
411 } catch (Exception e
) {
417 if (EventQueue
.isDispatchThread()) {
420 EventQueue
.invokeLater(run
);
422 } catch (Exception e
) {
423 Instance
.getTraceHandler().error(e
);
427 // This action must be synchronous, so wait until the frame is closed
431 } catch (InterruptedException e
) {