Page tree

Versions Compared

Key

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

...

Dissecting the Hello World IGB App

The rest of the information found on this page aims to give the developer a deeper understanding of the different pieces that allow the Hello World IGB App to hook into IGB.

Directory Structure

The Maven build system assumes that every Java project conforms to an expected directory structure. Here, the maven project includes

...

 

Code Block
languagejava
titleMenuBarEntryProvider
public interface MenuBarEntryProvider {
    public Optional<List<MenuItem>> getMenuItems();
	public MenuBarParentMenu getMenuExtensionParent();
 }

 

Note that this interface will likely change in future releases of the IGB API. The getParentMenuName method will likely a custom enum type and not a String literal.  Additionally, we will add "weight" as an attribute to our custom JMenuItem class.  So it is likely this interface will ultimately contain one simple getMenuItem() method.  

Creating a new Java class (MenuBarExtensionExample)

Within NetBeans, check that your IGB Menubar Extension project is the currently selected project - the name of the currently opened project is shown on the NetBeans title bar. If it isn't, select File > Open Project to open it.

Next, create a new Java class named IgbToobarExtension:

  • Select File > New File
  • Under Choose File Type, select Java
  • Select Next
  • Enter class Name MenuBarExtensionExample
  • Select package org.lorainelab.igb.menu.api.example
  • Select Finish

 

Your newly created class should match the code seen below:
Code Block
languagejava
package org.lorainelab.igb.menu.api.example;
import aQute.bnd.annotation.component.Component;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import javax.swing.JOptionPane;
import org.lorainelab.igb.menu.api.MenuBarEntryProvider;
import org.lorainelab.igb.menu.api.model.MenuBarParentMenu;
import org.lorainelab.igb.menu.api.model.MenuItem;
@Component(immediate = true)
public class MenuBarExtensionExample implements MenuBarEntryProvider {

    @Override
    public Optional<List<MenuItem>> getMenuItems() {
        MenuItem menuItem = new MenuItem("Hello World App", (Void t) -> {
            JOptionPane.showMessageDialog(null, "Hello IGB World!");
            return t;
        });
        menuItem.setWeight(1000000000);
        return Optional.ofNullable(Arrays.asList(menuItem));
    }

    @Override
    public MenuBarParentMenu getMenuExtensionParent() {
        return MenuBarParentMenu.TOOLS;
    }
}
Info

Note that NetBeans adds the package and class declarations automatically.

About the implementation class

The @Component annotation 

Code Block
languagejava
@Component(name = IgbToolbarExtension.COMPONENT_NAME, immediate = true, provide = IgbMenuItemProvider.class)
public class IgbToolbarExtension implements IgbMenuItemProvider {...}

This annotation is processed by the felix maven-bundle-plugin (BND) and declares that the OSGi runtime will manage creation and destruction of any instances of this class. This is an important point to understand about how OSGi-based applicaitions function. Your code will not directly manage the life cycle of your class. Instead, you specify the behavior and lifecycle of your objects using annotations.

The immediate=true annotation attribute specifies to the OSGi runtime that this component should be instantiated immediately instead of lazily. 

See http://www.aqute.biz/Bnd/Components for more information about the annotation attributes.

The LogEventAction

When a user selects your new menu item, this action will execute. Note it prints a message to the IGB console. Note also that it uses the SLF4j API to log events to the IGB console.