alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Glassfish example source code file (HelpTreeIndexAdaptor.java)

This example Glassfish source code file (HelpTreeIndexAdaptor.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Glassfish tags/keywords

arraylist, helptreeindexadaptor, index, index, indexitem, indexitem, layoutcomponent, list, map, object, object, string, string, uicomponent, util

The Glassfish HelpTreeIndexAdaptor.java source code

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2010 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 javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.glassfish.admingui.plugin.Index;
import org.glassfish.admingui.plugin.IndexItem;


/**
 *  <p> The HelpTreeAdaptor implementation must have a
 *	<code>public static HelpTreeAdaptor getInstance(FacesContext,
 *	LayoutComponent, UIComponent)</code> method in order to get access to
 *	an instance of the <code>HelpTreeAdaptor instance.

* * <p> This class is used by DynamicTreeNodeFactory.

* * @author Ken Paulsen (ken.paulsen@sun.com) */ public class HelpTreeIndexAdaptor extends TreeAdaptorBase { /** * <p> This constructor is not used.

*/ private HelpTreeIndexAdaptor() { } /** * <p> This constructor saves the LayoutComponent descriptor * and the <code>UIComponent associated with this * <code>TreeAdaptor. This constructor is used by the * getInstance() method.</p> */ protected HelpTreeIndexAdaptor(LayoutComponent desc, UIComponent parent) { super(desc, parent); } /** * <p> This method provides access to an HelpTreeAdaptor * instance. Each time it is invoked, it returns a new instance.</p> */ public static TreeAdaptor getInstance(FacesContext ctx, LayoutComponent desc, UIComponent parent) { return new HelpTreeIndexAdaptor(desc, parent); } /** * <p> This method is called shortly after * {@link #getInstance(FacesContext, LayoutComponent, UIComponent)}. * It provides a place for post-creation initialization to take * occur.</p> */ 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); } /** * <p> Returns child IndexItems for the given * <code>IndexItem or Index model Object. If * <code>null is supplied, null is returned * any other <code>Object type will result in an * <code>IllegalArgumentException.

*/ public List getChildTreeNodeObjects(Object nodeObject) { if (nodeObject == null) { return null; } List<IndexItem> result = null; if (nodeObject instanceof IndexItem) { result = new ArrayList<IndexItem>(((IndexItem) nodeObject).getIndexItems()); } if (nodeObject instanceof Index) { result = new ArrayList<IndexItem>(((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()); } /** * <p> This method returns the "options" that should be supplied to the * factory that creates the <code>TreeNode for the given tree * node model object.</p> * * <p> Some useful options for the standard TreeNode * component include:<p> * * <ul>
  • text
  • * <li>url * <li>imageURL * <li>target * <li>action
  • * <li>actionListener
  • * <li>expanded * * <p> See Tree / TreeNode component documentation for more details.

    */ public Map<String, Object> getFactoryOptions(Object nodeObject) { if (nodeObject == null) { return null; } Map<String, Object> 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; } /** * <p> 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); } } */ /** * <p> This method returns the id for the given tree node * model object.</p> */ 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; } /** * <p> 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.</p> * * @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); } /** * <p> This method returns any facets that should be applied to the * <code>TreeNode (comp). Useful facets for the sun * <code>TreeNode component are: "content" and "image".

    * * <p> Facets that already exist on comp, or facets that * are directly added to <code>comp do not need to be returned * from this method.</p> * * <p> This implementation directly adds a "content" and "image" facet and * returns <code>null from this method.

    * * @param comp The tree node <code>UIComponent. * @param nodeObject The (model) object representing the tree node. */ public Map<String, UIComponent> getFacets(UIComponent comp, Object nodeObject) { // Do nothing return null; } /** * <p> Advanced framework feature which provides better handling for * things such as expanding TreeNodes, beforeEncode, and other * events.</p> * * <p> This method should return a Map of List * of <code>Handler objects. Each List in the * <code>Map should be registered under a key that cooresponds * to to the "event" in which the <code>Handlers should be * invoked.</p> */ public Map getHandlersByType(UIComponent comp, Object nodeObject) { // Do nothing return null; } /** * <p> This method returns the UIComponent factory class * implementation that should be used to create a * <code>TreeNode for the given tree node model object.

    * * <p> This implementation returns the default return value, unless the * <code>nodeObject represents the root node. In that case it * will return a the TreeFactory factory class:</p> * * <p> 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; } /** * <p> Comparator class for {@link IndexItem}s. Uses "text" for * comparison, ignoring case.</p> */ private static class IndexItemComparator implements Comparator<IndexItem> { @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; } } }

    Other Glassfish examples (source code examples)

    Here is a short list of links related to this Glassfish HelpTreeIndexAdaptor.java source code file:

    ... this post is sponsored by my books ...

    #1 New Release!

    FP Best Seller

     

    new blog posts

     

    Copyright 1998-2021 Alvin Alexander, alvinalexander.com
    All Rights Reserved.

    A percentage of advertising revenue from
    pages under the /java/jwarehouse URI on this website is
    paid back to open source projects.