Rational Developer for System z


Lesson 2: Develop code for the BrowseMemberAction class

This lesson will guide you through the steps to develop the needed code for the BrowseMemberAction class.

To develop the code for the BrowseMemberAction class:

  1. Open the BrowseMemberAction class in the editor if it is not already open. In the Package Explorer expand com.ibm.carma.plugin.browse > src > browse, and double click on the BrowseMemberAction class.
  2. The first method you will write is selectionChanged. This method is used to control the items for which the browse action is enabled. In the following example code, the browse member action is enabled only on CARMA members and only for one member at a time. Because of this, checks must be performed before enabling the browser action. The following pseudocode demonstrates this:
    if (more than one item is selected)
       disable action;
    
    if (item selected is CARMA member)
       enable action
    else
       disable action
    Use the following sample code to implement this method:
    public void selectionChanged(IAction action, ISelection selection) {		
       Iterator i = ((IStructuredSelection) selection).iterator();
    
       // by default assume false
       action.setEnabled(false);
    
       if( ((IStructuredSelection) selection).size() != 1){
          return;
       }
    		
       while (i.hasNext()) {
          Object next = i.next();
          if (next instanceof CARMAMember) { // the element is a member
             //remember the item selected so if the action is run it knows
             //which item to run the action against
             this.itemSelected = (CARMAMember) next;
          } else {
             this.itemSelected = null;
             return;
          }
       }		
    
       // if we passed the test...then enable the action
       action.setEnabled(true);
    }
  3. The second method that you will write is the run method. This method is called when you want to invoke the BrowserMemberAction. To open the CARMAMember in browse-only mode, the workbench needs to download the contents of the file from the RAM into the IFile, a type of file in Eclipse, set the IFile properties to read-only, and then open the IFile. The following pseudocode demonstrates this:
    Get the CARMAMember the user wants to browse;
    Create an IFile that represents the CARMAMember;
    Download contents of the CARMAMember into the IFile;
    Set the properties of the IFile to read-only;
    Call Eclipse to open the IFile;
    Use the following sample code to implement this method:
    public void run(IAction action) {	
       //if itemSelected is null then the browse action was run
       //on something that is not a CARMA Member, this should never happen
       if (this.itemSelected != null) {
          //Get the name of the CARMA Member
          String memberName = itemSelected.getFileName();
    			
          //Create a temporary location on the workstation to hold the
          //local cache of the file
          IWorkspace myWorkspace = ResourcesPlugin.getWorkspace();
          IWorkspaceRoot myRoot = myWorkspace.getRoot();
    			
          IProject myResource = myRoot.getProject("/BootCampTemp");
    			
          //If the temporary directory that holds the temporary files
          //does not exist create it
          if( !myResource.exists() ){
             try{
                myResource.create(new NullProgressMonitor());
             } catch(Exception e){
                e.printStackTrace();
             }
          }
    			
          //If the temp location which is a project is not open
          //open it
          if( !myResource.isOpen()){
             try{
                myResource.open(new NullProgressMonitor());
             } catch(Exception e){
                e.printStackTrace();
             }
          }
    			
          //Make sure the temporary space is of the right type and exists
          if (myResource instanceof IContainer && myResource.exists()) {
             IContainer myContainer = (IContainer) myResource;
    	
          //Create the IFile in the temporary location 
          final IFile myFile = myContainer.getFile(new Path(memberName));
    				
          //Create the job that will get the contents of the file
          GetContentsJob myJob = new GetContentsJob("CRAJOB1", itemSelected);
    				
          //Run the Job
          myJob.schedule();
    
          try{
             InputStream myStream = null;
             while( (myStream = myJob.getStream()) == null){				
             }
             
             //Copy the contents into the IFile
             if(!myFile.exists())
                myFile.create(myStream, true, new NullProgressMonitor());
             } catch(Exception e){
                e.printStackTrace();
             }			
    				
             //Set the file's attributes to read-only and open the file
             Display display = Display.getDefault();
             display.syncExec(new Runnable() {
             public void run() {
                IWorkbenchPage page = 
                   PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                
                try {
                   ResourceAttributes myAttributes = myFile.getResourceAttributes();
                   if(myAttributes == null){
                      myAttributes = new ResourceAttributes();
                   }
                   //setting the attributes to readonly
                   myAttributes.setReadOnly(true);
                   try{
                      myFile.setResourceAttributes(myAttributes);
                   } catch(Exception e){
                      e.printStackTrace();
                   }
                   //opening the file in browse mode
                   IDE.openEditor(page, myFile, true);
                } catch (PartInitException e) {
                   //TODO handle exception
                   System.out.println(e);
                }
             }
          });
          }
       }
    }
  4. Verify that all the packages the class will need are imported. Add any that are listed below but are not already included in the import statements:
    import java.io.InputStream;
    import java.util.Iterator;
    
    import org.eclipse.core.resources.IContainer;
    import org.eclipse.core.resources.IFile;
    import org.eclipse.core.resources.IProject;
    import org.eclipse.core.resources.IWorkspace;
    import org.eclipse.core.resources.IWorkspaceRoot;
    import org.eclipse.core.resources.ResourceAttributes;
    import org.eclipse.core.resources.ResourcesPlugin;
    import org.eclipse.core.runtime.NullProgressMonitor;
    import org.eclipse.core.runtime.Path;
    import org.eclipse.jface.action.IAction;
    import org.eclipse.jface.viewers.ISelection;
    import org.eclipse.jface.viewers.IStructuredSelection;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.ui.IViewActionDelegate;
    import org.eclipse.ui.IViewPart;
    import org.eclipse.ui.IWorkbenchPage;
    import org.eclipse.ui.PartInitException;
    import org.eclipse.ui.PlatformUI;
    import org.eclipse.ui.ide.IDE;
    
    import com.ibm.carma.model.CARMAMember;
    import com.ibm.carma.ui.job.GetContentsJob;
  5. Save the source and debug any errors.

Feedback