Use Case
1. Disable browser context menu by default
2. Enable browser context menu on edit controls like EDIT boxes,
listboxes etc when such controls receive focus. This is required so
that you can do the normal highlighting, cut, copy and paste from such
controls

3. When edit controls lose focus, disable the browser content menu
again

4. Example


//disable browser context menu on main page
<code>
 private static native void disableBrowserContextMenuJSNI() /*-{
        $doc.oncontextmenu = function() { return false; };
    }-*/;
</code>

//enable browser context menu
 private static native void restoreContextMenuJSNI() /*-{
        $doc.oncontextmenu = function() { return true; };
    }-*/;


Create edit box with preview that allows switching between disbaled
and enabled context menu
<code>
public class PreviewTextBox extends TextBox implements FocusListener {

        public PreviewTextBox() {
            this.addFocusListener(this);
        }

         public void onDetach() {
             super.onDetach();
             disableBrowserContextMenuJSNI();
             DOM.removeEventPreview(m_preview); //tos - 1
         }

         public void onFocus(Widget w) {
             DOM.removeEventPreview(m_preview); //tos - 1
             DOM.addEventPreview(m_preview); //tos
             restoreContextMenuJSNI();
         }

         public void onLostFocus(Widget w) {
             disableBrowserContextMenuJSNI();
             DOM.removeEventPreview(m_preview); //tos - 1
         }

        private EventPreview m_preview = new EventPreview() {
            public boolean onEventPreview(Event event) {
                int type = DOM.eventGetType(event);
                switch (type) {
                    case Event.ONMOUSEDOWN:
                        Element target1 = DOM.eventGetTarget(event);
                        if (!
PreviewTextBox.this.getElement().isOrHasChild(target1)) {
                           disableBrowserContextMenuJSNI(); //by
defauly no browser context menu must show
                            DeferredCommand.addCommand(new Command() {
                                public void execute() {
 
DOM.removeEventPreview(m_preview); //tos - 1
                                }
                            });
                        }
                        break;
                    default:
                        break;
                }
                // But DO allow the event to fire.
                return true;
            }
        };
    }
</code>


The above code was working perfectly in GWT 1.4.x

With the advent of GWT1.5, this nolonger works and I get the browser
context menu anyway.

Is there anything else I need to do to make the oncontextmenu event
work in GWT 1.5.




Also I noticed that now we have Event.ONCONTEXTMENU as an event bit.

I tried using EventPreview to stop the context menu from showing but
most of the times this event is NOT PREVIEWED! See example below

<code>
 public boolean onEventPreview(Event event) {
            int type = DOM.eventGetType(event);
            switch (type) {
                case Event.ONCONTEXTMENU:
                    Element target = DOM.eventGetTarget(event);
                    if (!
m_search_textbox.getElement().isOrHasChild(target)) {
                        DOM.eventPreventDefault(event);
                        DOM.eventCancelBubble(event, true);
                        return false;//dont allow to bubble break;
                    }
                    break;
            }
      return true;
}
</code>

Any reason why it previews sometimes and not all the time.

Does it have anything to do with this line

"GWT Widgets now sink their events lazily: widgets no longer routinely
sink their events eagerly. Instead, the event is sunk the first time a
listener is added to the widget. So subclasses which relied on eagerly
sunk events will now have to manually sink the events they depend
upon."

from the breaking changes page:-

http://code.google.com/docreader/#p=google-web-toolkit-doc-1-5&s=google-web-toolkit-doc-1-5&t=ReleaseNotes_1_5_BreakingChanges


If so what do I need to do.

Thanks,

Melody
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to Google-Web-Toolkit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to