/src/mpv5/ui/dialogs/DialogForFile.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/ · Java · 428 lines · 288 code · 44 blank · 96 comment · 59 complexity · 04bed4dfa1fd90c06f82580454a97513 MD5 · raw file

  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.ui.dialogs;
  18. import java.awt.Container;
  19. import java.io.File;
  20. import java.io.FileNotFoundException;
  21. import java.io.IOException;
  22. import java.util.List;
  23. import javax.swing.JComponent;
  24. import javax.swing.JFileChooser;
  25. import javax.swing.JTextField;
  26. import javax.swing.filechooser.FileFilter;
  27. import mpv5.Main;
  28. import mpv5.globals.Constants;
  29. import mpv5.globals.Messages;
  30. import mpv5.logging.Log;
  31. import mpv5.ui.frames.MPView;
  32. import mpv5.utils.export.Export;
  33. import mpv5.utils.files.FileDirectoryHandler;
  34. import mpv5.utils.jobs.Waiter;
  35. /**
  36. * This class is useful for selecting files and directories
  37. *
  38. */
  39. public class DialogForFile extends JFileChooser implements Waiter {
  40. private static final long serialVersionUID = 1L;
  41. public static FileFilter DIRECTORIES = new FileFilter() {
  42. public boolean accept(File f) {
  43. return f.isDirectory();
  44. }
  45. public String getDescription() {
  46. return "Directories";
  47. }
  48. };
  49. public static FileFilter TEMPLATE_FILES = new FileFilter() {
  50. public boolean accept(File f) {
  51. return f.getName().toLowerCase().endsWith(".odt") || f.getName().toLowerCase().endsWith(".pdf") || f.isDirectory();
  52. }
  53. public String getDescription() {
  54. return "Templates";
  55. }
  56. };
  57. public static FileFilter HTML_FILES = new FileFilter() {
  58. public boolean accept(File f) {
  59. return f.getName().toLowerCase().endsWith(".html") || f.getName().toLowerCase().endsWith(".htm") || f.isDirectory();
  60. }
  61. public String getDescription() {
  62. return "HTML Format(*.htm[l])";
  63. }
  64. };
  65. public static FileFilter XML_FILES = new FileFilter() {
  66. public boolean accept(File f) {
  67. return f.getName().toLowerCase().endsWith(".xml") || f.isDirectory();
  68. }
  69. public String getDescription() {
  70. return "XML Format(*.xml)";
  71. }
  72. };
  73. public static FileFilter CSV_FILES = new FileFilter() {
  74. public boolean accept(File f) {
  75. return f.getName().toLowerCase().endsWith(".csv") || f.isDirectory();
  76. }
  77. public String getDescription() {
  78. return "CSV Format(*.csv)";
  79. }
  80. };
  81. public static FileFilter PDF_FILES = new FileFilter() {
  82. public boolean accept(File f) {
  83. return f.getName().toLowerCase().endsWith(".pdf") || f.isDirectory();
  84. }
  85. public String getDescription() {
  86. return "PDF Format(*.pdf)";
  87. }
  88. };
  89. public static FileFilter SQL_FILES = new FileFilter() {
  90. public boolean accept(File f) {
  91. return f.getName().toLowerCase().endsWith(".sql") || f.isDirectory();
  92. }
  93. public String getDescription() {
  94. return "SQL Format(*.sql)";
  95. }
  96. };
  97. public static FileFilter OOO_FILES = new FileFilter() {
  98. public boolean accept(File f) {
  99. return f.getName().toLowerCase().matches(".*sxw$|.*doc$|.*xls$|.*odt$|.*ods$|.*pps$|.*odt$|.*ppt$|.*odp$") || f.isDirectory();
  100. }
  101. public String getDescription() {
  102. return "Open Office Formats(*.odt etc.)";
  103. }
  104. };
  105. private File file = null;
  106. public static File CURRENT_DIR = new File(Main.USER_HOME);
  107. private Container mparent;
  108. /**
  109. * Create a new dialog for files and dirs
  110. */
  111. public DialogForFile() {
  112. super();
  113. this.setFileSelectionMode(DialogForFile.FILES_AND_DIRECTORIES);
  114. this.setSelectedFile(CURRENT_DIR);
  115. }
  116. /**
  117. * Create a new dialog for the given selection mode
  118. *
  119. * @param selectionMode DialogForFile.MODE
  120. */
  121. public DialogForFile(int selectionMode) {
  122. super();
  123. this.setFileSelectionMode(selectionMode);
  124. this.setSelectedFile(CURRENT_DIR);
  125. }
  126. /**
  127. * Create a new dialog for files and dirs with the given file selected
  128. *
  129. * @param file
  130. */
  131. public DialogForFile(File file) {
  132. super();
  133. if (!file.isDirectory()) {
  134. this.setFileSelectionMode(DialogForFile.FILES_AND_DIRECTORIES);
  135. this.setSelectedFile(file);
  136. } else {
  137. this.setFileSelectionMode(DialogForFile.FILES_ONLY);
  138. this.setSelectedFile(new File(file.getPath() + File.separator + "file"));
  139. }
  140. }
  141. /**
  142. * Create a new dialog for the given selection mode with the given file
  143. * seleced
  144. *
  145. * @param mode
  146. * @param filename
  147. */
  148. public DialogForFile(int mode, String filename) {
  149. super();
  150. this.setFileSelectionMode(mode);
  151. this.setSelectedFile(new File(filename));
  152. }
  153. /**
  154. * Create a new dialog for the given selection mode with the given file
  155. * seleced
  156. *
  157. * @param mode
  158. * @param file
  159. */
  160. public DialogForFile(int mode, File file) {
  161. super();
  162. this.setFileSelectionMode(mode);
  163. this.setSelectedFile(file);
  164. }
  165. /**
  166. * Show a file selection dialog
  167. *
  168. * @return true if a file/dir was selected
  169. */
  170. public boolean chooseFile() {
  171. try {
  172. if (this.showOpenDialog(mparent == null ? mpv5.YabsViewProxy.instance().getIdentifierFrame() : mparent) == JFileChooser.APPROVE_OPTION) {
  173. try {
  174. this.file = this.getSelectedFile();
  175. setCurrentDir(file);
  176. return true;
  177. } catch (Exception ex) {
  178. Log.Debug(this, ex);
  179. }
  180. }
  181. } catch (Exception n) {
  182. Log.Debug(this, n.getMessage());
  183. }
  184. return false;
  185. }
  186. /**
  187. * Show a file save dialog
  188. *
  189. * @return true if a file/dir was selected
  190. */
  191. public boolean saveFile() {
  192. try {
  193. if (this.showSaveDialog(mparent == null ? mpv5.YabsViewProxy.instance().getIdentifierFrame() : mparent) == JFileChooser.APPROVE_OPTION) {
  194. try {
  195. if (!this.getSelectedFile().exists()) {
  196. this.file = this.getSelectedFile();
  197. setCurrentDir(file);
  198. return true;
  199. } else {
  200. if (!Popup.Y_N_dialog(Messages.FILE_EXISTS + "\n" + getSelectedFile())) {
  201. saveFile();
  202. } else {
  203. this.file = this.getSelectedFile();
  204. CURRENT_DIR = file.getParentFile();
  205. return true;
  206. }
  207. }
  208. } catch (Exception ex) {
  209. Log.Debug(this, ex);
  210. }
  211. }
  212. } catch (Exception n) {
  213. Log.Debug(this, n.getMessage());
  214. }
  215. return false;
  216. }
  217. /**
  218. * Show a file save dialog
  219. *
  220. * @param fileToSave
  221. */
  222. public File saveFile(File fileToSave) {
  223. setSelectedFile(new File(fileToSave.getName()));
  224. if (this.showSaveDialog(mparent == null ? mpv5.YabsViewProxy.instance().getIdentifierFrame() : mparent) == JFileChooser.APPROVE_OPTION) {
  225. try {
  226. this.file = this.getSelectedFile();
  227. if (!file.exists()) {
  228. try {
  229. file.createNewFile();
  230. } catch (IOException ex) {
  231. mpv5.logging.Log.Debug(ex); //Logger.getLogger(DialogForFile.class.getName()).log(Level.SEVERE, null, ex);
  232. }
  233. } else {
  234. if (!Popup.Y_N_dialog(Messages.FILE_EXISTS + "\n" + file)) {
  235. saveFile(file);
  236. }
  237. }
  238. FileDirectoryHandler.copyFile2(fileToSave, file);
  239. } catch (FileNotFoundException ex) {
  240. Log.Debug(ex);
  241. } catch (IOException ex) {
  242. Log.Debug(ex);
  243. }
  244. return file;
  245. }
  246. return null;
  247. }
  248. /**
  249. *
  250. * @return The selected file
  251. */
  252. public File getFile() {
  253. return file;
  254. }
  255. /**
  256. * Show a file selection dialog
  257. *
  258. * @param field This gets the selected files' canonical path
  259. * @return True if a file was selected
  260. */
  261. public boolean getFilePath(JTextField field) {
  262. if (this.showOpenDialog(mparent == null ? mpv5.YabsViewProxy.instance().getIdentifierFrame() : mparent) == JFileChooser.APPROVE_OPTION) {
  263. try {
  264. field.setText(this.getSelectedFile().getCanonicalPath());
  265. this.file = this.getSelectedFile();
  266. setCurrentDir(file);
  267. return true;
  268. } catch (IOException ex) {
  269. Log.Debug(this, ex);
  270. }
  271. }
  272. return false;
  273. }
  274. /**
  275. * Calls saveFile((File) object)
  276. *
  277. * @param object
  278. * @param e
  279. * @throws Exception
  280. */
  281. @Override
  282. @SuppressWarnings("unchecked")
  283. public void set(Object object, Exception e) throws Exception {
  284. if (e == null) {
  285. if (object instanceof List) {
  286. if (!((List) object).isEmpty()) {
  287. if (((List) object).get(0) instanceof File) {
  288. saveFiles((List) object);
  289. } else if (((List) object).get(0) instanceof Export) {
  290. saveFiles2((List) object);
  291. }
  292. }
  293. } else if (object instanceof File) {
  294. saveFile((File) object);
  295. } else if (object instanceof Export) {
  296. File d = getCurrentDirectory();
  297. setSelectedFile(((Export) object).getTargetFile());
  298. setCurrentDirectory(d);
  299. if (saveFile()) {
  300. FileDirectoryHandler.copyFile2(((Export) object).getTargetFile(), file, false);
  301. ((Export) object).getTargetFile().delete();
  302. }
  303. }
  304. } else {
  305. Popup.error(e);
  306. }
  307. }
  308. /**
  309. * Shows a choose directory dialog and saves a files in the list to it.
  310. *
  311. * @param <T>
  312. * @param list
  313. */
  314. public <T extends Export> void saveFiles2(List<T> list) {
  315. try {
  316. this.setFileSelectionMode(DialogForFile.DIRECTORIES_ONLY);
  317. this.setSelectedFile(CURRENT_DIR);
  318. if (this.showSaveDialog(mparent == null ? mpv5.YabsViewProxy.instance().getIdentifierFrame() : mparent) == JFileChooser.APPROVE_OPTION) {
  319. if (!this.getSelectedFile().exists()) {
  320. this.file = this.getSelectedFile();
  321. setCurrentDir(file);
  322. file.mkdirs();
  323. } else {
  324. this.file = this.getSelectedFile();
  325. setCurrentDir(file);
  326. }
  327. for (int i = 0; i < list.size(); i++) {
  328. File f3 = list.get(i).getTargetFile();
  329. FileDirectoryHandler.copyFile2(f3, new File(CURRENT_DIR.getPath() + File.separator + f3.getName()), true);
  330. }
  331. }
  332. } catch (Exception n) {
  333. Log.Debug(this, n.getMessage());
  334. Popup.error(n);
  335. }
  336. }
  337. /**
  338. * Shows a choose directory dialog and saves a files in the list to it.
  339. *
  340. * @param <T>
  341. * @param list
  342. */
  343. public <T extends File> void saveFiles(List<T> list) {
  344. try {
  345. this.setFileSelectionMode(DialogForFile.DIRECTORIES_ONLY);
  346. this.setSelectedFile(CURRENT_DIR);
  347. if (this.showSaveDialog(mparent == null ? mpv5.YabsViewProxy.instance().getIdentifierFrame() : mparent) == JFileChooser.APPROVE_OPTION) {
  348. if (!this.getSelectedFile().exists()) {
  349. this.file = this.getSelectedFile();
  350. setCurrentDir(file);
  351. file.mkdirs();
  352. } else {
  353. this.file = this.getSelectedFile();
  354. setCurrentDir(file);
  355. }
  356. for (int i = 0; i
  357. < list.size(); i++) {
  358. File f3 = list.get(i);
  359. FileDirectoryHandler.copyFile2(f3, new File(CURRENT_DIR.getPath() + File.separator + f3.getName()), true);
  360. }
  361. }
  362. } catch (Exception n) {
  363. Log.Debug(this, n.getMessage());
  364. Popup.error(n);
  365. }
  366. }
  367. /**
  368. * Set the modality parent
  369. *
  370. * @param mparent
  371. */
  372. public void setModalityParent(JComponent mparent) {
  373. this.mparent = mparent;
  374. }
  375. private void setCurrentDir(File file) {
  376. if (file != null) {
  377. if (file.isDirectory()) {
  378. CURRENT_DIR = file;
  379. } else {
  380. CURRENT_DIR = file.getParentFile();
  381. }
  382. }
  383. }
  384. }