package brn.gui.fwdgraph.diagram.part;

import org.eclipse.emf.edit.ui.action.LoadResourceAction.LoadResourceDialog;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import brn.gui.fwdgraph.diagram.edit.parts.GraphEditPart;

/**
 * @generated
 */
public class ModelLoadResourceAction implements IObjectActionDelegate {

  /**
   * @generated
   */
  private GraphEditPart mySelectedElement;

  /**
   * @generated
   */
  private Shell myShell;

  /**
   * @generated
   */
  public void setActivePart(IAction action, IWorkbenchPart targetPart) {
    myShell = targetPart.getSite().getShell();
  }

  /**
   * @generated
   */
  public void run(IAction action) {
    LoadResourceDialog loadResourceDialog = new LoadResourceDialog(myShell,
        mySelectedElement.getEditingDomain());
    loadResourceDialog.open();
  }

  /**
   * @generated
   */
  public void selectionChanged(IAction action, ISelection selection) {
    mySelectedElement = null;
    if (selection instanceof IStructuredSelection) {
      IStructuredSelection structuredSelection = (IStructuredSelection) selection;
      if (structuredSelection.size() == 1
          && structuredSelection.getFirstElement() instanceof GraphEditPart) {
        mySelectedElement = (GraphEditPart) structuredSelection
            .getFirstElement();
      }
    }
    action.setEnabled(isEnabled());
  }

  /**
   * @generated
   */
  private boolean isEnabled() {
    return mySelectedElement != null;
  }

}
