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)