io.guise.framework.component.AbstractImageComponent 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.component;
import java.net.URI;
import static java.util.Objects.*;
import static com.globalmentor.java.Arrays.*;
import static com.globalmentor.net.URIs.*;
import com.globalmentor.io.*;
import com.globalmentor.net.MediaType;
import com.globalmentor.net.URIs;
import io.guise.framework.component.transfer.*;
import io.guise.framework.model.*;
/**
* An abstract implementation of an image component. This component installs a default export strategy supporting export of the following content types:
*
* text/uri-list
* - The label content type.
*
* @author Garret Wilson
*/
public abstract class AbstractImageComponent extends AbstractComponent implements ImageComponent {
/** The default export strategy for this component type. */
protected static final ExportStrategy DEFAULT_EXPORT_STRATEGY = new ExportStrategy() {
@Override
public Transferable exportTransfer(final ImageComponent component) {
return new DefaultTransferable(component); //return a default transferable for this component
}
};
/** The image model used by this component. */
private final ImageModel imageModel;
/** @return The image model used by this component. */
protected ImageModel getImageModel() {
return imageModel;
}
@Override
public URI getImageURI() {
return getImageModel().getImageURI();
}
@Override
public void setImageURI(final URI newImageURI) {
getImageModel().setImageURI(newImageURI);
}
/**
* Info model and image model constructor.
* @param infoModel The component info model.
* @param imageModel The component image model.
* @throws NullPointerException if the given info model and/or iamge model is null
.
*/
public AbstractImageComponent(final InfoModel infoModel, final ImageModel imageModel) {
super(infoModel); //construct the parent class
this.imageModel = requireNonNull(imageModel, "Image model cannot be null."); //save the image model
if(imageModel != infoModel) { //if the models are different (we'll already be listening to the info model)
this.imageModel.addPropertyChangeListener(getRepeatPropertyChangeListener()); //listen and repeat all property changes of the image model
this.imageModel.addVetoableChangeListener(getRepeatVetoableChangeListener()); //listen and repeat all vetoable changes of the image model
}
}
/**
* The default transferable object for an image.
* @author Garret Wilson
*/
protected static class DefaultTransferable extends AbstractTransferable {
/**
* Source constructor.
* @param source The source of the transferable data.
* @throws NullPointerException if the provided source is null
.
*/
public DefaultTransferable(final ImageComponent source) {
super(source); //construct the parent class
}
/**
* {@inheritDoc}
*
* This implementation returns a URI-list content type and the content type of the label.
*
*/
@Override
public MediaType[] getContentTypes() {
return new MediaType[] {URI_LIST_MEDIA_TYPE, getSource().getLabelContentType()};
}
@Override
public Object transfer(final MediaType contentType) {
final ImageComponent image = getSource(); //get the image
if(contentType.hasBaseType(URI_LIST_MEDIA_TYPE)) { //if this is a text/uri-list type
final URI imageURI = image.getImageURI(); //get the image URI
return imageURI != null ? createURIList(image.getSession().resolveURI(imageURI)) : null; //return the image URI, if there is one
} else if(contentType.hasBaseType(image.getLabelContentType())) { //if the label has the content type requested
final String label = image.getLabel(); //get the image label, if any
return label != null ? image.getSession().dereferenceString(image.getLabel()) : null; //return the resolved label text, if any
} else { //if we don't support this content type
throw new IllegalArgumentException("Content type not supported: " + contentType);
}
}
}
}