/*******************************************************************
* Copyright (c) 2006-2013, Cloudsmith Inc.
* The code, documentation and other materials contained herein
* are the sole and exclusive property of Cloudsmith Inc. and may
* not be disclosed, used, modified, copied or distributed without
* prior written consent or license from Cloudsmith Inc.
******************************************************************/
package org.eclipse.buckminster.generic.model;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
/**
* AbstractModel handles delegation to an instance of PropertyChangeSupport.
* This makes it possible to handle an extending class as a Model in a
* Model-View relationship, and using eclipse data binding in particular.
*
* Abstract Model also has a basic implementation of IAdaptable returning the
* instance if it is an instance of the requested class.
*
* @author Henrik Lindberg
*/
public class AbstractModel implements IPropertyChange, IAdaptable {
private final PropertyChangeSupport changeSupport;
public AbstractModel() {
changeSupport = new PropertyChangeSupport(this);
}
@Override
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
@Override
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(propertyName, listener);
}
protected void fireIndexedPropertyChange(String propertyName, int index, boolean oldValue, boolean newValue) {
changeSupport.fireIndexedPropertyChange(propertyName, index, oldValue, newValue);
}
protected void fireIndexedPropertyChange(String propertyName, int index, int oldValue, int newValue) {
changeSupport.fireIndexedPropertyChange(propertyName, index, oldValue, newValue);
}
protected void fireIndexedPropertyChange(String propertyName, int index, Object oldValue, Object newValue) {
changeSupport.fireIndexedPropertyChange(propertyName, index, oldValue, newValue);
}
protected void firePropertyChange(PropertyChangeEvent evt) {
changeSupport.firePropertyChange(evt);
}
protected void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {
changeSupport.firePropertyChange(propertyName, oldValue, newValue);
}
protected void firePropertyChange(String propertyName, int oldValue, int newValue) {
changeSupport.firePropertyChange(propertyName, oldValue, newValue);
}
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
changeSupport.firePropertyChange(propertyName, oldValue, newValue);
}
@SuppressWarnings("unchecked")
@Override
public <T> T getAdapter(Class<T> adapter) {
if (adapter.isInstance(this))
return (T) this;
return Platform.getAdapterManager().getAdapter(this, adapter);
}
public PropertyChangeListener[] getPropertyChangeListeners() {
return changeSupport.getPropertyChangeListeners();
}
public PropertyChangeListener[] getPropertyChangeListeners(String propertyName) {
return changeSupport.getPropertyChangeListeners(propertyName);
}
public boolean hasListeners(String propertyName) {
return changeSupport.hasListeners(propertyName);
}
@Override
public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
@Override
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(propertyName, listener);
}
}