3

I have a project in eclipse workspace.

enter image description here

When I open this project as IJavaProject using the following code, I have the project "not open".

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject orig = root.getProject(this.projectName);
orig.open(null /* IProgressMonitor */);
orig.refreshLocal(IResource.DEPTH_ZERO, null);
this.javaProject = JavaCore.create(orig);

I opened the IProject with open() method, and refreshed manually using refreshLocal following avoiding "resource is out of sync with the filesystem"

enter image description here

I have other resources not opened.

enter image description here

I tried "F5" to refresh the project, but it doesn't work.

What might be wrong? How can I open the resources?

ADDED

I tried to open the resource with new NullProgressMonitor(), but it didn't work.

orig.open(new NullProgressMonitor());
orig.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());

ADDED2

With this code that uses javaModel, I got the information that all of the projects in my workspace is closed. Does this mean my workspace is broken? When I opened the workspace using eclipse IDE, I could open and compile the Java code.

IJavaModel javaModel = JavaCore.create(root);
this.javaProject = javaModel.getJavaProject(this.projectName);

enter image description here

Community
  • 1
  • 1
prosseek
  • 182,215
  • 215
  • 566
  • 871

1 Answers1

3

Code style :

You should avoid 'null' as parameters when possible. This is always better for readability (and maintainability). here, the API designers have provided actual objects to say "no value" :

orig.open(new NullProgressMonitor());
orig.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());

Issue :

I believe that your problem lies in the second of these lines : you refresh the project, but not any of its members. What if you try with the INFINITE depth so that all contained files are actually refreshed?

orig.open(new NullProgressMonitor());
orig.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());

UPDATE :

Actually, this "(not open)" in the toString of your resource is not at the workspace level. Your IProject and IFiles are well opened and refreshed (you probably don't even need that if your project is already opened).

The Java model is not the same as the workspace, and java elements not being "opened" have no meaning at the workspace level, it only means that there is no buffer opened on the contents of the java element. You should not have to worry about opening the java elements, the jdt will do the work if and when needed during your manipulation of its elements.

You might want to create the whole java model instead of only what corresponds to the project though :

IJavaModel javaModel = JavaCore.create(root);
this.javaProject = javaModel.getJavaProject(this.projectName);

This might avoid strange errors later on :).

prosseek
  • 182,215
  • 215
  • 566
  • 871
Kellindil
  • 4,523
  • 21
  • 20
  • I tried with "IResource.DEPTH_INFINITE, new NullProgressMonitor()", but I still couldn't open the resource. – prosseek Jan 11 '13 at 11:45
  • Okay, your problem is actually not at the resource level :). Your project is actually properly opened, and the resources refreshed. updating the answer in a minute :). – Kellindil Jan 11 '13 at 11:54
  • Using javaModel reveals that all my projects in the workspace is not open. Does this mean there's something wrong with my workspace? I updated my OP. Thanks. – prosseek Jan 11 '13 at 13:24
  • As mentionned above, you do now have to worry about the "open" state of your files. The jdt will open them if and when needed as you use the elements. Please read the javadoc of IOpenable#open() and the associated recommandations (web version : http://www.phpeclipse.com/browser/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/core/IOpenable.java?rev=30bc011ca90a3672a588fd8179c1eae4506d5a52#L122) – Kellindil Jan 11 '13 at 13:30
  • you do now have to worry about the "open" state of your files --> I guess it should have been "do not now have to ...", and it means that I don't need the "orig.open()" anymore. – prosseek Jan 11 '13 at 14:17
  • yes, that was "do not have to". orig is an IProject, an Eclipse resource, and if it is initially opened you never use to open it in the first place (open/closed projects can be seen in the Eclipse ui : right-click the project, you have a "open" or "close" action on it). Workspace resources that can be opened are only the projects, this has no meaning on the files. the "not open" you see thus does not come from the IFile (the eclipse resource), but from the ICompilationUnit (the Java -JDT- representation of that resource).-continued below – Kellindil Jan 11 '13 at 14:33
  • previous comment continued- "open" compilation unit are java elements that have an opened buffer on the file content. This is an internal state managed by the JDT itself and that you do not need to worry about. – Kellindil Jan 11 '13 at 14:33