Eclipse RCP - refresh PackageExplorerPart programmatically - java

I'm writing an eclipse plugin which contributes to the standard workbench and one action creates a new file under the current project. My problem is though, that the explorer does not refresh when the file is created in the action. What I tried so far:
((PackageExplorerPart)part).refresh(treeSelection); //where the selection is the root project
((PackageExplorerPart)part).getTreeViewer().refresh;
Both are called right after the resource is created. What am I missing? Maybe the resource is not yet merged with the explorer's model? The manual refresh reveals the file...

It depends on how you create the new file. If it is created directly in the filesystem, i.e. without using Eclipse's IResource API, you should refresh the corresponding IResource. For example, as described in refreshLocal(). That should be enough.

Related

Can't use the downloaded java file

I am trying to write some code to use class from a downloaded java file. I saved them in the same folder and I can open the file with Eclipse. I noticed that there is a hollow "J" there, then I researched related problems online and knew that this is caused by the build path of files. Those methods told me to open the "properties" in "Project" section, but the icon is just grey and I cannot do anything with it!
How to solve this problem?
Eclipse does not automatically refresh changed content in the file system. You have to refresh the workspace whenever you edit/delete/add files directly in the file system. Right click on the project (or the subfolder) you want to refresh and choose 'refresh' (or F5).

Create duplicate web project in eclipse from WAR

I can't seem to get this to work. I have created a project using the standard method in Eclipse. I have another working project from which I created a WAR file. I want to create a duplicate project, so that I can style the UI differently from the other one and have the back-end work the same way as the previous project and compare.
I have tried this (importing from an archive file) method and this one (importing from a WAR file) and no cigar.
The second method gives me this error:
A project already exists with this name.
I am sure I am doing something wrong, but am at a loss as to what. Thanks for any ideas in the mean time.
I have tried importing from a WAR file option and it works perfectly. When you reach the following screen (see screenshot below), you have to give another name to the clone in the Web project: field and Eclipse will create a new project on workspace.

Intelli-J: cannot create class-file?

This is honestly a tiny problem but it's keeping me from proceeding with my small Java practice app.
I'm currently practicing making an API call in Java. I was trying to create a simple class file called "Film". However, when I try to create it as a class-file, Intelli-J keeps telling me that it is unable to create a class-file. I'm trying to set this file up in a folder called "models". All of these class files are going to be utilized as part of an app that performs API calls.
Is there some naming convention I haven't followed? I've been Googling but haven't really found anything.
More directly, based on what #Vishal Jumani touched on, you need to tell IntelliJ what directories are 'source' or 'test' directories.
A directory structure you can use as an example, but you can use whatever you wish.
Right click on the directory you wish to mark as 'root' --> find 'Mark Directory As' --> Select 'Sources Root'
Now you should be able to add java classes in the directory without IntelliJ interrupting you!
This would usually be because of the way your project is setup. It may not be setup correctly to indicate where your source code is.
To setup your project, in the Project Tab, click on the top most folder and select F4. This should bring up the Project Settings dialog window.
Now click on Modules in the LHS, and then select the Sources Tab on the RHS
Select your src folder and click on the Sources button, to indicate this is your source folder. Now IntelliJ is aware that this is where your code is.
Click on Apply and OK. This should close the Dialog Window.
Now right click on the src folder and then select New -> Java Class to create your Java class

How to force a refresh on the Project Explorer view?

I created a Wizard that when finished, adds two files at the Project Explorer.
One of them should be hidden, but when I press the Finish button at my wizard, Eclipse doesn't refresh the view automatically and it keeps showing the file. It just hide it when I press F5.
There's a way to force it to refresh the Project Explorer, right after I finish the wizard?
Another option is in eclipse
Go to Windows-->Preferences-->General-->Workspace--> and check the box for Refresh Automatically ("refresh using native hooks or polling")
If this option is turned on then the workspace resources will be synchronized with their corresponding resources in the file system automatically.
Note: This can potentially be a lengthy operation depending on the number of resources you have in your workspace.
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.platform.doc.user/reference/ref-9.htm
It sounds like you aren't using Eclipse resources API to make file system changes. You have two options:
The best option is to use Eclipse resource API instead of java.io when working with contents of Eclipse projects. See org.eclipse.resources plugin. Start with ResourcesPlugin.getWorkspace().getRoot().getProject( "name" ). Use IProject, IFile and IFolder API.
Alternatively, especially if your wizard is invoking code that isn't eclipse-aware, you need to invoke refresh after you are sure that all java.io based file system operations have been completed. Use refreshLocal() method, which is available on IProject, IFile and IFolder classes. For instance, the following snippet refreshes all contents of a given project. This is typically an overkill, so you will want to narrow down the scope as much as possible before invoking refresh.
ResourcesPlugin.getWorkspace().getRoot().getProject( "proj" ).refreshLocal( IResource.DEPTH_INFINITE, new NullProgressMonitor() );
Another option is to use "External tools" to call your tool, and check options to refresh and maybe compile target project/s in "Refresh" and "Build" tabs.
Run -> External Tools -> Externa tools configuration

Reload Class When Dependent Resource Changes in Spring

You know how Eclipse reloads classes automatically running in Tomcat when resource files they depend on such as spring context files are updated so that you don't have to re-start Tomcat? How do I make a class dependent on a resource file so that Eclipse re-publishes it when the resource file changes? Eclipse would re-publish Spring classes when their dependent resource files are updated, but would not re-publish mine.
I'm not sure if it's Spring that's doing this for you. Are you developing in an IDE like Eclipse or Netbeans? What might be happening is as you make code changes your IDE is 'publishing' (i.e. re-deploying) your app code, so the app re-initializes every time the IDE publishes it, giving the illusion that Spring is reloading context files as they change.
This is useful with a small app but get's very annoying the larger your app gets.
If you're looking for this behavior for development, take a look at JRebel:
http://www.zeroturnaround.com/jrebel/
You say that you want the class to be republished ? Do you mean that you want the contents of the bean in the application context to be updated ? If that's the case, then what you can do is the following :
1) Write a file system monitor to monitor the resource(s) for changes. There's an example on google code
2) Have that file system monitor fire a custom Spring ApplicationEvent whenever the file / resource changes, providing that event the information for the resource, if necessary. ie the file name, the previous modified time, the last modified time, etc.
3) Have the bean you want to be updated implement ApplicationEventListener and reload the resource when it catches your file system monitor event.
A simple but working workaround can be to add an ant builder to your project. Steps to take:
read the article
here
http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.platform.doc.user/gettingStarted/qs-93_project_builder.htm
create a simple ant file, that
contain a target, which touches
(simply changes the date of
modification) the dependent classes,
but nothing else (if you need help in it, let me know). Eclipse will provide you variables inside the script. You can print them easily with task. You will see the list in console.
right click on the project, and
press properties
open "builders" tab in properties
add your ant script as a builder to
the project
restrict the set of resources this
ant builder is called for. This can
be done in "build options" tab in ant
builder options. This way your
project will be fast, and the ant
script will only run for the changes
of the property file
set the set of resources (classes depending on properties) to refresh after running the ant script in "Refresh" tab
Set your ant script target to be called for "Auto build" in "target" tab. Others like after and before clean and manual build should be empty
You may redirect log of the ant script to file if you want. Otherwise it will open console view.
move your builder to be the first in the list of builders, since it must run before java builder
This is a workaround, and should work. It will have no bad side effect, since the content of java file will not change, and it will not affect the version control system, as well as the whole thing is workspace independent, if the ant script is in your project.

Categories

Resources