Example usage for org.eclipse.swt.widgets Display disposeExec

List of usage examples for org.eclipse.swt.widgets Display disposeExec

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Display disposeExec.

Prototype

public void disposeExec(Runnable runnable) 

Source Link

Document

Causes the run() method of the runnable to be invoked by the user-interface thread just before the receiver is disposed.

Usage

From source file:Snippet178.java

static void hookApplicationMenu(Display display, final String aboutName) {
    // Callback target
    Object target = new Object() {
        int commandProc(int nextHandler, int theEvent, int userData) {
            if (OS.GetEventKind(theEvent) == OS.kEventProcessCommand) {
                HICommand command = new HICommand();
                OS.GetEventParameter(theEvent, OS.kEventParamDirectObject, OS.typeHICommand, null,
                        HICommand.sizeof, null, command);
                switch (command.commandID) {
                case kHICommandPreferences:
                    return handleCommand("Preferences"); //$NON-NLS-1$
                case kHICommandAbout:
                    return handleCommand(aboutName);
                default:
                    break;
                }/*  ww w .ja va  2 s .  c  o  m*/
            }
            return OS.eventNotHandledErr;
        }

        int handleCommand(String command) {
            Shell shell = new Shell();
            MessageBox preferences = new MessageBox(shell, SWT.ICON_WARNING);
            preferences.setText(command);
            preferences.open();
            shell.dispose();
            return OS.noErr;
        }
    };

    final Callback commandCallback = new Callback(target, "commandProc", 3); //$NON-NLS-1$
    int commandProc = commandCallback.getAddress();
    if (commandProc == 0) {
        commandCallback.dispose();
        return; // give up
    }

    // Install event handler for commands
    int[] mask = new int[] { OS.kEventClassCommand, OS.kEventProcessCommand };
    OS.InstallEventHandler(OS.GetApplicationEventTarget(), commandProc, mask.length / 2, mask, 0, null);

    // create About ... menu command
    int[] outMenu = new int[1];
    short[] outIndex = new short[1];
    if (OS.GetIndMenuItemWithCommandID(0, kHICommandPreferences, 1, outMenu, outIndex) == OS.noErr
            && outMenu[0] != 0) {
        int menu = outMenu[0];

        int l = aboutName.length();
        char buffer[] = new char[l];
        aboutName.getChars(0, l, buffer, 0);
        int str = OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, buffer, l);
        OS.InsertMenuItemTextWithCFString(menu, str, (short) 0, 0, kHICommandAbout);
        OS.CFRelease(str);

        // add separator between About & Preferences
        OS.InsertMenuItemTextWithCFString(menu, 0, (short) 1, OS.kMenuItemAttrSeparator, 0);

        // enable pref menu
        OS.EnableMenuCommand(menu, kHICommandPreferences);

        // disable services menu
        OS.DisableMenuCommand(menu, kHICommandServices);
    }

    // schedule disposal of callback object
    display.disposeExec(new Runnable() {
        public void run() {
            commandCallback.dispose();
        }
    });
}

From source file:org.eclipse.swt.examples.accessibility.CTable.java

static void initImages(final Display display) {
    PaletteData arrowPalette = new PaletteData(new RGB(0, 0, 0), new RGB(255, 255, 255));
    if (display.getData(ID_ARROWDOWN) == null) {
        ImageData arrowDown = new ImageData(7, 4, 1, arrowPalette, 1,
                new byte[] { 0x00, (byte) 0x83, (byte) 0xC7, (byte) 0xEF });
        arrowDown.transparentPixel = 0x1; /* use white for transparency */
        display.setData(ID_ARROWDOWN, new Image(display, arrowDown));
    }//from w  ww.  ja  va  2  s  .  c om
    if (display.getData(ID_ARROWUP) == null) {
        ImageData arrowUp = new ImageData(7, 4, 1, arrowPalette, 1,
                new byte[] { (byte) 0xEF, (byte) 0xC7, (byte) 0x83, 0x00 });
        arrowUp.transparentPixel = 0x1; /* use white for transparency */
        display.setData(ID_ARROWUP, new Image(display, arrowUp));
    }

    PaletteData checkMarkPalette = new PaletteData(new RGB(0, 0, 0), new RGB(252, 3, 251));
    byte[] checkbox = new byte[] { 0, 0, 127, -64, 127, -64, 127, -64, 127, -64, 127, -64, 127, -64, 127, -64,
            127, -64, 127, -64, 0, 0 };
    ImageData checkmark = new ImageData(7, 7, 1, checkMarkPalette, 1,
            new byte[] { -4, -8, 112, 34, 6, -114, -34 });
    checkmark.transparentPixel = 1;
    if (display.getData(ID_CHECKMARK) == null) {
        display.setData(ID_CHECKMARK, new Image(display, checkmark));
    }

    if (display.getData(ID_UNCHECKED) == null) {
        PaletteData uncheckedPalette = new PaletteData(new RGB(128, 128, 128), new RGB(255, 255, 255));
        ImageData unchecked = new ImageData(11, 11, 1, uncheckedPalette, 2, checkbox);
        display.setData(ID_UNCHECKED, new Image(display, unchecked));
    }

    if (display.getData(ID_GRAYUNCHECKED) == null) {
        PaletteData grayUncheckedPalette = new PaletteData(new RGB(128, 128, 128), new RGB(192, 192, 192));
        ImageData grayUnchecked = new ImageData(11, 11, 1, grayUncheckedPalette, 2, checkbox);
        display.setData(ID_GRAYUNCHECKED, new Image(display, grayUnchecked));
    }

    display.disposeExec(() -> {
        Image unchecked = (Image) display.getData(ID_UNCHECKED);
        if (unchecked != null)
            unchecked.dispose();
        Image grayUnchecked = (Image) display.getData(ID_GRAYUNCHECKED);
        if (grayUnchecked != null)
            grayUnchecked.dispose();
        Image checkmark1 = (Image) display.getData(ID_CHECKMARK);
        if (checkmark1 != null)
            checkmark1.dispose();
        Image arrowDown = (Image) display.getData(ID_ARROWDOWN);
        if (arrowDown != null)
            arrowDown.dispose();
        Image arrowUp = (Image) display.getData(ID_ARROWUP);
        if (arrowUp != null)
            arrowUp.dispose();

        display.setData(ID_UNCHECKED, null);
        display.setData(ID_GRAYUNCHECKED, null);
        display.setData(ID_CHECKMARK, null);
        display.setData(ID_ARROWDOWN, null);
        display.setData(ID_ARROWUP, null);
    });
}