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
.UiConfig
;
21 import be
.nikiroo
.fanfix
.data
.MetaData
;
22 import be
.nikiroo
.fanfix
.data
.Story
;
23 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
24 import be
.nikiroo
.fanfix
.library
.CacheLibrary
;
25 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
26 import be
.nikiroo
.fanfix
.reader
.Reader
;
27 import be
.nikiroo
.utils
.Progress
;
28 import be
.nikiroo
.utils
.Version
;
29 import be
.nikiroo
.utils
.ui
.UIUtils
;
32 * This class implements a graphical {@link Reader} using the Swing library from
35 * It can thus be themed to look native-like, or metal-like, or use any other
36 * theme you may want to try.
38 * We actually try to enable native look-alike mode on start.
42 class GuiReader
extends BasicReader
{
43 static private boolean nativeLookLoaded
;
45 private CacheLibrary cacheLib
;
47 private File cacheDir
;
50 * Create a new graphical {@link Reader}.
53 * in case of I/O errors
55 public GuiReader() throws IOException
{
56 // TODO: allow different themes?
57 if (!nativeLookLoaded
) {
58 UIUtils
.setLookAndFeel();
59 nativeLookLoaded
= true;
62 cacheDir
= Instance
.getReaderDir();
64 if (!cacheDir
.exists()) {
65 throw new IOException(
66 "Cannote create cache directory for local reader: "
72 public synchronized BasicLibrary
getLibrary() {
73 if (cacheLib
== null) {
74 BasicLibrary lib
= super.getLibrary();
75 if (lib
instanceof CacheLibrary
) {
76 cacheLib
= (CacheLibrary
) lib
;
78 cacheLib
= new CacheLibrary(cacheDir
, lib
);
86 public void read(boolean sync
) throws IOException
{
87 MetaData meta
= getMeta();
90 throw new IOException("No story to read");
93 read(meta
.getLuid(), sync
, null);
97 * Check if the {@link Story} denoted by this Library UID is present in the
98 * {@link GuiReader} cache.
103 * @return TRUE if it is
105 public boolean isCached(String luid
) {
106 return cacheLib
.isCached(luid
);
110 public void browse(String type
) {
111 final Boolean
[] done
= new Boolean
[] { false };
113 // TODO: improve presentation of update message
114 final VersionCheck updates
= VersionCheck
.check();
115 StringBuilder builder
= new StringBuilder();
117 final JEditorPane updateMessage
= new JEditorPane("text/html", "");
118 if (updates
.isNewVersionAvailable()) {
119 builder
.append("A new version of the program is available at <span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>");
120 builder
.append("<br>");
121 builder
.append("<br>");
122 for (Version v
: updates
.getNewer()) {
123 builder
.append("\t<b>Version " + v
+ "</b>");
124 builder
.append("<br>");
125 builder
.append("<ul>");
126 for (String item
: updates
.getChanges().get(v
)) {
127 builder
.append("<li>" + item
+ "</li>");
129 builder
.append("</ul>");
133 updateMessage
.setText("<html><body>" //
137 // handle link events
138 updateMessage
.addHyperlinkListener(new HyperlinkListener() {
140 public void hyperlinkUpdate(HyperlinkEvent e
) {
141 if (e
.getEventType().equals(
142 HyperlinkEvent
.EventType
.ACTIVATED
))
144 Desktop
.getDesktop().browse(e
.getURL().toURI());
145 } catch (IOException ee
) {
146 Instance
.getTraceHandler().error(ee
);
147 } catch (URISyntaxException ee
) {
148 Instance
.getTraceHandler().error(ee
);
152 updateMessage
.setEditable(false);
153 updateMessage
.setBackground(new JLabel().getBackground());
156 final String typeFinal
= type
;
157 EventQueue
.invokeLater(new Runnable() {
160 if (updates
.isNewVersionAvailable()) {
161 int rep
= JOptionPane
.showConfirmDialog(null,
162 updateMessage
, "Updates available",
163 JOptionPane
.OK_CANCEL_OPTION
);
164 if (rep
== JOptionPane
.OK_OPTION
) {
171 new Thread(new Runnable() {
175 GuiReaderFrame gui
= new GuiReaderFrame(
176 GuiReader
.this, typeFinal
);
178 } catch (Exception e
) {
179 Instance
.getTraceHandler().error(e
);
189 // This action must be synchronous, so wait until the frame is closed
193 } catch (InterruptedException e
) {
199 public void start(File target
, String program
, boolean sync
)
202 boolean handled
= false;
203 if (program
== null && !sync
) {
205 Desktop
.getDesktop().browse(target
.toURI());
207 } catch (UnsupportedOperationException e
) {
212 super.start(target
, program
, sync
);
217 * Delete the {@link Story} from the cache if it is present, but <b>NOT</b>
218 * from the main library.
220 * The next time we try to retrieve the {@link Story}, it may be required to
224 * the luid of the {@link Story}
226 void clearLocalReaderCache(String luid
) {
228 cacheLib
.clearFromCache(luid
);
229 } catch (IOException e
) {
230 Instance
.getTraceHandler().error(e
);
235 * Forward the delete operation to the main library.
237 * The {@link Story} will be deleted from the main library as well as the
241 * the {@link Story} to delete
243 void delete(String luid
) {
245 cacheLib
.delete(luid
);
246 } catch (IOException e
) {
247 Instance
.getTraceHandler().error(e
);
252 * "Open" the given {@link Story}. It usually involves starting an external
253 * program adapted to the given file type.
255 * Asynchronous method.
258 * the luid of the {@link Story} to open
260 * execute the process synchronously (wait until it is terminated
263 * the optional progress (we may need to prepare the
264 * {@link Story} for reading
266 * @throws IOException
267 * in case of I/O errors
269 void read(String luid
, boolean sync
, Progress pg
) throws IOException
{
270 MetaData meta
= cacheLib
.getInfo(luid
);
272 boolean textInternal
= Instance
.getUiConfig().getBoolean(
273 UiConfig
.NON_IMAGES_DOCUMENT_USE_INTERNAL_READER
, true);
274 boolean imageInternal
= Instance
.getUiConfig().getBoolean(
275 UiConfig
.IMAGES_DOCUMENT_USE_INTERNAL_READER
, true);
277 boolean useInternalViewer
= true;
278 if (meta
.isImageDocument() && !imageInternal
) {
279 useInternalViewer
= false;
281 if (!meta
.isImageDocument() && !textInternal
) {
282 useInternalViewer
= false;
285 if (useInternalViewer
) {
286 GuiReaderViewer viewer
= new GuiReaderViewer(cacheLib
,
287 cacheLib
.getStory(luid
, null));
291 viewer
.setVisible(true);
294 File file
= cacheLib
.getFile(luid
, pg
);
295 openExternal(meta
, file
, sync
);
300 * Change the source of the given {@link Story} (the source is the main
301 * information used to group the stories together).
303 * In other words, <b>move</b> the {@link Story} into other source.
305 * The source can be a new one, it needs not exist before hand.
308 * the luid of the {@link Story} to move
312 void changeSource(String luid
, String newSource
) {
314 cacheLib
.changeSource(luid
, newSource
, null);
315 } catch (IOException e
) {
316 Instance
.getTraceHandler().error(e
);
321 * Change the title of the given {@link Story}.
324 * the luid of the {@link Story} to change
328 void changeTitle(String luid
, String newTitle
) {
330 cacheLib
.changeTitle(luid
, newTitle
, null);
331 } catch (IOException e
) {
332 Instance
.getTraceHandler().error(e
);
337 * Change the author of the given {@link Story}.
339 * The author can be a new one, it needs not exist before hand.
342 * the luid of the {@link Story} to change
346 void changeAuthor(String luid
, String newAuthor
) {
348 cacheLib
.changeAuthor(luid
, newAuthor
, null);
349 } catch (IOException e
) {
350 Instance
.getTraceHandler().error(e
);
355 * Start a frame and wait until it is closed before returning.
360 static private void sync(final JFrame frame
) {
361 if (EventQueue
.isDispatchThread()) {
362 throw new IllegalStateException(
363 "Cannot call a sync method in the dispatch thread");
366 final Boolean
[] done
= new Boolean
[] { false };
368 Runnable run
= new Runnable() {
372 frame
.addWindowListener(new WindowAdapter() {
374 public void windowClosing(WindowEvent e
) {
375 super.windowClosing(e
);
380 frame
.setVisible(true);
381 } catch (Exception e
) {
387 if (EventQueue
.isDispatchThread()) {
390 EventQueue
.invokeLater(run
);
392 } catch (Exception e
) {
393 Instance
.getTraceHandler().error(e
);
397 // This action must be synchronous, so wait until the frame is closed
401 } catch (InterruptedException e
) {