001: /*
002: * $Id$ $Revision$ $Date$
003: *
004: * ==============================================================================
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package wicket.extensions.markup.html.tree;
018:
019: import javax.swing.tree.TreeModel;
020: import javax.swing.tree.TreeNode;
021:
022: import wicket.Component;
023: import wicket.MarkupContainer;
024: import wicket.behavior.AbstractBehavior;
025: import wicket.markup.ComponentTag;
026: import wicket.markup.html.WebMarkupContainer;
027: import wicket.markup.html.basic.Label;
028: import wicket.model.AbstractReadOnlyModel;
029: import wicket.model.IModel;
030:
031: /**
032: * A complete tree implementation where three item consists of junction link,
033: * icon and label.
034: *
035: * @author Matej Knopp
036: */
037: public class Tree extends DefaultAbstractTree {
038: private static final long serialVersionUID = 1L;
039:
040: /**
041: * Tree constructor.
042: *
043: * @param id
044: * The component id
045: */
046: public Tree(String id) {
047: super (id);
048: }
049:
050: /**
051: * Tree constructor.
052: *
053: * @param id
054: * The component id
055: * @param model
056: * The tree model
057: */
058: public Tree(String id, IModel model) {
059: super (id, model);
060: }
061:
062: /**
063: * Tree constructor.
064: *
065: * @param id
066: * The component id
067: * @param model
068: * The tree model
069: */
070: public Tree(String id, TreeModel model) {
071: super (id, model);
072: }
073:
074: /**
075: * Populates the tree item. It creates all necesary components for the tree
076: * to work properly.
077: *
078: * @param item
079: * @param level
080: */
081: protected void populateTreeItem(WebMarkupContainer item, int level) {
082: final TreeNode node = (TreeNode) item.getModelObject();
083:
084: item.add(newIndentation(item, "indent", (TreeNode) item
085: .getModelObject(), level));
086:
087: item.add(newJunctionLink(item, "link", "image", node));
088:
089: MarkupContainer nodeLink = newNodeLink(item, "nodeLink", node);
090: item.add(nodeLink);
091:
092: nodeLink.add(newNodeIcon(nodeLink, "icon", node));
093:
094: nodeLink.add(new Label("label", new AbstractReadOnlyModel() {
095: private static final long serialVersionUID = 1L;
096:
097: public Object getObject(Component c) {
098: return renderNode(node);
099: }
100: }));
101:
102: // do distinguish between selected and unselected rows we add an
103: // behavior
104: // that modifies row css class.
105: item.add(new AbstractBehavior() {
106: private static final long serialVersionUID = 1L;
107:
108: /**
109: * @see wicket.behavior.AbstractBehavior#onComponentTag(wicket.Component,
110: * wicket.markup.ComponentTag)
111: */
112: public void onComponentTag(Component component,
113: ComponentTag tag) {
114: super .onComponentTag(component, tag);
115: if (getTreeState().isNodeSelected(node)) {
116: tag.put("class", "row-selected");
117: } else {
118: tag.put("class", "row");
119: }
120: }
121: });
122: }
123:
124: /**
125: * This method is called for every node to get it's string representation.
126: *
127: * @param node
128: * The tree node to get the string representation of
129: * @return The string representation
130: */
131: protected String renderNode(TreeNode node) {
132: return node.toString();
133: }
134: }
|