Using
JDeveloper we can change every ADF Rich faces component's look and feel with CSS.
(1)Creating
a New ADF Skin and Changing the Skin Configuration
(a)
In
the Application Navigator, Right-click the ViewController
project and select New
from the context menu. In
the New Gallery, expand the Web
Tier node,
select JSF/Facelets
and in the right hand pane, select ADF
Skin
as the item.
(b)In
the Create ADF Skin File dialog, change the file name to
mySkinCustom.css.
Append \mySkinCustom
to the existing
skins
directory path. Check the
Use as the default skin family for this project
checkbox and in the Extends field choose to extend the
fusionFx-simple-v2.desktop
skin from the list of values.
(c)The
new skin file appears in the Application Navigator under the Web
Content | skins | mySkinCustom nodes.
It also updates trinidad-skins.xml
and trinidad-config.xml
with the newly created skin.
(d)mySkinCustom.css
opens by default in the design editor with a selector tree of
components that you can navigate to.
(e)Go
to property
Inspector
panel and make the required changes to the properties like background
color, margin, text properties etc.
NOTE:
Whenever
we create a new skin , first start by changing the Global Selector
Aliases. A
global selector alias defines style properties in one location in the
ADF skin that you can apply to multiple ADF Faces and ADF Data
Visualization components. We can also create new aliases (just like
.AFStretchWidth)
and later apply it to Components.
(2)Dynamically
changing skins at Runtime:
The
trinidad-skins.xml
file is a registry file of all custom skins available to an
application The source shows that your skin file, mySkinCustom.css
has been added to other existing skins automatically when you create
the CSS. These skins are registered skins and available for use by
your project.
Create
a managed bean to handle the skin change at runtime.(Code given at
the end of blog.)
Modify
trinidad-config.xml
so that we can change the skins at runtime.
You
set the
CHECK_FILE_MODIFICATION
and DISABLE_CONTENT_COMPRESSION
context initialization parameters to true
in the web.xml
file of your application. (Recommended only for Development phase,
Unset it during production phase)<context-param>
<param-name>org.apache.myfaces.trinidad.CHECK_FILE_MODIFICATION</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.trinidad.DISABLE_CONTENT_COMPRESSION</param-name>
<param-value>true</param-value>
</context-param>
Changes
that you make to a selector for an ADF Faces component (other than
changes to icon and skin properties) render immediately when you
refresh a Fusion web application's page that renders the ADF Faces
component.
Add
a menu
on the page to select the skin at runtime. Change the Selected
property
of the
Menu Item
to #{YourBeanName.skinFamily}
and
provide an ActionListener
to process the skin change.
After
we run the page, we can see the changes on selecting different skins
from the Menu we'd provided.
Bean class Code:
package sampleApp1.view;
import java.io.IOException;
import javax.faces.event.ActionEvent;
import java.util.Locale;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.faces.event.ValueChangeEvent;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import oracle.adf.share.ADFContext;
import oracle.adf.view.rich.component.rich.nav.RichCommandMenuItem;
public class SampleSkinBean {
public SampleSkinBean() {
}
Locale preferredLocale;
private String skinFamily = _RICH_DEMO_SKIN;
static private final String _SKIN_TEST_DIRECTORY_INPUT = "/skinningKeys/input";
static private final String _SKIN_TEST_DIRECTORY_SELECT = "/skinningKeys/select";
static private final String _SKIN_TEST_DIRECTORY = "/skinningKeys";
static private final String _TEST_SKIN_INPUT_SELECT = "demoComponentsInputSelect";
static private final String _TEST_SKIN_INPUT_OTHER = "demoComponentsOther";
static private final String _RICH_DEMO_SKIN = "Default"; // 'Default' skin is loaded at start.
public String getSkinFamily()
{
// If we're in the skin component tests, return the skinComponentTest skin.
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot viewRoot = context.getViewRoot();
if ((viewRoot != null) &&
(viewRoot.getViewId().indexOf(_SKIN_TEST_DIRECTORY) >= 0))
{
String viewId = viewRoot.getViewId();
if (viewId != null && viewId.indexOf(_SKIN_TEST_DIRECTORY) >= 0)
{
if ((viewId.indexOf(_SKIN_TEST_DIRECTORY_INPUT) >=0) ||
(viewId.indexOf(_SKIN_TEST_DIRECTORY_SELECT) >=0))
return _TEST_SKIN_INPUT_SELECT;
else
return _TEST_SKIN_INPUT_OTHER;
}
}
return skinFamily;
}
public void change(String language) {
Locale newLocale = new Locale(language);
FacesContext fctx = FacesContext.getCurrentInstance();
fctx.getViewRoot().setLocale(newLocale);
ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
UIViewRoot uiViewRoot = FacesContext.getCurrentInstance().getViewRoot();
setPreferredLocale(uiViewRoot.getLocale());
}
public void changeLocale(ValueChangeEvent valueChangeEvent) throws NoSuchFieldException {
change(valueChangeEvent.getNewValue().toString());
}
public void setSkinFamily(String family)
{
skinFamily = family;
}
public void skinMenuAction(ActionEvent ae)
{
RichCommandMenuItem menuItem = (RichCommandMenuItem) ae.getComponent();
setSkinFamily(menuItem.getText());
reloadThePage();
}
public void afterload(PhaseEvent phaseEvent) {
System.out.println("User:"+ ADFContext.getCurrent().getSecurityContext()
.getUserPrincipal().getName() );
if (ADFContext.getCurrent().getSecurityContext()
.getUserPrincipal().getName().equals("super1")) {
setSkinFamily("Default");
}
}
public static void reloadThePage()
{
FacesContext fContext = FacesContext.getCurrentInstance();
String viewId = fContext.getViewRoot().getViewId();
String actionUrl = fContext.getApplication().getViewHandler().getActionURL
(fContext, viewId);
try
{
ExternalContext eContext = fContext.getExternalContext();
String resourceUrl = actionUrl; //eContext.encodeResourceURL(actionUrl);
// Use the action URL directly since the encoding a resource URL will NPE in isEmailablePage()
eContext.redirect(resourceUrl);
}
catch (IOException ioe)
{
System.err.println("Problem trying to reload the page:");
ioe.printStackTrace();
}
}
public void refreshpage(ActionEvent actionEvent) {
FacesContext fc = FacesContext.getCurrentInstance();
String refreshpage = fc.getViewRoot().getViewId();
ViewHandler ViewH =
fc.getApplication().getViewHandler();
UIViewRoot UIV = ViewH.createView(fc,refreshpage);
UIV.setViewId(refreshpage);
fc.setViewRoot(UIV);
}
public void setPreferredLocale(Locale preferredLocale) {
this.preferredLocale = preferredLocale;
}
public Locale getPreferredLocale() {
return preferredLocale;
}
}
No comments:
Post a Comment