要为 BrowseMemberAction 类开发代码,请执行下列操作:
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);
}
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);
}
}
});
}
}
}
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;