Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Introduction to the Context Menu API

 

Design

Interface

Code Block
themeMidnight
package org.lorainelab.igb.context.menu;
import java.util.Optional;
import org.lorainelab.igb.context.menu.model.AnnotationContextEvent;
import org.lorainelab.igb.context.menu.model.ContextMenuItem;

/**
 *
 * @author dcnorris
 */
public interface AnnotationContextMenuProvider {
    public Optional<ContextMenuItem> buildMenuItem(AnnotationContextEvent event);
    public MenuSection getMenuSection();
}

...

Code Block
themeMidnight
package org.lorainelab.igb.context.menu.modelapi.quickstart;

import static com.google.common.base.Preconditions.checkNotNullaQute.bnd.annotation.component.Component;
import java.util.Optional;
import static comorg.googlelorainelab.commonigb.basecontext.Preconditionsmenu.checkStateAnnotationContextMenuProvider;
import comorg.lorainelab.googleigb.commoncontext.basemenu.StringsMenuSection;
import com.google.common.collect.ImmutableSetorg.lorainelab.igb.context.menu.model.AnnotationContextEvent;
import java.util.Optionalorg.lorainelab.igb.context.menu.model.ContextMenuItem;
import javaorg.utilslf4j.SetLogger;
import javaorg.utilslf4j.function.FunctionLoggerFactory;
/**
 *
 * @author dcnorris
 */
public@Component(immediate class ContextMenuItem {
    private final String menuLabel;= true)
public class ContextMenuExtension implements AnnotationContextMenuProvider {
    private MenuIconstatic menuIcon;
final    private Function<Void, Void> action;
    private Set<ContextMenuItem> subMenuItems;
    private int weight = 0;
    public ContextMenuItem(String menuLabel, Set<ContextMenuItem> subMenuItems) {Logger logger = LoggerFactory.getLogger(ContextMenuExtension.class);
        checkNotNull(menuLabel);@Override
    public Optional<ContextMenuItem>   checkNotNull(subMenuItems);
        checkState(!Strings.isNullOrEmpty(menuLabel));
        checkState(!subMenuItems.isEmpty());buildMenuItem(AnnotationContextEvent event) {
        this.menuLabel = menuLabel;
        this.subMenuItems = ImmutableSet.copyOf(subMenuItems);
    }
    public ContextMenuItem(String menuLabel, Function<Void, Void> action)ContextMenuItem contextMenuItem = new ContextMenuItem("Log Selection ids", (Void t) -> {
        checkNotNull(menuLabel);
        checkState(!Strings.isNullOrEmpty(menuLabel));
        checkNotNull(action);
        this.menuLabel = menuLabel;
        this.action = action;
        subMenuItems = ImmutableSet.of(event.getSelectedItems().stream().map(selectedSym -> selectedSym.getID()).forEach(logger::info);
    }
    public String getMenuLabel() {
        return menuLabelt;
    }
    public Function<Void, Void> getAction() {
        return action});
    }
    public Set<ContextMenuItem> getSubMenuItems() {
        return subMenuItems;
    }
    public void setWeight(int weight) {
        this.weight = weightreturn Optional.ofNullable(contextMenuItem);
    }
    public void setMenuIcon(MenuIcon menuIcon) {@Override
        this.menuIcon = menuIcon;
    }
    public Optional<MenuIcon>MenuSection getMenuIcongetMenuSection() {
        return Optional.ofNullable(menuIcon)MenuSection.INFORMATION;
    }
}

MenuIcon

Code Block
themeMidnight
package org.lorainelab.igb.context.menu.model;
import com.google.common.io.BaseEncoding;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 *
 * @author dcnorris
 */
public class MenuIcon {
    private String encodedImage;
    private static final Logger logger = LoggerFactory.getLogger(MenuIcon.class);
    public MenuIcon(InputStream resourceAsStream) {
        try {
            encodedImage = BaseEncoding.base64().encode(IOUtils.toByteArray(resourceAsStream));
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }
    public byte[] getEncodedImage() {
        return BaseEncoding.base64().decode(encodedImage);
    }
}