/src/mpv5/db/objects/FileToProduct.java

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