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
;
19 public class Actions
{
20 static public void openExternal(final BasicLibrary lib
, MetaData meta
, Container parent
, final Runnable onDone
) {
21 while (!(parent
instanceof Window
) && parent
!= null) {
22 parent
= parent
.getParent();
26 final JDialog wait
= new JDialog((Window
) parent
);
27 wait
.setTitle("Opening story");
28 wait
.setSize(400, 300);
29 wait
.setLayout(new BorderLayout());
30 wait
.add(new JLabel("Waiting..."));
34 final Object waitLock
= new Object();
35 final Boolean
[] waitScreen
= new Boolean
[] { false };
36 new Thread(new Runnable() {
41 } catch (InterruptedException e
) {
44 synchronized (waitLock
) {
47 wait
.setVisible(true);
53 final String luid
= meta
.getLuid();
54 final boolean isImageDocument
= meta
.isImageDocument();
56 final SwingWorker
<File
, Void
> worker
= new SwingWorker
<File
, Void
>() {
60 protected File
doInBackground() throws Exception
{
61 target
= lib
.getFile(luid
, null);
66 protected void done() {
68 openExternal(target
, isImageDocument
);
69 } catch (IOException e
) {
74 synchronized (waitLock
) {
76 wait
.setVisible(false);
91 * Open the {@link Story} with an external reader (the program will be passed
92 * the given target file).
94 * @param target the target {@link File}
95 * @param isImageDocument TRUE for image documents, FALSE for not-images
98 * @throws IOException in case of I/O error
100 static public void openExternal(File target
, boolean isImageDocument
) throws IOException
{
101 String program
= null;
102 if (isImageDocument
) {
103 program
= Instance
.getInstance().getUiConfig().getString(UiConfig
.IMAGES_DOCUMENT_READER
);
105 program
= Instance
.getInstance().getUiConfig().getString(UiConfig
.NON_IMAGES_DOCUMENT_READER
);
108 if (program
!= null && program
.trim().isEmpty()) {
112 start(target
, program
, false);
116 * Start a file and open it with the given program if given or the first default
117 * system starter we can find.
119 * @param target the target to open
120 * @param program the program to use or NULL for the default system starter
121 * @param sync execute the process synchronously (wait until it is terminated
124 * @throws IOException in case of I/O error
126 static protected void start(File target
, String program
, boolean sync
) throws IOException
{
128 if (program
== null) {
130 for (String starter
: new String
[] { "xdg-open", "open", "see", "start", "run" }) {
132 Instance
.getInstance().getTraceHandler().trace("starting external program");
133 proc
= Runtime
.getRuntime().exec(new String
[] { starter
, target
.getAbsolutePath() });
136 } catch (IOException e
) {
140 throw new IOException("Cannot find a program to start the file");
143 Instance
.getInstance().getTraceHandler().trace("starting external program");
144 proc
= Runtime
.getRuntime().exec(new String
[] { program
, target
.getAbsolutePath() });
147 if (proc
!= null && sync
) {
150 } catch (InterruptedException e
) {