Package giggler.ui.impl

Source Code of giggler.ui.impl.TrayMenu$Callback

package giggler.ui.impl;


import giggler.ui.swt.skin.Skinner;
import giggler.ui.swt.skin.SkinningSample;

import java.io.InputStream;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;



public class TrayMenu extends PluginViewer
{
  private static final String ACTION_CONFIG = "CONFIG";
  private static final String ACTION_ABOUT  = "ABOUT";
  private static final String ACTION_EXIT   = "EXIT";

  @SuppressWarnings("unused")
  private static final Logger log = Logger.getLogger(TrayMenu.class);
 
    private TrayItem trayItem = null;

    private Menu pluginMenu = null;
    private MenuItem pluginMenuItem = null;
   
    private Callback callback = null;

   
    public TrayMenu() {

        super();
       
        Runnable trayItemDispatcher = new Runnable() {

            public void run() {

                openTray();
            }
        };

        new Thread(trayItemDispatcher).start();
    }

    @Override
    public void add(final PluginView pluginView) {

        super.add(pluginView);

        // XXX
//        for (int i=0; i < 500000; i++) {
//           
//            System.out.print(i);
//        }
       
        if (pluginMenuItem == null)
            return;
       
        pluginMenuItem.getDisplay().asyncExec(new Runnable() {

            public void run() {

                synchronized(pluginView) {
               
                    if (pluginView.getMenuItem() != null)
                        return;
                   
                    MenuItem menuItem = createMenuItem(pluginView);
                   
//                    MenuItem menuItem = new MenuItem(pluginMenu, SWT.PUSH);
////                    menuItem.setText(pluginView.getTitle()+"\t  "+pluginView.getVersion());
//                    menuItem.setText(pluginView.getTitle()+"    "+pluginView.getVersion());
//                    menuItem.setData(pluginView);
//
//                    InputStream iconStream = pluginView.getIcon16x16();
//
//                    if (iconStream != null)
//                        menuItem.setImage(new Image(pluginMenu.getDisplay(), iconStream));

                    if (pluginView.isConfigurable()) {
                       
                        menuItem.addSelectionListener(new SelectionAdapter() {

                            public void widgetSelected(SelectionEvent e) {

                                PluginView pluginView = (PluginView) e.widget.getData();
                               
                                callback.config(pluginView);
                            }
                        });
                    }

                    pluginView.setMenuItem(menuItem);
                   
                    pluginMenuItem.setEnabled(true);
                }
            }
        });
    }

    @Override
    public void remove(PluginView pluginView) {

        super.remove(pluginView);
    }

    public void register(Callback callback) {
       
        if (this.callback != null)
            throw new IllegalPluginException("a callback has already been registered");
       
        this.callback = callback;
    }
   
    // PRIVATE ///////////////////////////////////////////////////////////////////////////////////////

    public void openTray() {
       
        Display display = new Display();
//        Display display = Display.getDefault();
       
        final Tray tray = display.getSystemTray();
       
        if (tray == null)
            throw new IllegalPluginException("system tray is not available");

        final Shell shell = new Shell(display);

        InputStream inputStream = getClass().getResourceAsStream("tray-logo-24x24.png");
        final Image image = new Image(display, inputStream);

        trayItem = new TrayItem(tray, SWT.NONE);
        trayItem.setImage(image);
       
        trayItem.addListener(SWT.MenuDetect, new Listener() {

            public void handleEvent(Event event) {

              System.out.println("START");
                openTrayMenu(shell);
//                trayMenu.setVisible(true);
            }
        });

        while (!shell.isDisposed()) {
           
            if (!display.readAndDispatch())
                display.sleep();
        }

        image.dispose();
        display.dispose();

        pluginMenuItem = null;
        pluginMenu = null;
        trayItem = null;

//        callback.exit();
    }
   
    private void openTrayMenu(final Shell shell) {
       
        final Menu trayMenu = new Menu(shell, SWT.POP_UP);

        pluginMenuItem = new MenuItem(trayMenu, SWT.CASCADE);
        pluginMenuItem.setText("Plugins");
       
        trayMenu.addMenuListener(new MenuAdapter() {

            public void menuShown(MenuEvent e) {

                pluginMenuItem.setEnabled(getPluginViews().size()>0);
               
                SelectionAdapter selectionListener = new SelectionAdapter() {

                    public void widgetSelected(SelectionEvent e) {

                        PluginView pluginView = (PluginView) e.widget.getData();
                       
                        callback.config(pluginView);
                    }
                };

                for (PluginView pluginView : getPluginViews()) {

                    if (pluginView.getMenuItem() == null) {

                        MenuItem menuItem = createMenuItem(pluginView);
                       
                        if (pluginView.isConfigurable())
                            menuItem.addSelectionListener(selectionListener);
                       
//                        MenuItem menuItem = new MenuItem(pluginMenu, SWT.PUSH);
//                        menuItem.setText(pluginView.getTitle()+"\t  "+pluginView.getVersion());
////                        menuItem.setText(pluginView.getTitle()+" ... "+pluginView.getVersion());
//                        menuItem.setData(pluginView);
//                        menuItem.addSelectionListener(selectionListener);
//                       
//                        InputStream iconStream = pluginView.getIcon16x16();
//                       
//                        if (iconStream != null)
//                            menuItem.setImage(new Image(display, iconStream));
                       
                        pluginView.setMenuItem(menuItem);
                    }
                }
            }
        });
       
        pluginMenu = new Menu(trayMenu);
        pluginMenuItem.setMenu(pluginMenu);
       
        trayMenu.setDefaultItem(pluginMenuItem);

        final MenuItem configMenuItem = new MenuItem(trayMenu, SWT.NONE);
        configMenuItem.setData(ACTION_CONFIG);
        configMenuItem.setText("Configure");

        final MenuItem aboutMenuItem = new MenuItem(trayMenu, SWT.PUSH);
        aboutMenuItem.setData(ACTION_ABOUT);
        aboutMenuItem.setText("About...");
       
        aboutMenuItem.addSelectionListener(new SelectionAdapter() {

            public void widgetSelected(SelectionEvent e) {

                new SkinningSample().open();
            }
        });
       
        new MenuItem(trayMenu, SWT.SEPARATOR);

        final MenuItem exitMenuItem = new MenuItem(trayMenu, SWT.PUSH);
        exitMenuItem.setData(ACTION_EXIT);
        exitMenuItem.setText("Exit");

        SelectionAdapter selectionAdapter = new SelectionAdapter() {

            public void widgetSelected(SelectionEvent e) {

                setEnabled(false);

                String data = (String) e.widget.getData();
               
                if (data.equals(ACTION_CONFIG)) {
                   
                    callback.config(null);
                   
                } else if (data.equals(ACTION_EXIT)) {
                   
                  callback.exit();
                    shell.dispose();
                    return;
                   
                } else if (data.equals(ACTION_ABOUT)) {
                   
                    System.out.println("about");
                }
               
                setEnabled(true);
            }
           
            private void setEnabled(boolean enabled) {
               
                configMenuItem.setEnabled(enabled);
                aboutMenuItem.setEnabled(enabled);
                exitMenuItem.setEnabled(enabled);
            }
        };

        configMenuItem.addSelectionListener(selectionAdapter);
        aboutMenuItem.addSelectionListener(selectionAdapter);
        exitMenuItem.addSelectionListener(selectionAdapter);

        // XXX
        System.out.println("SKINNER START");       
        Skinner.apply(trayMenu);
        System.out.println("SKINNER END");       

        trayMenu.setVisible(true);
        System.out.println("END");       

    }

    private MenuItem createMenuItem(PluginView pluginView) {
       
        MenuItem menuItem = new MenuItem(pluginMenu, SWT.PUSH);
        // XXX SWT Bug 396682
//        menuItem.setText(pluginView.getTitle()+"\t  "+pluginView.getVersion());
        menuItem.setText(pluginView.getTitle()+"   "+pluginView.getVersion());
        menuItem.setData(pluginView);
       
        InputStream iconStream = pluginView.getIcon16x16();
       
        if (iconStream != null)
            menuItem.setImage(new Image(pluginMenu.getDisplay(), iconStream));

        return menuItem;
    }
   
    // INNER CLASS ///////////////////////////////////////////////////////////////////////////////////
   
    static interface Callback
    {
        public void config(PluginView pluginView);
        public void about();
        public void exit();
    }
}
TOP

Related Classes of giggler.ui.impl.TrayMenu$Callback

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.