PageRenderTime 263ms CodeModel.GetById 113ms app.highlight 63ms RepoModel.GetById 79ms app.codeStats 0ms

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