Write Custom Gadget

Version 2 by Robin Shen
on Oct 14, 2011 02:41.


compared with
Version 3 by Robin Shen
on Oct 14, 2011 02:46.


Key
These lines were removed. This word was removed.
These lines were added. This word was added.

View page history


There are 13 changes. View first change.

 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:
  We will further enhance the plugin in tutorial [write your first plugin] to add a custom gadget to display a specified message. Firstable, we create below class:
 {code}
package com.pmease.quickbuild.plugin.basis;
  package com.example.myplugin;
  
 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 {
  @Editable(name="My Message", order=100001, category=GadgetCategory.OTHERS)
 public class MyMessageGadget 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.")
   @Editable(name="Message", description="Specify a message to display.")
  @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"));
  }
   return new Label(componentId, getMessage()).add(new AttributeAppender("class", "p8p"));
  }
  
 }
 
 {code}
  
 Then modify class _MyPlugin_ to contribute the message gadget: