import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
/**
* A simple SWT based program to handle the moving of an composite widget (Tomcat)
* on a canvas. It includes the code for,
*
* - handling the movement of a widget by the user using the mouse
* - pull-down menus
* - popup menus associated with a widget
* - use of images on a label
*
* @author Asanga Udugama (adu@comnets.uni-bremen.de)
*
*
*/
public class SWTTest implements Runnable {
        Shell shell;
        Display display;
        Composite displayComp;
        int beginX, beginY, locX, locY;
        boolean mouseDown;
        Label titleLabel, imageLabel;
        /**
        * Main thread method handling the view.
        *
        */
        public void run() {
                ImageData imageData;
                Image image;
               
                display = new Display();
                shell = new Shell(display, SWT.SHELL_TRIM);
                shell.setLocation(1, 1);
                shell.setMaximized(true);
                shell.setText("SWT Test");
               
                // setup title bar image
                imageData = null;
                try {
                        imageData = new ImageData(getClass().getClassLoader()
                                        .getResource("images/eclipse.jpg").openStream());
                } catch(Exception e) {
                        System.out.println(e.toString());
                        System.exit(1);
                }
                image = new Image(display, imageData);
                shell.setImage(image);
                // setup pull down menus
                setupDropDownMenus();
               
                //
                setupDisplayItem();
                // start the display loop
                shell.open();
                while(!shell.isDisposed()) {
                        if(!display.readAndDispatch())
                                display.sleep();
                }
                image.dispose ();
                display.dispose();
        }
       
        /**
        * Method to build the drop down menus of the view.
        *
        */
        void setupDropDownMenus() {
                Menu mainMenuBar;
                MenuItem actionsMenuItem;
                Menu actionsMenu;
                MenuItem quitMenuItem;
                // setup the main menu bar
                mainMenuBar = new Menu(shell, SWT.BAR);
                shell.setMenuBar(mainMenuBar);
                // setup the actions pull down
                actionsMenuItem = new MenuItem(mainMenuBar, SWT.CASCADE);
                actionsMenuItem.setText("Actions");
                actionsMenu = new Menu(shell, SWT.DROP_DOWN);
                actionsMenuItem.setMenu(actionsMenu);
                // setup object appear menu item and event handler
                quitMenuItem = new MenuItem(actionsMenu, SWT.PUSH);
                quitMenuItem.setText("Make Tomcat Visible");
                quitMenuItem.addListener(SWT.Selection, new Listener() {
                        public void handleEvent(Event e) {
                                displayComp.setVisible(true);
                        }
                });
                               
                // setup quit menu item and event handler
                quitMenuItem = new MenuItem(actionsMenu, SWT.PUSH);
                quitMenuItem.setText("Quit");
                quitMenuItem.addListener(SWT.Selection, new Listener() {
                        public void handleEvent(Event e) {
                                display.dispose();
                                System.exit(0);                               
                        }
                });
        }
        /**
        * Method to create the composite widget shown on the view that is able to be moved
        * using the mouse.
        *
        */
        void setupDisplayItem() {
                Point compSize, labelSize;
                ImageData imageData;
                Image image;
                // setup the composite
                locX = 10;
                locY = 10;
                displayComp = new Composite(shell, SWT.BORDER);
                displayComp.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
                displayComp.setLocation(locX, locY);
                displayComp.setSize(150, 150);
                displayComp.addMouseListener(new CommonMouseHandler());
                displayComp.addMouseMoveListener(new CommonMouseHandler());
                displayComp.setMenu(buildPopupMenu());
                displayComp.setLayout( new FillLayout());
                                       
                // setup title label
                titleLabel = new Label(displayComp, SWT.BORDER);
                titleLabel.setText("Tomcat");
                titleLabel.pack();
                titleLabel.addMouseListener(new CommonMouseHandler());
                titleLabel.addMouseMoveListener(new CommonMouseHandler());
                titleLabel.setMenu(buildPopupMenu());
                compSize = displayComp.getSize();
                labelSize = titleLabel.getSize();
                titleLabel.setLocation((compSize.x - labelSize.x) / 2,        5);
                // setup image label
                imageData = null;
                try {
                        imageData = new ImageData(getClass().getClassLoader().getResource("images/tomcat.gif").openStream());
                } catch(Exception e) {
                        System.out.println(e.toString());
                        System.exit(1);
                }
                image = new Image(display, imageData);
                imageLabel = new Label(displayComp, SWT.BORDER);
                imageLabel.setImage(image);
                imageLabel.pack();
                imageLabel.addMouseListener(new CommonMouseHandler());
                imageLabel.addMouseMoveListener(new CommonMouseHandler());
                imageLabel.setMenu(buildPopupMenu());
                compSize = displayComp.getSize();
                labelSize = imageLabel.getSize();
                System.out.println(labelSize.x);
                imageLabel.setLocation((compSize.x - labelSize.x) / 2, 30);
        }
        /**
        * Method to build the popup menu items for making the widget
        * dissapear.
        */
        public Menu buildPopupMenu() {
                Menu popupmenu;
                MenuItem actionItem;
                       
                // setup main menu
                popupmenu = new Menu(shell, SWT.POP_UP);
                // setup make visible menu items & handler
                actionItem = new MenuItem(popupmenu, SWT.PUSH);
                actionItem.setText("Make Tomcat Invisivle");
                actionItem.addListener(SWT.Selection, new Listener() {
                        public void handleEvent(Event e) {
                                displayComp.setVisible(false);
                                mouseDown = false;
                        }
                });
                // setup the cancel menu item and the even handler
                actionItem = new MenuItem(popupmenu, SWT.PUSH);
                actionItem.setText("Cancel");
                return popupmenu;                       
        }
        /**
        * An inner class that handles the mouse events on the displayed widget for
        * the purpose of handling user mouse actions on the displayed widget.
        *
        */
        class CommonMouseHandler extends MouseAdapter implements MouseMoveListener {
                /**
                * When user clicks the right mouse on a widget setup
                * for a possible movement of the widget in the view
                */
                public void mouseDown (MouseEvent event) {
                        mouseDown = true;
                        beginX = event.x;
                        beginY = event.y;
                }
                /**
                * When user releases the right mouse on a widget
                * move widget to new location.
                */
                public void mouseUp (MouseEvent event) {
                        mouseDown = false;
                        locX += (event.x - beginX);
                        locY += (event.y - beginY);
                        displayComp.setLocation(locX, locY);
                }
                       
                /**
                * When mouse is down and user moves cursor, move the widget
                * on the view.
                */
                public void mouseMove (MouseEvent event) {
                        if(mouseDown) {
                                locX += (event.x - beginX);
                                locY += (event.y - beginY);
                                displayComp.setLocation(locX, locY);
                        }
                }
        }
        /**
        * Main method starting the application.
        *
        * @param String args - command line arguments
        */
        public static void main(String args[]) {
                SWTTest swtTest;
                Thread displayThread;
                swtTest = new SWTTest();
                displayThread = new Thread(swtTest);
                displayThread.start();
                try {
                        displayThread.join();
                } catch(Exception e) {
                        System.out.println(e.toString());
                        System.exit(1);
                }
        }
}