/*******************************************************************************
* Copyright 2009, 2010 Innovation Gate GmbH. All Rights Reserved.
*
* This file is part of the OpenWGA server platform.
*
* OpenWGA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, a special exception is granted by the copyright holders
* of OpenWGA called "OpenWGA plugin exception". You should have received
* a copy of this exception along with OpenWGA in file COPYING.
* If not, see <http://www.openwga.com/gpl-plugin-exception>.
*
* OpenWGA 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenWGA in file COPYING.
* If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package de.innovationgate.wgpublisher.webtml;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import de.innovationgate.utils.WGUtils;
import de.innovationgate.webgate.api.WGAPIException;
import de.innovationgate.webgate.api.WGContent;
import de.innovationgate.wgpublisher.WGACore;
import de.innovationgate.wgpublisher.webtml.utils.RootTagReceptor;
public class Image extends Base {
public static final String OPTION_EDITOR_FIELD = "$EDITORFIELD";
private String name;
private String src;
private String item;
private String label;
private String db;
private String cssclass;
private String cssstyle;
/**
* Gets the name
* @return Returns a String
*/
public String getName() {
return this.getTagAttributeValue("name", name, null);
}
public String getDoc() {
return this.getName();
}
/**
* Sets the name
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}
public void setDoc(String name) {
this.setName(name);
}
/**
* Gets the src
* @return Returns a String
*/
public String getSrc() {
return this.getTagAttributeValue("src", src, null);
}
public String getFile() {
return this.getSrc();
}
/**
* Sets the src
* @param src The src to set
*/
public void setSrc(String src) {
this.src = src;
}
public void setFile(String src) {
this.setSrc(src);
}
public void tmlEndTag() throws WGAPIException {
try {
// Retrieve info from doc/file attributes or from item attribute
String doc = null;
String file = null;
String altAttribbute = null;
String imgAlign = "";
String borderAttribute = null;
String borderColorAttribute = null;
StringBuffer borderAttributesHTML = new StringBuffer("");
String item = this.getItem();
if (item != null) {
List itemValues = this.getTMLContext().itemlist(item);
if (itemValues.size() == 0 || String.valueOf(itemValues.get(0)).trim().equals("")) {
//addWarning("Image item '" + item + "' is empty", false);
}
else {
file = itemValues.get(0).toString();
}
if( itemValues.size() >= 2 && itemValues.get(1) != null && !itemValues.get(1).equals("") ) {
doc = itemValues.get(0).toString();
file = itemValues.get(1).toString();
}
if (itemValues.size() >= 3) {
altAttribbute = itemValues.get(2).toString();
}
if( itemValues.size() > 4 ){
//Is set by new dialog
altAttribbute = itemValues.get(2).toString();
borderAttribute = itemValues.get(3).toString();
borderColorAttribute = itemValues.get(4).toString();
if( borderAttribute!=null && !borderAttribute.equals("") ){
borderAttributesHTML.append(" border=\"");
borderAttributesHTML.append(borderAttribute);
borderAttributesHTML.append("\" ");
}
if( borderColorAttribute!=null && !borderColorAttribute.equals("") ){
borderAttributesHTML.append(" style=\"border-color:");
borderAttributesHTML.append(borderColorAttribute);
borderAttributesHTML.append("\" ");
}
}
if( itemValues.size() > 5 && !itemValues.get(5).toString().trim().equals("")){
imgAlign = " align=\"" + itemValues.get(5).toString() + "\" ";
}
}
else {
doc = this.getName();
file = this.getSrc();
}
String titleAttributeString="";
if( altAttribbute!=null && !altAttribbute.equals("") ){
titleAttributeString=" alt=\"" + altAttribbute + "\" title=\"" + altAttribbute + "\" ";
}
// Cleanup file and doc
if (file != null && file.equals("")) {
file = null;
}
if (doc != null && doc.equals("")) {
doc = null;
}
if(doc == null && file == null && item == null) {
this.addWarning("No image information given", false);
}
StringBuffer css = new StringBuffer();
String cssClass = this.getCssclass();
if (cssClass != null) {
css.append("class=\"" + cssClass + "\" ");
}
String cssStyle = this.getCssstyle();
if (cssStyle != null) {
css.append("style=\"" + cssStyle + "\" ");
}
// Build image html
String imageHTML = null;
if (file != null) {
WGContent content = this.getTMLContext().content();
imageHTML = "<img " + imgAlign + borderAttributesHTML.toString() + titleAttributeString + " src=\"" + getTMLContext().fileurl(getDb(), doc, file) + "\" " + css.toString() + this.getResultString() + ">";
}
// If in edit mode, show editing link
Object attribEdit = this.getPageContext().getRequest().getAttribute( WGACore.ATTRIB_EDITDOCUMENT );
if( attribEdit != null && attribEdit.equals(this.getTMLContext().getcontent().getContentKey().toString()) && item != null){
String theLabel = getLabel();
StringBuffer prefix = createItemEditorDeclaration(item, "image", theLabel);
prefix.append("<span class=\"WGA-Item-Value\" id=\"item_"+ item +"\">");
setPrefix(prefix.toString());
setSuffix("</span>\n</span>");
if (imageHTML == null) {
imageHTML = "";
}
}
this.setResult(imageHTML);
}
catch (UnsupportedEncodingException e) {
addWarning("Cannot create image link because of unsupported encoding: " + e.getMessage(), true);
}
}
/**
* Returns the item.
* @return String
*/
public String getItem() {
return this.getTagAttributeValue("item", item, null);
}
/**
* Sets the item.
* @param item The item to set
*/
public void setItem(String item) {
this.item = item;
}
/**
* Returns the title.
* @return String
*/
public String getLabel() {
return this.getTagAttributeValue("label", label, "Insert image here");
}
/**
* Sets the title.
* @param title The title to set
*/
public void setLabel(String title) {
this.label = title;
}
/**
* @return Returns the db.
*/
public String getDb() {
return getTagAttributeValue("db", db, null);
}
/**
* @param db The db to set.
*/
public void setDb(String db) {
this.db = db;
}
public String getCssclass() {
return getTagAttributeValue("cssclass", cssclass, null);
}
public void setCssclass(String cssclass) {
this.cssclass = cssclass;
}
public String getCssstyle() {
return getTagAttributeValue("cssstyle", cssstyle, null);
}
public void setCssstyle(String cssstyle) {
this.cssstyle = cssstyle;
}
}