PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 0ms

/src/mpv5/utils/models/MPComboboxModel.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 68 lines | 30 code | 7 blank | 31 comment | 6 complexity | 1b574db978a538f00e200bd6fef5c9f7 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.utils.models;
18
19import javax.swing.DefaultComboBoxModel;
20
21/**
22 * MP Implementation of a {@link DefaultComboBoxModel}
23 */
24public class MPComboboxModel extends DefaultComboBoxModel {
25
26    private static final long serialVersionUID = 1L;
27
28    /**
29     * Constructs a DefaultComboBoxModel object initialized with an array of {@link MPComboboxModelItem}objects.
30     * @param items
31     */
32    public MPComboboxModel(MPComboBoxModelItem[] items) {
33        super(items);
34    }
35
36    /**
37     * Returns all elements in the model
38     * @return
39     */
40    public MPComboBoxModelItem[] getElements() {
41        MPComboBoxModelItem[] m = new MPComboBoxModelItem[getSize()];
42        for (int i = 0; i < m.length; i++) {
43            m[i] = (MPComboBoxModelItem) getElementAt(i);
44        }
45        return m;
46    }
47
48    @Override
49    public MPComboBoxModelItem getSelectedItem() {
50        if (super.getSelectedItem() != null) {
51            if (super.getSelectedItem() instanceof MPComboBoxModelItem) {
52                return (MPComboBoxModelItem) super.getSelectedItem();
53            } else {
54                return new MPComboBoxModelItem(0, super.getSelectedItem().toString());
55            }
56        } else {
57            return null;
58        }
59    }
60
61    /**
62     * Set the value of the selected item. The selected item may be null.
63     * @param item - The combo box value or null for no selection.
64     */
65    public void setSelectedItem(MPComboBoxModelItem item) {
66        super.setSelectedItem(item);
67    }
68}