📄 defaultlistmodel.java.svn-base
字号:
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package com.sun.lwuit.list;
import com.sun.lwuit.Display;
import com.sun.lwuit.events.DataChangedListener;
import com.sun.lwuit.events.SelectionListener;
import java.util.Enumeration;
import java.util.Vector;
/**
* Default implementation of the list model based on a vector of elements
*
* @author Chen Fishbein
*/
public class DefaultListModel implements ListModel{
private Vector items;
private Vector dataListeners = new Vector();
private Vector selectionListeners = new Vector();
private int selectedIndex = 0;
/**
* Creates a new instance of DefaultListModel
*/
public DefaultListModel() {
this.items = new Vector();
}
/**
* Creates a new instance of DefaultListModel
*/
public DefaultListModel(Vector items) {
this.items = items;
}
/**
* Creates a new instance of DefaultListModel
*/
public DefaultListModel(Object[] items) {
this.items = createVector(items);
}
private static Vector createVector(Object[] items) {
if (items == null) {
items = new Object[] {""};
}
Vector vec = new Vector(items.length);
for(int iter = 0 ; iter < items.length ; iter++) {
vec.addElement(items[iter]);
}
return vec;
}
/**
* @inheritDoc
*/
public Object getItemAt(int index) {
if(index < getSize() && index >= 0){
return items.elementAt(index);
}
return null;
}
/**
* @inheritDoc
*/
public int getSize() {
return items.size();
}
/**
* @inheritDoc
*/
public int getSelectedIndex() {
return selectedIndex;
}
/**
* @inheritDoc
*/
public void addItem(Object item){
items.addElement(item);
fireDataChangedEvent(DataChangedListener.ADDED, items.size());
}
/**
* Change the item at the given index
*/
public void setItem(int index, Object item){
items.setElementAt(item, index);
fireDataChangedEvent(DataChangedListener.CHANGED, index);
}
/**
* Adding an item to list at given index
* @param item - the item to add
* @param index - the index position in the list
*/
public void addItemAtIndex(Object item, int index){
if (index <= items.size()) {
items.insertElementAt(item, index);
fireDataChangedEvent(DataChangedListener.ADDED, index);
}
}
/**
* @inheritDoc
*/
public void removeItem(int index){
if(index < getSize() && index >= 0){
items.removeElementAt(index);
if(index != 0){
setSelectedIndex(index - 1);
}
fireDataChangedEvent(DataChangedListener.REMOVED, index);
}
}
/**
* Removes all elements from the model
*/
public void removeAll(){
while(getSize() > 0) {
removeItem(0);
}
}
/**
* @inheritDoc
*/
public void setSelectedIndex(int index) {
int oldIndex = selectedIndex;
this.selectedIndex = index;
fireSelectionEvent(oldIndex, selectedIndex);
}
/**
* @inheritDoc
*/
public void addDataChangedListener(DataChangedListener l) {
dataListeners.addElement(l);
}
/**
* @inheritDoc
*/
public void removeDataChangedListener(DataChangedListener l) {
dataListeners.removeElement(l);
}
private void fireDataChangedEvent(final int status, final int index){
if(!Display.getInstance().isEdt()) {
Display.getInstance().callSeriallyAndWait(new Runnable() {
public void run() {
fireDataChangedEvent(status, index);
}
});
return;
}
// we query size with every iteration and avoid an Enumeration since a data
// changed event can remove a listener instance thus break the enum...
for(int iter = 0 ; iter < dataListeners.size() ; iter++) {
DataChangedListener l = (DataChangedListener)dataListeners.elementAt(iter);
l.dataChanged(status, index);
}
}
/**
* @inheritDoc
*/
public void addSelectionListener(SelectionListener l) {
selectionListeners.addElement(l);
}
/**
* @inheritDoc
*/
public void removeSelectionListener(SelectionListener l) {
selectionListeners.removeElement(l);
}
private void fireSelectionEvent(int oldIndex, int newIndex){
Enumeration listenersEnum = selectionListeners.elements();
while(listenersEnum.hasMoreElements()){
SelectionListener l = (SelectionListener)listenersEnum.nextElement();
l.selectionChanged(oldIndex, newIndex);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -