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
, final Container parent
,
32 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(), "Cannot open the story", e
);
88 synchronized (waitLock
) {
90 wait
.setVisible(false);
105 * Open the {@link Story} with an external reader (the program will be passed
106 * the given target file).
108 * @param target the target {@link File}
109 * @param isImageDocument TRUE for image documents, FALSE for not-images
112 * @throws IOException in case of I/O error
114 static public void openExternal(File target
, boolean isImageDocument
) throws IOException
{
115 String program
= null;
116 if (isImageDocument
) {
117 program
= Instance
.getInstance().getUiConfig().getString(UiConfig
.IMAGES_DOCUMENT_READER
);
119 program
= Instance
.getInstance().getUiConfig().getString(UiConfig
.NON_IMAGES_DOCUMENT_READER
);
122 if (program
!= null && program
.trim().isEmpty()) {
126 start(target
, program
, false);
130 * Start a file and open it with the given program if given or the first default
131 * system starter we can find.
133 * @param target the target to open
134 * @param program the program to use or NULL for the default system starter
135 * @param sync execute the process synchronously (wait until it is terminated
138 * @throws IOException in case of I/O error
140 static protected void start(File target
, String program
, boolean sync
) throws IOException
{
142 if (program
== null) {
144 for (String starter
: new String
[] { "xdg-open", "open", "see", "start", "run" }) {
146 Instance
.getInstance().getTraceHandler().trace("starting external program");
147 proc
= Runtime
.getRuntime().exec(new String
[] { starter
, target
.getAbsolutePath() });
150 } catch (IOException e
) {
154 throw new IOException("Cannot find a program to start the file");
157 Instance
.getInstance().getTraceHandler().trace("starting external program");
158 proc
= Runtime
.getRuntime().exec(new String
[] { program
, target
.getAbsolutePath() });
161 if (proc
!= null && sync
) {
164 } catch (InterruptedException e
) {
170 * Import a {@link Story} into the main {@link LocalLibrary}.
172 * Should be called inside the UI thread.
174 * @param askUrl TRUE for an {@link URL}, false for a {@link File}
176 static public void imprt(final Container parent
, boolean askUrl
, final Runnable onSuccess
) {
177 JFileChooser fc
= new JFileChooser();
181 String clipboard
= "";
183 clipboard
= ("" + Toolkit
.getDefaultToolkit().getSystemClipboard().getData(DataFlavor
.stringFlavor
))
185 } catch (Exception e
) {
186 // No data will be handled
189 if (clipboard
== null || !(clipboard
.startsWith("http://") || //
190 clipboard
.startsWith("https://"))) {
194 url
= JOptionPane
.showInputDialog(parent
,
195 Instance
.getInstance().getTransGui().getString(StringIdGui
.SUBTITLE_IMPORT_URL
),
196 Instance
.getInstance().getTransGui().getString(StringIdGui
.TITLE_IMPORT_URL
),
197 JOptionPane
.QUESTION_MESSAGE
, null, null, clipboard
);
198 } else if (fc
.showOpenDialog(parent
) != JFileChooser
.CANCEL_OPTION
) {
199 url
= fc
.getSelectedFile().getAbsolutePath();
204 if (url
!= null && !url
.toString().isEmpty()) {
205 imprt(parent
, url
.toString(), null, null);
210 * Actually import the {@link Story} into the main {@link LocalLibrary}.
212 * Should be called inside the UI thread.
214 * @param url the {@link Story} to import by {@link URL}
215 * @param onSuccess Action to execute on success
216 * @param onSuccessPgName the name to use for the onSuccess progress bar
218 static public void imprt(final Container parent
, final String url
, final Runnable onSuccess
,
219 String onSuccessPgName
) {
220 final Progress pg
= new Progress();
221 final Progress pgImprt
= new Progress();
222 final Progress pgOnSuccess
= new Progress(onSuccessPgName
);
223 pg
.addProgress(pgImprt
, 95);
224 pg
.addProgress(pgOnSuccess
, 5);
226 new SwingWorker
<Void
, Void
>() {
228 protected Void
doInBackground() throws Exception
{
230 MetaData meta
= null;
232 meta
= Instance
.getInstance().getLibrary().imprt(BasicReader
.getUrl(url
), pgImprt
);
233 } catch (IOException e
) {
238 final Exception e
= ex
;
240 final boolean ok
= (e
== null);
242 pgOnSuccess
.setProgress(0);
244 if (e
instanceof UnknownHostException
) {
245 UiHelper
.error(parent
,
246 Instance
.getInstance().getTransGui().getString(StringIdGui
.ERROR_URL_NOT_SUPPORTED
,
248 Instance
.getInstance().getTransGui().getString(StringIdGui
.TITLE_ERROR
), null);
250 UiHelper
.error(parent
,
251 Instance
.getInstance().getTransGui().getString(StringIdGui
.ERROR_URL_IMPORT_FAILED
, url
,
253 Instance
.getInstance().getTransGui().getString(StringIdGui
.TITLE_ERROR
), e
);
256 if (onSuccess
!= null) {