Commit | Line | Data |
---|---|---|
3cdf3fd8 NR |
1 | package be.nikiroo.fanfix_swing; |
2 | ||
3 | import java.awt.BorderLayout; | |
4 | import java.awt.Container; | |
5 | import java.awt.Window; | |
6 | import java.io.File; | |
7 | import java.io.IOException; | |
8 | ||
9 | import javax.swing.JDialog; | |
10 | import javax.swing.JLabel; | |
11 | import javax.swing.SwingWorker; | |
12 | ||
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 | ||
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(); | |
23 | } | |
24 | ||
25 | // TODO: UI | |
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...")); | |
31 | ||
32 | // TODO: pg? | |
33 | ||
34 | final Object waitLock = new Object(); | |
35 | final Boolean[] waitScreen = new Boolean[] { false }; | |
36 | new Thread(new Runnable() { | |
37 | @Override | |
38 | public void run() { | |
39 | try { | |
40 | Thread.sleep(200); | |
41 | } catch (InterruptedException e) { | |
42 | } | |
43 | ||
44 | synchronized (waitLock) { | |
45 | if (!waitScreen[0]) { | |
46 | waitScreen[0] = true; | |
47 | wait.setVisible(true); | |
48 | } | |
49 | } | |
50 | } | |
51 | }).start(); | |
52 | ||
53 | final String luid = meta.getLuid(); | |
54 | final boolean isImageDocument = meta.isImageDocument(); | |
55 | ||
56 | final SwingWorker<File, Void> worker = new SwingWorker<File, Void>() { | |
57 | private File target; | |
58 | ||
59 | @Override | |
60 | protected File doInBackground() throws Exception { | |
61 | target = lib.getFile(luid, null); | |
62 | return null; | |
63 | } | |
64 | ||
65 | @Override | |
66 | protected void done() { | |
67 | try { | |
68 | openExternal(target, isImageDocument); | |
69 | } catch (IOException e) { | |
70 | // TODO: error? | |
71 | e.printStackTrace(); | |
72 | } | |
73 | ||
74 | synchronized (waitLock) { | |
75 | if (waitScreen[0]) { | |
76 | wait.setVisible(false); | |
77 | } | |
78 | waitScreen[0] = true; | |
79 | } | |
80 | ||
81 | if (onDone != null) { | |
82 | onDone.run(); | |
83 | } | |
84 | } | |
85 | }; | |
86 | ||
87 | worker.execute(); | |
88 | } | |
89 | ||
90 | /** | |
91 | * Open the {@link Story} with an external reader (the program will be passed | |
92 | * the given target file). | |
93 | * | |
94 | * @param target the target {@link File} | |
95 | * @param isImageDocument TRUE for image documents, FALSE for not-images | |
96 | * documents | |
97 | * | |
98 | * @throws IOException in case of I/O error | |
99 | */ | |
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); | |
104 | } else { | |
105 | program = Instance.getInstance().getUiConfig().getString(UiConfig.NON_IMAGES_DOCUMENT_READER); | |
106 | } | |
107 | ||
108 | if (program != null && program.trim().isEmpty()) { | |
109 | program = null; | |
110 | } | |
111 | ||
112 | start(target, program, false); | |
113 | } | |
114 | ||
115 | /** | |
116 | * Start a file and open it with the given program if given or the first default | |
117 | * system starter we can find. | |
118 | * | |
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 | |
122 | * before returning) | |
123 | * | |
124 | * @throws IOException in case of I/O error | |
125 | */ | |
126 | static protected void start(File target, String program, boolean sync) throws IOException { | |
127 | Process proc = null; | |
128 | if (program == null) { | |
129 | boolean ok = false; | |
130 | for (String starter : new String[] { "xdg-open", "open", "see", "start", "run" }) { | |
131 | try { | |
132 | Instance.getInstance().getTraceHandler().trace("starting external program"); | |
133 | proc = Runtime.getRuntime().exec(new String[] { starter, target.getAbsolutePath() }); | |
134 | ok = true; | |
135 | break; | |
136 | } catch (IOException e) { | |
137 | } | |
138 | } | |
139 | if (!ok) { | |
140 | throw new IOException("Cannot find a program to start the file"); | |
141 | } | |
142 | } else { | |
143 | Instance.getInstance().getTraceHandler().trace("starting external program"); | |
144 | proc = Runtime.getRuntime().exec(new String[] { program, target.getAbsolutePath() }); | |
145 | } | |
146 | ||
147 | if (proc != null && sync) { | |
148 | try { | |
149 | proc.waitFor(); | |
150 | } catch (InterruptedException e) { | |
151 | } | |
152 | } | |
153 | } | |
154 | } |