io.guise.framework.platform.AbstractDepictor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of guise-framework Show documentation
Show all versions of guise-framework Show documentation
Guise™ Internet application framework.
/*
* Copyright © 2005-2008 GlobalMentor, Inc.
*
* 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 io.guise.framework.platform;
import java.beans.*;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import com.globalmentor.beans.PropertyBindable;
import io.guise.framework.*;
import io.guise.framework.event.*;
/**
* An abstract strategy for depicting objects on some platform.
*
* The {@link Depictor#GENERAL_PROPERTY} is used to indicate that some general property has changed.
*
* @param The type of object being depicted.
* @author Garret Wilson
*/
public abstract class AbstractDepictor implements Depictor {
/** The Guise session that owns this object. */
private final GuiseSession session;
@Override
public GuiseSession getSession() {
return session;
}
private final Platform platform;
@Override
public Platform getPlatform() {
return platform;
}
/**
* {@inheritDoc}
*
* This method delegates to {@link Platform#getDepictContext()}.
*
*/
@Override
public DepictContext getDepictContext() {
return getPlatform().getDepictContext();
}
/** The thread-safe list of properties that are to be ignored. */
private final Set ignoredProperties = new CopyOnWriteArraySet();
/** @return The depicted object properties that are to be ignored. */
protected Set getIgnoredProperties() {
return ignoredProperties;
}
/** The thread-safe list of modified properties. */
private final Set modifiedProperties = new CopyOnWriteArraySet();
/** @return The depicted object properties that have been modified. */
protected Set getModifiedProperties() {
return modifiedProperties;
}
/**
* Calls when a property has been modified to sets whether a property has been modified. If the property's modified status is set to true
, the
* depictor's {@link #isDepicted()} status is changed to false
. If the property's modified status is set to false
and there are no
* other modified properties, the depictor's {@link #isDepicted()} status is set to true
.
* @param property The property that has been modified.
* @param modified Whether the property has been modified.
* @see #setDepicted(boolean)
*/
protected void setPropertyModified(final String property, final boolean modified) {
if(modified) { //if the property is modified
modifiedProperties.add(property); //add this property to the list of modified properties
depicted = false; //note that the depiction is not updated
} else { //if the property is not modified
if(modifiedProperties.remove(property)) { //remove the property from the set of modified properties; if the property was in the set
if(modifiedProperties.isEmpty()) { //if there are no modified properties
depicted = true; //count the depiction as updated
}
}
}
}
/** The listener that marks this depiction as dirty if a change occurs. */
private final DepictedPropertyChangeListener depictedPropertyChangeListener = new DepictedPropertyChangeListener();
/** @return The listener that marks this depiction as dirty if a change occurs. */
protected DepictedPropertyChangeListener getDepictedPropertyChangeListener() {
return depictedPropertyChangeListener;
}
/** The object being depicted. */
private O depictedObject = null;
@Override
public O getDepictedObject() {
return depictedObject;
}
/** Whether this depictor's representation of the depicted object is up to date. */
private boolean depicted = false;
@Override
public boolean isDepicted() {
return depicted;
}
@Override
public void setDepicted(final boolean newDepicted) {
if(newDepicted) { //if the depiction is being marked as updated
modifiedProperties.clear(); //remove all modified properties
} else { //if the depiction is being marked as not updated
modifiedProperties.add(GENERAL_PROPERTY); //add the general property to the list of modified properties
}
depicted = newDepicted; //update the depicted status
}
/** Default constructor. */
public AbstractDepictor() {
this.session = Guise.getInstance().getGuiseSession(); //store a reference to the current Guise session
this.platform = this.session.getPlatform(); //store a reference to the platform
}
/**
* {@inheritDoc}
*
* This version listens for property changes of a {@link PropertyBindable} object.
*
*
* This version listens for list changes of a {@link ListListenable} object.
*
* @see #depictedPropertyChangeListener
*/
@Override
public void installed(final O depictedObject) {
if(this.depictedObject != null) { //if this depictor is already installed
throw new IllegalStateException("Depictor is already installed in a depicted object.");
}
this.depictedObject = depictedObject; //change depicted objects
if(depictedObject instanceof PropertyBindable) { //if the depicted object allows bound properties
((PropertyBindable)depictedObject).addPropertyChangeListener(getDepictedPropertyChangeListener()); //listen for property changes
}
if(depictedObject instanceof ListListenable) { //if the depicted object notifies of list changes
((ListListenable