Remove or move java.awt dependencies
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / ui / GuiReader.java
1 package be.nikiroo.fanfix.reader.ui;
2
3 import java.awt.Desktop;
4 import java.awt.EventQueue;
5 import java.io.File;
6 import java.io.IOException;
7 import java.net.URISyntaxException;
8
9 import javax.swing.JEditorPane;
10 import javax.swing.JLabel;
11 import javax.swing.JOptionPane;
12 import javax.swing.event.HyperlinkEvent;
13 import javax.swing.event.HyperlinkListener;
14
15 import be.nikiroo.fanfix.Instance;
16 import be.nikiroo.fanfix.VersionCheck;
17 import be.nikiroo.fanfix.data.MetaData;
18 import be.nikiroo.fanfix.data.Story;
19 import be.nikiroo.fanfix.library.BasicLibrary;
20 import be.nikiroo.fanfix.library.CacheLibrary;
21 import be.nikiroo.fanfix.reader.BasicReader;
22 import be.nikiroo.utils.Progress;
23 import be.nikiroo.utils.Version;
24 import be.nikiroo.utils.ui.UIUtils;
25
26 class GuiReader extends BasicReader {
27 static private boolean nativeLookLoaded;
28
29 private CacheLibrary cacheLib;
30
31 private File cacheDir;
32
33 public GuiReader() throws IOException {
34 if (!nativeLookLoaded) {
35 UIUtils.setLookAndFeel();
36 nativeLookLoaded = true;
37 }
38
39 cacheDir = Instance.getReaderDir();
40 cacheDir.mkdirs();
41 if (!cacheDir.exists()) {
42 throw new IOException(
43 "Cannote create cache directory for local reader: "
44 + cacheDir);
45 }
46 }
47
48 @Override
49 public synchronized BasicLibrary getLibrary() {
50 if (cacheLib == null) {
51 BasicLibrary lib = super.getLibrary();
52 if (lib instanceof CacheLibrary) {
53 cacheLib = (CacheLibrary) lib;
54 } else {
55 cacheLib = new CacheLibrary(cacheDir, lib);
56 }
57 }
58
59 return cacheLib;
60 }
61
62 @Override
63 public void read() throws IOException {
64 MetaData meta = getMeta();
65
66 if (meta == null) {
67 throw new IOException("No story to read");
68 }
69
70 read(meta.getLuid(), null);
71 }
72
73 /**
74 * Check if the {@link Story} denoted by this Library UID is present in the
75 * {@link GuiReader} cache.
76 *
77 * @param luid
78 * the Library UID
79 *
80 * @return TRUE if it is
81 */
82 public boolean isCached(String luid) {
83 return cacheLib.isCached(luid);
84 }
85
86 @Override
87 public void browse(String type) {
88 // TODO: improve presentation of update message
89 final VersionCheck updates = VersionCheck.check();
90 StringBuilder builder = new StringBuilder();
91
92 final JEditorPane updateMessage = new JEditorPane("text/html", "");
93 if (updates.isNewVersionAvailable()) {
94 builder.append("A new version of the program is available at <span style='color: blue;'>https://github.com/nikiroo/fanfix/releases</span>");
95 builder.append("<br>");
96 builder.append("<br>");
97 for (Version v : updates.getNewer()) {
98 builder.append("\t<b>Version " + v + "</b>");
99 builder.append("<br>");
100 builder.append("<ul>");
101 for (String item : updates.getChanges().get(v)) {
102 builder.append("<li>" + item + "</li>");
103 }
104 builder.append("</ul>");
105 }
106
107 // html content
108 updateMessage.setText("<html><body>" //
109 + builder//
110 + "</body></html>");
111
112 // handle link events
113 updateMessage.addHyperlinkListener(new HyperlinkListener() {
114 @Override
115 public void hyperlinkUpdate(HyperlinkEvent e) {
116 if (e.getEventType().equals(
117 HyperlinkEvent.EventType.ACTIVATED))
118 try {
119 Desktop.getDesktop().browse(e.getURL().toURI());
120 } catch (IOException ee) {
121 Instance.getTraceHandler().error(ee);
122 } catch (URISyntaxException ee) {
123 Instance.getTraceHandler().error(ee);
124 }
125 }
126 });
127 updateMessage.setEditable(false);
128 updateMessage.setBackground(new JLabel().getBackground());
129 }
130
131 final String typeFinal = type;
132 EventQueue.invokeLater(new Runnable() {
133 @Override
134 public void run() {
135 if (updates.isNewVersionAvailable()) {
136 int rep = JOptionPane.showConfirmDialog(null,
137 updateMessage, "Updates available",
138 JOptionPane.OK_CANCEL_OPTION);
139 if (rep == JOptionPane.OK_OPTION) {
140 updates.ok();
141 } else {
142 updates.ignore();
143 }
144 }
145
146 new GuiReaderFrame(GuiReader.this, typeFinal).setVisible(true);
147 }
148 });
149 }
150
151 @Override
152 public void start(File target, String program) throws IOException {
153 if (program == null) {
154 try {
155 Desktop.getDesktop().browse(target.toURI());
156 } catch (UnsupportedOperationException e) {
157 super.start(target, program);
158 }
159 } else {
160 super.start(target, program);
161 }
162 }
163
164 // delete from local reader library
165 void clearLocalReaderCache(String luid) {
166 try {
167 cacheLib.clearFromCache(luid);
168 } catch (IOException e) {
169 Instance.getTraceHandler().error(e);
170 }
171 }
172
173 // delete from main library
174 void delete(String luid) {
175 try {
176 cacheLib.delete(luid);
177 } catch (IOException e) {
178 Instance.getTraceHandler().error(e);
179 }
180 }
181
182 // open the given book
183 void read(String luid, Progress pg) throws IOException {
184 File file = cacheLib.getFile(luid, pg);
185
186 // TODO: show a special page for the chapter?
187 openExternal(getLibrary().getInfo(luid), file);
188 }
189
190 void changeType(String luid, String newSource) {
191 try {
192 cacheLib.changeSource(luid, newSource, null);
193 } catch (IOException e) {
194 Instance.getTraceHandler().error(e);
195 }
196 }
197 }