Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse rcp disable exit button in an eclipse application

how can I disable the window top-right button to close application?

Thanks a lot

like image 420
picciopiccio Avatar asked Dec 07 '25 09:12

picciopiccio


1 Answers

Another possibility is to "iconify" the application if the user wants to close the application, that means the application is moved into the tray-item. Add the following to your ApplicationWorkbenchWindowAdvisor:

a. Override postWindowOpen

public void postWindowOpen() {
   Shell shell = getWindowConfigurer().getWindow().getShell();
   boolean trayEnabled = false;
   trayEnabled = enableTray();
}

b. Implement tray support

    /* Enable System-Tray Support */
    private boolean enableTray() {
        /* Avoid that this is being called redundantly */
        if (this.fTrayEnabled) {
            return true;
        }
        /* Only enable for Primary Window */
        IWorkbenchWindow primaryWindow = PlatformUI.getWorkbench().getWorkbenchWindows()[0];
        if (primaryWindow == null || !primaryWindow.equals(getWindowConfigurer().getWindow())) {
            return false;
        }

        final Shell shell = primaryWindow.getShell();
        final Tray tray = shell.getDisplay().getSystemTray();

        /* Tray not support on the OS */
        if (tray == null) {
            return false;
        }

        /* Create Item in Tray */
        this.fTrayItem = new TrayItem(tray, SWT.NONE);
        this.fTrayItem.setToolTipText(Platform.getProduct().getName());
        this.fTrayEnabled = true;

        this.fTrayItem.setVisible(false);
        /* Apply Image */
        this.fTrayItem.setImage(trayIcon);
        /* Minimize to Tray on Shell Iconify if set */
        this.fTrayShellListener = new ShellAdapter() {
            @Override
            public void shellIconified(final ShellEvent e) {
                if (!ApplicationWorkbenchWindowAdvisor.this.fBlockIconifyEvent && ApplicationWorkbenchWindowAdvisor.this.fMinimizeFromClose) {
                    moveToTray(shell);
                }
            }
        };
        shell.addShellListener(this.fTrayShellListener);

        /* Show Menu on Selection */
        this.fTrayItem.addListener(SWT.MenuDetect, new Listener() {


            public void handleEvent(final Event event) {
                MenuManager trayMenu = new MenuManager();

                /* Restore */
                trayMenu.add(new ContributionItem() {
                    @Override
                    public void fill(final Menu menu, final int index) {
                        MenuItem restoreItem = new MenuItem(menu, SWT.PUSH);
                        restoreItem.setText("Restore from Tray");
                        restoreItem.addSelectionListener(new SelectionAdapter() {
                            @Override
                            public void widgetSelected(final SelectionEvent e) {
                                restoreFromTray(shell);
                            }
                        });
                        menu.setDefaultItem(restoreItem);
                    }
                });

                /* Separator */
                trayMenu.add(new Separator());

                /* Other Items */
                ApplicationWorkbenchWindowAdvisor.this.fActionBarAdvisor.fillTrayItem(trayMenu);

                Menu menu = trayMenu.createContextMenu(shell);
                menu.setVisible(true);
            }
        });
        /* Handle DefaultSelection */
        this.fTrayItem.addListener(SWT.DefaultSelection, new Listener() {
            public void handleEvent(final Event event) {
                /* Restore from Tray */
                if (!shell.isVisible()) {
                    restoreFromTray(shell);
                } else {
                    moveToTray(shell);
                }
            }
        });
        return true;
    }
    /* Move to System Tray */
    private void moveToTray(final Shell shell) {
        this.fTrayItem.setVisible(true);
        this.fBlockIconifyEvent = true;
        try {
            shell.setVisible(false);
        } finally {
            this.fBlockIconifyEvent = false;
        }

        this.fMinimizedToTray = true;
    /**
     * @param shell
     */
    public void restoreFromTray(final Shell shell) {

        /* Restore Shell */
        shell.setVisible(true);
        shell.setActive();
        shell.layout(true);

        /* Un-Minimize if minimized */
        if (shell.getMinimized()) {
            shell.setMinimized(false);
        }
        this.fTrayItem.setVisible(false);

        if (this.fTrayTeasing) {
            this.fTrayItem.setImage(this.trayImage);
        }

        this.fTrayTeasing = false;
        this.fMinimizedToTray = false;
    }

    /* Disable System-Tray Support */
    private void disableTray() {

        /* Avoid that this is being called redundantly */
        if (!this.fTrayEnabled) {
            return;
        }

        /* First make sure to have the Window restored */
        restoreFromTray(getWindowConfigurer().getWindow().getShell());

        this.fTrayEnabled = false;
        this.fMinimizedToTray = false;

        if (this.fTrayItem != null) {
            this.fTrayItem.dispose();
        }

        if (this.fTrayShellListener != null) {
            getWindowConfigurer().getWindow().getShell().removeShellListener(this.fTrayShellListener);
        }
    }

c. Override preWindowShellClose

@Override
public boolean preWindowShellClose() {
    final res =  true ;
    this.fMinimizeFromClose = true;
    getWindowConfigurer().getWindow().getShell().notifyListeners(SWT.Iconify, new Event());
    res = false;
    this.fMinimizeFromClose = false;
    return res;
}

HTH Tom

like image 102
Tom Seidel Avatar answered Dec 10 '25 18:12

Tom Seidel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!