Commit | Line | Data |
---|---|---|
e3fef8b5 NR |
1 | package be.nikiroo.fanfix_swing.gui; |
2 | ||
3 | import java.awt.Container; | |
4 | import java.awt.Toolkit; | |
5 | import java.awt.datatransfer.DataFlavor; | |
6 | import java.io.File; | |
7 | import java.net.URL; | |
8 | ||
9 | import javax.swing.JFileChooser; | |
10 | import javax.swing.JFrame; | |
11 | import javax.swing.JOptionPane; | |
12 | ||
13 | import be.nikiroo.fanfix.Instance; | |
14 | import be.nikiroo.fanfix.bundles.StringIdGui; | |
15 | import be.nikiroo.fanfix.library.LocalLibrary; | |
16 | import be.nikiroo.fanfix_swing.Actions; | |
17 | import be.nikiroo.utils.Progress; | |
18 | ||
19 | public class ImporterFrame extends JFrame { | |
20 | public ImporterFrame() { | |
21 | ||
22 | } | |
23 | ||
24 | /** | |
25 | * Ask for and import an {@link URL} into the main {@link LocalLibrary}. | |
26 | * <p> | |
27 | * Should be called inside the UI thread. | |
28 | * | |
29 | * @param parent | |
30 | * a container we can use to display the {@link URL} chooser and | |
31 | * to show error messages if any | |
32 | * @param onSuccess | |
33 | * Action to execute on success | |
34 | */ | |
35 | public void imprtUrl(final Container parent, final Runnable onSuccess) { | |
36 | String clipboard = ""; | |
37 | try { | |
38 | clipboard = ("" + Toolkit.getDefaultToolkit().getSystemClipboard() | |
39 | .getData(DataFlavor.stringFlavor)).trim(); | |
40 | } catch (Exception e) { | |
41 | // No data will be handled | |
42 | } | |
43 | ||
44 | if (clipboard == null || !(clipboard.startsWith("http://") || // | |
45 | clipboard.startsWith("https://"))) { | |
46 | clipboard = ""; | |
47 | } | |
48 | ||
49 | Object url = JOptionPane.showInputDialog(parent, | |
50 | Instance.getInstance().getTransGui() | |
51 | .getString(StringIdGui.SUBTITLE_IMPORT_URL), | |
52 | Instance.getInstance().getTransGui() | |
53 | .getString(StringIdGui.TITLE_IMPORT_URL), | |
54 | JOptionPane.QUESTION_MESSAGE, null, null, clipboard); | |
55 | ||
56 | Progress pg = null; | |
57 | if (url != null && !url.toString().isEmpty()) { | |
58 | Actions.imprt(parent, url.toString(), pg, onSuccess); | |
59 | } | |
60 | } | |
61 | ||
62 | /** | |
63 | * Ask for and import a {@link File} into the main {@link LocalLibrary}. | |
64 | * <p> | |
65 | * Should be called inside the UI thread. | |
66 | * | |
67 | * @param parent | |
68 | * a container we can use to display the {@link File} chooser and | |
69 | * to show error messages if any | |
70 | * @param onSuccess | |
71 | * Action to execute on success | |
72 | */ | |
73 | ||
74 | public void imprtFile(final Container parent, final Runnable onSuccess) { | |
75 | JFileChooser fc = new JFileChooser(); | |
76 | ||
77 | Progress pg = null; | |
78 | if (fc.showOpenDialog(parent) != JFileChooser.CANCEL_OPTION) { | |
79 | Object url = fc.getSelectedFile().getAbsolutePath(); | |
80 | if (url != null && !url.toString().isEmpty()) { | |
81 | Actions.imprt(parent, url.toString(), pg, onSuccess); | |
82 | } | |
83 | } | |
84 | } | |
85 | } |