Monthly Archives: July 2011

How to show af:message programatically

Environment (JDeveloper 11.1.2.0.0, ADF Faces)
ADF Faces uses the standard JSF messaging API. JSF supports a built-in framework for messaging by allowing FacesMessage instances to be added to the FacesContext object using the addMessage(java.lang.String clientId, FacesMessage message) method. In general there are two types of messages that can be created:component-level messages, which are associated with a specific component based on any client ID that was passed to the addMessage method, and global-level messages, which are not associated with a component because no client ID was passed to the addMessage method.

in this post, I will show how to show af:message programatically.

  • global level message:

To show a global level message, use this method:

public String showMessage() {
        String messageText=”A prgramatic af:message”;
        FacesMessage fm = new FacesMessage(messageText);
        /**
         * set the type of the message.
         * Valid types: error, fatal,info,warning
         */
        fm.setSeverity(FacesMessage.SEVERITY_INFO);
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, fm);
        return null;
    }

the code above will show the message in pop-up dialog as shown below.

pop-up global message

However, you can show the global message inline with the page, what you want to do is to change the inline attribute of the af:messages in your page, this component is created automatically for you when you create a page. The inline attribute controls whether to render the message list inline with the page or in a popup window, the default value is false. Normally the messages are rendered in a pop up. If this attribute is set to true, the messages list will be rendered inline with the page. To find the af:messages, open your page in the main window, from the structure window you can find it under f:view,af:document nodes. Your global message will be shown inline with page as shown below.

global message - inline with the page

 

  • component-level message:

To show a message inline with a component (i.e. associated with a specific UI component) you need to expose the UI component in the managed bean using the Binding property, then you can use this method:

public String showMessage() {
        String messageText=”A prgramatic af:message”;
        FacesMessage fm = new FacesMessage(messageText);
        /**
         * set the type of the message.
         * Valid types: error, fatal,info,warning
         */
        fm.setSeverity(FacesMessage.SEVERITY_INFO);
        FacesContext context = FacesContext.getCurrentInstance();
        //departmentName is the binding property for our field.
        context.addMessage(getDepartmentName().getClientId(context), fm);
        return null;
    }

The code above will show the message associated with department name field as shown below:

component-level messages

Advertisements

13 Comments

Filed under ADF

Control row updatability – part 2

Environment (JDeveloper 11.1.2.0.0, ADF BC, ADF Faces)

In the previous post, I have shown how to control the row updatability based on some conditions. Another popular use case for row updatability is making the row or some attributes within the row only updatable while the row status is new, but after being successfully committed to the database they should become read-only. In this post I will show how to implement this use case in declarative and programmatic ways.

  • Declarative method:

The Updatable property controls that is available for each attribute within the entity and view objects has a While New option, we can set this property for single, multiple, or all attributes as shown below.

Updatable Property

 

  • Programmatic method:

we can override isAttributeUpdateable method in the EntityImpl class. If we want the entire row to be updatable while it is  new then override  isAttributeUpdateable method as shown below:

  public boolean isAttributeUpdateable(int i) {
        if (getEntityState()!=STATUS_NEW )
            return false;
        else
            return super.isAttributeUpdateable(i);
    }

If we want single or many attributes to be updatable while the row is new, then override  isAttributeUpdateable method as shown below:

public boolean isAttributeUpdateable(int i) {
        /**
         * ID is the attributeEnum.
         * you can add attributesEnum as required
         * by your use case
         */
        if (getEntityState()!=STATUS_NEW && i== ID)
            return false;
        else
            return super.isAttributeUpdateable(i);
    }

 

see also: Control row updatability – part 1.

Leave a comment

Filed under ADF

Control row updatability – part 1

Environment (JDeveloper 11.1.2.0.0, ADF BC, ADF Faces)

Sometimes developers want to make an entire row as read only based on some conditions, As an example assume that you have an application that allows users to apply for something, once the application is approved they are not allowed to modify any attribute instead they can only see their application, this can be done by setting the read only property for each attribute from the property inspector. However, doing this is a headache approach especially if you want to repeat this in many pages and your row contains many attributes.

To implement such similar case in effective approach, we can override the isAttributeUpdateable method either in the EntityImpl class or in the ViewRowImpl class. If you want your row to be non updatable regardless of the view object being used, then override the isAttributeUpdateable method in the EntityImpl class otherwise override the method in your ViewRowImpl class.

The steps required to do this are, (assumed we need to override the isAttributeUpdateable method in EntityImpl class).

  1. generate your custom EntityImpl class.
  2. Assumed that your EntityImpl class is opened in Jdeveloper main window, click on the source menu, then select Override methods  as shown below.

    Override isAttributeUpdateable method

  3. The Override methods window will open, select the isAttributeUpdateable method and click OK as shown below.

    Select isAttributeUpdateable method

  4. Now override your method based on your condition as shown below:

    public boolean isAttributeUpdateable(int i) {
//you can add your condition here
     //in this case Flag attribute indicates whether
    //an application is approved or not
if (this.getFlag().equals(“Y”))
return false;
else
return super.isAttributeUpdateable(i);
}


see also:  Control row updatability – part 2.

Leave a comment

Filed under ADF

How to control the location of the new row in af:table

Environment (JDeveloper 11.1.2.0.0, ADF BC, ADF Faces, hr schema)

When we add a new row in af:table,the default behavior is that the new row is added before the current row. Sometimes we may need to change this behavior. In this post I will show how we can add new row at different locations within our table, and automatically scrolling to that new row.

This post is based on hr schema (EMPLOYEES table) and assumed that you have already built your BC. The button used to add a new row has its PartialSubmit property set to true, and the table has its PartialTrigger property references the button’s id, also our table should have its DisplayRow property set to selected, the table binding in the managed bean is empTable (setEmpTable(RichTable empTable), getEmpTable()).

  1. To add a new row at the first location on the table use this method:   
    public String addRowAtFirst() {
    // Add event code here…
    CollectionModel tableModel = (CollectionModel)getEmpTable().getValue();
    JUCtrlHierBinding adfModel = (JUCtrlHierBinding)tableModel.getWrappedData();
    DCIteratorBinding dciter = adfModel.getDCIteratorBinding();
    NavigatableRowIterator nav=dciter.getNavigatableRowIterator();
    Row newRow = nav.createRow();
    newRow.setNewRowState(Row.STATUS_INITIALIZED);
    nav.insertRowAtRangeIndex(0, newRow);        dciter.setCurrentRowWithKey(newRow.getKey().toStringFormat(true));
    return null;
    }
  2. To add a new row at the end of the table use this method:   
    public String addRowAtLast() {
    // Add event code here…
    CollectionModel tableModel = (CollectionModel)getEmpTable().getValue();
    JUCtrlHierBinding adfModel = (JUCtrlHierBinding)tableModel.getWrappedData();
    DCIteratorBinding dciter = adfModel.getDCIteratorBinding();
    NavigatableRowIterator nav=dciter.getNavigatableRowIterator();
    Row newRow = nav.createRow();
    newRow.setNewRowState(Row.STATUS_INITIALIZED);
    Row lastRow = nav.last();
    int lastRowIndex = nav.getRangeIndexOf(lastRow);
    nav.insertRowAtRangeIndex(lastRowIndex+1, newRow);
    dciter.setCurrentRowWithKey(newRow.getKey().toStringFormat(true));
    return null;
    }
  3. To add a new row before the current selected row (default behavior) use this method:   
    public String addRowBefore() {
    // Add event code here…
    CollectionModel tableModel = (CollectionModel)getEmpTable().getValue();
    JUCtrlHierBinding adfModel = (JUCtrlHierBinding)tableModel.getWrappedData();
    DCIteratorBinding dciter = adfModel.getDCIteratorBinding();
    NavigatableRowIterator nav=dciter.getNavigatableRowIterator();
    Row newRow = nav.createRow();
    newRow.setNewRowState(Row.STATUS_INITIALIZED);
    nav.insertRow(newRow);        dciter.setCurrentRowWithKey(newRow.getKey().toStringFormat(true));
    return null;
    }
  4. To add a new row after the current selected row use this method:   
    public String addRowAfter() {
    // Add event code here…
    CollectionModel tableModel = (CollectionModel)getEmpTable().getValue();
    JUCtrlHierBinding adfModel = (JUCtrlHierBinding)tableModel.getWrappedData();
    DCIteratorBinding dciter = adfModel.getDCIteratorBinding();
    NavigatableRowIterator nav=dciter.getNavigatableRowIterator();
    Row newRow = nav.createRow();
    newRow.setNewRowState(Row.STATUS_INITIALIZED);
    Row currentRow = nav.getCurrentRow();
    int currentRowIndex = nav.getRangeIndexOf(currentRow);
    nav.insertRowAtRangeIndex(currentRowIndex+1, newRow);
    dciter.setCurrentRowWithKey(newRow.getKey().toStringFormat(true));
    return null;
    }

7 Comments

Filed under ADF