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; 022import java.util.Collection; 023 024import javax.servlet.jsp.JspException; 025import javax.servlet.jsp.tagext.BodyContent; 026import javax.servlet.jsp.tagext.BodyTagSupport; 027 028import org.shredzone.flattr4j.model.Category; 029import org.shredzone.flattr4j.model.CategoryId; 030import org.shredzone.flattr4j.model.Language; 031import org.shredzone.flattr4j.model.LanguageId; 032import org.shredzone.flattr4j.model.User; 033import org.shredzone.flattr4j.model.UserId; 034import org.shredzone.flattr4j.web.ButtonType; 035import org.shredzone.flattr4j.web.builder.ButtonBuilder; 036 037/** 038 * A button to a Flattr thing. 039 * 040 * @author Richard "Shred" Körber 041 */ 042public class ButtonTag extends BodyTagSupport implements Attributed { 043 private static final long serialVersionUID = -2011193251581466746L; 044 045 private ButtonBuilder builder; 046 047 private boolean descripted = false; 048 private String var = null; 049 private String scope = null; 050 051 public void setUrl(String url) { 052 setupBuilder(); 053 builder.url(url); 054 } 055 056 public void setUser(Object user) { 057 setupBuilder(); 058 if (user instanceof UserId) { 059 builder.user((UserId) user); 060 } else { 061 builder.user(User.withId(user.toString())); 062 } 063 } 064 065 public void setTitle(String title) { 066 setupBuilder(); 067 builder.title(title); 068 } 069 070 public void setDescription(String description) { 071 setupBuilder(); 072 builder.description(description); 073 descripted = true; 074 } 075 076 public void setCategory(Object category) { 077 setupBuilder(); 078 if (category instanceof CategoryId) { 079 builder.category((CategoryId) category); 080 } else { 081 builder.category(Category.withId(category.toString())); 082 } 083 } 084 085 public void setLanguage(Object language) { 086 setupBuilder(); 087 if (language instanceof LanguageId) { 088 builder.language((LanguageId) language); 089 } else { 090 builder.language(Language.withId(language.toString())); 091 } 092 } 093 094 public void setRevsharekey(String key) { 095 setupBuilder(); 096 builder.revsharekey(key); 097 } 098 099 public void setButton(Object type) { 100 setupBuilder(); 101 if (type instanceof ButtonType) { 102 builder.button((ButtonType) type); 103 } else { 104 builder.button(ButtonType.valueOf(type.toString().toUpperCase())); 105 } 106 } 107 108 public void setPopout(boolean popout) { 109 setupBuilder(); 110 builder.popout(popout); 111 } 112 113 public void setHidden(boolean hidden) { 114 setupBuilder(); 115 if (hidden) builder.hidden(); 116 } 117 118 public void setHtml5(boolean html5) { 119 setupBuilder(); 120 if (html5) builder.html5(); 121 } 122 123 public void setPrefix(String prefix) { 124 setupBuilder(); 125 builder.prefix(prefix); 126 } 127 128 public void setStyle(String style) { 129 setupBuilder(); 130 builder.style(style); 131 } 132 133 public void setStyleClass(String styleClass) { 134 setupBuilder(); 135 builder.styleClass(styleClass); 136 } 137 138 public void setVar(String var) { 139 this.var = var; 140 } 141 142 public void setScope(String scope) { 143 this.scope = scope; 144 } 145 146 public void addTag(String tag) { 147 setupBuilder(); 148 builder.tag(tag); 149 } 150 151 public void addTags(Collection<String> tags) { 152 setupBuilder(); 153 builder.tags(tags); 154 } 155 156 @Override 157 public void setAttribute(String name, String value) { 158 setupBuilder(); 159 builder.attribute(name, value); 160 } 161 162 @Override 163 public int doStartTag() throws JspException { 164 setupBuilder(); 165 166 if (descripted) { 167 return SKIP_BODY; 168 } else { 169 return EVAL_BODY_BUFFERED; 170 } 171 } 172 173 @Override 174 public int doEndTag() throws JspException { 175 if (!descripted) { 176 BodyContent bc = getBodyContent(); 177 if (bc != null) { 178 String description = bc.getString().trim(); 179 if (!description.isEmpty()) { 180 builder.descriptionTruncate(description); 181 } 182 } 183 } 184 185 String tag = builder.toString(); 186 187 if (var != null) { 188 TagUtils.setScopedAttribute(pageContext, var, tag, scope); 189 190 } else { 191 try { 192 pageContext.getOut().print(tag); 193 } catch (IOException ex) { 194 throw new JspException(ex); 195 } 196 } 197 198 disposeBuilder(); 199 return EVAL_PAGE; 200 } 201 202 /** 203 * Creates a new builder instance, if not already done. 204 */ 205 protected void setupBuilder() { 206 if (builder == null) { 207 builder = new ButtonBuilder(); 208 } 209 } 210 211 /** 212 * Disposes the builder instance. 213 */ 214 protected void disposeBuilder() { 215 builder = null; 216 } 217 218}