org.glassfish.admingui.common.help.HelpTreeIndexAdaptor Maven / Gradle / Ivy
Show all versions of payara-micro Show documentation
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2011 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.admingui.common.help;
import com.sun.jsftemplating.component.factory.tree.TreeAdaptor;
import com.sun.jsftemplating.component.factory.tree.TreeAdaptorBase;
import com.sun.jsftemplating.layout.descriptors.LayoutComponent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.Serializable;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.glassfish.admingui.connector.Index;
import org.glassfish.admingui.connector.IndexItem;
/**
* The HelpTreeAdaptor
implementation must have a
* public static HelpTreeAdaptor getInstance(FacesContext,
* LayoutComponent, UIComponent)
method in order to get access to
* an instance of the HelpTreeAdaptor
instance.
*
* This class is used by DynamicTreeNodeFactory
.
*
* @author Ken Paulsen ([email protected])
*/
public class HelpTreeIndexAdaptor extends TreeAdaptorBase {
/**
* This constructor is not used.
*/
private HelpTreeIndexAdaptor() {
}
/**
* This constructor saves the LayoutComponent
descriptor
* and the UIComponent
associated with this
* TreeAdaptor
. This constructor is used by the
* getInstance() method.
*/
protected HelpTreeIndexAdaptor(LayoutComponent desc, UIComponent parent) {
super(desc, parent);
}
/**
* This method provides access to an HelpTreeAdaptor
* instance. Each time it is invoked, it returns a new instance.
*/
public static TreeAdaptor getInstance(FacesContext ctx, LayoutComponent desc, UIComponent parent) {
return new HelpTreeIndexAdaptor(desc, parent);
}
/**
* This method is called shortly after
* {@link #getInstance(FacesContext, LayoutComponent, UIComponent)}.
* It provides a place for post-creation initialization to take
* occur.
*/
public void init() {
// Get the FacesContext
FacesContext ctx = FacesContext.getCurrentInstance();
// This is the descriptor for this dynamic TreeNode, it contains all
// information (options) necessary for this Adaptor
LayoutComponent desc = getLayoutComponent();
// The parent UIComponent
UIComponent parent = getParentUIComponent();
// Get the Index
Index index = (Index) desc.getEvaluatedOption(ctx, "index", parent);
// The following method should set the "key" to the node containing all
// the children... the children will also have keys which must be
// retrievable by the next method (getChildTreeNodeObjects)... these
// "keys" will be used by the rest of the methods in this file for
// getting information about the TreeNode that should be built.
setTreeNodeObject(index);
}
/**
* Returns child IndexItem
s for the given
* IndexItem
or Index
model Object. If
* null
is supplied, null
is returned
* any other Object
type will result in an
* IllegalArgumentException
.
*/
public List getChildTreeNodeObjects(Object nodeObject) {
if (nodeObject == null) {
return null;
}
List result = null;
if (nodeObject instanceof IndexItem) {
result = new ArrayList(((IndexItem) nodeObject).getIndexItems());
}
if (nodeObject instanceof Index) {
result = new ArrayList(((Index) nodeObject).getIndexItems());
}
if (null != result) {
Collections.sort(result, new HelpTreeIndexAdaptor.IndexItemComparator());
return result;
}
throw new IllegalArgumentException("Invalid node type for Index: "
+ nodeObject.getClass().getName());
}
/**
* This method returns the "options" that should be supplied to the
* factory that creates the TreeNode
for the given tree
* node model object.
*
* Some useful options for the standard TreeNode
* component include:
*
*
- text
* - url
* - imageURL
* - target
* - action
-
*
- actionListener
* - expanded
*
* See Tree / TreeNode component documentation for more details.
*/
public Map getFactoryOptions(Object nodeObject) {
if (nodeObject == null) {
return null;
}
Map props = new HashMap();
if (nodeObject instanceof Index) {
// This case deals with the top node.
// Do (almost) nothing so that the root node does not show up...
props.put("clientSide", true);
Object value = getLayoutComponent().getOption("style");
if (value != null) {
props.put("style", value);
}
return props;
} else if (!(nodeObject instanceof IndexItem)) {
throw new IllegalArgumentException("Invalid node type for Index: "
+ nodeObject.getClass().getName());
}
IndexItem item = (IndexItem) nodeObject;
String htmlFile = null;
// Setup the properties...
// NOTE: All supported options must be handled here,
// otherwise they'll be ignored.
// NOTE: Options will be evaluated later, do not eval here.
props.put("text", item.getText());
if (null != (htmlFile = item.getHtmlFileForTarget())) {
// Add leading "/resource/" to ensure it's treated as *context root* relative.
props.put("url", "/resource/" + htmlFile);
}
// LayoutComponent desc = getLayoutComponent();
// setProperty(props, "imageURL", desc.getOption("imageURL"));
// setProperty(props, "target", desc.getOption("target"));
// setProperty(props, "action", desc.getOption("action"));
// NOTE: Although actionListener is supported, LH currently
// implements this to be the ActionListener of the "turner"
// which is inconsistent with "action". We should make use
// of the "Handler" feature which provides a "toggle"
// CommandEvent.
// setProperty(props, "rendered", desc.getOption("rendered"));
// setProperty(props, "actionListener", desc.getOption("actionListener"));
// Use "childActionListener" for the hyperlink, not the TreeNode??
// setProperty(props, "actionListener", desc.getOption("childActionListener"));
// Return the options
return props;
}
/**
* Helper method for setting Properties while avoiding NPE's.
private void setProperty(Map props, String key, Object value) {
if (value != null) {
props.put(key, value);
}
}
*/
/**
* This method returns the id
for the given tree node
* model object.
*/
public String getId(Object nodeObject) {
String id = "invalideNodeObjectType";
if (nodeObject == null) {
id = "nullNodeObject";
} else if (nodeObject instanceof IndexItem) {
String target = ((IndexItem) nodeObject).getTarget();
id = genId(null != target ? target : "i" + Integer.toString(nodeObject.hashCode()));
} else if (nodeObject instanceof Index) {
id = getLayoutComponent().getId(
FacesContext.getCurrentInstance(), getParentUIComponent());
}
return id;
}
/**
* This method generates an ID that is safe for JSF for the given
* String. It does not guarantee that the id is unique, it is the
* responsibility of the caller to pass in a String that will result
* in a UID. All non-ascii characters will be stripped.
*
* @param uid A non-null String.
*/
private String genId(String uid) {
char[] chArr = uid.toCharArray();
int len = chArr.length;
int newIdx = 0;
for (int idx = 0; idx < len; idx++) {
char test = chArr[idx];
if (Character.isLetterOrDigit(test) || test == '_' || test == '-') {
chArr[newIdx++] = test;
}
}
return new String(chArr, 0, newIdx);
}
/**
* This method returns any facets that should be applied to the
* TreeNode (comp)
. Useful facets for the sun
* TreeNode
component are: "content" and "image".
*
* Facets that already exist on comp
, or facets that
* are directly added to comp
do not need to be returned
* from this method.
*
* This implementation directly adds a "content" and "image" facet and
* returns null
from this method.
*
* @param comp The tree node UIComponent
.
* @param nodeObject The (model) object representing the tree node.
*/
public Map getFacets(UIComponent comp, Object nodeObject) {
// Do nothing
return null;
}
/**
* Advanced framework feature which provides better handling for
* things such as expanding TreeNodes, beforeEncode, and other
* events.
*
* This method should return a Map
of List
* of Handler
objects. Each List
in the
* Map
should be registered under a key that cooresponds
* to to the "event" in which the Handler
s should be
* invoked.
*/
public Map getHandlersByType(UIComponent comp, Object nodeObject) {
// Do nothing
return null;
}
/**
* This method returns the UIComponent
factory class
* implementation that should be used to create a
* TreeNode
for the given tree node model object.
*
* This implementation returns the default return value, unless the
* nodeObject
represents the root node. In that case it
* will return a the TreeFactory factory class:
*
* com.sun.jsftemplating.component.factory.sun.TreeFactory
*/
public String getFactoryClass(Object nodeObject) {
String factory = super.getFactoryClass(nodeObject);
if (nodeObject instanceof Index) {
// For the root... make it the tree itself.
factory =
"com.sun.jsftemplating.component.factory.sun.TreeFactory";
}
return factory;
}
/**
* Comparator class for {@link IndexItem}s. Uses "text" for
* comparison, ignoring case.
*/
private static class IndexItemComparator implements Comparator, Serializable {
@Override
public int compare(IndexItem x, IndexItem y) {
int result = 0;
if (null != x && null != y) {
if (!x.equals(y)) {
String xText = x.getText(), yText = y.getText();
if (null != xText && null != yText) {
result = xText.compareToIgnoreCase(yText);
}
}
} else {
if (null == x && null == y) {
result = 0;
} else {
// consider null to be less.
if (null == x) {
result = -1;
} else {
result = 1;
}
}
}
return result;
}
}
}