001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     troger
018 */
019package org.nuxeo.ecm.platform.annotations.gwt.client.view.menu;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import org.nuxeo.ecm.platform.annotations.gwt.client.view.AbstractAnnotationCommand;
025
026import com.google.gwt.user.client.ui.MenuBar;
027import com.google.gwt.user.client.ui.MenuItem;
028import com.google.gwt.user.client.ui.PopupPanel;
029
030/**
031 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
032 */
033public class AnnotationPopupMenu extends PopupPanel {
034
035    private List<AbstractAnnotationCommand> commands;
036
037    private boolean initialized = false;
038
039    public AnnotationPopupMenu(List<AbstractAnnotationCommand> commands) {
040        super(true);
041        this.commands = new ArrayList<AbstractAnnotationCommand>(commands);
042        setStyleName("annotationPopupMenu");
043        // Fix for IE: don't display the context menu on right click
044        getElement().setAttribute("oncontextmenu", "return false;");
045    }
046
047    @Override
048    public void show() {
049        if (!initialized) {
050            createPopupMenu();
051            initialized = true;
052        }
053        super.show();
054    }
055
056    private void createPopupMenu() {
057        MenuBar popupMenuBar = new MenuBar(true);
058
059        for (AbstractAnnotationCommand command : commands) {
060            command.setAnnotationPopupMenu(this);
061
062            MenuItem item = new MenuItem(command.getTitle(), true, command);
063            item.addStyleName("annotationPopupMenuItem");
064            popupMenuBar.addItem(item);
065        }
066
067        popupMenuBar.setVisible(true);
068        add(popupMenuBar);
069    }
070
071}