📄 observablelist.java
字号:
/*
* Copyright 2006 Marcel Schoffelmeer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.s10r.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* ObservableList is a generic observable list that contains
* Observable items. When changes are made to either the list
* or the contained observable items, the observers added to this list
* are notified using an UpdateEvent object with a type
* appropriate for the change (ADD, REMOVE, etc).
*
* Note that using this class, observers do not have to add themselves
* to individual observables. Being added to the top level list is
* sufficient for observers to listen for contained object changes.
*/
public class ObservableList<T extends Observable> implements Observable, Observer
{
private ObservableSupport observers = new ObservableSupport();
private List<T> items = new ArrayList<T>();
public ObservableList()
{
}
public ObservableList(List<T> newItems)
{
for (T item: newItems)
{
add(item);
}
}
public T get(int i)
{
return items.get(i);
}
/**
* Adds a task to the list. Adds the TaskList to the Task as an
* observer to listen for task changes. Notifies the observers
* or this list with an ADD event.
*
* @param task
*/
public void add(T item)
{
items.add(item);
item.addObserver(this);
observers.notify(UpdateEvent.Type.ADD, item);
}
/**
* Adds all tasks to the list. Adds the TaskList to each task
* as an observer to listen for task changes
*/
public void addAll(Collection<T> c)
{
items.addAll(c);
for (T item: c)
{
item.addObserver(this);
}
observers.notify(UpdateEvent.Type.ADD_ALL, null);
}
public int size()
{
return items.size();
}
/**
* Removes a task from the list. Removes the TaskList from the Task as an
* observers. Notifies the observers of this list with a REMOVE event.
*
* @param task
*/
public void remove(T item)
{
items.remove(item);
item.removeObserver(this);
observers.notify(UpdateEvent.Type.REMOVE, item);
}
/**
* Removes all tasks from the list. Removes this list as an observer
* from contained tasks. Finally, observers of this list are notified
* with a REMOVE_ALL event.
*/
public void clear()
{
// first remove this object as the observer of all contained tasks:
// no need to getting notified about task updates if this list is
// not managing the tasks anymore
removeObserverFromItems(this);
items.clear();
// tell observers that all have been removed
observers.notify(UpdateEvent.Type.REMOVE_ALL, null);
}
private void removeObserverFromItems(Observer observer)
{
for (T item : items)
{
item.removeObserver(observer);
}
}
/**
* Convenience method, necessary if this list is used to
* implement an IStructuredContentProvider (see getElements method).
*
* @return
*/
public Object[] toArray()
{
return items.toArray();
}
public List<T> toList()
{
return items;
}
/**
* Note: only adds observers at this level: since this list
* already listens to changes of the tasks themselves,
* this observer will get notified through propagation
*
* @param o
*/
public void addObserver(Observer o)
{
observers.add(o);
}
public void removeObserver(Observer o)
{
observers.remove(o);
}
public void clearObservers()
{
observers.clear();
}
// bubbles events from child tasks up to observers of this list
public void update(UpdateEvent event)
{
observers.notify(event);
}
public List<T> getItems()
{
return items;
}
public Iterator<T> getItemIterator()
{
return items.iterator();
}
public boolean contains(Object obj)
{
return items.contains(obj);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -