The easy Weblog and Wiki Software
[ start | index | login ]
start > List Formatter Tutorial 2

List Formatter Tutorial 2

Created by funzel. Last edited by funzel, 5 years and 276 days ago. Viewed 1,389 times. #1
[edit] [rdf]
labels
attachments
This tutorial describes how to write a Macro that uses List Formatters. For an tutorial about list formatters see List Formatter Tutorial 1. List formatters give you the ability to let your users change the output of your macro. They can be reused by different macro authors, for example to output a AtoZ style with your macro, reuse the AtoZ style formatter.

Introduction

The easiest way to use list formatters with your macros is to inherit from org.snipsnap.snip.filter.macro.ListOutputMacro. Instead of writing to the writer for outputing your list of "things" (Snips, Users, etc.) you use the output method of ListOutputMacro:

public void output(Writer writer, String listComment, 
                     Collection c, String emptyText, String style, 
                     boolean showSize) throws IOException;
which takes a java.io.Writer object, a comment for the list, the collection with things to ouput, an empty text to display when the collection is empty and whether the formatter should output the size of the collection.

Example

An example for a Macro using a List Formatter is the index macro:

public void execute(Writer writer, MacroParameter params)
      throws IllegalArgumentException,IOException {
    String type = null;
    boolean showSize = true;
    if(params != null) {
      if(params.getLength() > 0) {
        type = params.get("0");
      }
    }

if (params == null || params.getLength() <= 2) { output(writer, "All Snips:", SnipSpace.getInstance().getAll(), "none written yet.", type, showSize); } else { throw new IllegalArgumentException("Number of arguments does not match"); } }

This reads the style of the formatter from the parameters (type), e.g. {index:AtoZ} will put "AtoZ" into type. Then the output method is called with a collection of all snips in the system.

Filtering output

To improve this, the index macro uses a filter to filter all snips out that start with "comment-". To do this use Collections.filter:

Collections.filter(SnipSpace.getInstance().getAll(),
   new Filterator() {
     public boolean filter(Object obj) {
       String name = ((Snip) obj).getName();
       if (name.startsWith("comment-")) {
         return true;
       }
       return false;
     }
}

Sorting output

The same idea could be used to sort the snips before output. To do this, use java.util.Collections with the sort method.

no comments | post comment

What is SnipSnap?
SnipSnap is a free and easy to install weblog and wiki tool written in Java.

SnipSnap download
Current version: 1.0b3-uttoxeter
Try our >>Web Start Demo!

Resources

5567 Users and 13713 Snips. Installed 6 years and 42 days ago

Logged in Users: (1)
… and 18 Guests.

snipsnap-changed for older changes.

< August 2008 >
SunMonTueWedThuFriSat
12
3456789
10111213141516
17181920212223
24252627282930
31

snipsnap
Listed on BlogShares
XHTML 1.0 validated
CSS validated
RSS 2.0 validated
RSS Feed

pico-powered

Powered by SnipSnap 1.0b3-uttoxeter
YourKit >>Java Profiler

Fraunhofer FIRST

snipsnap.org | Copyright 2000-2006 Fraunhofer FIRST