Eclipse RCP Resource Management (custom project)

Source: Internet
Author: User

Basic concepts

The Eclipse development platform provides a resource plug-in (Org.eclipse.core.resources) that provides management and manipulation of projects (project), Files, folders (folder).

The workspace (workspace) is the organization and save area of the user data file in the platform, and the files in the workspace are organized through the concept of resources. The Workbench (Workbench) can be understood as a tool for users to browse and manipulate workspaces. The resource plug-in provides APIs for creating, navigating, and manipulating resources in the workspace. The workbench uses these APIs to provide the appropriate functionality to the user, and these APIs also allow the user to scale.

There are three types of resources: project, file, folder. A project contains several files and folders that are not nested between items as a resource unit and container that represents a particular area. There are a number of projects in the workspace. The concepts of files and folders are similar to those in the OS.

The resources in the workspace are organized in the form of a resource tree, and the files and folders are descendant nodes of the project. Defines a workspace root as a special kind of resource existence, as the root node of the resource tree.

Access to resources

In the development platform, the workspace is represented by an instance of Iworkspace, and Iworkspace provides access to the resources in the workspace. Specifically, get the workspace instance by using the following statement:

Iworkspace workspace = Resourcesplugin.getworkspace ();

In the file system, the workspace is consistent with the general folder structure. However, there are two types of hidden files (folders),. Metadata is stored in the workspace root location, is a folder, folder contains the workspace structure of information files, in the development platform must be accessed through special plug-in API ;. Project exists at the project node location, which is a file that holds consistent information with iprojectdescription.

The resource plug-in provides Iproject,ifolder, and ifile to implement user access to these resource types, and Iresource defines an extended generic operation interface. You can also use the interface Ipath in Org.eclipse.core.runtime to represent the access path to the resource or file system of the workspace.

Access to resources is similar to access to Java.io.File, when the corresponding API is called, a handle is returned, and there is no need to ensure that the resource exists before access. Specifically, access is achieved through the following code:

// Get workspace Root  =//   Get project instance from workspace root = Myworkspaceroot.getproject ("MyWeb"//  Open If necessary    if (mywebproject.exists () &&!mywebproject.isopen ()) Mywebproject.open (null);

You must perform an open operation before you work on the project. Opening a project operation reads the project structure from the file system and reads it into memory, creating the corresponding project resource tree, which is an explicit operation.

The next level of getting a project instance is to access the files and folders in the project:

//Get folder InstanceIFolder Imagesfolder= Mywebproject.getfolder ("Images"); if(Imagesfolder.exists ()) {//Create a new fileIFile Newlogo= Imagesfolder.getfile ("Newlogo.png"); //the process of creating a file systemFileInputStream FileStream=NewFileInputStream ("C:/myotherdata/newlogo.png"); Newlogo.create (FileStream,false,NULL); //Create closes the file stream, so no worries.   }  

The above procedure, first gets the handle of the images folder, after judging its existence, creates a new file in it Newlogo.

Resource-to-file system Association

The absolute file system path of the resource is obtained through the interface Iresource.getlocationuri. In turn, the mapping of the file system path to the workspace path is achieved through Iworkspaceroot's Findfilesforlocationuri or Findcontainersforlocationuri.

Resource properties

Used to save special information about a resource, including session-level and persistence-level. Session-level expires when the project is closed, used in memory to save the property in the form of a name, the persistence level in the file system to retain the resource attributes, there is a length limit (not greater than 2kb, for the persistence of objects seems difficult), you need to pay attention to the naming of the conflict.

Item-Level parameters

You can define project-level scopes (project-scope) for runtime Parameters Runtime Preferences, which are used to save and manipulate parameters within a project scope:

// get Contextiscopecontext projectscope = new Projectscope (MyProject); // Gets the parameter node for the corresponding item preferences Projectnode = Projectscope.node ("Com.example.myplugin"); if (projectnode! = null) {value = Node.getboolean ("Mypreference", "true"); //  // parameter write-back Projectnode.flush ();

Resource Hook up

Resources in the workspace can come from different locations in the file system and are implemented through resource hooks. Instance code:

Iworkspace workspace =Resourcesplugin.getworkspace (); Ipathvariablemanager Pathman=Workspace.getpathvariablemanager (); String name= "TEMP"; IPath value=NewPath ("C:\Temp"); //define a path variable to implement a resource hook-up  if(Pathman.validatename (name). IsOK () &&Pathman.validatevalue (value). IsOK ())       {Pathman.setvalue (name, value); } Else {         //invalid name or value, throw an exception or warn user} IProject Project= Workspace.getproject ("Project");//assume this existsIFolder Link= Project.getfolder ("Link"); IPath Location=NewPath ("Temp/folder"); //need to add validation action before resource operation  if(workspace.validatelinklocation (location). IsOK ()) {Link.createlink (location, Iresource.none,NULL); } Else {         //invalid, throw an exception or warn user     }       //Run ResultsLink.getfullpath () ==> "/project/link"link.getlocation ()==> "C:tempfolder"link.getrawlocation ()==> "Temp/folder"link.islinked ()==> "true"IFile Child= Link.getfile ("Abc.txt");       Child.create (...); Child.getfullpath ()==> "/project/link/abc.txt"child.getlocation ()==> "C:tempfolderabc.txt"child.getrawlocation ()==> "C:tempfolderabc.txt"child.islinked ()==> "false"

Transferred from: http://lengbingteng-163-com.iteye.com/blog/1118641

Eclipse RCP Resource Management (custom project) one (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.