You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

KFileChooser.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.dcc.kde;
  18. import com.dmdirc.addons.dcc.DCCManager;
  19. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  20. import java.awt.Component;
  21. import java.awt.HeadlessException;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import javax.swing.JFileChooser;
  27. /**
  28. * JFileChooser that uses KDialog to show the actual chooser. This is quite hacky, and not
  29. * guarenteed to behave identically to JFileChooser, altho it tries to be as close as possible.
  30. * Almost a drop in replacement for JFileChooser, replace: new JFileChooser(); with:
  31. * KFileChooser.getFileChooser();
  32. *
  33. * There are obviously some differences: - File filters must be set using setKDEFileFilter() not
  34. * using FileFilter objects. - FileSystemView's are ignored - showOpenDialog and showSaveDialog
  35. * shell kdialog, so only options available in kdialog work. - getFileChooser() will return a
  36. * JFileChooser object unless the DCC plugin's config option "general.useKFileChooser" is set to
  37. * "true" (defaults to false) and kdialog is in either /usr/bin or /bin - Selection mode
  38. * FILES_AND_DIRECTORIES can not be used
  39. */
  40. public class KFileChooser extends JFileChooser {
  41. /** A version number for this class. */
  42. private static final long serialVersionUID = 200806141;
  43. /** The plugin that this file chooser is for. */
  44. private final DCCManager plugin;
  45. /** Used to read settings from. */
  46. private final AggregateConfigProvider config;
  47. /**
  48. * Constructs a FileChooser pointing to the user's default directory.
  49. *
  50. * @param plugin The plugin that owns this KFileChooser
  51. */
  52. private KFileChooser(final AggregateConfigProvider config, final DCCManager plugin) {
  53. this.plugin = plugin;
  54. this.config = config;
  55. }
  56. /**
  57. * Constructs a FileChooser using the given File as the path.
  58. *
  59. * @param plugin The plugin that owns this KFileChooser
  60. * @param currentDirectory Directory to use as the base directory
  61. */
  62. private KFileChooser(final AggregateConfigProvider config, final DCCManager plugin,
  63. final File currentDirectory) {
  64. super(currentDirectory);
  65. this.plugin = plugin;
  66. this.config = config;
  67. }
  68. /**
  69. * Constructs a FileChooser using the given path.
  70. *
  71. * @param plugin The plugin that owns this KFileChooser
  72. * @param currentDirectoryPath Directory to use as the base directory
  73. */
  74. private KFileChooser(final AggregateConfigProvider config, final DCCManager plugin,
  75. final String currentDirectoryPath) {
  76. super(currentDirectoryPath);
  77. this.plugin = plugin;
  78. this.config = config;
  79. }
  80. /**
  81. * Should a KFileChooser be used rather than a JFileChooser?
  82. *
  83. * @param config Config manager
  84. * @param plugin The DCC Plugin that is requesting a chooser
  85. *
  86. * @return return true if getFileChooser() will return a KFileChooser not a JFileChooser
  87. */
  88. public static boolean useKFileChooser(final AggregateConfigProvider config,
  89. final DCCManager plugin) {
  90. return KDialogProcess.hasKDialog() && config.getOptionBool(plugin.getDomain(),
  91. "general.useKFileChooser");
  92. }
  93. /**
  94. * Constructs a FileChooser pointing to the user's default directory.
  95. *
  96. * @param config Config provider used to retrieve settings
  97. * @param plugin The DCC Plugin that is requesting a chooser
  98. *
  99. * @return The relevant FileChooser
  100. */
  101. public static JFileChooser getFileChooser(final AggregateConfigProvider config,
  102. final DCCManager plugin) {
  103. return useKFileChooser(config, plugin) ? new KFileChooser(config, plugin)
  104. : new JFileChooser();
  105. }
  106. /**
  107. * Constructs a FileChooser using the given File as the path.
  108. *
  109. * @param config Config provider used to retrieve settings
  110. * @param plugin The DCC Plugin that is requesting a chooser
  111. * @param currentDirectory Directory to use as the base directory
  112. *
  113. * @return The relevant FileChooser
  114. */
  115. public static JFileChooser getFileChooser(final AggregateConfigProvider config,
  116. final DCCManager plugin, final File currentDirectory) {
  117. return useKFileChooser(config, plugin) ? new KFileChooser(config, plugin, currentDirectory)
  118. : new JFileChooser(currentDirectory);
  119. }
  120. /**
  121. * Constructs a FileChooser using the given path.
  122. *
  123. * @param config Config provider used to retrieve settings
  124. * @param plugin The DCC Plugin that is requesting a chooser
  125. * @param currentDirectoryPath Directory to use as the base directory
  126. *
  127. * @return The relevant FileChooser
  128. */
  129. public static JFileChooser getFileChooser(final AggregateConfigProvider config,
  130. final DCCManager plugin, final String currentDirectoryPath) {
  131. return useKFileChooser(config, plugin) ? new KFileChooser(config, plugin,
  132. currentDirectoryPath) : new JFileChooser(currentDirectoryPath);
  133. }
  134. @Override
  135. public int showOpenDialog(final Component parent) throws HeadlessException {
  136. if (!useKFileChooser(config, plugin)) {
  137. return super.showOpenDialog(parent);
  138. }
  139. final ArrayList<String> params = new ArrayList<>();
  140. if (isMultiSelectionEnabled()) {
  141. params.add("--multiple");
  142. params.add("--separate-output");
  143. }
  144. if (getDialogTitle() != null && !getDialogTitle().isEmpty()) {
  145. params.add("--caption");
  146. params.add(getDialogTitle());
  147. }
  148. if (getFileSelectionMode() == DIRECTORIES_ONLY) {
  149. params.add("--getexistingdirectory");
  150. } else {
  151. params.add("--getopenfilename");
  152. }
  153. if (getSelectedFile() != null && getFileSelectionMode() != DIRECTORIES_ONLY
  154. && !getSelectedFile().getPath().isEmpty()) {
  155. if (getSelectedFile().getPath().charAt(0) != '/') {
  156. params.add(getCurrentDirectory().getPath() + File.separator + getSelectedFile().
  157. getPath());
  158. } else {
  159. params.add(getSelectedFile().getPath());
  160. }
  161. } else if (getCurrentDirectory() != null) {
  162. params.add(getCurrentDirectory().getPath());
  163. }
  164. final KDialogProcess kdp;
  165. try {
  166. kdp = new KDialogProcess(params.toArray(new String[params.size()]));
  167. kdp.waitFor();
  168. } catch (IOException | InterruptedException e) {
  169. return JFileChooser.ERROR_OPTION;
  170. }
  171. if (kdp.getProcess().exitValue() == 0) {
  172. if (isMultiSelectionEnabled()) {
  173. final List<String> list = kdp.getStdOut();
  174. final File[] fileList = new File[list.size()];
  175. for (int i = 0; i < list.size(); ++i) {
  176. fileList[i] = new File(list.get(i));
  177. }
  178. setSelectedFiles(fileList);
  179. } else {
  180. setSelectedFile(new File(kdp.getStdOut().get(0)));
  181. }
  182. return JFileChooser.APPROVE_OPTION;
  183. } else {
  184. return JFileChooser.ERROR_OPTION;
  185. }
  186. }
  187. @Override
  188. public int showSaveDialog(final Component parent) throws HeadlessException {
  189. if (!useKFileChooser(config, plugin)) {
  190. return super.showSaveDialog(parent);
  191. }
  192. final ArrayList<String> params = new ArrayList<>();
  193. if (getDialogTitle() != null && !getDialogTitle().isEmpty()) {
  194. params.add("--caption");
  195. params.add(getDialogTitle());
  196. }
  197. params.add("--getsavefilename");
  198. if (getSelectedFile() != null && !getSelectedFile().getPath().isEmpty()) {
  199. if (getSelectedFile().getPath().charAt(0) != '/') {
  200. params.add(getCurrentDirectory().getPath() + File.separator + getSelectedFile().
  201. getPath());
  202. } else {
  203. params.add(getSelectedFile().getPath());
  204. }
  205. } else if (getCurrentDirectory() != null) {
  206. params.add(getCurrentDirectory().getPath());
  207. }
  208. final KDialogProcess kdp;
  209. try {
  210. kdp = new KDialogProcess(params.toArray(new String[params.size()]));
  211. kdp.waitFor();
  212. } catch (IOException | InterruptedException e) {
  213. return JFileChooser.ERROR_OPTION;
  214. }
  215. if (kdp.getProcess().exitValue() == 0) {
  216. setSelectedFile(new File(kdp.getStdOut().get(0)));
  217. return JFileChooser.APPROVE_OPTION;
  218. } else {
  219. return JFileChooser.ERROR_OPTION;
  220. }
  221. }
  222. }