Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Types of Shell creation in Eclipse framework

 

Types of Shell creation in Eclipse framework

Introduction

This article gives you an insight into the creation of different types of SWT Shell. I have received mails from my friends and juniors asking about how to create your own independent shell. Some have asked me that “is it possible to create a dialog box without any default minimize, maximize and close button”. One of my juniors asked me about how to handle the event while closing the dialog window. Sometimes it becomes necessary to handle the event while closing the window and user should be notified whether to close the window. In case of project or product development requirements come in such a manner that you have to create your own shell by placing many UI controls.

Technicalities
Eclipse framework provides better environment for the creation of user interface for the desktop application development. Before developing any shell, you have to think whether your UI is intuitive or not. A developer can create many types of UI but it matters whether your application can attract the customer. At the time of creation of shell in eclipse, you have to look into factors related to shell creation, placing of UI controls, proper layout in a shell and appropriate event handling for the shell and the UI controls. However let us have a look into the following code where you will find different type of shell and the use of shell listener.

package com.core.plugin.shell;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

/**
* This class is used to display the different styles of Shell creation
* @author Debadatta Mishra(PIKU)
*
*/
public class ShellStyle
{
/**
* This method is used to create default listener for a shell.
* This listener is used to drag the shell from one position
* to another of your display monitor.
*
* @param shell of type {@link Shell}
*/
public static void addDefaultShellListener( final Shell shell )
{
try
{
Listener shellListener = new Listener()
{
int startX, startY;
public void handleEvent(Event e)
{
//For the Escape key to close the shell
if (e.type == SWT.KeyDown && e.character == SWT.ESC)
{
shell.dispose();
}
//To track the x and y position
if (e.type == SWT.MouseDown && e.button == 1)
{
startX = e.x;
startY = e.y;
}
//Track the position of x and y and set the location of the shell accordingly
if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0)
{
Point p = shell.toDisplay(e.x, e.y);
p.x -= startX;
p.y -= startY;
shell.setLocation(p);
}
}
};
shell.addListener(SWT.KeyDown, shellListener);
shell.addListener(SWT.MouseDown, shellListener);
shell.addListener(SWT.MouseMove, shellListener);
}
catch( Exception e )
{
e.printStackTrace();
}
}

public static void main(String[] args)
{
final Display display = new Display ();
/*
* Uncomment the line one by one and see the creation of Shell
*/
// final Shell shell = new Shell (display, SWT.TITLE );//Shell with Title bar
// final Shell shell = new Shell (display, SWT.CLOSE );//Shell with close
// final Shell shell = new Shell (display, SWT. MIN );//Shell with close and minimize button with no maximize button
// final Shell shell = new Shell (display, SWT. MAX );//Shell with close and maximize button with no minimize button
// final Shell shell = new Shell (display, SWT. NO_TRIM );//Shell with no button and a complete blank screen
// final Shell shell = new Shell (display, SWT. RESIZE );//Shell with default resizing window
// final Shell shell = new Shell (display, SWT. ON_TOP );//Shell with blank screen with a line border
// final Shell shell = new Shell (display, SWT.TOOL );//Shell with blank screen
// final Shell shell = new Shell (display, SWT.APPLICATION_MODAL );//Shell with modality similar to  SWT. ON_TOP
// final Shell shell = new Shell (display, SWT.MODELESS );//Shell with modality similar to  SWT. ON_TOP
// final Shell shell = new Shell (display, SWT.PRIMARY_MODAL );//Shell with modality similar to  SWT. ON_TOP
// final Shell shell = new Shell (display, SWT.SYSTEM_MODAL );//Shell with modality similar to  SWT. ON_TOP
// final Shell shell = new Shell (display, SWT.ABORT ); //Shell with default horizontal scroll bar
// final Shell shell = new Shell (display, SWT.Activate  ); //Shell with default horizontal scroll bar
// final Shell shell = new Shell (display, SWT.ALPHA   ); //Shell with blank screen
// final Shell shell = new Shell (display, SWT.ALT ); //Shell with blank screen with a line border
// final Shell shell = new Shell (display, SWT.Arm  ); //Shell with blank screen with a line border
// final Shell shell = new Shell (display, SWT.ARROW ); //Shell with blank screen with a line border
// final Shell shell = new Shell (display, SWT.BALLOON ); //Shell with blank screen with a line border
// final Shell shell = new Shell (display, SWT.TRANSPARENCY_ALPHA ); //Shell with blank screen with a line border
// final Shell shell = new Shell (display, SWT.SHADOW_IN ); //Shell with blank screen with a line border
// final Shell shell = new Shell (display, SWT.SHADOW_ETCHED_IN ); //Shell with a rectangular border
// final Shell shell = new Shell (display, SWT.HORIZONTAL |  SWT.VERTICAL); //Shell with horizontal and vertical scroll bar
final Shell shell = new Shell (display, SWT.BORDER); //Shell with border
Label label = new Label( shell , SWT.NONE );
label.setText("Press Escape key to close the shell");
label.setBounds(90, 10, 200, 30);
addDefaultShellListener(shell);

shell.setSize(400, 200);
shell.open ();
while (!shell.isDisposed ())
{
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}

In the above program,uncomment the commented lines and see the effect of shell creation. There is a default listener attached to the shell, where you can drag the shell from one place another of your monitor. User can press key “Escape” to close the shell.

package com.core.plugin.shell;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
* This class is used to display the use of shell listener
* If the user closes the window, a confirmation popup message
* will be displayed and it will work accordingly.
* @author Debadatta Mishra(PIKU)
*
*/
public class CloseShell
{
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell( display , SWT.CLOSE );
//Add the shell listener to handle the close event
shell.addShellListener( new ShellAdapter()
{
public void shellClosed(ShellEvent se)
{
boolean flag = MessageDialog.openConfirm(new Shell(), "Confirmation",
"Are you sure to close it ?");
if( flag )
se.doit = true;
else
se.doit = false;
return;
}
}
);

shell.setSize(400, 200);
shell.open ();
while (!shell.isDisposed ())
{
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}

In this above program, a shell has been created and there is a shell listener which provides notification to the user while closing the dialog shell.

package com.core.plugin.shell;

import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.bindings.keys.SWTKeySupport;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

/**
* This class is used to create a blank shell with border.
* It can be used as a normal and a customized dialog shell.
* This blank shell does not contain any windows specific
* close, minimize and maximize button. This class defines
* how to use a default shell listener to close the shell.
* This class provides a better mechanism to implement the
* key listener. You can have a differenet key mapping for
* close operation. It is also possible to drag the blank
* shell to any position of your monitor display.
*
* @author Debadatta Mishra(PIKU)
*
*/
public class CustomApplicationShell
{
/**
* String mapping to close the shell.
*/
private static final String CLOSE_SHELL_KEY = "CTRL+X";
/**
* String message for shell message.
*/
private static final String SHELL_MESSAGE = " Shell with no specific button and better key listner implementation";

/**
* This method is used to handle the key operation
* defined by you. In this method, if the user presses
* "CTRL+X", or "Escape" key of your key board the shell
* will be closed. The main feature of this method is
* how KeyStroke has been used to track the key event.
*
* @param e of type {@link Event}
* @param shell of type {@link Shell}
* @author Debadatta Mishra(PIKU)
* <a href="mailto:debadatta.mishra@gmail.com">Contact me</a>
*/
public static void handleKeys( Event e , Shell shell )
{
try
{
int accelerator = SWTKeySupport
.convertEventToUnmodifiedAccelerator(e);
KeyStroke keyStroke = SWTKeySupport
.convertAcceleratorToKeyStroke(accelerator);
if( CLOSE_SHELL_KEY.equalsIgnoreCase(keyStroke.format()))
shell.dispose();
}
catch( Exception ex )
{
ex.printStackTrace();
}
}

/**
* This method is used to add the default listener
* for a defined shell. In this shell listener, you can
* move the shell to any location of your monitor. User
* can press the key "ESCAPE" to close the shell.
* @param shell of type {@link Shell}
* @author Debadatta Mishra(PIKU)
*/
public static void addDefaultShellListener( final Shell shell )
{
try
{
Listener shellListener = new Listener()
{
int positionX, positionY;
public void handleEvent(Event e)
{
//To track CTRL+X key press
if( e.type == SWT.KeyDown )
{
handleKeys(e,shell);
}
//To track whether "ESCAPE" key is pressed
if (e.type == SWT.KeyDown && e.character == SWT.ESC)
{
shell.dispose();
}
//To track the first button of your mouse click.
if (e.type == SWT.MouseDown && e.button == 1)
{
positionX = e.x;
positionY = e.y;
}
//To track the mouse movement
if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0)
{
Point p = shell.toDisplay(e.x, e.y);
p.x -= positionX;
p.y -= positionY;
shell.setLocation(p);
}
}
};
//Add all the listeners to the shell
shell.addListener(SWT.KeyDown, shellListener);
shell.addListener(SWT.MouseDown, shellListener);
shell.addListener(SWT.MouseMove, shellListener);
}
catch( Exception e )
{
e.printStackTrace();
}
}

/**
* Main method to execute the test
* @author Debadatta Mishra(PIKU)
*/
public static void main(String[] args)
{
final Display display = new Display ();
final Shell shell = new Shell (display, SWT.BORDER);
//Method to add all the shell listener
addDefaultShellListener(shell);
Label lbl = new Label( shell , SWT.None);
lbl.setText(SHELL_MESSAGE);
lbl.setBounds(10, 10, SHELL_MESSAGE.length()*8, 20);
shell.setSize(400, 200);
shell.open ();
while (!shell.isDisposed ())
{
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}

In this above program, you have to create specific to your application requirement. The above shell creation provides a blank shell with no default buttons. User can press either “Escape” or “CTRL+X” to close the window. In this above program you will get to know how to use the key or key sequence to handle the appropriate key listner.

Assumptions
I assume that reader of this article has
Exposure to eclipse plugin development
Knowledge on Java language
Knowledge on running programs in Eclipse editor

Test Case details
I have tested the above program in the following conditions.
OS Name : Windows Vista
Eclipse API : 3.2
Java : 1.6.0_16
Java Editor : Eclipse 3.2

Conclusion
I hope that you will enjoy my article. This article does not bear any commercial significance , it is only meant for learning and for novice developers. In case of any problem or errors , feel free to contact me in the email debadatta.mishra@gmail.com .