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

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.

Comments:

Post a Comment:
Comments are closed for this entry.
About

The Oracle JDeveloper forum ranks in the Top 5 of the most active forums on the Oracle Technology Network (OTN).



The OTN Harvest blog is a summary of selected topics posted on the OTN Oracle JDeveloper forum.



It is an effort to turn knowledge exchange into an interesting read for developers who enjoy little nuggets of wisdom





Frank Nimphius

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today