X

The blog for hints & tips about Oracle Application Development Framework and Mobile Application Framework

  • ADFv
    November 25, 2010

How-to dynamically detect and list skin names in a managed bean

Frank Nimphius
Master Principal Product Manager

Skins can be dynamically looked up from available application
configurations and the class path (which includes libraries). Find below
a code listing that shows available skin definitions in a selectOneChoice
component.

//managed
bean class

public class SkinManager {

  public SkinManager() {

    ADFContext adfctx =
ADFContext.getCurrent();

    Map sessionScope =
adfctx.getSessionScope();

    Object skinFamily = sessionScope.get("skinFamily");

      if(skinFamily == null){

        sessionScope.put("skinFamily",
"fusion");

      }

  }



  //method referenced from a
selectOneChoice f:selectItems component

  public List getSkinChoices() {

    List choices = new ArrayList();

    String skinFamily = null;

    String skinLabel = null;

    SkinFactory sf =
SkinFactory.getFactory();

    FacesContext context = FacesContext.getCurrentInstance();

   
for (Iterator i = sf.getSkinIds(); i.hasNext(); ) {

      String skinID = (String)i.next();

      Skin skin = sf.getSkin(context,
skinID);

      skinFamily = skin.getFamily();

      skinLabel = skinFamily;

      if (skin.getRenderKitId().indexOf("desktop")
> 0) {

         choices.add(new
SelectItem(skinFamily, skinLabel));

     }

   }

  return choices;

 }

  //method referenced from a selectOneChoice
valueChangeListener

  public void onNewSkinSelection(ValueChangeEvent
valueChangeEvent) {

   ADFContext adfctx =
ADFContext.getCurrent();

   Map sessionScope = adfctx.getSessionScope();

   sessionScope.put("skinFamily",
(String)

   valueChangeEvent.getNewValue());

   redirectToSelf();

  }

  //redirect that needs to be performed
in case a selected

  //skin shouldbe applied

  private void redirectToSelf() {

   FacesContext fctx = FacesContext.getCurrentInstance();

   ExternalContext ectx =
fctx.getExternalContext();

   String viewId = fctx.getViewRoot().getViewId();

   ControllerContext controllerCtx =
null;

   controllerCtx = ControllerContext.getInstance();

   String activityURL = controllerCtx.getGlobalViewActivityURL(viewId);

   try {

     ectx.redirect(activityURL);

     fctx.responseComplete();

   } catch (IOException e) {

    //Can't redirect

    e.printStackTrace();

    fctx.renderResponse();

   }

  }

}

As select one choice to display the skin definitions would
look as shown below:

<af:selectOneChoice
label="Choose a Skin"

       value="#{sessionScope.skinFamily}"
id="soc1"

       autoSubmit="true"                                   

       valueChangeListener="#{SkinManager.onNewSkinSelection}">

       <f:selectItems
value="#{SkinManager.skinChoices}" id="si1"/>

</af:selectOneChoice>

Note:  Feel free to use the managed bean code for
other use cases as well.


To make sure the trinidad-config.xml file reads the
selected skin definition upon redirecting the page, the following entry needs
to be defined

<skin-family>
   #{sessionScope.skinFamily!=null?sessionScope.skinFamily
: 'fusion'}

</skin-family>

The skin family is read from the "skinFamily"
name in session scope. If the attribute is not set, then the skin falls back to
use the Oracle Fusion skin. If the selectOneChoice described above is used on
the initial application home page, then the session attribute is always set with
the instantiation of the managed bean.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.