forked from LupusNobilis/java-org.hwo.ui
Improved ComboBoxHistoryModel
parent
53eafa9b4b
commit
c545d64984
|
@ -1,36 +1,50 @@
|
|||
package org.hwo.models;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import javax.swing.ComboBoxModel;
|
||||
import javax.swing.event.ListDataEvent;
|
||||
import javax.swing.event.ListDataListener;
|
||||
|
||||
public class ComboBoxHistoryModel<E> implements ComboBoxModel<E> {
|
||||
import org.hwo.models.History.HistoryListener;
|
||||
|
||||
public class ComboBoxHistoryModel<E> implements ComboBoxModel<E>,HistoryListener<E> {
|
||||
|
||||
private LinkedList<ListDataListener> listDataListeners = new LinkedList<>();
|
||||
private LinkedList<E> items = new LinkedList<>();
|
||||
private History<E> history;
|
||||
|
||||
private E selectedItem;
|
||||
private int historySize;
|
||||
|
||||
|
||||
public ComboBoxHistoryModel()
|
||||
{
|
||||
this.setHistory(new History<>());
|
||||
this.selectedItem = null;
|
||||
this.historySize = 10;
|
||||
}
|
||||
public ComboBoxHistoryModel(int historySize)
|
||||
{
|
||||
this.setHistory(new History<E>(historySize));
|
||||
this.selectedItem = null;
|
||||
this.historySize = historySize;
|
||||
}
|
||||
public ComboBoxHistoryModel(History<E> history){
|
||||
this.setHistory(history);
|
||||
}
|
||||
|
||||
|
||||
public History<E> getHistory() {
|
||||
return history;
|
||||
}
|
||||
public void setHistory(History<E> history) {
|
||||
if (this.history != null) {
|
||||
this.history.removeHistoryListener(this);
|
||||
}
|
||||
this.history = history;
|
||||
if (this.history != null) {
|
||||
this.history.addHistoryListener(this);
|
||||
fireContentsChanged();
|
||||
}
|
||||
}
|
||||
private void fireContentsChanged()
|
||||
{
|
||||
ListDataEvent lde = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, this.items.size());
|
||||
ListDataEvent lde = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, this.history.size());
|
||||
for (ListDataListener l: listDataListeners) {
|
||||
l.contentsChanged(lde);
|
||||
}
|
||||
|
@ -43,12 +57,12 @@ public class ComboBoxHistoryModel<E> implements ComboBoxModel<E> {
|
|||
|
||||
@Override
|
||||
public E getElementAt(int arg0) {
|
||||
return items.get(arg0);
|
||||
return history.get(arg0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return items.size();
|
||||
return history.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -64,65 +78,54 @@ public class ComboBoxHistoryModel<E> implements ComboBoxModel<E> {
|
|||
@Override
|
||||
public void setSelectedItem(Object arg0) {
|
||||
selectedItem = (E)arg0;
|
||||
if (items.contains(selectedItem)) {
|
||||
items.remove(selectedItem);
|
||||
}
|
||||
items.add(0,selectedItem);
|
||||
if (!clampHistory()) {
|
||||
fireContentsChanged();
|
||||
if (selectedItem != null) {
|
||||
this.history.add(selectedItem);
|
||||
}
|
||||
}
|
||||
|
||||
public int getHistorySize() {
|
||||
return historySize;
|
||||
return history.getHistorySize();
|
||||
}
|
||||
|
||||
public void setHistorySize(int historySize) {
|
||||
this.historySize = historySize;
|
||||
this.history.setHistorySize(historySize);
|
||||
}
|
||||
|
||||
public Object[] getItems(){
|
||||
return items.toArray();
|
||||
return history.getItems();
|
||||
}
|
||||
|
||||
public E[] getItems(E[] at) {
|
||||
return items.toArray(at);
|
||||
return history.getItems(at);
|
||||
}
|
||||
|
||||
public void setItems(E[] items) {
|
||||
this.items.clear();
|
||||
this.items.addAll(Arrays.asList(items));
|
||||
if (!this.items.contains(selectedItem)) {
|
||||
selectedItem = null;
|
||||
}
|
||||
if (!clampHistory())
|
||||
fireContentsChanged();
|
||||
history.setItems(items);
|
||||
}
|
||||
|
||||
private boolean clampHistory() {
|
||||
if (items.size() > historySize) {
|
||||
while (items.size() > historySize) {
|
||||
items.removeLast();
|
||||
}
|
||||
fireContentsChanged();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void addElement(E element) {
|
||||
this.items.add(0,element);
|
||||
if (!clampHistory())
|
||||
fireContentsChanged();
|
||||
this.history.add(element);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
items.clear();
|
||||
fireContentsChanged();
|
||||
this.history.clear();
|
||||
}
|
||||
|
||||
public boolean contains(E val) {
|
||||
return items.contains(val);
|
||||
return history.contains(val);
|
||||
}
|
||||
@Override
|
||||
public void HistoryChanged(History<E> history) {
|
||||
fireContentsChanged();
|
||||
}
|
||||
@Override
|
||||
public void HistoryItemAdded(History<E> history, E item) {
|
||||
fireContentsChanged();
|
||||
}
|
||||
@Override
|
||||
public void HistoryItemRemoved(History<E> history, E item) {
|
||||
fireContentsChanged();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,120 @@
|
|||
package org.hwo.models;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class History<E> {
|
||||
private LinkedList<HistoryListener<E>> historyListeners = new LinkedList<>();
|
||||
private LinkedList<E> items = new LinkedList<>();
|
||||
private int historySize = 10;
|
||||
|
||||
public History(){
|
||||
}
|
||||
|
||||
public History(int historySize) {
|
||||
this.historySize = historySize;
|
||||
}
|
||||
|
||||
public void addHistoryListener(HistoryListener<E> listener) {
|
||||
historyListeners.add(listener);
|
||||
}
|
||||
public void removeHistoryListener(HistoryListener<E> listener) {
|
||||
historyListeners.remove(listener);
|
||||
}
|
||||
|
||||
protected void fireHistoryChanged() {
|
||||
for (HistoryListener<E> historyListener : historyListeners) {
|
||||
historyListener.HistoryChanged(this);
|
||||
}
|
||||
}
|
||||
protected void fireHistoryItemAdded(E item) {
|
||||
for (HistoryListener<E> historyListener : historyListeners) {
|
||||
historyListener.HistoryItemAdded(this,item);
|
||||
}
|
||||
}
|
||||
protected void fireHistoryItemRemoved(E item) {
|
||||
for (HistoryListener<E> historyListener : historyListeners) {
|
||||
historyListener.HistoryItemRemoved(this, item);
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
items.clear();
|
||||
fireHistoryChanged();
|
||||
}
|
||||
|
||||
public boolean contains(E item) {
|
||||
return items.contains(item);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return this.items.size();
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
return this.items.get(index);
|
||||
}
|
||||
|
||||
public Object[] getItems(){
|
||||
return items.toArray();
|
||||
}
|
||||
|
||||
public E[] getItems(E[] at) {
|
||||
return items.toArray(at);
|
||||
}
|
||||
|
||||
public void setItems(E[] items) {
|
||||
this.items.clear();
|
||||
this.items.addAll(Arrays.asList(items));
|
||||
fireHistoryChanged();
|
||||
|
||||
clampHistory();
|
||||
}
|
||||
|
||||
public void add(E item) {
|
||||
if (this.items.contains(item)) {
|
||||
if (this.items.indexOf(item) > 0) {
|
||||
this.items.remove(item);
|
||||
this.items.add(0,item);
|
||||
fireHistoryChanged();
|
||||
clampHistory();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
this.items.add(0,item);
|
||||
fireHistoryItemAdded(item);
|
||||
clampHistory();
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(E item) {
|
||||
this.items.remove(item);
|
||||
fireHistoryItemRemoved(item);
|
||||
}
|
||||
|
||||
private void clampHistory() {
|
||||
if (items.size() > historySize) {
|
||||
while (items.size() > historySize) {
|
||||
fireHistoryItemRemoved(items.removeLast());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getHistorySize() {
|
||||
return historySize;
|
||||
}
|
||||
|
||||
public void setHistorySize(int historySize) {
|
||||
this.historySize = historySize;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static interface HistoryListener<T> {
|
||||
void HistoryChanged(History<T> history);
|
||||
void HistoryItemAdded(History<T> history,T item);
|
||||
void HistoryItemRemoved(History<T> history,T item);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue