Wednesday, January 25, 2017

Adding filter to tree viewer



PatternFilter filter = new PatternFilter();
FilteredTree tree = new FilteredTree(sash, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL, filter, true);
TreeViewer viewer = tree.getViewer();

//go on..

Tuesday, January 10, 2017

Removing unwanted preference pages from eclipse


To remove unwanted preferences from your eclipse rcp app, we can do the following.


As part of your App MyRCPWorkbenchAdvisor#postStartup() method, you need to invoke the following code with the preference page id.


private void removeUnwantedPreferences()
{
PreferenceManager pm = PlatformUI.getWorkbench().getPreferenceManager();
IPreferenceNode root = getRoot(pm);
if (root != null)
{
removePrefsPage(root"com.windows.preference.mypage.id"); 
}
}


//Identify preference node
private IPreferenceNode getRoot(PreferenceManager pm)
{
try
{
Method m = PreferenceManager.class.getDeclaredMethod("getRoot", (Class[]) null); 
m.setAccessible(true);
return (IPreferenceNode) m.invoke(pm);
}
catch (Exception e)
{
IdeLog.logWarning(TitaniumCorePlugin.getDefault(), e);
}
return null;
}


//remove preference id from the preference node
private void removePrefsPage(IPreferenceNode root, String id)
{
for (IPreferenceNode node : root.getSubNodes())
{
if (node.getId().equals(id))
{
root.remove(node);
}
else
{
removePrefsPage(node, id);
}
}

}



This seems to be work without any issues.

However, I could see other direct approach but that doesn't seem to work well in all scenarios.

PreferenceManager pm = PlatformUI.getWorkbench().getPreferenceManager();
pm.remove("com.windows.preference.mypage.id");


Sunday, January 8, 2017

Tip: Eclipse plugins error - prerequisite build errors

I've faced this issue many times with eclipse.

Even though we have all the plugins and their dependencies defined correctly, still eclipse shows the errors by saying the prerequisite has to be build first.

It's frustrating since I have not made any changes in the project from the last time.

Okay, so the solution for this is:

1. Identify the root prerequisite plugin which eclipse is complaining about.
2. Close that root plugin
3. Re-open it
4. Do clean build for all projects



Thursday, September 15, 2016

How to get your colleague Pull request into your system for testing

For my own reference:

Example:
git remote add coworker https://github.com/coworker/titanium_studio.git
git fetch coworker
git checkout --track coworker/TIMOB-23628

If your co-worker has made some more changes, then you pull the latest changes in the below way.

git checkout TIMOB-23628

git pull


To push it back to your colleague branch
git puh coworker TIMOB-23628




Another way would be

To fetch a remote PR into your local repo,
git fetch origin pull/ID/head:BRANCHNAME

where ID is the pull request id and BRANCHNAME is the name of the new branch that you want to create. Once you have created the branch, then simply

git checkout BRANCHNAME
Reference: https://stackoverflow.com/questions/27567846/how-can-i-check-out-a-github-pull-request-with-git

Thursday, September 8, 2016

Best Practice: Never ever execute long running operations in the Display.asyncExec() also

//The thread which calls this method is suspended until the runnable completes.
Display.getDefault().syncExec(new Runnable()
{
public void run()
{
//Do only UI operation
}
});


//The caller of this method continues to run in parallel
Display.getDefault().asyncExec(new Runnable()
{
public void run()
{
//Do only UI operation
}

});



I've seen most of the people will misuse Display.getDefault().asyncExec() by thinking that it will not impact the application performance. But the important thing to remember here is, SWT is a single threaded model - Yes, it's a single-threaded UI model. Any UI operation which you do, will happen through Display main thread. 


Example:

Display.getDefault().asyncExec(new Runnable()
{
public void run()
{
   //step 1: network connection involvement
                               
   //step 2: UI code to execute - example: opening a browser

}

});



From above, it has non-UI and UI code. If accessing the network takes time, UI thread will be blocked for a long time so that no other thread can able to get an access to execute other UI code.

That means, at any point of time - only one UI thread will be there i.e, Display thread.


To resolve this kind of things, we need to use jobs and invoke only necessary code in the UI thread.


new Job("Browser opening job...")
{
@Override
protected IStatus run(IProgressMonitor monitor)
{
//step 1: network connection involvement
Display.getDefault().asyncExec(new Runnable()
{
public void run()
{
 //step 2: UI code to execute - example: opening a browser                  
         
}
});

return Status.OK_STATUS;
}
}.schedule();



Best Practice: Always check for Control disposed in Display.asyncExec()



Display.getDefault().asyncExec(new Runnable()
{
public void run()
{
//Always check for whether the control is disposed.
if (!progressComposite.isDisposed())
{
//perform UI operation.
}
}
});


Reason being, when we say asyncExec thread - it will execute on the next reasonable opportunity. But by that time the control would have been disposed.



Wednesday, September 7, 2016

HttpURLConnection setConnectTimeout issues

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Give it a 4 seconds delay before deciding that it's a dead connection
connection.setConnectTimeout(10000);
connection.setRequestMethod("HEAD"); // Don't ask for content
connection.setAllowUserInteraction(false);
connection.connect(); 



Problem here is, application can hung for more than 10 sec even after configuring the timeout for connection.

How that can happen?

connection.setConnectTimeout() is only for configuring timeout to accept the connection, but after accepting the request server might take a long time to respond to you due to various issues at the server end. 

As per the doc - “timeout for opening a communications link to the resource referenced by this URLConnection”.

This kind of things will lead to application hung if you’re waiting for the response.


To resolve this - we should also set the connection.setReadTimeout(5000). 

As per doc - “timeout when reading from Input stream when a connection is established to a resource.”