Naveen
Naveen

Reputation: 6936

Java equivalent of getComponent() in SSJS

I know that we can access the XPages global objects like this in Java

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
...
...

But I am unable to find any equivalent for using getComponent() is Java. Is there any class or method in Java which is similar to getComponent() in SSJS?

Upvotes: 0

Views: 3040

Answers (2)

keithstric
keithstric

Reputation: 1064

In the Group extension of the Extension Library there is a query package that contains the XspQuery class and some filters. This class was intended to function like dojo.query to give you many different ways to find a component, not just by the id but also things like Component Class, Client ID, Server ID, etc. Here's an example of using the server ID to locate a component:

XspQuery query = new XspQuery();
query.byId("someId");
List<UIComponent> componentList = query.locate();

You can find it here: https://svn-166.openntf.org/svn/xpages/extlib/eclipse/plugins/com.ibm.xsp.extlib.group/src/com/ibm/xsp/extlib/query/

The Group extension was never distributed with the extension library, but was in the svn repository and to get it you had to go through the OpenNTF svn server.

Upvotes: 2

Panu Haaramo
Panu Haaramo

Reputation: 2932

It might be easiest by evaluating SSJS in Java. Code from Sven:

String valueExpr = "#{javascript: getComponent('xxx').getValue();}";
FacesContext fc = FacesContext.getCurrentInstance();
ExpressionEvaluatorImpl evaluator = new ExpressionEvaluatorImpl( fc );
ValueBinding vb = evaluator.createValueBinding( fc.getViewRoot(), valueExpr, null, null);
vreslt = (String) vb.getValue(fc);

How to call ad hoc SSJS from a Java Bean

Here is pure Java solution by Karsten Lehmann:

/** 
 * Finds an UIComponent by its component identifier in the current 
 * component tree. 
 * 
 * @param compId the component identifier to search for 
 * @return found UIComponent or null 
 * 
 * @throws NullPointerException if <code>compId</code> is null 
 */ 
public static UIComponent findComponent(String compId) { 
    return findComponent(FacesContext.getCurrentInstance().getViewRoot(), compId); 
} 

/** 
 * Finds an UIComponent by its component identifier in the component tree 
 * below the specified <code>topComponent</code> top component. 
 * 
 * @param topComponent first component to be checked 
 * @param compId the component identifier to search for 
 * @return found UIComponent or null 
 * 
 * @throws NullPointerException if <code>compId</code> is null 
 */ 
public static UIComponent findComponent(UIComponent topComponent, String compId) { 
    if (compId==null) 
        throw new NullPointerException("Component identifier cannot be null"); 

    if (compId.equals(topComponent.getId())) 
        return topComponent; 

    if (topComponent.getChildCount()>0) { 
        List childComponents=topComponent.getChildren(); 

        for (UIComponent currChildComponent : childComponents) { 
            UIComponent foundComponent=findComponent(currChildComponent, compId); 
            if (foundComponent!=null) 
                return foundComponent; 
        } 
    } 
    return null; 
} 

http://www.mindoo.com/web/blog.nsf/dx/18.07.2009191738KLENAL.htm

Upvotes: 5

Related Questions