How to read Projects and files from navigator explorer using eclipse client java
4 answers
I don't understand this question. Please provide more details, if you hope for an answer. The tag extending implies you want to do something with the API. Was that a wrong tag?
Hi ,
The following code will help you to read all files imported into workspace.
Based on this code we can able to design custom File Open Dialog.
try {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IPath location = root.getLocation();
File currentDir = new File(location+"\\.metadata\\.plugins\\org.eclipse.core.resources\\.projects");
if(currentDir.isDirectory())
{
String[] files = currentDir.list();
for(int i = 0;i<files.length;i++){
String locfile = location+"\\.metadata\\.plugins\\org.eclipse.core.resources\\.projects\\"+files[i]+"\\.location";
File locationfile = new File(location+"\\.metadata\\.plugins\\org.eclipse.core.resources\\.projects\\"+files[i]+"\\.location");
if(locationfile.isFile()) {
if(isInWorkSpace(locfile)){
//if this condition is true the imported projects available in current workspace
}else{
// if it is not there in workspace then the path will be availble location file inside this project folder
String locactpath = getFileActualPath(locfile,files[i]);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
getFileActualPath
private String getFileActualPath(String locfile,String projectname) throws IOException {
FileInputStream fstream = new FileInputStream(locfile);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
if(strLine.contains("?file")){
int startindex = strLine.indexOf("file")+6;
int endindex = strLine.lastIndexOf(projectname)+projectname.length();
return strLine.substring(startindex, endindex);
}
}
//Close the input stream
in.close();
return null;
}
isInWorkSpace
public static boolean isInWorkSpace(String file) throws IOException {
FileInputStream fstream = new FileInputStream(file);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
if(strLine.contains("?file")){
return false;
}
}
//Close the input stream
in.close();
return true;
}
The following code will help you to read all files imported into workspace.
Based on this code we can able to design custom File Open Dialog.
try {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IPath location = root.getLocation();
File currentDir = new File(location+"\\.metadata\\.plugins\\org.eclipse.core.resources\\.projects");
if(currentDir.isDirectory())
{
String[] files = currentDir.list();
for(int i = 0;i<files.length;i++){
String locfile = location+"\\.metadata\\.plugins\\org.eclipse.core.resources\\.projects\\"+files[i]+"\\.location";
File locationfile = new File(location+"\\.metadata\\.plugins\\org.eclipse.core.resources\\.projects\\"+files[i]+"\\.location");
if(locationfile.isFile()) {
if(isInWorkSpace(locfile)){
//if this condition is true the imported projects available in current workspace
}else{
// if it is not there in workspace then the path will be availble location file inside this project folder
String locactpath = getFileActualPath(locfile,files[i]);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
getFileActualPath
private String getFileActualPath(String locfile,String projectname) throws IOException {
FileInputStream fstream = new FileInputStream(locfile);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
if(strLine.contains("?file")){
int startindex = strLine.indexOf("file")+6;
int endindex = strLine.lastIndexOf(projectname)+projectname.length();
return strLine.substring(startindex, endindex);
}
}
//Close the input stream
in.close();
return null;
}
isInWorkSpace
public static boolean isInWorkSpace(String file) throws IOException {
FileInputStream fstream = new FileInputStream(file);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
if(strLine.contains("?file")){
return false;
}
}
//Close the input stream
in.close();
return true;
}
I'm still not sure exactly what you are trying to do, but here are some hints that will hopefully help.
From the Eclipse side, there is API which can help you navigate the workspace's resource tree. The resource visitor pattern is useful.
IWorkspace ws = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = ws.getRoot();
root.accept(new IResourceProxyVisitor() {
public boolean visit(IResourceProxy proxy) throws CoreException {
if (proxy.getName().equals("my name"))
System.out.println("found it");
return true;
}
}, IResource.DEPTH_INFINITE);
Note you can also get the resource's location in the file-system by calling IResource#getLocation.
If you have a path in the filesystem and want to retrieve the workspace object used to represent that path, then you can use IWorkspaceRoot#findFilesForLocation(URI)