| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
THIS PAGE IS A WORK IN PROGRESS. THE TUTORIAL IS NOT YET COMPLETE.
This tutorial will go over creating a simple GuestBook? Tile for Brix.
For the purposes of this tutorial I will be writing code directly inside brix-demo project because it makes it trivial to try out the tile in action. You may do the same or create a seperate project.
GuestBook? will store its entries inside the JCR. It is helpful to decide on how the data will be stored, for the purposes of this tutorial we will go with a very simple format:
GuestBook Tile Node
Entry
Message
Author
Timestamp
Entry
Message
Author
Timestamp
First step of creating a tile in Brix is creating a class that implements the brix.plugin.site.page.tile.Tile interface. Below is first cut at such a class:
public class GuestBookTile implements Tile
{
public String getDisplayName()
{
return "Guest Book"; // #1
}
public String getTypeName()
{
return "brix.tile.GuestBook"; // #2
}
public TileEditorPanel newEditor(String id, IModel<BrixNode> tileContainerNode)
{
return new EmptyTileEditorPanel(id); // #3
}
public Component newViewer(String id, IModel<BrixNode> tileNode)
{
return new EmptyPanel(id); // #4
}public class DemoBrix extends Brix
{
public DemoBrix(BrixConfig config)
{
super(config);
config.getRegistry().register(Tile.POINT, new GuestBookTile()); // #1
...
}
}As with most Wicket components we start by creating a Wicket panel:
public class GuestBookPanel extends Panel
{
public GuestBookPanel(String id, IModel<BrixNode> model)
{
super(id, model);
}
}GuestBookPanel.html
<wicket:panel>I am a guestbook tile</wicket:panel>The next step is to tie the panel to the tile:
public class GuestBookTile implements Tile
{
...
public Component newViewer(String id, IModel<BrixNode> tileNode)
{
return new GuestBookPanel(id, tileNode); // #1
}
}First, lets create a simple bean that will represent an entry in the guest book:
public class Entry implements Serializable
{
public String name;
public String message;
public Date timestamp;
}Now, lets create a Wicket form that will be used to submit data:
private class MessageForm extends Form
{
private Entry entry = new Entry(); // #1
public MessageForm(String id)
{
super(id);
add(new TextField("name", new PropertyModel(this, "entry.name")));
add(new TextArea("message", new PropertyModel(this, "entry.message")));
}
@Override
protected void onSubmit()
{
onMessage(entry); // #2
entry = new Entry(); // #3
}
}It is important to note that MessageForm is an inner class of the GuestBookPanel we created earlier.
public GuestBookPanel(String id, IModel<BrixNode> model)
{
super(id, model);
add(new MessageForm("form"));
}<form wicket:id="form">
author: <input wicket:id="name"/><br/>
message: <textarea wicket:id="message" cols="30" rows="3"></textarea><br/>
<input type="submit"/>
</form>Now that we have the form working we need to persist submitted Entry into tile's JCR node:
public class GuestBookPanel extends Panel {
...
protected void onMessage(Entry message)
{
JcrNode tile = (JcrNode)getDefaultModelObject(); // #1
JcrNode entry = tile.addNode("entry"); // #2
entry.setProperty("name", message.name);
entry.setProperty("message", message.message);
entry.setProperty("timestamp", System.currentTimeMillis());
tile.getSession().save(); // #3
}
}A guest book is pretty useless if noone can see the entries made by others. The first thing we need to do is create a Wicket model that will produce a list of Entry objects. The model below is an inner class of the GuestBookPanel just like the MessageForm above.
private class EntriesModel extends LoadableDetachableModel<List<Entry>>
{
@Override
protected List<Entry> load()
{
JcrNode tile = (JcrNode)getDefaultModelObject(); // #1
JcrNodeIterator entryNodes = tile.getNodes("entry"); // #2
ArrayList<Entry> entries = new ArrayList<Entry>((int)entryNodes.getSize());
while (entryNodes.hasNext())
{
JcrNode entryNode = entryNodes.nextNode(); // #3
Entry entry = new Entry();
entry.name = entryNode.getProperty("name").getString();
entry.message = entryNode.getProperty("message").getString();
entry.timestamp = new Date(entryNode.getProperty("timestamp").getLong());
entries.add(entry);
}
return entries;
}
}public class GuestBookPanel extends Panel
{
public GuestBookPanel(String id, IModel<BrixNode> model)
{
super(id, model);
add(new MessageForm("form"));
add(new PropertyListView<Entry>("entries", new EntriesModel())
{
@Override
protected void populateItem(ListItem<Entry> item)
{
item.add(new Label("name"));
item.add(new Label("message"));
item.add(new Label("timestamp"));
}
});
}
}<table>
<tr wicket:id="entries">
<td><span wicket:id="timestamp"></span></td>
<td><span wicket:id="name"></span></td>
<td><span wicket:id="message"></span></td>
</tr>
</table>
```# | Back | FazBrowse Home | New Git URL |