GWTでの右クリックコンテキストメニューの非表示


GWTの開発では、あるパネルでブラウザの元の右クリックコンテキストメニューを遮断し、自分で定義したコンテキストメニューを使用することがある.具体的には、以下のように実現される. 
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

public interface AdvClickListener extends ClickListener {
	public void onClick(Widget sender, Event event);
	public void onRightClick(Widget sender, Event event);
}
 
public interface AdvClickNotifier {
	public void addClickListener(AdvClickListener listener);
	public void removeClickListener(AdvClickListener listener);
}
 
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.DeckPanel;

public class AdvDeckPanel extends DeckPanel implements AdvClickNotifier {

	private AdvClickListener listener = null;

	public AdvDeckPanel() {
		super();
		sinkEvents(Event.ONMOUSEUP | Event.ONDBLCLICK | Event.ONCONTEXTMENU); 
	}
	
	public void onBrowserEvent(Event event) {
		GWT.log("onBrowserEvent", null);
        event.cancelBubble(true);//This will stop the event from being propagated to parent elements.
        event.preventDefault();
        switch (DOM.eventGetType(event)) {
	        case Event.ONMOUSEUP:
	        	if (DOM.eventGetButton(event) == Event.BUTTON_LEFT) {
	        		GWT.log("Event.BUTTON_LEFT", null);
	        		listener.onClick(this, event);
	        	}
	        	if (DOM.eventGetButton(event) == Event.BUTTON_RIGHT) {
	        		GWT.log("Event.BUTTON_RIGHT", null);
	        		listener.onRightClick(this, event);
	        	}
	        	break;
	
	        case Event.ONDBLCLICK: 

	        	break;
	        	
	        case Event.ONCONTEXTMENU: 
	        	GWT.log("Event.ONCONTEXTMENU", null);

	        	break;

	        default: 
	                // Do nothing
        }//end switch
	}
	
	public void addClickListener(AdvClickListener listener) {
		this.listener = listener;
	}

	public void removeClickListener(AdvClickListener listener) {
		this.listener = null;
	}

	
import com.google.gwt.core.client.EntryPoint;

public class RightClickEx implements EntryPoint, AdvClickListener {

	private AdvDeckPanel deckPanel = new AdvDeckPanel();
	final private PopupPanel popupPanel = new PopupPanel(true);
	private Label defaultLabel = new Label("Right click here to see the GWT context menu.");
	private Image image = new Image("images/oliver.jpg");
	private Frame sponser = new Frame("green-energy.html");
	
	private VerticalPanel defaultPanel = new VerticalPanel();
	private SimplePanel imagePanel = new SimplePanel();
	private SimplePanel sponserPanel = new SimplePanel();
	
	Command showAlertCommand = new Command() {
		public void execute() {
			deckPanel.showWidget(0);
			popupPanel.hide();
			Window.alert("Hope this example helps.");
		}
	};
	Command showImageCommand = new Command() {
		public void execute() {
			deckPanel.showWidget(1);
			popupPanel.hide();
		}
	};
	Command showSponserCommand = new Command() {
		public void execute() {
			deckPanel.showWidget(2);
			popupPanel.hide();
		}
	};

	
	public void onModuleLoad() {
		VerticalPanel vPanel = new VerticalPanel();
		vPanel.setWidth("250px");
		vPanel.setHeight("280px");
		vPanel.add(deckPanel);
		vPanel.setBorderWidth(2);
		
		deckPanel.addClickListener(this);

		defaultLabel.setPixelSize(220, 280);
		defaultLabel.setStyleName("text-label");
		defaultPanel.add(defaultLabel);
		deckPanel.add(defaultPanel);
		
		imagePanel.add(image);
		deckPanel.add(imagePanel);
		
		VerticalPanel outerVp = new VerticalPanel();
		VerticalPanel tmpVp = new VerticalPanel();
		tmpVp.setPixelSize(220, 15);
		
		sponser.setPixelSize(250, 250);
		sponserPanel.add(sponser);
		outerVp.add(sponserPanel);
		outerVp.add(tmpVp);
		deckPanel.add(outerVp);
		
		createPopupMenu();
		
		deckPanel.showWidget(0);
		RootPanel.get("rightclickex").add(vPanel);
	}
	
	private void createPopupMenu() {
		MenuBar popupMenuBar = new MenuBar(true);
		MenuItem alertItem = new MenuItem("Show alert", true, showAlertCommand);
		MenuItem imageItem = new MenuItem("Show Oliver ", true, showImageCommand);
		MenuItem sponserItem = new MenuItem("Show sponser ", true, showSponserCommand);
		
		popupPanel.setStyleName("popup");

		alertItem.addStyleName("popup-item");
		imageItem.addStyleName("popup-item");
		sponserItem.addStyleName("popup-item");
		popupMenuBar.addItem(alertItem);
		popupMenuBar.addItem(imageItem);
		popupMenuBar.addItem(sponserItem);

    	popupMenuBar.setVisible(true);
    	popupPanel.add(popupMenuBar);
	}

	public void onRightClick(Widget sender, Event event) {
    	int x = DOM.eventGetClientX(event);
    	int y = DOM.eventGetClientY(event);
    	popupPanel.setPopupPosition(x, y);
    	popupPanel.show();
	}
	
	public void onClick(Widget sender, Event event) {
	}


	public void onClick(Widget sender) {
	}
}


 }