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