*链接的含义
---代表一个资源,但是这个资源存在的地方,是在其他地方,非当前显示位置.这和软链接的概念一致,可以说是EFS的软链接.
*相关API
---哪些元素能创建link
IFolder,IFile,org.eclipse.core.internal.resources.Resource
---验证是否为link
IResource.isLinked(int options),options=IResource.CHECK_ANCESTORS,则自己或父资源任一为link,则方法返回true.
IResource.isLinked() == isLinked(IResource.NONE)
一般以createLink方式创建的为link,其下的内容就不属于link.所以CHECK_ANCESTORS区分了这2中情况.
*Broken Link
---含义,link定义的path不存在或未定义的情况
---resource对Broken link的处理协议,
* The copy and move methods will fail when called on broken links.
* Calling refreshLocal on a broken link will not cause the resource to be removed from the workspace, as it does for normal resources that are missing from the file system.
* The method getLocation will return null for linked resources whose locations are relative to undefined path variables.
* You can still use delete to remove broken links from the workspace.
*禁用link的方式
---org.eclipse.core.resources.natures扩展点中设置"<options allowLinking="false"/>"
---org.eclipse.core.resources.teamHook,可参考TeamHook
---设置禁用属性ResourcesPlugin.PREF_DISABLE_LINKING
1.在首选项中配置,Preferences->Workspace->Linked Resources
2.Platform.getPreferencesService().getRootNode().node(InstanceScope.SCOPE)
.node(ResourcesPlugin.PI_RESOURCES).putBoolean(ResourcesPlugin.PREF_DISABLE_LINKING, true);
3.ResourcesPlugin.getPlugin().getPluginPreferences().setValue(ResourcesPlugin.PREF_DISABLE_LINKING, "false");//@deprecated
*创建link
Linked Resource示例IWorkspace workspace =ResourcesPlugin.getWorkspace(); IPathVariableManager pvm =workspace.getPathVariableManager(); String name ="TEMP"; URI value =URI.create("file:/C:/temp"); //if(pvm.validateName(name).isOK()&&pvm.validateValue(value).isOK()) pvm.setURIValue(name, value); IProject proj =workspace.getRoot().getProject("bank"); IFolder folder =proj.getFolder("apps"); IFolder link =folder.getFolder("link"); //linked resource IPath loc =new Path("TEMP/link"); if(workspace.validateLinkLocation(link, loc).isOK()){ link.createLink(loc, IResource.ALLOW_MISSING_LOCAL|IResource.REPLACE, null); System.out.println(link.getFullPath()); //-->/bank/apps/link System.out.println(link.getLocation()); //-->C:/temp/link System.out.println(link.getRawLocation()); //-->TEMP/link System.out.println(link.isLinked()); //true IFile child = link.getFile("abc.txt"); //不存在 System.out.println(child.getFullPath()); //-->/bank/apps/link/abc.txt System.out.println(child.getLocation()); //-->C:/temp/link/abc.txt System.out.println(child.getRawLocation()); //-->C:/temp/link/abc.txt System.out.println(child.isLinked()); //false System.out.println(child.isLinked(IResource.CHECK_ANCESTORS)); //true System.out.println(child.exists()); //false }
*记录link
---对于包含link的Project,在Project的".project"文件中会记录下link的信息
"When a user creates a linked resource in a project, a description of the linked resource is added to the project description file (".project") in the project's location. "
项目对link的记录<linkedResources> <link> <name>apps/link</name> <type>2</type> <!--2表示文件夹,1表示文件--> <locationURI>TEMP/link</locationURI> </link> </linkedResources>
*参考
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/resAdv_events.htm
http://www.eclipse.org/eclipse/platform-core/documents/user_settings/faq.html