Rational Developer for System z


课程 2:为 BrowseMemberAction 类开发代码

本课程将指导您完成为 BrowseMemberAction 类开发所需代码时需要执行的步骤。

要为 BrowseMemberAction 类开发代码,请执行下列操作:

  1. 如果尚未打开 BrowseMemberAction 类,请在编辑器中将它打开。在包资源管理器中,展开 com.ibm.carma.plugin.browse > src > browse,并双击 BrowseMemberAction 类。
  2. 您将编写的第一个方法是 selectionChanged 此方法用来控制为其启用了“浏览”操作的项。在以下示例代码中,仅对 CARMA 成员启用了“浏览成员”操作,并且一次仅对一个成员启用此操作。正因为如此,在启用浏览器操作之前必须执行检查。以下伪码演示了此情况:
    if (more than one item is selected)
       	disable action;
    
    if (item selected is CARMA member)
       enable action
    else
       disable action
    使用以下样本代码来实现此方法:
    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. 您将编写的第二个方法是 run 方法。 当您要调用 BrowserMemberAction 时就会调用此方法。要以“仅浏览”方式打开 CARMAMember,工作台需要将文件内容从 RAM 下载到 IFile(这是 Eclipse 中的一种文件),将 IFile 属性设置为只读,然后打开 IFile。以下伪码演示了此情况:
    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;
    使用以下样本代码来实现此方法:
    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. 验证是否已导入此类将需要的所有包。添加下面所列示的、尚未包括在 import 语句中的任何包:
    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. 保存源代码,并调试存在的任何错误。

使用条款 | 反馈



本信息中心基于 Eclipse 技术。(http://www.eclipse.org)