1 package be
.nikiroo
.fanfix_swing
;
3 import java
.awt
.BorderLayout
;
4 import java
.awt
.Container
;
5 import java
.awt
.Window
;
7 import java
.io
.IOException
;
9 import java
.net
.UnknownHostException
;
11 import javax
.swing
.JDialog
;
12 import javax
.swing
.JLabel
;
13 import javax
.swing
.SwingWorker
;
15 import be
.nikiroo
.fanfix
.Instance
;
16 import be
.nikiroo
.fanfix
.bundles
.StringIdGui
;
17 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
18 import be
.nikiroo
.fanfix
.data
.MetaData
;
19 import be
.nikiroo
.fanfix
.data
.Story
;
20 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
21 import be
.nikiroo
.fanfix
.library
.LocalLibrary
;
22 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
23 import be
.nikiroo
.fanfix_swing
.gui
.utils
.UiHelper
;
24 import be
.nikiroo
.utils
.Progress
;
26 public class Actions
{
27 static public void openExternal(final BasicLibrary lib
, MetaData meta
,
28 final Container parent
, final Runnable onDone
) {
29 Container parentWindow
= parent
;
30 while (!(parentWindow
instanceof Window
) && parentWindow
!= null) {
31 parentWindow
= parentWindow
.getParent();
35 final JDialog wait
= new JDialog((Window
) parentWindow
);
36 wait
.setTitle("Opening story");
37 wait
.setSize(400, 300);
38 wait
.setLayout(new BorderLayout());
39 wait
.add(new JLabel("Waiting..."));
43 final Object waitLock
= new Object();
44 final Boolean
[] waitScreen
= new Boolean
[] { false };
45 new Thread(new Runnable() {
50 } catch (InterruptedException e
) {
53 synchronized (waitLock
) {
56 wait
.setVisible(true);
62 final String luid
= meta
.getLuid();
63 final boolean isImageDocument
= meta
.isImageDocument();
65 final SwingWorker
<File
, Void
> worker
= new SwingWorker
<File
, Void
>() {
69 protected File
doInBackground() throws Exception
{
70 target
= lib
.getFile(luid
, null);
75 protected void done() {
78 openExternal(target
, isImageDocument
);
79 } catch (Exception e
) {
81 UiHelper
.error(parent
, e
.getLocalizedMessage(),
82 "Cannot open the story", e
);
85 synchronized (waitLock
) {
87 wait
.setVisible(false);
102 * Open the {@link Story} with an external reader (the program will be
103 * passed the given target file).
106 * the target {@link File}
107 * @param isImageDocument
108 * TRUE for image documents, FALSE for not-images documents
110 * @throws IOException
111 * in case of I/O error
113 static public void openExternal(File target
, boolean isImageDocument
)
115 String program
= null;
116 if (isImageDocument
) {
117 program
= Instance
.getInstance().getUiConfig()
118 .getString(UiConfig
.IMAGES_DOCUMENT_READER
);
120 program
= Instance
.getInstance().getUiConfig()
121 .getString(UiConfig
.NON_IMAGES_DOCUMENT_READER
);
124 if (program
!= null && program
.trim().isEmpty()) {
128 start(target
, program
, false);
132 * Start a file and open it with the given program if given or the first
133 * default system starter we can find.
138 * the program to use or NULL for the default system starter
140 * execute the process synchronously (wait until it is terminated
143 * @throws IOException
144 * in case of I/O error
146 static protected void start(File target
, String program
, boolean sync
)
149 if (program
== null) {
151 for (String starter
: new String
[] { "xdg-open", "open", "see",
154 Instance
.getInstance().getTraceHandler()
155 .trace("starting external program");
156 proc
= Runtime
.getRuntime().exec(
157 new String
[] { starter
, target
.getAbsolutePath() });
160 } catch (IOException e
) {
164 throw new IOException(
165 "Cannot find a program to start the file");
168 Instance
.getInstance().getTraceHandler()
169 .trace("starting external program");
170 proc
= Runtime
.getRuntime()
171 .exec(new String
[] { program
, target
.getAbsolutePath() });
174 if (proc
!= null && sync
) {
177 } catch (InterruptedException e
) {
183 * Actually import the {@link Story} into the main {@link LocalLibrary}.
185 * Should be called inside the UI thread, will start a worker (i.e., this is
189 * a container we can use to show error messages if any
191 * the {@link Story} to import by {@link URL}
193 * the optional progress reporter
195 * Action to execute on success
197 static public void imprt(final Container parent
, final String url
,
198 final Progress pg
, final Runnable onSuccess
) {
199 final Progress fpg
= pg
;
200 new SwingWorker
<Void
, Void
>() {
202 protected Void
doInBackground() throws Exception
{
208 Instance
.getInstance().getLibrary()
209 .imprt(BasicReader
.getUrl(url
), fpg
);
212 if (onSuccess
!= null) {
215 } catch (IOException e
) {
217 if (e
instanceof UnknownHostException
) {
218 UiHelper
.error(parent
,
219 Instance
.getInstance().getTransGui().getString(
220 StringIdGui
.ERROR_URL_NOT_SUPPORTED
,
222 Instance
.getInstance().getTransGui().getString(
223 StringIdGui
.TITLE_ERROR
),
226 UiHelper
.error(parent
,
227 Instance
.getInstance().getTransGui().getString(
228 StringIdGui
.ERROR_URL_IMPORT_FAILED
,
229 url
, e
.getMessage()),
230 Instance
.getInstance().getTransGui()
231 .getString(StringIdGui
.TITLE_ERROR
),