PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 51ms app.codeStats 1ms

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