Permalink
Browse files

added a default implementation modeled after Chuck Norris

  • Loading branch information...
kohsuke
kohsuke committed Oct 9, 2010
1 parent 67b143d commit 38f42c12f97e23d1f238cd58b1644fafac3043ec
@@ -24,12 +24,14 @@
package hudson.plugins.persona;
import com.thoughtworks.xstream.converters.basic.AbstractBasicConverter;
+import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.model.AbstractBuild;
import hudson.model.Action;
import hudson.model.Hudson;
import hudson.model.ModelObject;
+import hudson.plugins.persona.simple.DefaultQuoteImpl;
/**
* @author Kohsuke Kawaguchi
@@ -63,20 +65,20 @@ public static Persona byId(String id) {
return null;
}
- public static final class ConverterImpl extends AbstractBasicConverter {
+ public static final class ConverterImpl extends AbstractSingleValueConverter {
@Override
- protected Persona fromString(String id) {
+ public Persona fromString(String id) {
return byId(id);
}
@Override
- protected String toString(Object obj) {
+ public String toString(Object obj) {
return ((Persona)obj).id;
}
@Override
public boolean canConvert(Class type) {
- return type==Persona.class;
+ return Persona.class.isAssignableFrom(type);
}
}
}
@@ -21,19 +21,29 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
-package hudson.plugins.persona;
+package hudson.plugins.persona.simple;
+import hudson.model.AbstractBuild;
import hudson.model.InvisibleAction;
+import hudson.plugins.persona.Quote;
/**
* Default implementation of quote that renders a simple non-localized text.
*
* @author Kohsuke Kawaguchi
*/
public final class DefaultQuoteImpl extends InvisibleAction implements Quote {
+ public final AbstractBuild<?,?> build;
+ public final SimplePersona persona;
public final String quote;
- public DefaultQuoteImpl(String quote) {
+ public DefaultQuoteImpl(AbstractBuild<?, ?> build, SimplePersona persona, String quote) {
+ this.build = build;
+ this.persona = persona;
this.quote = quote;
}
+
+ public Image getImage() {
+ return persona!=null ? persona.getImage(build) : null;
+ }
}
@@ -1,4 +1,4 @@
-package hudson.plugins.persona;
+package hudson.plugins.persona.simple;
/**
* @author Kohsuke Kawaguchi
@@ -0,0 +1,37 @@
+package hudson.plugins.persona.simple;
+
+import hudson.model.AbstractBuild;
+import hudson.model.Action;
+import hudson.plugins.persona.Persona;
+
+import java.util.List;
+import java.util.Random;
+
+/**
+ * @author Kohsuke Kawaguchi
+ */
+public abstract class SimplePersona extends Persona {
+ private final List<String> quotes;
+ private final Random random = new Random();
+
+ /**
+ * @param id
+ * Unique identifier of this persona.
+ * @param quotes
+ * Collection of quotes.
+ */
+ protected SimplePersona(String id, List<String> quotes) {
+ super(id);
+ this.quotes = quotes;
+ }
+
+ /**
+ * Determines the icon and the background to render.
+ */
+ public abstract Image getImage(AbstractBuild<?, ?> build);
+
+ @Override
+ public synchronized Action generateQuote(AbstractBuild<?, ?> build) {
+ return new DefaultQuoteImpl(build,this,quotes.get(random.nextInt(quotes.size())));
+ }
+}
@@ -0,0 +1,6 @@
+/**
+ * Simple partial implementation of the persona modeled
+ * after Chuck Norris. Each build gets a small icon plus a quote,
+ * and a custom background.
+ */
+package hudson.plugins.persona.simple;
@@ -21,17 +21,20 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
+<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt" xmlns:local="local">
- <img src="${rootURL}/plugin/chucknorris/images/icon.jpg" width="16" height="16" alt="${from.displayName} Icon"/><st:nbsp/><j:out value="${from.fact}"/>
- <script>
- Element.setStyle($('main-table'), {
- 'background-image': 'none'
- });
- Element.setStyle($('main-panel'), {
- 'background-image': 'url(${rootURL}/plugin/chucknorris/images/<j:out value="${from.style.toString().toLowerCase()}"/>.jpg)',
- 'background-repeat': 'no-repeat',
- 'background-position': 'bottom right',
- 'padding-bottom': '270px'
- });
- </script>
+ <j:set var="img" value="${it.image}"/>
+
+ <img src="${rootURL}/${img.smallIconUrl}" width="16" height="16" /> ${it.quote}
+ <script>
+ Element.setStyle($('main-table'), {
+ 'background-image': 'none'
+ });
+ Element.setStyle($('main-panel'), {
+ 'background-image': 'url(${rootURL}/${img.backgroundImageUrl}"/>.jpg)',
+ 'background-repeat': 'no-repeat',
+ 'background-position': 'bottom right',
+ 'padding-bottom': '270px'
+ });
+ </script>
</j:jelly>

0 comments on commit 38f42c1

Please sign in to comment.