Package de.innovationgate.eclipse.wgadesigner.actions

Source Code of de.innovationgate.eclipse.wgadesigner.actions.StartStopWGARuntime

/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* 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.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner.actions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowPulldownDelegate;
import org.eclipse.ui.PartInitException;

import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.wgadesigner.ResourceIDs;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.natures.WGARuntime;
import de.innovationgate.eclipse.wgadesigner.tomcat.TomcatServerStateListener;
import de.innovationgate.eclipse.wgadesigner.tomcat.TomcatUtils;
import de.innovationgate.eclipse.wgadesigner.utils.WGARuntimeSortByName;

public class StartStopWGARuntime implements IWorkbenchWindowPulldownDelegate, SelectionListener, TomcatServerStateListener {

  private static final String ATTRIB_COMMAND = "menuItemCommandAttribute";
 
  private static final String COMMAND_START = "menuItemCommandStart";
  private static final String COMMAND_STOP = "menuItemCommandStop";
  private static final String COMMAND_EDIT = "menuItemCommandEdit";
 
  private IWorkbenchWindow _window;

  private WGARuntime _lastStartedRuntime;

  public Menu getMenu(Control parent) {
   
    Menu menu = new Menu(parent);
   
    List<WGARuntime> runtimeList = new ArrayList<WGARuntime>(WGADesignerPlugin.getAllRuntimes().values());
    Collections.sort(runtimeList, new WGARuntimeSortByName());
    Iterator<WGARuntime> runtimes = runtimeList.iterator();
    while (runtimes.hasNext()) {
      WGARuntime runtime = runtimes.next();
     
      MenuItem runtimeItem = new MenuItem(menu, SWT.CASCADE);
      runtimeItem.setText(runtime.getName());
      runtimeItem.setData(runtime);
     
      Menu runtimeMenu = new Menu(parent.getShell(), SWT.DROP_DOWN);
      runtimeItem.setMenu(runtimeMenu);
     
      MenuItem startItem = new MenuItem(runtimeMenu, SWT.DROP_DOWN);
      startItem.setText("start");
      startItem.setImage(WGADesignerPlugin.getDefault().getImageRegistry().get(WGADesignerPlugin.IMAGE_WGA_SERVER_START));
      startItem.setData(runtime);
      startItem.setData(ATTRIB_COMMAND, COMMAND_START);
      startItem.addSelectionListener(this);
      if (TomcatUtils.getInstance().isRunning(runtime)) {
        startItem.setEnabled(false);
      }
     
      MenuItem stopItem = new MenuItem(runtimeMenu, SWT.DROP_DOWN);
      stopItem.setText("stop");
      stopItem.setImage(WGADesignerPlugin.getDefault().getImageRegistry().get(WGADesignerPlugin.IMAGE_WGA_SERVER_STOP));
      stopItem.setData(runtime);
      stopItem.setData(ATTRIB_COMMAND, COMMAND_STOP);
      stopItem.addSelectionListener(this);
      if (!TomcatUtils.getInstance().isRunning(runtime)) {
        stopItem.setEnabled(false);
      }
     
      MenuItem configureItem = new MenuItem(runtimeMenu, SWT.DROP_DOWN);
      configureItem.setText("configure");
      configureItem.setImage(WGADesignerPlugin.getDefault().getImageRegistry().get(WGADesignerPlugin.IMAGE_WGA_RUNTIME_EDIT));
      configureItem.setData(runtime);
      configureItem.setData(ATTRIB_COMMAND, COMMAND_EDIT);
      configureItem.addSelectionListener(this);
    }
   
   
    return menu;
  }

  public void dispose() {
    TomcatUtils.getInstance().removeListener(this);
  }

  public void init(IWorkbenchWindow window) { 
    _window = window;
    TomcatUtils.getInstance().addListener(this);
  }

  public void run(IAction action) {
    if (_lastStartedRuntime != null) {
      if (!TomcatUtils.getInstance().isRunning(_lastStartedRuntime)) {
        StartWGARuntime.call(_lastStartedRuntime);
      } else if (TomcatUtils.getInstance().isRunning(_lastStartedRuntime)) {
        RestartWGARuntime.call();       
      }
    }
  }

  public void selectionChanged(IAction action, ISelection selection) {
    // TODO Auto-generated method stub

  }

  public void widgetDefaultSelected(SelectionEvent e) {   
  }

  public void widgetSelected(SelectionEvent e) {
    if (e.getSource() instanceof MenuItem) {
      MenuItem item = (MenuItem) e.getSource();
      WGARuntime runtime = (WGARuntime) item.getData();
      String command = (String) item.getData(ATTRIB_COMMAND);
      if (COMMAND_START.equals(command)) {
        StartWGARuntime.call(runtime);
      } else if (COMMAND_STOP.equals(command)){
        StopWGARuntime.call();
      } else if (COMMAND_EDIT.equals(command)) {       
        try {
          WorkbenchUtils.openEditor(WGADesignerPlugin.getDefault().getWorkbench(), runtime.getConfigFile(), ResourceIDs.EDITOR_WGA_RUNTIME);
        } catch (PartInitException e1) {
          WorkbenchUtils.showErrorDialog(WGADesignerPlugin.getDefault(), _window.getShell(), "Unable to open runtime configuration.", "Unable to open configuration of runtime '" + runtime.getName() + "'.", e1);
        }
      }
    }
   
  }

  public void stateChanged(int state) {
    if (state == TomcatServerStateListener.STATE_RUNNING) {
      _lastStartedRuntime = TomcatUtils.getInstance().getCurrentRuntime();
    }   
  }

}
TOP

Related Classes of de.innovationgate.eclipse.wgadesigner.actions.StartStopWGARuntime

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.