Showing posts with label SWT. Show all posts
Showing posts with label SWT. Show all posts

Thursday, June 8, 2017

Programmatically executing a command in eclipse


If you know the eclipse command and you want to execute that in the programmatical way, you need to use IHandlerService.

Below is the example to perform toggle full-screen command from eclipse.


IHandlerService handlerService =
 (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);

if (handlerService == null)
{
                 return null;
}

try
{
   handlerService.executeCommand("org.eclipse.ui.cocoa.fullscreenWindow", null);
}
catch (Exception ex)
{
//log exception

}

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..

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();



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.”




Wednesday, November 26, 2014

StackLayout to switch between Composites

For example, If your dialog is having 2 radio buttons and based on the radio button selection you wanted to change the composite area. So rather than disposing the composite every time and recreating based on the selection of a radio button, we can manage this through stacklayout.

As per eclipse doc,This Layout stacks all the controls one on top of the other and resizes all controls to have the same size and location. The control specified in topControl is visible and all other controls are not visible. Users must set the topControl value to flip between the visible items and then call layout() on the composite which has the StackLayout.


public static void main(String[] args) {
                Display display = new Display();
                Shell shell = new Shell(display);
                shell.setLayout(new GridLayout());
        
                final Composite parent = new Composite(shell, SWT.NONE);
                parent.setLayoutData(new GridData(GridData.FILL_BOTH));
                final StackLayout layout = new StackLayout();
                parent.setLayout(layout);
                final Button[] bArray = new Button[10];
                for (int i = 0; i < 10; i++) {
                        bArray[i] = new Button(parent, SWT.PUSH);
                        bArray[i].setText("Button "+i);
                }
                layout.topControl = bArray[0];
        
                Button b = new Button(shell, SWT.PUSH);
                b.setText("Show Next Button");
                final int[] index = new int[1];
                b.addListener(SWT.Selection, new Listener(){
                        public void handleEvent(Event e) {
                                index[0] = (index[0] + 1) % 10;
                                layout.topControl = bArray[index[0]];
                                parent.layout();
                        }
                });
        
                shell.open();
                while (shell != null && !shell.isDisposed()) {
                        if (!display.readAndDispatch())
                                display.sleep(); 
                }       
        }


http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet249.java


This is something new learning for me!!

Monday, April 7, 2014

Creating an executable JAR file for SWT Plug-in - Runnable JAR

You would have developed a small utility application, and now you wanted to create an executable JAR for that application.
Basically, end user should be able to launch the application by just double clicking on that.

Eclipse provides an option called “Runnable JAR” in the export menu items to perform this.






This will generate Plugin_Refactor_x86_1.0.jar file, including with all the required swt jars. 

Since swt native jars will be platform specific, we need to generate 32 bit or 64 bit files separately.