IProject IResource IFolder IFile

IProject IResource IFolder IFile
     public   static  ToolsProject getDefaultProject() throws  Exception {
        
if(instance!= null){
            
return instance;
        }

        IWorkspaceRoot root 
= ResourcesPlugin.getWorkspace().getRoot();
        IProject prj 
= root.getProject("DefaultProject");
        
if (!prj.exists()) {
            
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
            
final IProjectDescription desc = workspace.newProjectDescription(prj.getName());
            desc.setNatureIds(
new String[] "com.test.tools" });
            desc.setComment(
"TOOLS");
            prj.create(desc, 
null);
            prj.open(IResource.BACKGROUND_REFRESH, 
null);
        }

        ToolsProject prject 
= new ToolsProject();
        prject.setPrject(prj);
        
return prject;
    }

IContainer container  =  (IContainer) resource;
            
final  IFile file  =  container.getFile( new  Path(sName  +   " / "   +   " s "   +   " .xml " ));
            ensureFolder((IFolder) file.getParent());


InputStream stream 
=  openContentStream(siteName);
            
if  (file.exists())  {
                file.setContents(stream, 
truetrue, monitor);
            }
  else   {
                file.create(stream, 
true, monitor);
            }


stream.close();

private  InputStream openContentStream(String siteName)  {
        Site site 
= new Site();
        site.setName(siteName);
        XStream xstream 
= new XStream(new DomDriver());
                String xml 
= xstream.toXML(site);
        
return new ByteArrayInputStream(xml.getBytes());
    }

创建目录 mkdirs
public   static   void  ensureFolder(IFolder folder)  throws  CoreException  {
        IContainer parent 
= folder.getParent();
        
if (parent instanceof IFolder) {
            ensureFolder((IFolder) parent);
        }

        
if (!folder.exists()) {
            folder.create(
truefalsenull);
        }


    }



删除目录
dFolder.delete(
true null );


监听到资源发生变化

private   void  hookResourceListener()  {
        IWorkspace workspace 
= ResourcesPlugin.getWorkspace();
        IResourceChangeListener listener 
= new IResourceChangeListener() {
            
public void resourceChanged(IResourceChangeEvent event) {
                viewer.refresh();
            }

        }
;
        workspace.addResourceChangeListener(listener);
    }

你可能感兴趣的:(IProject IResource IFolder IFile)