/*
* This file is part of TextScout.
*
* TextScout is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* TextScout is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with TextScout. If not, see <http://www.gnu.org/licenses/>.
*/
package ui.models;
import java.util.LinkedList;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import logic.Constraints;
public class InputComboBoxModel implements ComboBoxModel
{
private List<String> entries = null;
private Object selectedItem = "";
private ListDataListener listener = null;
public InputComboBoxModel() {
entries = new LinkedList<>();
}
@Override
public void setSelectedItem(Object anItem) {
Constraints.ensureArgumentNotNull(anItem);
this.selectedItem = anItem;
}
@Override
public Object getSelectedItem() {
return this.selectedItem;
}
@Override
public int getSize() {
return entries.size();
}
@Override
public Object getElementAt(int index) {
return entries.get(index);
}
@Override
public void addListDataListener(ListDataListener l) {
listener = l;
}
@Override
public void removeListDataListener(ListDataListener l) {
listener = null;
}
public boolean isEmpty() {
return entries.isEmpty();
}
public void add(String e) {
Constraints.ensureArgumentNotNull(e);
entries.add(e);
listener.intervalAdded(new ListDataEvent(this, ListDataEvent.INTERVAL_ADDED, 0, entries.size()-1));
}
public void remove(Object o) {
remove(entries.indexOf(o));
}
public void clear() {
entries.clear();
listener.intervalRemoved(new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, 0, 0));
}
public String get(int index) {
Constraints.ensureIndexInRange(entries, index);
return entries.get(index);
}
public void remove(int index) {
Constraints.ensureIndexInRange(entries, index);
entries.remove(index);
listener.intervalRemoved(new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, 0, Math.max(0, entries.size()-1)));
}
public void setSelectedIndex(int index) {
this.setSelectedItem(entries.get(index));
}
}