Custom Gadgets

You are viewing an old version (v. 2) of this page.
The latest version is v. 6, last edited on Oct 14, 2011 (view differences | )
<< View previous version | view page history | view next version >>

We will further enhance the plugin in tutorial [create your first plugin] to add a custom gadget to display a specified message. Firstable, we create below class:

package com.pmease.quickbuild.plugin.basis;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.basic.Label;
import org.hibernate.validator.constraints.NotEmpty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pmease.quickbuild.Context;
import com.pmease.quickbuild.ScriptEngine;
import com.pmease.quickbuild.annotation.Editable;
import com.pmease.quickbuild.annotation.Multiline;
import com.pmease.quickbuild.annotation.Scriptable;
import com.pmease.quickbuild.extensionpoint.support.Gadget;
import com.pmease.quickbuild.extensionpoint.support.GadgetCategory;
import com.pmease.quickbuild.util.ExceptionUtils;

@Editable(name="Html Message", order=100000, category=GadgetCategory.OTHERS)
public class MessageGadget extends Gadget {

	private static final long serialVersionUID = 1L;
	
	private static final Logger logger = LoggerFactory.getLogger(MessageGadget.class);

	private String message;

	@Editable(name="Message", description="Specify a message to display. Html tags can be used " +
			"to format the message.")
	@NotEmpty
	@Multiline
	@Scriptable
	public String getMessage() {
		return message;
	}

	@Override
	public String getCssClass() {
		return "window";
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public Component renderBody(String componentId) {
		String interpolated;
		try {
			interpolated = ScriptEngine.instance.interpolate(getMessage(), 
					Context.buildEvalContext(null, null));
			return new Label(componentId, interpolated).setEscapeModelStrings(false)
					.add(new AttributeAppender("class", "p8p"));
		} catch (Exception e) {
			logger.error("Error interpolating message.", e);
			interpolated = StringEscapeUtils.escapeHtml(ExceptionUtils.extractImportantMessage(e));
			return new Label(componentId, "<div class='red'>" + interpolated + "</div>")
					.setEscapeModelStrings(false).add(new AttributeAppender("class", "p8p"));
		}
	}

}

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.