Thursday, August 19, 2010

Change Current Tab Title Using Java Code

Sometime there is project requirement to change the tab title of currently selected tabs. This can be a common feature in any application. I am explaining a way to update the tab title using the Java code.

But there is a requirement to pass tabContext parameter to your task flow definition because this piece of code require the access to TabContext.

   public void resetTabTitle(String title) {
    //Retrieve TabContext current instance
    TabContext tabContext = TabContext.getCurrentInstance();

    //Retrieve selected tab index
    int index= tabContext.getSelectedTabIndex();

    //Retrieve Selected tab
    Tab currentTab = tabContext.getTabs().get(index);
    //Set the new title for tab
    currentTab.setTitle(title);

    //Here I am setting the selected tab index so
    //that currently selected tab can be refresh
    tabContext.setSelectedTabIndex(index);
}
It works for me, lets try it at your end.
 


Saturday, July 31, 2010

Select Once Choice inside ADF Query Referencing Parent table

Here By this sample I am explaining the way of using ADF SelectOneChoice inside Af:Query component as a reference to parents table. In that application I am using Employees as a child table and Departments as a parent table. I have used HR schema to create this sample application.

Create a Generic Application with the name of SocInADFQueryControlApp.


Create a project as with name ViewController. Select the ADF Faces for Project Technologies.

Enter the default package name for ViewController project.



Click on finish button. It will create a project in your application with the specified name.

Now Create a model project go to à new àGeneral àProjects àGeneric Project.

Click on Ok button.

Enter the name Model for model project. Select ADF Business Components from project technologies.

I have create ViewController and Model project using the Generic project, Because of better understanding why these are called ViewController and which technology supported by both the project. There is also a shortcut way to create ViewController and Model project either using the ADF Fusion Web Application or using Project creation wizard for ViewController and Model project.

Now let’s create Business component for Model project. Select Model Project click on New à Business Tier à ADF Business Component à Business Components from Table.
Create a connection to oracle database.

Select the Employees and Departments table.

Create the updateable view if required else skip it and create Read-Only view object.

Provide App Module name as SampleAppModule.

Skip Business Components Diagram as it is not required.

Click on the finish button.

Once you have clicked the finish button it will create Entity Object, View Object, Association, View Links and App Module.

Create a webpage. Select the ViewController project; go to à new à Web Tire àJSF à JSF Page.


Enter the page name as main.jspx; I have created this page using the Oracle Three Column Template. You can select whatever template you want or can select blank page.

Select the Employees View, go to à Attributes àSelect the DepartmentId Column.


Click on + icon and DepartmentView1 as a reference.
Click on + icon and DepartmentView1 as a reference.
Select the list Attribute DepartmentId.

Go to UI Hints Select the DepartmentName that will appear in the combo box, to make the component combo box select the Default List Type Choice List.

Now select the query and the view criteria.

Click on ok button.

Select the criteria from Employees View Object drag drop it to page. Select the Query à Adf Query panel with Table.

Enable Row Selection, Filtering and sorting.

Surround table with panel collection and select style Class as AFStretchWidth.

Change the result component id to panel collection id.

Once you deploy and run application screen look like:



Sunday, July 25, 2010

JDeveloper Extension for Generating Hibernate Units.


I am working on the JDeveloper Extension project to make hibernate development work a little bit easier. I have creating Extension for JDeveloper that will create following file:
  • Entity Class (Which will be serialized to the database).
  • DTO Class (which will interact with the user)
  • Manager Class (This class will contain all the operation that can we performed on entity)
  • HBM File (This file will contain the all the mapping from java object to the table fields in the data base)
  • Hibernate Configuration file (This will contain mapping of all generated HBM file).
  • Database Configuration file (Contain all the Database details on which these files are generated.)
These all file will be created on the table selected from database
These are the steps to generate all these files:
Welcome Screen:





This is the connection screen in that screen you can define new Connection or can use exiting Connection. To select existing connection remove check for create new connection.



You can also save the connection details for the future use by selecting the save connection check box.
A Combo box will appear at place of input text for connection, you can select the existing Connections from the Combo box.




Click on the text connection button if success full connection established with database then next button will appear. Click on the next button. It will bring new screen which will have list of schema available to the connected database. Select the appropriate schema. Below the schema
Combo box there is a filter for table name you can find your table there or just can click on fetch button it will display the entire table list in the available Units:




Select the table on which you want to generate the hibernate units. Add table to the select units list by clicking on the shuttle button.



Once you click on the next button it will bring the new screen which will have database column to the java fields. On this screen you can select the Key fields, select the data type for java class fields.




Once you finished with the mapping just click on the validate mapping this will validated all mapping and enable the next button.
On this screen it will ask for the unit you want to generate from this extension.



On this screen it will ask for:
Folder Location: This is the location where you want to keep all generated unit.
Default Package: This is the package structure inside the folder location where all file will be placed.
Default File Name: This the default file name for all units create by default it will filled with the table you selected if you want to change the File name you can change here.


Once you click on Generate file button all file will be generate on specified location.
Click to finished button to complete the wizard.

Please find the first release of extension on HibernateUnitGenerator let me know if anything is incorrect in that  








Thursday, January 28, 2010

what are inner classes and what are advanstage of inner classes?

Believe it or not, there are advantages to Java's inner classes. But before we go into that, I'll provide a short background on inner classes.

Inner classes nest within other classes. A normal class is a direct member of a package, a top-level class. Inner classes, which became available with Java 1.1, come in four flavors:

  • Static member classes
  • Member classes
  • Local classes
  • Anonymous classes

Let's take a quick look at each in turn.

Briefly, a static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or top-level, class.

Like a static member class, a member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference.

Local classes are declared within a block of code and are visible only within that block, just as any other method variable.

Finally, an anonymous class is a local class that has no name.

To answer your specific question, I'll focus on the member and anonymous inner classes since those are the ones you'll likely encounter and use. To me, the advantages of inner classes can be divided into three categories: an object-oriented advantage, an organizational advantage, and a call-back advantage.

The object-oriented advantage

In my humble opinion, the most important feature of the inner class is that it allows you to turn things into objects that you normally wouldn't turn into objects. That allows your code to be even more object-oriented than it would be without inner classes.

Let's look at the member class. Since its instance is a member of its parent instance, it has access to every member and method in the parent. At first glance, this might not seem like much; we already have that sort of access from within a method in the parent class. However, the member class allows us to take logic out of the parent and objectify it. For example, a tree class may have a method and many helper methods that perform a search or walk of the tree. From an object-oriented point of view, the tree is a tree, not a search algorithm. However, you need intimate knowledge of the tree's data structures to accomplish a search.

An inner class allows us to remove that logic and place it into its own class. So from an object-oriented point of view, we've taken functionality out of where it doesn't belong and have put it into its own class. Through the use of an inner class, we have successfully decoupled the search algorithm from the tree. Now, to change the search algorithm, we can simply swap in a new class. I could go on, but that opens up our code to many of the advantages provided by object-oriented techniques.

The organizational advantage

Object-oriented design isn't everyone's thing, but luckily, inner classes provide more. From an organizational point of view, inner classes allow us to further organize our package structure through the use of namespaces. Instead of dumping everything in a flat package, classes can be further nested within classes. Explicitly, without inner classes, we were limited to the following hierarchy structure:

package1    
 class 1
        class 2
        ...       
 class n 
 ... 
package n

With inner classes we can do the following:

package 1
    class 1
    class 2
       class 1
       class 2      
       ...
       class n

Used carefully, inner classes can provide a structural hierarchy that more naturally fits your classes.

The callback advantage

Inner member classes and anonymous classes both provide a convenient method for defining callbacks. The most obvious example relates to GUI code. However, the application of the callback can extend to many domains.

Most Java GUIs have some kind of component that instigates an actionPerformed() method call. Unfortunately, most developers simply have their main window implement ActionListener. As a result, all components share the same actionPerformed() method. To figure out which component performed the action, there is normally a giant, ugly switch in the actionPerformed() method.

Here's an example of a monolithic implementation:

public class LoginManager extends JFrame implements ActionListener {    
 
 protected JButton okButton;    
 protected JButton cancelButton;   
  ...        
 public void actionPerformed(ActionEvent e)    {
        if(e.getSource()== okButton)     {  
          // do something         
   }  else if (e.getSource()== cancelButton)     { 

         ... you get the picture

Whenever you see switches or large if/if else blocks, loud alarm bells should begin to ring in your mind. In general, such constructs are bad object-oriented design since a change in one section of the code may require a corresponding change in the switch statement. Inner member classes and anonymous classes allow us to get away from the switched actionPerformed() method.

Instead, we can define an inner class that implements ActionListener for each component to which we want to listen. That may result in many inner classes. However, we can avoid large switch statements and have the added bonus of encapsulating our action logic. Moreover, that approach may improve performance. In a switch where there are ncomparisons, we can expect n/2 comparisons in the average case. Inner classes allow us to set up a 1:1 correspondence between the action performer and the action listener. In a large GUI, such optimizations can make a substantial impact on performance. An anonymous approach may look like this:

public class LoginManager extends JFrame {
     ... 
   button member declarations
   ...
   protected void drawLoginPage()    {
 
        okButton = new JButton();
        cancelButton = new JButton();
        ...
        okButton.addActionListener(
  new java.awt.event.ActionListener()          {
           
     public void actionPerformed(java.awt.event.ActionEvent e)             { 
                // write your handling code here.  
            }
        }  
      );    

   .. repeat for each button

Using inner member classes, the same program would look like this:

public class LoginManager extends JFrame {
     ... 
 button member declarations
       // inner class definitions
 
 class OkButtonHandler implements ActionListener    {

        public void actionPerformed(ActionEvent e)    {  
         
  // do something      
  
  }    
 }

    ... define an inner member class for each button


     protected void drawLoginPage()    {

        // initialize the buttons       
  okButton = new JButton();       
  cancelButton = new JButton();

        ...      

   // register an inner class action listener instance      
   // for each button       
  
  okButton.addActionListener(new OkButtonHandler ());


.. repeat for each button

Disadvantages?

As with anything else, you have to take the good with the bad. Inner classes have their disadvantages. From a maintenance point of view, inexperienced Java developers may find the inner class difficult to understand. The use of inner classes will also increase the total number of classes in your code. Moreover, from a development point of view, most Java tools come up a bit short on their support of inner classes.