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 javax
.swing
.JDialog
;
10 import javax
.swing
.JLabel
;
11 import javax
.swing
.SwingWorker
;
13 import be
.nikiroo
.fanfix
.Instance
;
14 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
15 import be
.nikiroo
.fanfix
.data
.MetaData
;
16 import be
.nikiroo
.fanfix
.data
.Story
;
17 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
18 import be
.nikiroo
.fanfix_swing
.gui
.utils
.UiHelper
;
20 public class Actions
{
21 static public void openExternal(final BasicLibrary lib
, MetaData meta
, final Container parent
,
22 final Runnable onDone
) {
23 Container parentWindow
= parent
;
24 while (!(parentWindow
instanceof Window
) && parentWindow
!= null) {
25 parentWindow
= parentWindow
.getParent();
29 final JDialog wait
= new JDialog((Window
) parentWindow
);
30 wait
.setTitle("Opening story");
31 wait
.setSize(400, 300);
32 wait
.setLayout(new BorderLayout());
33 wait
.add(new JLabel("Waiting..."));
37 final Object waitLock
= new Object();
38 final Boolean
[] waitScreen
= new Boolean
[] { false };
39 new Thread(new Runnable() {
44 } catch (InterruptedException e
) {
47 synchronized (waitLock
) {
50 wait
.setVisible(true);
56 final String luid
= meta
.getLuid();
57 final boolean isImageDocument
= meta
.isImageDocument();
59 final SwingWorker
<File
, Void
> worker
= new SwingWorker
<File
, Void
>() {
63 protected File
doInBackground() throws Exception
{
64 target
= lib
.getFile(luid
, null);
69 protected void done() {
72 openExternal(target
, isImageDocument
);
73 } catch (Exception e
) {
75 UiHelper
.error(parent
, e
.getLocalizedMessage(), "Cannot open the story", e
);
78 synchronized (waitLock
) {
80 wait
.setVisible(false);
95 * Open the {@link Story} with an external reader (the program will be passed
96 * the given target file).
98 * @param target the target {@link File}
99 * @param isImageDocument TRUE for image documents, FALSE for not-images
102 * @throws IOException in case of I/O error
104 static public void openExternal(File target
, boolean isImageDocument
) throws IOException
{
105 String program
= null;
106 if (isImageDocument
) {
107 program
= Instance
.getInstance().getUiConfig().getString(UiConfig
.IMAGES_DOCUMENT_READER
);
109 program
= Instance
.getInstance().getUiConfig().getString(UiConfig
.NON_IMAGES_DOCUMENT_READER
);
112 if (program
!= null && program
.trim().isEmpty()) {
116 start(target
, program
, false);
120 * Start a file and open it with the given program if given or the first default
121 * system starter we can find.
123 * @param target the target to open
124 * @param program the program to use or NULL for the default system starter
125 * @param sync execute the process synchronously (wait until it is terminated
128 * @throws IOException in case of I/O error
130 static protected void start(File target
, String program
, boolean sync
) throws IOException
{
132 if (program
== null) {
134 for (String starter
: new String
[] { "xdg-open", "open", "see", "start", "run" }) {
136 Instance
.getInstance().getTraceHandler().trace("starting external program");
137 proc
= Runtime
.getRuntime().exec(new String
[] { starter
, target
.getAbsolutePath() });
140 } catch (IOException e
) {
144 throw new IOException("Cannot find a program to start the file");
147 Instance
.getInstance().getTraceHandler().trace("starting external program");
148 proc
= Runtime
.getRuntime().exec(new String
[] { program
, target
.getAbsolutePath() });
151 if (proc
!= null && sync
) {
154 } catch (InterruptedException e
) {