how do I use progress monitor in eclipse plugin code? - eclipse

In my eclipse plugin, I created an utility class that does some work when I create my files, with my wizard.
I´d like to show to user some text, warning him about what´s happening, in the progress bar.
How could I do this?
Thanks a lot.
UPDATE: my wizard calls my utility class, that unzips 2 ZIP files and import 2 projects into workspace. So, I´d like to show a message after each operation, like:
Unzipping file1...
Unzipping file2...
Importing project1...
Importing project2...

In your Wizard class enable the progress monitor by calling
setNeedsProgressMonitor(true);
in the constructor.
You can then run your IRunnableWithProgress class with:
IRunnableWithProgress runnable = new MyRunnable();
getContainer().run(true, true, runnable);
This can be in the Wizard or in a WizardPage.
The IRunnableWithProgress class might look like:
private class MyRunnable implements IRunnableWithProgress
{
#Override
public void run(final IProgressMonitor monitor)
throws InvocationTargetException, InterruptedException
{
monitor.beginTask("title", progress count);
try
{
... your work
}
finally
{
monitor.done();
}
}
}

Related

Eclipse Plug-In developement, long running task

I have several UI-Components which have listeners. All these listeners invoke method dialogChanged(). My goal is to make some long processing in this method and don't let the UI freeze. According to Lars Vogel it is possible to do this with help of UISynchronize being injected during runtime. But it fails for me, field of this type is not being injected and i get a NullPointerException. Here's relevant part of my code:
#Inject UISynchronize sync;
Job job = new Job("My Job") {
#Override
protected IStatus run(IProgressMonitor arg0)
{
sync.asyncExec(new Runnable()
{
#Override
public void run()
{
updateStatus("Checking connection...");
if (bisInstallDirSelected)
bisSettingsChanged();
else
jarSettingsChanged();
}
});
return Status.OK_STATUS;
}
};
protected void dialogChanged()
{
job.schedule();
}
The methods updateStatus(String s), bisSettingsChanged() and jarSettingsChanged() interact with UI, to be presice, they use method setErrorMessage(String newMessage) of superclass org.eclipse.jface.wizard.WizardPage
I'd appreciate if somebody could tell me what I am doing wrong or suggest a better way to handle this problem.
You can only use #Inject in classes that the e4 application model creates (such as the class for a Part or a Command Handler).
You can also use ContextInjectionFactory to do injection on your own classes.
For classes where injection has not been done you can use the 'traditional' way of running code in the UI thread:
Display.getDefault().asyncExec(runnable);

Play framework 2 + JPA with multiple persistenceUnit

I'm struggling with Play and JPA in order to be able to use two different javax.persistence.Entity model associated to two different persistence units (needed to be able to connect to different DB - for example an Oracle and a MySQL db).
The problem come from the Transaction which is always bind to the default JPA persitenceUnit (see jpa.default option).
Here is two controller actions which show the solution I found to manually define the persistence :
package controllers;
import models.Company;
import models.User;
import play.db.jpa.JPA;
import play.db.jpa.Transactional;
import play.mvc.Controller;
import play.mvc.Result;
public class Application extends Controller {
//This method run with the otherPersistenceUnit
#Transactional(value="other")
public static Result test1() {
JPA.em().persist(new Company("MyCompany"));
//Transaction is run with the "defaultPersistenceUnit"
JPA.withTransaction(new play.libs.F.Callback0() {
#Override
public void invoke() throws Throwable {
JPA.em().persist(new User("Bobby"));
}
});
return ok();
}
//This action run with the otherPersistenceUnit
#Transactional
public static Result test2() {
JPA.em().persist(new User("Ryan"));
try {
JPA.withTransaction("other", false, new play.libs.F.Function0<Void>() {
public Void apply() throws Throwable {
JPA.em().persist(new Company("YourCompany"));
return null;
}
});
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
return ok();
}
}
This solution doesn't seem to be really "clean". I'd like to know if you know a better way to avoid the need to manually modify the transaction used.
For this purpose, I created a repo on git with a working sample application which shows how I configured the project.
https://github.com/cm0s/play2-jpa-multiple-persistenceunit
Thank you for your help
i met the same problem, too. too many advices are about PersistenceUnit annotation or getJPAConfig. but both them seem not work in play framework.
i found out a method which works well in my projects. maybe you can try it.
playframework2 how to open multi-datasource configuration with jpa
gud luk!

Writing to stdout from an Eclipse plugin

My aim is to extend the eclipse QuickFix component and automate the process of solving syntax errors. Basically, the QuickFix component provides a list of solutions and my task is to select the best possible fix and apply it to the buggy code. But, for now I've been requested to print the resolutions for a marker in the console. I've tried to work out a tutorial and I'm kind of stuck right now. The tutorial I've tried to workout is: http://www.informit.com/articles/article.aspx?p=370625&seqNum=21
I've first added the extension in my plugin.xml file
<extension point="org.eclipse.ui.ide.markerResolution">
<markerResolutionGenerator
markerType="org.eclipse.core.resources.problemmarker"
class="org.eclipse.escript.quickfix.QuickFixer"/>
</extension>
Then i have created the two classes QuickFixer and QuickFix.
package quickfixer;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ui.IMarkerResolution;
import org.eclipse.ui.IMarkerResolutionGenerator;
class QuickFixer implements IMarkerResolutionGenerator {
public IMarkerResolution[] getResolutions(IMarker arg0) {
try {
Object problem = arg0.getAttribute("Whatsup");
return new IMarkerResolution[] {
new QuickFix("Fix #1 for "+problem),
new QuickFix("Fix #2 for "+problem),
};
} catch(CoreException e) {
return new IMarkerResolution[0];
}
}
}
then the class QuickFix:
package quickfixer;
import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IMarkerResolution;
public class QuickFix implements IMarkerResolution {
String label;
QuickFix(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public void run(IMarker arg0) {
MessageDialog.openInformation(null, "QuickFix Demo",
"This quick-fix is not yet implemented");
System.out.println("Label: " + label);
}
}
I've managed to correct all the errors i encountered and then i have run the plugin.
I have not been able to get the label printed out in the console.Any suggestions???...
Using System.out is not a good idea. Check the relevant FAQ on why
you should avoid using standard output or standard error in your
plug-in
and use proper logging (or the debugger).

How to programmatically new a java class which implements sepecified interface in eclipse plugin development

Friends,
Now we are developing a eclipse plugin, it contains a action to generated a service interface and it's impl stub.
Now the interface is generated, I want to use eclipse JDT to create a java class which implements sepecified interface, but don't know how.
The info we have:
the interface name, the impl class name, the packagename, the java project contains them.
Thanks in advance for your kindly help.
A quick scan of how the new class wizard does it, it seems that there is no public easy to use API for this. You can have a look at org.eclipse.jdt.ui.wizards.NewTypeWizardPage.createType(IProgressMonitor) method to see how JDT itself creates new classes.
It should be possible to extend the org.eclipse.jdt.ui.wizards.NewTypeWizardPage, so you can leverage the createType() method.
Probably the minimal steps you would have to do is simply generate source content into the correctly placed IFile. ex:
public Object execute(ExecutionEvent event) throws ExecutionException {
final String PACKAGE_PATH = "z.ex/src/z/ex/go";
final String CONTENT = "package z.ex.go;\n"
+ "public class RunAway {\npublic static void main(String[] args) {\n"
+ "System.out.println(\"Run Away\");\n}\n}\n";
final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
final IResource packageResource = root.findMember(PACKAGE_PATH);
if (packageResource instanceof IFolder) {
IFolder packageFolder = (IFolder) packageResource;
final IFile file = packageFolder.getFile("RunAway.java");
try {
if (!file.exists()) {
file.create(new ByteArrayInputStream(CONTENT.getBytes()),
true, new NullProgressMonitor());
} else {
file.setContents(
new ByteArrayInputStream(CONTENT.getBytes()),
IFile.FORCE | IFile.KEEP_HISTORY,
new NullProgressMonitor());
}
} catch (CoreException e) {
e.printStackTrace();
}
}
return null;
}
See AbstractNewClassWizard for a smaller example that is similar to NewTypeWizardPage and uses some of the JDT APIs.
You can use the new class wizard to create classes.
This will prompt the user for the class name, et cetera. You can initialize the values of the wizard page. Below I am setting the source folder only (and tell the wizard that it cannot be changed, thus the second false parameter). You might want to set the interface and possible the package as well.
OpenNewClassWizardAction wizard = new OpenNewClassWizardAction();
wizard.setOpenEditorOnFinish(false);
NewClassWizardPage page = new NewClassWizardPage();
page.setPackageFragmentRoot(sourceFolder, false);
wizard.setConfiguredWizardPage(page);
wizard.run();
return (IType) wizard.getCreatedElement();
Hope that helps!
Create the entire java file using the JDT - AST. First create the AST and then write it to a java file. It might look as hefty work, but its the best one. You will have complete control.

How to write message to status line from handler class in Eclipse RCP programming

I need to change status line message from a handler class. After reading the RCP tutorial and eclipse FAQ, I finally did something like this:
HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().findView(AView.ID).getViewSite().getActionBars().getStatusLineManager().setMessage( "Ha, I'm finished");
What a long invoking chain!
Am I doing it the right way? Thanks.
From the threads I see in the forums, that looks about right.
Beware though if you have asynchronous feedback to put in this status line.
See this thread for instance.
UIJob job = new UIJob() {
public IStatus run(IProgressMonitor monitor) {
//do the long running work here
Runnable results = new Runnable() {
public void run(){
// update UI elements here;
getViewSite().getActionBars().getStatusLineManager().
setMessage("End Pasting");
}
};
display.asyncExec(results);
}
};
job.schedule();
(Note: that may be not your case, but I add this code snippet just for information)