package net.kldp.logviewer;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.GroupMarker;
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.ToolBarContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.RetargetAction;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.plugin.AbstractUIPlugin;

/**
 * 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 {

    // 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.
    private IWorkbenchAction exitAction;
    private IWorkbenchAction aboutAction;
//    private IWorkbenchAction newWindowAction;
    private OpenViewAction logViewStartAction;	// 로그 보기 시작
//    private Action logViewEndAction;			// 로그 보기 종료
    private Action delLogGroupAction;    		// 삭제하기 로그 그룹
    
    private Action newLogAction;				// 새로운 로그 만들기
    private Action modifyLogAction;				// 로그 정보 수
    private Action delLogAction;    			// 삭제하기 로그 항
//    private Action allViewAction;    			// 로그파일의 전체를 본다.
    private RetargetAction stopAction;			// 로그보기 잠시 종료
    private RetargetAction restrartAction;			// 로그보기 리스타
    
    private NewExtensionAction newExtensionAction;
    private ManageExtensionsAction manageAction;

    public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
        super(configurer);
    }
    
    protected void makeActions(final IWorkbenchWindow window) {
        // Creates the actions and registers them.
        // Registering is needed to ensure that key bindings work.
        // The corresponding commands keybindings are defined in the plugin.xml file.
        // Registering also provides automatic disposal of the actions when
        // the window is closed.

        exitAction = ActionFactory.QUIT.create(window);
        register(exitAction);
        
        aboutAction = ActionFactory.ABOUT.create(window);
        register(aboutAction);
        
        newExtensionAction = new NewExtensionAction(window);
		register(newExtensionAction);
		
		manageAction = new ManageExtensionsAction(window);
		register(manageAction);
        
//        newWindowAction = ActionFactory.OPEN_NEW_WINDOW.create(window);
//        register(newWindowAction);
        
        newLogAction = new AddLogAction(window, "&로그추가");
        register(newLogAction);
        
        modifyLogAction = new ModifyLogAction(window, "&로그수정");
        register(modifyLogAction);
        
//        delLogGroupAction = new DelGroupAction(window, "그룹 삭제");
//        register(delLogGroupAction);
        
        delLogAction = new DelLogAction(window, "&항목삭제");
        register(delLogAction);
        
        logViewStartAction = new OpenViewAction(window, "&보기시작", View.ID);
        register(logViewStartAction);
        
        //-----------------------------------------------------------------------
        stopAction = new RetargetAction("StopView", "&잠시멈춤");
        stopAction.setActionDefinitionId(View.ID + "Stop");
        stopAction.setToolTipText("잠시멈춤");
        stopAction.setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKeys.LOG_STOP));
		window.getPartService().addPartListener(stopAction);
        register(stopAction);
        
        restrartAction = new RetargetAction("RestartView", "&다시시작");
        restrartAction.setActionDefinitionId(View.ID + "ReStart");
        restrartAction.setToolTipText("다시시작");
        restrartAction.setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKeys.LOG_RESTART));
		window.getPartService().addPartListener(restrartAction);
        register(restrartAction);
        //----------------------------------------------------
        
        // 로그 내용 전체를 보여줌
//        allViewAction = new AllLogViewAction(window, "로그 전체 보기", AllLogView.ID);
//        register(allViewAction);                
    }
    
    protected void fillMenuBar(IMenuManager menuBar) {
        MenuManager fileMenu = new MenuManager("&File", IWorkbenchActionConstants.M_FILE);
        MenuManager helpMenu = new MenuManager("&Help", IWorkbenchActionConstants.M_HELP);
        
        menuBar.add(fileMenu);
        // Add a group marker indicating where action set menus will appear.
        menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
        menuBar.add(helpMenu);
        
        // File
//        fileMenu.add(newWindowAction);
        
//        fileMenu.add(new Separator());
//        fileMenu.add(delLogGroupAction);	// 그룹 삭제
//        fileMenu.add(new Separator());
        
        fileMenu.add(newLogAction);			// 생성
        fileMenu.add(modifyLogAction);		// 수정
        fileMenu.add(delLogAction);			// 수정        
        fileMenu.add(new Separator());
        
        fileMenu.add(new Separator());
        fileMenu.add(logViewStartAction);
//        fileMenu.add(allViewAction);
        
        fileMenu.add(new Separator());
        fileMenu.add(exitAction);
        
        // Help
        helpMenu.add(newExtensionAction);
        helpMenu.add(manageAction);
        helpMenu.add(aboutAction);        
    }
    
    protected void fillCoolBar(ICoolBarManager coolBar) {
        IToolBarManager toolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);
        coolBar.add(new ToolBarContributionItem(toolbar, "main"));   

        // 그룹관련
        // toolbar.add(delLogGroupAction);
        
        // entity 관련
        ActionContributionItem newCI = new ActionContributionItem(newLogAction);        
        newCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);
        toolbar.add(newCI);        
        
        // 항목수정
        ActionContributionItem modifyCI = new ActionContributionItem(modifyLogAction);        
        modifyCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);
        toolbar.add(modifyCI);
        
        // 항목삭제
        ActionContributionItem delCI = new ActionContributionItem(delLogAction);        
        delCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);
        toolbar.add(delCI);        
        
        toolbar.add(new Separator());
        // 보기시작
        ActionContributionItem startCI = new ActionContributionItem(logViewStartAction);        
        startCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);	
        toolbar.add(startCI);

        // 전체로그
//        ActionContributionItem allViewCI = new ActionContributionItem(allViewAction);        
//        allViewCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);	
//        toolbar.add(allViewCI);
        
        // 잠시정지
        ActionContributionItem stopCI = new ActionContributionItem(stopAction);        
        stopCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);		
		toolbar.add(stopCI);
		
		// 정지한것 다시 시작
		ActionContributionItem restartCI = new ActionContributionItem(restrartAction);
		restartCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);		
		toolbar.add(restartCI);
		toolbar.add(new Separator());
    }
}
