PageRenderTime 203ms CodeModel.GetById 111ms app.highlight 6ms RepoModel.GetById 84ms app.codeStats 0ms

/src/mpv5/data/MPList.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 128 lines | 73 code | 12 blank | 43 comment | 9 complexity | 2dd5d54a36e5831ab55bcdf016a7c2c0 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.data;
 18
 19import com.lowagie.text.ListItem;
 20import java.awt.Color;
 21import java.awt.Component;
 22import java.util.Vector;
 23import javax.swing.BorderFactory;
 24import javax.swing.DefaultListModel;
 25import javax.swing.JLabel;
 26import javax.swing.JList;
 27import javax.swing.ListCellRenderer;
 28import mpv5.db.common.DatabaseObject;
 29
 30/**
 31 * This is our own implementation of a list holding {@link DatabaseObject}s
 32 * @author andreasw
 33 */
 34public class MPList extends Vector<DatabaseObject> {
 35
 36    /**
 37     * Invokes the save method on every entry in the list. If the list is empty, returns true
 38     * @return true if every item in the last was saved successfully
 39     */
 40    public boolean saveAll() {
 41        boolean success = false;
 42        for (int i = 0; i < this.size(); i++) {
 43            if (success) {
 44                success = this.get(i).save();
 45            } else {//save state
 46                this.get(i).save();
 47            }
 48        }
 49        return success;
 50    }
 51
 52    /**
 53     * Invokes the delete method on every entry in the list. If the list is empty, returns true
 54     * @return true if every item in the last was deleted successfully
 55     */
 56    public boolean deleteAll() {
 57        boolean success = false;
 58        for (int i = 0; i < this.size(); i++) {
 59            if (success) {
 60                success = this.get(i).delete();
 61            } else {//save state
 62                this.get(i).delete();
 63            }
 64        }
 65        return success;
 66    }
 67
 68    /**
 69     * Creates a new list model from the actual values in the list
 70     * @return
 71     */
 72    public DefaultListModel getListModel() {
 73        DefaultListModel model = new DefaultListModel();
 74        for (int i = 0; i < this.size(); i++) {
 75            DatabaseObject databaseObject = this.get(i);
 76            model.addElement(databaseObject);
 77        }
 78        return model;
 79    }
 80
 81    /**
 82     * Returns a custom cell renderer which displays an icon and text
 83     * @return
 84     */
 85    public static ListCellRenderer getDBORenderer() {
 86        return new MyCellRenderer();
 87    }
 88
 89    static class MyCellRenderer extends JLabel
 90            implements ListCellRenderer {
 91
 92        public MyCellRenderer() {
 93            // Don't paint behind the component
 94            setOpaque(true);
 95        }
 96
 97        // Set the attributes of the
 98        //class and return a reference
 99        public Component getListCellRendererComponent(JList list,
100                Object value, // value to display
101                int index, // cell index
102                boolean iss, // is selected
103                boolean chf) // cell has focus?
104        {
105            // Set the text and
106            //background color for rendering
107            setText(((DatabaseObject) value).toString());
108            setIcon(((DatabaseObject) value).getIcon());
109
110            // Set a border if the
111            //list item is selected
112            if (iss) {
113                setBorder(BorderFactory.createLineBorder(
114                        Color.blue, 1));
115            } else {
116                setBorder(BorderFactory.createLineBorder(
117                        list.getBackground(), 1));
118            }
119
120            return this;
121        }
122    }
123
124    @Override
125    public DatabaseObject[] toArray() {
126        return super.toArray(new DatabaseObject[0]);
127    }
128}