Getting Values from the Persistence Object Chooser

As a user interacts with the persistence object chooser to open or save components, the persistence object chooser reads their interaction. For example, if you display an Open dialog box, the persistence object chooser captures user interaction to determine the folder in which to look for an object and the object that the user wants to open.

You call the following methods on the persistence object chooser to find the user's selections. You can then pass the values in calls to the persistence service:

Call the getSelectedObjectName method or the getSelectedObjectFullPathName method to find the name of the component that the user wants to open or the name that the user wants to assign to an object during a save operation. Pass the value as the Name argument to the lookup, bind, or rebind method. If you call a method of the current folder, then pass the selected object name. If you call a method of a different folder, then pass the full path name of the selected object.

Call the getCurrentDirectory method to get the current folder. You can call the lookup, bind, or rebind method of this context.  

Example: Opening an object

The following code sample displays the persistence object chooser as an Open dialog box. It then retrieves the information about the object that the user wants to open, and it uses this information to retrieve the object. This code assumes that you have an InitialPersistenceManager named pmRoot.


PersistenceObjectChooser dialog = new PersistenceObjectChooser(pmRoot); JFrame frame = new JFrame(); int result = dialog.showOpenDialog(frame); if (result == PersistenceObjectChooser.OK_OPTION){ PersistenceManager pmCurrent =   (PersistenceManager)dialog.getCurrentDirectory(); String name = dialog.getSelectedObjectName();   try{      Object retrieved = pmCurrent.lookup(name);   }   catch (NamingException ne){      ne.printStackTrace();   } } // if result is OK  

Example: Saving a component

The following code shows how to save a component that the user has selected through the persistence object chooser. This code assumes that you have an InitialPersistenceManager named pmRoot and a graph named mySalesGraph.

PersistenceObjectChooser dialog = new PersistenceObjectChooser(pmRoot);
JFrame frame = new JFrame();
int result = dialog.showSaveDialog(frame);
if (result == PersistenceObjectChooser.OK_OPTION){
   PersistenceManager pmCurrent =
      (PersistenceManager)dialog.getCurrentDirectory();
   String name = dialog.getSelectedObjectName();
   if (name != null){
      try{
         //if name is not bound, then rebind will bind it
         pmCurrent.rebind(name, mySalesGraph);
      }catch (NamingException ne){
         ne.printStackTrace();
      } //catch
   } // if name != null
}// if result == OK_OPTION