Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

KFileChooser.java 9.7KB

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