com.kopson.cite.application.ApplicationActionBarAdvisor.java Source code

Java tutorial

Introduction

Here is the source code for com.kopson.cite.application.ApplicationActionBarAdvisor.java

Source

/*******************************************************************************
 * Copyright (c) 2012 Piotr Kopka
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.kopson.cite.com/legal/epl-v10.html
 *
 * Contributors:
 *     Piotr Kopka
 *******************************************************************************/

package com.kopson.cite.application;

import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

import com.kopson.cite.actions.AddDevice;
import com.kopson.cite.actions.AddSmartLogRow;
import com.kopson.cite.actions.DeleteSmartLogRow;
import com.kopson.cite.actions.ScriptEditorRun;

/**
 * An action bar advisor is responsible for creating, adding, and disposing of
 * the actions added to a workbench window. Each window will be populated with
 * new actions.
 */
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

    /**
     * Program exit menu action. 
     */
    private IWorkbenchAction exitAction;

    /**
     * Show about dialog box menu action.
     */
    private IWorkbenchAction aboutAction;

    /**
     * Add device menu action.
     */
    private AddDevice addDeviceAction;

    /**
     * Open script editor menu action.
     */
    private ScriptEditorRun runScriptEditorAction;

    /**
     * Add row to logs table menu action.
     */
    private AddSmartLogRow addSmartLogRowAction;

    /**
     * Delete row from logs table menu action.
     */
    private DeleteSmartLogRow deleteSmartLogRowAction;

    /** 
     * Actions - important to allocate these only in makeActions, and then use
     * them in the fill methods. This ensures that the actions aren't recreated
     * when fillActionBars is called with FILL_PROXY.
     * 
     * @param configurer
     */
    public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
        super(configurer);
    }

    /**
     * Make menu actions.
     * 
     * @param window
     */
    @Override
    protected void makeActions(IWorkbenchWindow window) {
        exitAction = ActionFactory.QUIT.create(window);
        register(exitAction);
        aboutAction = ActionFactory.ABOUT.create(window);
        aboutAction.setText("About");
        register(aboutAction);
        addDeviceAction = new AddDevice(window);
        register(addDeviceAction);
        runScriptEditorAction = new ScriptEditorRun(window);
        register(runScriptEditorAction);
        addSmartLogRowAction = new AddSmartLogRow(window);
        register(addSmartLogRowAction);
        deleteSmartLogRowAction = new DeleteSmartLogRow(window);
        register(deleteSmartLogRowAction);
        //Register actions in action registy so the can by run from any point of application
        Activator.getDefault().addActionToRegistry(runScriptEditorAction.getId(), runScriptEditorAction);
    }

    /**
     * Fill menu bar with actions.
     * 
     * @param menu bar.
     */
    @Override
    protected void fillMenuBar(IMenuManager menuBar) {

        MenuManager fileMenu = new MenuManager(CITE.MENU_FILE, CITE.MENU_FILE_ID);
        fileMenu.add(exitAction);

        MenuManager actionMenu = new MenuManager(CITE.MENU_ACTION, CITE.MENU_ACTION_ID);
        actionMenu.add(addDeviceAction);
        actionMenu.add(new Separator());
        actionMenu.add(runScriptEditorAction);

        MenuManager smartLogMenu = new MenuManager(CITE.MENU_SMARTLOG, CITE.MENU_SMARTLOG_ID);
        smartLogMenu.add(addSmartLogRowAction);
        smartLogMenu.add(deleteSmartLogRowAction);

        MenuManager helpMenu = new MenuManager(CITE.MENU_HELP, CITE.MENU_HELP_ID);
        helpMenu.add(aboutAction);

        menuBar.add(fileMenu);
        menuBar.add(actionMenu);
        menuBar.add(smartLogMenu);
        menuBar.add(helpMenu);
    }

    /**
     * 
     */
    @Override
    protected void fillCoolBar(ICoolBarManager coolBar) {
        IToolBarManager toolbar = new ToolBarManager(coolBar.getStyle());
        coolBar.add(toolbar);
        toolbar.add(addDeviceAction);
    }

    protected void fillTrayItem(IMenuManager trayItem) {
        trayItem.add(aboutAction);
        trayItem.add(exitAction);
    }
}