1 package be
.nikiroo
.fanfix_swing
;
3 import java
.awt
.BorderLayout
;
4 import java
.awt
.Container
;
5 import java
.awt
.Toolkit
;
6 import java
.awt
.Window
;
7 import java
.awt
.datatransfer
.DataFlavor
;
9 import java
.io
.IOException
;
11 import java
.net
.UnknownHostException
;
13 import javax
.swing
.JDialog
;
14 import javax
.swing
.JFileChooser
;
15 import javax
.swing
.JLabel
;
16 import javax
.swing
.JOptionPane
;
17 import javax
.swing
.SwingWorker
;
19 import be
.nikiroo
.fanfix
.Instance
;
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
.LocalLibrary
;
26 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
27 import be
.nikiroo
.fanfix_swing
.gui
.utils
.UiHelper
;
28 import be
.nikiroo
.utils
.Progress
;
30 public class Actions
{
31 static public void openExternal(final BasicLibrary lib
, MetaData meta
,
32 final Container parent
, final Runnable onDone
) {
33 Container parentWindow
= parent
;
34 while (!(parentWindow
instanceof Window
) && parentWindow
!= null) {
35 parentWindow
= parentWindow
.getParent();
39 final JDialog wait
= new JDialog((Window
) parentWindow
);
40 wait
.setTitle("Opening story");
41 wait
.setSize(400, 300);
42 wait
.setLayout(new BorderLayout());
43 wait
.add(new JLabel("Waiting..."));
47 final Object waitLock
= new Object();
48 final Boolean
[] waitScreen
= new Boolean
[] { false };
49 new Thread(new Runnable() {
54 } catch (InterruptedException e
) {
57 synchronized (waitLock
) {
60 wait
.setVisible(true);
66 final String luid
= meta
.getLuid();
67 final boolean isImageDocument
= meta
.isImageDocument();
69 final SwingWorker
<File
, Void
> worker
= new SwingWorker
<File
, Void
>() {
73 protected File
doInBackground() throws Exception
{
74 target
= lib
.getFile(luid
, null);
79 protected void done() {
82 openExternal(target
, isImageDocument
);
83 } catch (Exception e
) {
85 UiHelper
.error(parent
, e
.getLocalizedMessage(),
86 "Cannot open the story", e
);
89 synchronized (waitLock
) {
91 wait
.setVisible(false);
106 * Open the {@link Story} with an external reader (the program will be
107 * passed the given target file).
110 * the target {@link File}
111 * @param isImageDocument
112 * TRUE for image documents, FALSE for not-images documents
114 * @throws IOException
115 * in case of I/O error
117 static public void openExternal(File target
, boolean isImageDocument
)
119 String program
= null;
120 if (isImageDocument
) {
121 program
= Instance
.getInstance().getUiConfig()
122 .getString(UiConfig
.IMAGES_DOCUMENT_READER
);
124 program
= Instance
.getInstance().getUiConfig()
125 .getString(UiConfig
.NON_IMAGES_DOCUMENT_READER
);
128 if (program
!= null && program
.trim().isEmpty()) {
132 start(target
, program
, false);
136 * Start a file and open it with the given program if given or the first
137 * default system starter we can find.
142 * the program to use or NULL for the default system starter
144 * execute the process synchronously (wait until it is terminated
147 * @throws IOException
148 * in case of I/O error
150 static protected void start(File target
, String program
, boolean sync
)
153 if (program
== null) {
155 for (String starter
: new String
[] { "xdg-open", "open", "see",
158 Instance
.getInstance().getTraceHandler()
159 .trace("starting external program");
160 proc
= Runtime
.getRuntime().exec(
161 new String
[] { starter
, target
.getAbsolutePath() });
164 } catch (IOException e
) {
168 throw new IOException(
169 "Cannot find a program to start the file");
172 Instance
.getInstance().getTraceHandler()
173 .trace("starting external program");
174 proc
= Runtime
.getRuntime()
175 .exec(new String
[] { program
, target
.getAbsolutePath() });
178 if (proc
!= null && sync
) {
181 } catch (InterruptedException e
) {
187 * Import a {@link Story} into the main {@link LocalLibrary}.
189 * Should be called inside the UI thread.
192 * TRUE for an {@link URL}, false for a {@link File}
194 static public void imprt(final Container parent
, boolean askUrl
,
195 final Runnable onSuccess
) {
196 JFileChooser fc
= new JFileChooser();
200 String clipboard
= "";
202 clipboard
= ("" + Toolkit
.getDefaultToolkit()
203 .getSystemClipboard().getData(DataFlavor
.stringFlavor
))
205 } catch (Exception e
) {
206 // No data will be handled
209 if (clipboard
== null || !(clipboard
.startsWith("http://") || //
210 clipboard
.startsWith("https://"))) {
214 url
= JOptionPane
.showInputDialog(parent
,
215 Instance
.getInstance().getTransGui()
216 .getString(StringIdGui
.SUBTITLE_IMPORT_URL
),
217 Instance
.getInstance().getTransGui()
218 .getString(StringIdGui
.TITLE_IMPORT_URL
),
219 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
220 } else if (fc
.showOpenDialog(parent
) != JFileChooser
.CANCEL_OPTION
) {
221 url
= fc
.getSelectedFile().getAbsolutePath();
226 if (url
!= null && !url
.toString().isEmpty()) {
227 imprt(parent
, url
.toString(), null, null);
232 * Actually import the {@link Story} into the main {@link LocalLibrary}.
234 * Should be called inside the UI thread.
237 * the {@link Story} to import by {@link URL}
239 * Action to execute on success
240 * @param onSuccessPgName
241 * the name to use for the onSuccess progress bar
243 static public void imprt(final Container parent
, final String url
,
244 final Runnable onSuccess
, String onSuccessPgName
) {
245 final Progress pg
= new Progress();
246 final Progress pgImprt
= new Progress();
247 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
248 pg
.addProgress(pgImprt
, 95);
249 pg
.addProgress(pgOnSuccess
, 5);
251 new SwingWorker
<Void
, Void
>() {
253 protected Void
doInBackground() throws Exception
{
255 MetaData meta
= null;
257 meta
= Instance
.getInstance().getLibrary()
258 .imprt(BasicReader
.getUrl(url
), pgImprt
);
259 } catch (IOException e
) {
264 final Exception e
= ex
;
266 final boolean ok
= (e
== null);
268 pgOnSuccess
.setProgress(0);
270 if (e
instanceof UnknownHostException
) {
271 UiHelper
.error(parent
,
272 Instance
.getInstance().getTransGui().getString(
273 StringIdGui
.ERROR_URL_NOT_SUPPORTED
,
275 Instance
.getInstance().getTransGui().getString(
276 StringIdGui
.TITLE_ERROR
),
279 UiHelper
.error(parent
,
280 Instance
.getInstance().getTransGui().getString(
281 StringIdGui
.ERROR_URL_IMPORT_FAILED
,
282 url
, e
.getMessage()),
283 Instance
.getInstance().getTransGui()
284 .getString(StringIdGui
.TITLE_ERROR
),
288 if (onSuccess
!= null) {