Monday, April 13, 2015

Eclipse Spy plugins

To make spy plugins available for your run-time eclipse, add below 2 plugins in dependencies.
1. org.eclipse.pde.runtime
2. org.eclipse.pde.ui

Showing notification or popup dialog in Eclispe

Will use PopupDialog Class from eclipse Jface framework.

I am taking about something like this.











Example:

import org.eclipse.jface.dialogs.PopupDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class PopupDialogExample {

public static void main(String[] args) {
open();
}
static void open(){
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("PopupDialog");
shell.setLayout(new GridLayout());
shell.setSize(400, 300);
Button button = new Button(shell,SWT.PUSH);
button.setText("Click here!");
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
int shellStyle = PopupDialog.INFOPOPUPRESIZE_SHELLSTYLE;
boolean takeFocusOnOpen = true;
boolean persistSize = true;
boolean persistLocation = true;
boolean showDialogMenu = true;
boolean showPersistActions = true;
String titleText = "Updates Available"
String infoText = "Eclipse updates are available!";
PopupDialog  dialog = new PopupDialog(shell, shellStyle, takeFocusOnOpen, persistSize, persistLocation, showDialogMenu, showPersistActions, titleText, infoText){

@Override
protected Control createDialogArea(Composite parent) {
Composite composite = (Composite) super.createDialogArea(parent);
Label text = new Label(composite,SWT.SINGLE);
text.setText("There is a update for your plugin, please install them before proceed!");
return composite;
}
};
dialog.open();
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
}


}

Thursday, April 9, 2015

Eclipse Jobs framework best practice: Handling the long running background jobs while Eclipse shutdown

How to handle background jobs which are running silently for a long time during the Eclipse shutdown ?

Solution would be to cancel all the jobs before before plugin stops,so that jobs won't complain about the other resources availablity. If we don't do this, some times we can get some null pointer issues in the log.

In Plugin Activator class, just invoke Job.getJobManager().cancel(jobName);


import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.osgi.framework.BundleContext;

public abstract class AbstractJob extends Job
{

private String jobFamilyName;

public AbstractJob(String name, String jobFamilyName)
{
super(name);
this.jobFamilyName = jobFamilyName;
}

@Override
public boolean belongsTo(Object family)
{
return this.jobFamilyName.equals(family);
}
}

Your Actual Job Implementation:

Job coreJob = new AbstractJob("Collecting assets", "ASSETS_FAMILY")
{

@Override
protected IStatus run(IProgressMonitor monitor)
{
//TODO: some long work job

return Status.OK_STATUS;
}
};

coreJob.schedule();


In Activate Class:

public void stop(BundleContext context) throws Exception {
plugin = null;
Job.getJobManager().cancel("ASSETS_FAMILY");
super.stop(context);
}

This will cancel all the jobs who's family name is ASSETS_FAMILY.


Wednesday, April 8, 2015

Adding resource filter for project in eclipse

try
{
project.createFilter(IResourceFilterDescription.EXCLUDE_ALL | IResourceFilterDescription.FOLDERS,
new FileInfoMatcherDescription("org.eclipse.core.resources.regexFilterMatcher", //$NON-NLS-N$
"node_modules"), IResource.BACKGROUND_REFRESH, new NullProgressMonitor());
}
catch (CoreException e)
{

}


This piece code will hide the "node_modules" folder from project.

Tuesday, March 24, 2015

Registering a listener for changes in network connections and proxy from eclipse preferences

import org.eclipse.core.net.proxy.IProxyChangeEvent;
import org.eclipse.core.net.proxy.IProxyChangeListener;
import org.eclipse.core.net.proxy.IProxyService;
import org.osgi.util.tracker.ServiceTracker;

public class ProxyHelper
{

public void registerListener()
{
ServiceTracker tracker = new ServiceTracker(MyPlugin.getDefault().getBundle().getBundleContext(),
IProxyService.class.getName(), null);
tracker.open();
IProxyService proxyService = (IProxyService) tracker.getService();
proxyService.addProxyChangeListener(new IProxyChangeListener()
{

@Override
public void proxyInfoChanged(IProxyChangeEvent event)
{
// TODO: your action for proxy changed
}
});
}

}

Sunday, March 22, 2015

Tool for checking MD5 and SHA1 values for any file

Git: Updating a forked repository from the original repository

Let's take a scenario:

This is the remote repository: https://github.com/apache/mystudio.git
Your forked repository from the above:  https://github.com/kolipakakondal/mystudio.git

'development' is the branch name both in remote and forked repository.

1. Add remote repository to forked repository
>git remote add upstream https://github.com/apache/mystudio.git

2.  Fetch from remote repository .i.e upstream
> git fetch upstream

3. Rest your branch(ex: development) to the remote branch(ex: development)
> git reset --hard upstream/development

4. Pushed changes which we got it form remote repo to remote forked repo.
> git push origin  development --force