/src/mpv5/db/objects/FileToItem.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/ · Java · 148 lines · 91 code · 14 blank · 43 comment · 5 complexity · 43d6bdd7c9ddf0fb8ff0b7d74002b441 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.db.objects;
  18. import java.io.File;
  19. import javax.swing.Icon;
  20. import javax.swing.JComponent;
  21. import javax.swing.JFileChooser;
  22. import mpv5.db.common.Context;
  23. import mpv5.db.common.DatabaseObject;
  24. import mpv5.db.common.QueryHandler;
  25. import mpv5.logging.Log;
  26. import mpv5.utils.files.FileDirectoryHandler;
  27. import mpv5.utils.images.MPIcon;
  28. /**
  29. *
  30. *
  31. */
  32. public class FileToItem extends DatabaseObject {
  33. private String description = "";
  34. private int itemsids;
  35. private String filename = "";
  36. private File file;
  37. public FileToItem() {
  38. setContext(Context.getFilesToItems());
  39. }
  40. @Override
  41. public JComponent getView() {
  42. try {
  43. FileDirectoryHandler.open(getFile());
  44. } catch (Exception e) {
  45. Log.Debug(e);
  46. }
  47. return null;
  48. }
  49. /**
  50. * @return the description
  51. */
  52. public String __getDescription() {
  53. return description;
  54. }
  55. /**
  56. * @param description the description to set
  57. */
  58. public void setDescription(String description) {
  59. this.description = description;
  60. }
  61. /**
  62. * @return the filename
  63. */
  64. public String __getFilename() {
  65. return filename;
  66. }
  67. /**
  68. * @param filename the filename to set
  69. */
  70. public void setFilename(String filename) {
  71. this.filename = filename;
  72. }
  73. MPIcon icon;
  74. @Override
  75. public mpv5.utils.images.MPIcon getIcon() {
  76. if (icon == null) {
  77. try {
  78. Log.Debug(this, "Determining Icon for " + __getCname());
  79. icon = new MPIcon(MPIcon.DIRECTORY_DEFAULT_ICONS + __getCname().substring(__getCname().lastIndexOf(".") + 1, __getCname().length()) + ".png");
  80. return icon;
  81. } catch (Exception e) {
  82. Log.Debug(this, "Icon file not existing in " + MPIcon.DIRECTORY_DEFAULT_ICONS);
  83. try {
  84. JFileChooser chooser = new JFileChooser();
  85. icon = new MPIcon(chooser.getIcon(new File(filename)));
  86. return icon;
  87. } catch (Exception ez) {
  88. Log.Debug(this, ez);
  89. icon = new MPIcon(MPIcon.DIRECTORY_DEFAULT_ICONS + "folder_tar.png");
  90. return icon;
  91. }
  92. }
  93. } else {
  94. return icon;
  95. }
  96. }
  97. /**
  98. * Fetches the physical file from db
  99. *
  100. * @return
  101. */
  102. public synchronized File getFile() {
  103. if (file == null) {
  104. try {
  105. file = QueryHandler.instanceOf().clone(Context.getFiles()).retrieveFile(filename,
  106. new File(FileDirectoryHandler.getTempDir() + getCname()));
  107. } catch (Exception e) {
  108. Log.Debug(e);
  109. }
  110. }
  111. return file;
  112. }
  113. /**
  114. * @return the itemsids
  115. */
  116. public int __getItemsids() {
  117. return itemsids;
  118. }
  119. /**
  120. * @param itemsids the itemsids to set
  121. */
  122. public void setItemsids(int itemsids) {
  123. this.itemsids = itemsids;
  124. }
  125. @Override
  126. public boolean delete() {
  127. try {
  128. QueryHandler.instanceOf().clone(Context.getFiles()).removeFile(filename);
  129. } catch (Exception ex) {
  130. Log.Debug(ex);
  131. }
  132. return super.delete();
  133. }
  134. }