001/* 002 * flattr4j - A Java library for Flattr 003 * 004 * Copyright (C) 2011 Richard "Shred" Körber 005 * http://flattr4j.shredzone.org 006 * 007 * This program is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License / GNU Lesser 009 * General Public License as published by the Free Software Foundation, 010 * either version 3 of the License, or (at your option) any later version. 011 * 012 * Licensed under the Apache License, Version 2.0 (the "License"); 013 * you may not use this file except in compliance with the License. 014 * 015 * This program is distributed in the hope that it will be useful, 016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 018 */ 019package org.shredzone.flattr4j.web.tag; 020 021import java.io.IOException; 022 023import javax.servlet.jsp.JspException; 024import javax.servlet.jsp.tagext.BodyTagSupport; 025 026import org.shredzone.flattr4j.model.AutoSubmission; 027import org.shredzone.flattr4j.model.Thing; 028import org.shredzone.flattr4j.web.BadgeType; 029import org.shredzone.flattr4j.web.builder.StaticButtonBuilder; 030 031/** 032 * A static button to a Flattr thing. 033 * 034 * @author Richard "Shred" Körber 035 */ 036public class StaticTag extends BodyTagSupport implements Attributed { 037 private static final long serialVersionUID = -7356980489242218537L; 038 039 private StaticButtonBuilder builder; 040 041 private String var = null; 042 private String scope = null; 043 044 public void setThing(Object thing) { 045 setupBuilder(); 046 if (thing instanceof Thing) { 047 builder.thing((Thing) thing); 048 } else if (thing instanceof AutoSubmission) { 049 builder.thing((AutoSubmission) thing); 050 } else { 051 builder.thing(thing.toString()); 052 } 053 } 054 055 public void setBadgeUrl(String url) { 056 setupBuilder(); 057 builder.badgeUrl(url); 058 } 059 060 public void setBadge(Object type) { 061 setupBuilder(); 062 if (type instanceof BadgeType) { 063 builder.badge((BadgeType) type); 064 } else { 065 builder.badge(BadgeType.valueOf(type.toString().toUpperCase())); 066 } 067 } 068 069 public void setStyle(String style) { 070 setupBuilder(); 071 builder.style(style); 072 } 073 074 public void setStyleClass(String styleClass) { 075 setupBuilder(); 076 builder.styleClass(styleClass); 077 } 078 079 public void setVar(String var) { 080 this.var = var; 081 } 082 083 public void setScope(String scope) { 084 this.scope = scope; 085 } 086 087 @Override 088 public void setAttribute(String name, String value) { 089 setupBuilder(); 090 builder.attribute(name, value); 091 } 092 093 @Override 094 public int doStartTag() throws JspException { 095 setupBuilder(); 096 return EVAL_BODY_INCLUDE; 097 } 098 099 @Override 100 public int doEndTag() throws JspException { 101 String tag = builder.toString(); 102 103 if (var != null) { 104 TagUtils.setScopedAttribute(pageContext, var, tag, scope); 105 106 } else { 107 try { 108 pageContext.getOut().print(tag); 109 } catch (IOException ex) { 110 throw new JspException(ex); 111 } 112 } 113 114 disposeBuilder(); 115 return EVAL_PAGE; 116 } 117 118 /** 119 * Creates a new builder instance, if not already done. 120 */ 121 protected void setupBuilder() { 122 if (builder == null) { 123 builder = new StaticButtonBuilder(); 124 } 125 } 126 127 /** 128 * Disposes the builder instance. 129 */ 130 protected void disposeBuilder() { 131 builder = null; 132 } 133 134}