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
.utils
.Progress
;
29 import be
.nikiroo
.utils
.Version
;
30 import be
.nikiroo
.utils
.ui
.UIUtils
;
33 * This class implements a graphical {@link Reader} using the Swing library from
36 * It can thus be themed to look native-like, or metal-like, or use any other
37 * theme you may want to try.
39 * We actually try to enable native look-alike mode on start.
43 class GuiReader
extends BasicReader
{
44 static private boolean nativeLookLoaded
;
46 private CacheLibrary cacheLib
;
48 private File cacheDir
;
51 * Create a new graphical {@link Reader}.
54 * in case of I/O errors
56 public GuiReader() throws IOException
{
57 // TODO: allow different themes?
58 if (!nativeLookLoaded
) {
59 UIUtils
.setLookAndFeel();
60 nativeLookLoaded
= true;
63 cacheDir
= Instance
.getReaderDir();
65 if (!cacheDir
.exists()) {
66 throw new IOException(
67 "Cannote create cache directory for local reader: "
73 public synchronized BasicLibrary
getLibrary() {
74 if (cacheLib
== null) {
75 BasicLibrary lib
= super.getLibrary();
76 if (lib
instanceof CacheLibrary
) {
77 cacheLib
= (CacheLibrary
) lib
;
79 cacheLib
= new CacheLibrary(cacheDir
, lib
);
87 public void read(boolean sync
) throws IOException
{
88 MetaData meta
= getMeta();
91 throw new IOException("No story to read");
94 read(meta
.getLuid(), sync
, null);
98 * Check if the {@link Story} denoted by this Library UID is present in the
99 * {@link GuiReader} cache.
104 * @return TRUE if it is
106 public boolean isCached(String luid
) {
107 return cacheLib
.isCached(luid
);
111 public void browse(String type
) {
112 final Boolean
[] done
= new Boolean
[] { false };
114 // TODO: improve presentation of update message
115 final VersionCheck updates
= VersionCheck
.check();
116 StringBuilder builder
= new StringBuilder();
118 final JEditorPane updateMessage
= new JEditorPane("text/html", "");
119 if (updates
.isNewVersionAvailable()) {
120 builder
.append(trans(StringIdGui
.NEW_VERSION_AVAILABLE
,
121 "<span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>"));
122 builder
.append("<br>");
123 builder
.append("<br>");
124 for (Version v
: updates
.getNewer()) {
125 builder
.append("\t<b>"
126 + trans(StringIdGui
.NEW_VERSION_VERSION
, v
.toString())
128 builder
.append("<br>");
129 builder
.append("<ul>");
130 for (String item
: updates
.getChanges().get(v
)) {
131 builder
.append("<li>" + item
+ "</li>");
133 builder
.append("</ul>");
137 updateMessage
.setText("<html><body>" //
141 // handle link events
142 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
144 public void hyperlinkUpdate(HyperlinkEvent e
) {
145 if (e
.getEventType().equals(
146 HyperlinkEvent
.EventType
.ACTIVATED
))
148 Desktop
.getDesktop().browse(e
.getURL().toURI());
149 } catch (IOException ee
) {
150 Instance
.getTraceHandler().error(ee
);
151 } catch (URISyntaxException ee
) {
152 Instance
.getTraceHandler().error(ee
);
156 updateMessage
.setEditable(false);
157 updateMessage
.setBackground(new JLabel().getBackground());
160 final String typeFinal
= type
;
161 EventQueue
.invokeLater(new Runnable() {
164 if (updates
.isNewVersionAvailable()) {
165 int rep
= JOptionPane
.showConfirmDialog(null,
167 trans(StringIdGui
.NEW_VERSION_TITLE
),
168 JOptionPane
.OK_CANCEL_OPTION
);
169 if (rep
== JOptionPane
.OK_OPTION
) {
176 new Thread(new Runnable() {
180 GuiReaderFrame gui
= new GuiReaderFrame(
181 GuiReader
.this, typeFinal
);
183 } catch (Exception e
) {
184 Instance
.getTraceHandler().error(e
);
194 // This action must be synchronous, so wait until the frame is closed
198 } catch (InterruptedException e
) {
204 public void start(File target
, String program
, boolean sync
)
207 boolean handled
= false;
208 if (program
== null && !sync
) {
210 Desktop
.getDesktop().browse(target
.toURI());
212 } catch (UnsupportedOperationException e
) {
217 super.start(target
, program
, sync
);
222 * Delete the {@link Story} from the cache if it is present, but <b>NOT</b>
223 * from the main library.
225 * The next time we try to retrieve the {@link Story}, it may be required to
229 * the luid of the {@link Story}
231 void clearLocalReaderCache(String luid
) {
233 cacheLib
.clearFromCache(luid
);
234 } catch (IOException e
) {
235 Instance
.getTraceHandler().error(e
);
240 * Forward the delete operation to the main library.
242 * The {@link Story} will be deleted from the main library as well as the
246 * the {@link Story} to delete
248 void delete(String luid
) {
250 cacheLib
.delete(luid
);
251 } catch (IOException e
) {
252 Instance
.getTraceHandler().error(e
);
257 * "Open" the given {@link Story}. It usually involves starting an external
258 * program adapted to the given file type.
260 * Asynchronous method.
263 * the luid of the {@link Story} to open
265 * execute the process synchronously (wait until it is terminated
268 * the optional progress (we may need to prepare the
269 * {@link Story} for reading
271 * @throws IOException
272 * in case of I/O errors
274 void read(String luid
, boolean sync
, Progress pg
) throws IOException
{
275 MetaData meta
= cacheLib
.getInfo(luid
);
277 boolean textInternal
= Instance
.getUiConfig().getBoolean(
278 UiConfig
.NON_IMAGES_DOCUMENT_USE_INTERNAL_READER
, true);
279 boolean imageInternal
= Instance
.getUiConfig().getBoolean(
280 UiConfig
.IMAGES_DOCUMENT_USE_INTERNAL_READER
, true);
282 boolean useInternalViewer
= true;
283 if (meta
.isImageDocument() && !imageInternal
) {
284 useInternalViewer
= false;
286 if (!meta
.isImageDocument() && !textInternal
) {
287 useInternalViewer
= false;
290 if (useInternalViewer
) {
291 GuiReaderViewer viewer
= new GuiReaderViewer(cacheLib
,
292 cacheLib
.getStory(luid
, null));
296 viewer
.setVisible(true);
299 File file
= cacheLib
.getFile(luid
, pg
);
300 openExternal(meta
, file
, sync
);
305 * Change the source of the given {@link Story} (the source is the main
306 * information used to group the stories together).
308 * In other words, <b>move</b> the {@link Story} into other source.
310 * The source can be a new one, it needs not exist before hand.
313 * the luid of the {@link Story} to move
317 void changeSource(String luid
, String newSource
) {
319 cacheLib
.changeSource(luid
, newSource
, null);
320 } catch (IOException e
) {
321 Instance
.getTraceHandler().error(e
);
326 * Change the title of the given {@link Story}.
329 * the luid of the {@link Story} to change
333 void changeTitle(String luid
, String newTitle
) {
335 cacheLib
.changeTitle(luid
, newTitle
, null);
336 } catch (IOException e
) {
337 Instance
.getTraceHandler().error(e
);
342 * Change the author of the given {@link Story}.
344 * The author can be a new one, it needs not exist before hand.
347 * the luid of the {@link Story} to change
351 void changeAuthor(String luid
, String newAuthor
) {
353 cacheLib
.changeAuthor(luid
, newAuthor
, null);
354 } catch (IOException e
) {
355 Instance
.getTraceHandler().error(e
);
360 * Simple shortcut method to call {link Instance#getTransGui()#getString()}.
363 * the ID to translate
365 * @return the translated result
367 static String
trans(StringIdGui id
, Object
... params
) {
368 return Instance
.getTransGui().getString(id
, params
);
372 * Start a frame and wait until it is closed before returning.
377 static private void sync(final JFrame frame
) {
378 if (EventQueue
.isDispatchThread()) {
379 throw new IllegalStateException(
380 "Cannot call a sync method in the dispatch thread");
383 final Boolean
[] done
= new Boolean
[] { false };
385 Runnable run
= new Runnable() {
389 frame
.addWindowListener(new WindowAdapter() {
391 public void windowClosing(WindowEvent e
) {
392 super.windowClosing(e
);
397 frame
.setVisible(true);
398 } catch (Exception e
) {
404 if (EventQueue
.isDispatchThread()) {
407 EventQueue
.invokeLater(run
);
409 } catch (Exception e
) {
410 Instance
.getTraceHandler().error(e
);
414 // This action must be synchronous, so wait until the frame is closed
418 } catch (InterruptedException e
) {