diff dwtx/jface/text/contentassist/PopupCloser.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/jface/text/contentassist/PopupCloser.d	Sat Aug 23 19:10:48 2008 +0200
@@ -0,0 +1,275 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2008 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module dwtx.jface.text.contentassist.PopupCloser;
+
+import dwt.dwthelper.utils;
+
+
+
+import dwt.DWT;
+import dwt.events.FocusEvent;
+import dwt.events.FocusListener;
+import dwt.events.SelectionEvent;
+import dwt.events.SelectionListener;
+import dwt.events.ShellAdapter;
+import dwt.events.ShellEvent;
+import dwt.widgets.Control;
+import dwt.widgets.Display;
+import dwt.widgets.Event;
+import dwt.widgets.Listener;
+import dwt.widgets.ScrollBar;
+import dwt.widgets.Shell;
+import dwt.widgets.Table;
+import dwtx.jface.internal.text.DelayedInputChangeListener;
+import dwtx.jface.internal.text.InformationControlReplacer;
+import dwtx.jface.text.IDelayedInputChangeProvider;
+import dwtx.jface.text.IInformationControl;
+import dwtx.jface.text.IInformationControlExtension5;
+import dwtx.jface.text.IInputChangedListener;
+
+
+/**
+ * A generic closer class used to monitor various
+ * interface events in order to determine whether
+ * a content assistant should be terminated and all
+ * associated windows be closed.
+ */
+class PopupCloser : ShellAdapter , FocusListener, SelectionListener, Listener {
+
+    /** The content assistant to be monitored. */
+    private ContentAssistant fContentAssistant;
+    /** The table of a selector popup opened by the content assistant. */
+    private Table fTable;
+    /** The scroll bar of the table for the selector popup. */
+    private ScrollBar fScrollbar;
+    /** Indicates whether the scroll bar thumb has been grabbed. */
+    private bool fScrollbarClicked= false;
+    /**
+     * The shell on which some listeners are registered.
+     * @since 3.1
+     */
+    private Shell fShell;
+    /**
+     * The display on which some filters are registered.
+     * @since 3.4
+     */
+    private Display fDisplay;
+    /**
+     * The additional info controller, or <code>null</code>.
+     * @since 3.4
+     */
+    private AdditionalInfoController fAdditionalInfoController;
+
+    /**
+     * Installs this closer on the given table opened by the given content assistant.
+     *
+     * @param contentAssistant the content assistant
+     * @param table the table to be tracked
+     */
+    public void install(ContentAssistant contentAssistant, Table table) {
+        install(contentAssistant, table, null);
+    }
+    
+    /**
+     * Installs this closer on the given table opened by the given content assistant.
+     *
+     * @param contentAssistant the content assistant
+     * @param table the table to be tracked
+     * @param additionalInfoController the additional info controller, or <code>null</code>
+     * @since 3.4
+     */
+    public void install(ContentAssistant contentAssistant, Table table, AdditionalInfoController additionalInfoController) {
+        fContentAssistant= contentAssistant;
+        fTable= table;
+        fAdditionalInfoController= additionalInfoController;
+        
+        if (Helper.okToUse(fTable)) {
+            fShell= fTable.getShell();
+            fDisplay= fShell.getDisplay();
+            
+            fShell.addShellListener(this);
+            fTable.addFocusListener(this);
+            fScrollbar= fTable.getVerticalBar();
+            if (fScrollbar !is null)
+                fScrollbar.addSelectionListener(this);
+            
+            fDisplay.addFilter(DWT.Activate, this);
+            fDisplay.addFilter(DWT.MouseWheel, this);
+            
+            fDisplay.addFilter(DWT.Deactivate, this);
+            
+            fDisplay.addFilter(DWT.MouseUp, this);
+        }
+    }
+
+    /**
+     * Uninstalls this closer if previously installed.
+     */
+    public void uninstall() {
+        fContentAssistant= null;
+        if (Helper.okToUse(fShell))
+            fShell.removeShellListener(this);
+        fShell= null;
+        if (Helper.okToUse(fScrollbar))
+            fScrollbar.removeSelectionListener(this);
+        if (Helper.okToUse(fTable))
+            fTable.removeFocusListener(this);
+        if (fDisplay !is null && ! fDisplay.isDisposed()) {
+            fDisplay.removeFilter(DWT.Activate, this);
+            fDisplay.removeFilter(DWT.MouseWheel, this);
+            
+            fDisplay.removeFilter(DWT.Deactivate, this);
+            
+            fDisplay.removeFilter(DWT.MouseUp, this);
+        }
+    }
+
+    /*
+     * @see dwt.events.SelectionListener#widgetSelected(dwt.events.SelectionEvent)
+     */
+    public void widgetSelected(SelectionEvent e) {
+        fScrollbarClicked= true;
+    }
+
+    /*
+     * @see dwt.events.SelectionListener#widgetDefaultSelected(dwt.events.SelectionEvent)
+     */
+    public void widgetDefaultSelected(SelectionEvent e) {
+        fScrollbarClicked= true;
+    }
+
+    /*
+     * @see dwt.events.FocusListener#focusGained(dwt.events.FocusEvent)
+     */
+    public void focusGained(FocusEvent e) {
+    }
+
+    /*
+     * @see dwt.events.FocusListener#focusLost(dwt.events.FocusEvent)
+     */
+    public void focusLost(final FocusEvent e) {
+        fScrollbarClicked= false;
+        Display d= fTable.getDisplay();
+        d.asyncExec(new Runnable() {
+            public void run() {
+                if (Helper.okToUse(fTable) && !fTable.isFocusControl() && !fScrollbarClicked && fContentAssistant !is null)
+                    fContentAssistant.popupFocusLost(e);
+            }
+        });
+    }
+
+    /*
+     * @see dwt.events.ShellAdapter#shellDeactivated(dwt.events.ShellEvent)
+     * @since 3.1
+     */
+    public void shellDeactivated(ShellEvent e) {
+        if (fContentAssistant !is null && ! fContentAssistant.hasProposalPopupFocus())
+            fContentAssistant.hide();
+    }
+
+
+    /*
+     * @see dwt.events.ShellAdapter#shellClosed(dwt.events.ShellEvent)
+     * @since 3.1
+     */
+    public void shellClosed(ShellEvent e) {
+        if (fContentAssistant !is null)
+            fContentAssistant.hide();
+    }
+
+    /*
+     * @see dwt.widgets.Listener#handleEvent(dwt.widgets.Event)
+     * @since 3.4
+     */
+    public void handleEvent(Event event) {
+        switch (event.type) {
+            case DWT.Activate:
+            case DWT.MouseWheel:
+                if (fAdditionalInfoController is null)
+                    return;
+                if (event.widget is fShell || event.widget is fTable || event.widget is fScrollbar)
+                    return;
+                
+                if (fAdditionalInfoController.getInternalAccessor().getInformationControlReplacer() is null)
+                    fAdditionalInfoController.hideInformationControl();
+                else if (!fAdditionalInfoController.getInternalAccessor().isReplaceInProgress()) {
+                    IInformationControl infoControl= fAdditionalInfoController.getCurrentInformationControl2();
+                    // During isReplaceInProgress(), events can come from the replacing information control
+                    if (event.widget instanceof Control && infoControl instanceof IInformationControlExtension5) {
+                        Control control= (Control) event.widget;
+                        IInformationControlExtension5 iControl5= (IInformationControlExtension5) infoControl;
+                        if (!(iControl5.containsControl(control)))
+                            fAdditionalInfoController.hideInformationControl();
+                        else if (event.type is DWT.MouseWheel)
+                            fAdditionalInfoController.getInternalAccessor().replaceInformationControl(false);
+                    } else if (infoControl !is null && infoControl.isFocusControl()) {
+                        fAdditionalInfoController.getInternalAccessor().replaceInformationControl(true);
+                    }
+                }
+                break;
+                
+            case DWT.MouseUp:
+                if (fAdditionalInfoController is null || fAdditionalInfoController.getInternalAccessor().isReplaceInProgress())
+                    break;
+                if (event.widget instanceof Control) {
+                    Control control= (Control) event.widget;
+                    IInformationControl infoControl= fAdditionalInfoController.getCurrentInformationControl2();
+                    if (infoControl instanceof IInformationControlExtension5) {
+                        final IInformationControlExtension5 iControl5= (IInformationControlExtension5) infoControl;
+                        if (iControl5.containsControl(control)) {
+                            if (infoControl instanceof IDelayedInputChangeProvider) {
+                                final IDelayedInputChangeProvider delayedICP= (IDelayedInputChangeProvider) infoControl;
+                                final IInputChangedListener inputChangeListener= new DelayedInputChangeListener(delayedICP, fAdditionalInfoController.getInternalAccessor().getInformationControlReplacer());
+                                delayedICP.setDelayedInputChangeListener(inputChangeListener);
+                                // cancel automatic input updating after a small timeout:
+                                control.getShell().getDisplay().timerExec(1000, new Runnable() {
+                                    public void run() {
+                                        delayedICP.setDelayedInputChangeListener(null);
+                                    }
+                                });
+                            }
+                            
+                            // XXX: workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=212392 :
+                            control.getShell().getDisplay().asyncExec(new Runnable() {
+                                public void run() {
+                                    fAdditionalInfoController.getInternalAccessor().replaceInformationControl(true);
+                                }
+                            });
+                        }
+                    }
+                }
+                break;
+    
+            case DWT.Deactivate:
+                if (fAdditionalInfoController is null)
+                    break;
+                InformationControlReplacer replacer= fAdditionalInfoController.getInternalAccessor().getInformationControlReplacer();
+                if (replacer !is null && fContentAssistant !is null) {
+                    IInformationControl iControl= replacer.getCurrentInformationControl2();
+                    if (event.widget instanceof Control && iControl instanceof IInformationControlExtension5) {
+                        Control control= (Control) event.widget;
+                        IInformationControlExtension5 iControl5= (IInformationControlExtension5) iControl;
+                        if (iControl5.containsControl(control)) {
+                            control.getDisplay().asyncExec(new Runnable() {
+                                public void run() {
+                                    if (fContentAssistant !is null && ! fContentAssistant.hasProposalPopupFocus())
+                                        fContentAssistant.hide();
+                                }
+                            });
+                        }
+                    }
+                }
+                break;
+        }
+    }
+}