Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
A module that creates a .jar from the classes in wicket, wicket-util and wicket-request modules in order
to create a valid OSGi bundle of the wicket framework.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.wicket.markup.html.tree;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.IMarkupFragment;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.internal.HtmlHeaderContainer;
import org.apache.wicket.markup.html.list.AbstractItem;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IDetachable;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.IRequestHandler;
import org.apache.wicket.request.Response;
import org.apache.wicket.request.resource.JavaScriptResourceReference;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.util.string.AppendingStringBuffer;
import org.apache.wicket.util.visit.IVisit;
import org.apache.wicket.util.visit.IVisitor;
/**
* This class encapsulates the logic for displaying and (partial) updating the tree. Actual
* presentation is out of scope of this class. User should derive they own tree (if needed) from
* {@link BaseTree} (recommended).
*
* @author Matej Knopp
*/
public abstract class AbstractTree extends Panel
implements
ITreeStateListener,
TreeModelListener,
AjaxRequestTarget.ITargetRespondListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Interface for visiting individual tree items.
*/
private static interface IItemCallback
{
/**
* Visits the tree item.
*
* @param item
* the item to visit
*/
void visitItem(TreeItem item);
}
/**
* This class represents one row in rendered tree (TreeNode). Only TreeNodes that are visible
* (all their parent are expanded) have TreeItem created for them.
*/
private final class TreeItem extends AbstractItem
{
/**
* whether this tree item should also render it's children to response. this is set if we
* need the whole subtree rendered as one component in ajax response, so that we can replace
* it in one step (replacing individual rows is very slow in javascript, therefore we
* replace the whole subtree)
*/
private final static int FLAG_RENDER_CHILDREN = FLAG_RESERVED8;
private static final long serialVersionUID = 1L;
/**
* tree item children - we need this to traverse items in correct order when rendering
*/
private List children = null;
/** tree item level - how deep is this item in tree */
private final int level;
private final TreeItem parent;
/**
* Construct.
*
* @param id
* The component id
* @param node
* tree node
* @param level
* current level
* @param parent
*/
public TreeItem(TreeItem parent, String id, final Object node, int level)
{
super(id, new Model((Serializable)node));
this.parent = parent;
nodeToItemMap.put(node, this);
this.level = level;
setOutputMarkupId(true);
// if this isn't a root item in rootless mode
if (level != -1)
{
populateTreeItem(this, level);
}
}
public TreeItem getParentItem()
{
return parent;
}
/**
* @return The children
*/
public List getChildren()
{
return children;
}
/**
* @return The current level
*/
public int getLevel()
{
return level;
}
/**
* @see org.apache.wicket.Component#getMarkupId()
*/
@Override
public String getMarkupId()
{
// this is overridden to produce id that begins with id of tree
// if the tree has set (shorter) id in markup, we can use it to
// shorten the id of individual TreeItems
return AbstractTree.this.getMarkupId() + "_" + getId();
}
/**
* Sets the children.
*
* @param children
* The children
*/
public void setChildren(List children)
{
this.children = children;
}
/**
* Whether to render children.
*
* @return whether to render children
*/
protected final boolean isRenderChildren()
{
return getFlag(FLAG_RENDER_CHILDREN);
}
/**
* Whether the TreeItem has any child TreeItems
*
* @return true if there are one or more child TreeItems; false otherwise
*/
public boolean hasChildTreeItems()
{
return children != null && !children.isEmpty();
}
/**
* @see org.apache.wicket.MarkupContainer#onRender()
*/
@Override
protected void onRender()
{
// is this root and tree is in rootless mode?
if (this == rootItem && isRootLess() == true)
{
// yes, write empty div with id
// this is necessary for createElement js to work correctly
String tagName = ((ComponentTag)getMarkup().get(0)).getName();
Response response = getResponse();
response.write("<" + tagName + " style=\"display:none\" id=\"" + getMarkupId() +
"\">");
if ("table".equals(tagName))
{
response.write("
");
}
response.write("" + tagName + ">");
}
else
{
// render the item
super.onRender();
// should we also render children (ajax response)
if (isRenderChildren())
{
// visit every child
visitItemChildren(this, new IItemCallback()
{
public void visitItem(TreeItem item)
{
// render child
item.onRender();
// go through the behaviors and invoke IBehavior.afterRender
List extends Behavior> behaviors = item.getBehaviors();
for (Behavior behavior : behaviors)
{
behavior.afterRender(item);
}
}
});
}
}
}
/**
*
* @return model object
*/
public Object getModelObject()
{
return getDefaultModelObject();
}
@Override
public void renderHead(final HtmlHeaderContainer container)
{
super.renderHead(container);
if (isRenderChildren())
{
// visit every child
visitItemChildren(this, new IItemCallback()
{
public void visitItem(TreeItem item)
{
// write header contributions from the children of item
item.visitChildren(new IVisitor()
{
public void component(final Component component,
final IVisit visit)
{
if (component.isVisible())
{
component.renderHead(container);
}
else
{
visit.dontGoDeeper();
}
}
});
}
});
}
}
protected final void setRenderChildren(boolean value)
{
setFlag(FLAG_RENDER_CHILDREN, value);
}
@Override
protected void onDetach()
{
super.onDetach();
Object object = getModelObject();
if (object instanceof IDetachable)
{
((IDetachable)object).detach();
}
if (isRenderChildren())
{
// visit every child
visitItemChildren(this, new IItemCallback()
{
public void visitItem(TreeItem item)
{
item.detach();
}
});
}
// children are rendered, clear the flag
setRenderChildren(false);
}
@Override
protected void onBeforeRender()
{
onBeforeRenderInternal();
super.onBeforeRender();
if (isRenderChildren())
{
// visit every child
visitItemChildren(this, new IItemCallback()
{
public void visitItem(TreeItem item)
{
item.prepareForRender();
}
});
}
}
@Override
protected void onAfterRender()
{
super.onAfterRender();
if (isRenderChildren())
{
// visit every child
visitItemChildren(this, new IItemCallback()
{
public void visitItem(TreeItem item)
{
item.afterRender();
}
});
}
}
private boolean hasParentWithChildrenMarkedToRecreation()
{
return getParentItem() != null &&
(getParentItem().getChildren() == null || getParentItem().hasParentWithChildrenMarkedToRecreation());
}
}
/**
* Components that holds tree items. This is similar to ListView, but it renders tree items in
* the right order.
*/
private class TreeItemContainer extends WebMarkupContainer
{
private static final long serialVersionUID = 1L;
/**
* Construct.
*
* @param id
* The component id
*/
public TreeItemContainer(String id)
{
super(id);
}
/**
* @see org.apache.wicket.MarkupContainer#remove(org.apache.wicket.Component)
*/
@Override
public TreeItemContainer remove(Component component)
{
// when a treeItem is removed, remove reference to it from
// nodeToItemMAp
if (component instanceof TreeItem)
{
nodeToItemMap.remove(((TreeItem)component).getModelObject());
}
super.remove(component);
return this;
}
/**
* @see org.apache.wicket.MarkupContainer#onRender()
*/
@Override
protected void onRender()
{
// is there a root item? (non-empty tree)
if (rootItem != null)
{
IItemCallback callback = new IItemCallback()
{
public void visitItem(TreeItem item)
{
// render component
item.render();
}
};
// visit item and it's children
visitItemAndChildren(rootItem, callback);
}
}
@Override
public IMarkupFragment getMarkup(final Component child)
{
// The childs markup is always equal to the parents markup.
return getMarkup();
}
}
private boolean attached = false;
/** comma separated list of ids of elements to be deleted. */
private final AppendingStringBuffer deleteIds = new AppendingStringBuffer();
/**
* whether the whole tree is dirty (so the whole tree needs to be refreshed).
*/
private boolean dirtyAll = false;
/**
* list of dirty items. if children property of these items is null, the children will be
* rebuild.
*/
private final Set dirtyItems = new HashSet();
/**
* list of dirty items which need the DOM structure to be created for them (added items)
*/
private final Set dirtyItemsCreateDOM = new HashSet();
/** counter for generating unique ids of every tree item. */
private int idCounter = 0;
/** Component whose children are tree items. */
private TreeItemContainer itemContainer;
/**
* map that maps TreeNode to TreeItem. TreeItems only exists for TreeNodes, that are visible
* (their parents are not collapsed).
*/
// TODO this field is not serializable but nested inside an serializable component
private final Map