| 1 | |
package org.jtheque.core.managers.view.impl.components.model; |
| 2 | |
|
| 3 | |
import org.jtheque.utils.collections.CollectionUtils; |
| 4 | |
|
| 5 | |
import javax.swing.DefaultListModel; |
| 6 | |
import java.util.ArrayList; |
| 7 | |
import java.util.Arrays; |
| 8 | |
import java.util.Collection; |
| 9 | |
import java.util.List; |
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public class SimpleListModel<T> extends DefaultListModel { |
| 35 | |
private final List<T> objects; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public SimpleListModel(){ |
| 41 | 0 | this(10); |
| 42 | 0 | } |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public SimpleListModel(int capacity){ |
| 50 | 0 | super(); |
| 51 | |
|
| 52 | 0 | objects = new ArrayList<T>(capacity); |
| 53 | 0 | } |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public SimpleListModel(Collection<T> objects){ |
| 61 | 0 | super(); |
| 62 | |
|
| 63 | 0 | this.objects = CollectionUtils.copyOf(objects); |
| 64 | 0 | } |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public SimpleListModel(T[] objects){ |
| 72 | 0 | super(); |
| 73 | |
|
| 74 | 0 | this.objects = Arrays.asList(objects); |
| 75 | 0 | } |
| 76 | |
|
| 77 | |
@Override |
| 78 | |
public final Object getElementAt(int index){ |
| 79 | 0 | return objects.get(index); |
| 80 | |
} |
| 81 | |
|
| 82 | |
@Override |
| 83 | |
public final Object get(int index){ |
| 84 | 0 | return objects.get(index); |
| 85 | |
} |
| 86 | |
|
| 87 | |
@Override |
| 88 | |
public final int getSize(){ |
| 89 | 0 | return objects.size(); |
| 90 | |
} |
| 91 | |
|
| 92 | |
@Override |
| 93 | |
public final Object remove(int index){ |
| 94 | 0 | T category = objects.remove(index); |
| 95 | 0 | fireIntervalRemoved(this, index, index); |
| 96 | 0 | return category; |
| 97 | |
} |
| 98 | |
|
| 99 | |
@Override |
| 100 | |
public final void addElement(Object obj){ |
| 101 | 0 | objects.add((T) obj); |
| 102 | 0 | fireIntervalAdded(this, getSize(), getSize()); |
| 103 | 0 | } |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
public void addElements(Iterable<T> elements){ |
| 111 | 0 | int index = objects.size(); |
| 112 | |
|
| 113 | 0 | for (T category : elements){ |
| 114 | 0 | objects.add(category); |
| 115 | |
} |
| 116 | |
|
| 117 | 0 | fireIntervalAdded(this, index, getSize()); |
| 118 | 0 | } |
| 119 | |
|
| 120 | |
@Override |
| 121 | |
public final void clear(){ |
| 122 | 0 | objects.clear(); |
| 123 | 0 | fireContentsChanged(this, 0, getSize()); |
| 124 | 0 | } |
| 125 | |
|
| 126 | |
@Override |
| 127 | |
public final boolean removeElement(Object obj){ |
| 128 | 0 | T category = (T) obj; |
| 129 | |
|
| 130 | 0 | int index = objects.indexOf(category); |
| 131 | 0 | boolean remove = objects.remove(category); |
| 132 | 0 | fireIntervalRemoved(this, index, index); |
| 133 | 0 | return remove; |
| 134 | |
} |
| 135 | |
|
| 136 | |
@Override |
| 137 | |
public final void removeAllElements(){ |
| 138 | 0 | objects.clear(); |
| 139 | 0 | fireContentsChanged(this, 0, getSize()); |
| 140 | 0 | } |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public void setElements(Collection<T> elements){ |
| 148 | 0 | objects.clear(); |
| 149 | 0 | objects.addAll(elements); |
| 150 | 0 | fireContentsChanged(this, 0, getSize()); |
| 151 | 0 | } |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
public void setElements(T[] elements){ |
| 159 | 0 | objects.clear(); |
| 160 | 0 | objects.addAll(Arrays.asList(elements)); |
| 161 | 0 | fireContentsChanged(this, 0, getSize()); |
| 162 | 0 | } |
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
public Collection<T> getObjects(){ |
| 170 | 0 | return objects; |
| 171 | |
} |
| 172 | |
} |