/**
   * @see org.teiid.designer.ui.common.actions.IActionWorker#getEnableState()
   * @since 4.2
   */
  @Override
  public boolean setEnabledState() {
    boolean enable = false;
    Object selection = getSelection();
    boolean foundResource = false;
    if (selection instanceof ISelection) {
      ISelection iSelection = (ISelection) selection;
      if (!iSelection.isEmpty() && !isReadOnly() && canLegallyEditResource()) {
        if (SelectionUtilities.isAllEObjects(iSelection)) {
          enable = true;
          // check each object, break out if false
          for (Iterator iter = SelectionUtilities.getSelectedEObjects(iSelection).iterator();
              iter.hasNext() && enable; ) {
            EObject obj = (EObject) iter.next();
            // Check Read-Only, Check if Diagram (ask diagram), Check Helper
            if (obj == null || ModelObjectUtilities.isReadOnly(obj)) {
              enable = false; // and quit checking
            } else {
              if (obj instanceof Diagram) {
                enable = DiagramHelperManager.canDelete((Diagram) obj);
              } else {
                enable = ModelObjectEditHelperManager.canDelete(obj); // and keep checking
              }
              // We got this far, now we need to cache up a focused object for opening an editor
              // so it reveals the proper container (i.e. package diagram)
              // Only do this once for effiency
              if (!foundResource && enable) {
                focusedObject = obj;
                foundResource = true;
                modelResource = ModelUtilities.getModelResourceForModelObject(obj);
              }
            }
            if (!enable) break;
          }
        }
      }
    }

    if (!enable) {
      modelResource = null;
      focusedObject = null;
    }

    if (UiPlugin.getDefault().getCurrentWorkbenchWindow().getActivePage() != null) {
      IWorkbenchPart theActivePart =
          UiPlugin.getDefault().getCurrentWorkbenchWindow().getActivePage().getActivePart();
      if (theActivePart != null) {
        // System.out.println(" >>> DeleteWorker.setEnabledState()  Active Part = " +
        // theActivePart);
      }
    }
    return enable;
  }
  /**
   * Returns a VDB editor given a vdb resource if editor is open
   *
   * @param vdb the vdb
   * @return the vdb editor
   */
  public static VdbEditor getVdbEditorForFile(IResource vdb) {
    if (vdb != null && vdb.exists()) {
      IWorkbenchWindow window = UiPlugin.getDefault().getCurrentWorkbenchWindow();

      if (window != null) {
        final IWorkbenchPage page = window.getActivePage();

        if (page != null) {
          // look through the open editors and see if there is one available for this model file.
          IEditorReference[] editors = page.getEditorReferences();
          for (int i = 0; i < editors.length; ++i) {

            IEditorPart editor = editors[i].getEditor(false);
            if (editor != null) {
              IEditorInput input = editor.getEditorInput();
              if (input instanceof IFileEditorInput) {
                if (vdb.equals(((IFileEditorInput) input).getFile())
                    || vdb.getFullPath()
                        .equals(((IFileEditorInput) input).getFile().getFullPath())) {
                  // found it;
                  if (ModelUtil.isVdbArchiveFile(vdb)) {
                    return (VdbEditor) editor;
                  }
                  break;
                }
              }
            }
          }
        }
      }
    }

    return null;
  }
  /**
   * We will compute column-level statistics (min-value, max-value, # of null values, # of distinct
   * values) for all columns in tables in the model IFF the model is physical relational with a Jdbc
   * source. We must first prompt the user for the password, as it is not stored with the Jdbc
   * import settings.
   *
   * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
   * @since 4.3
   */
  @Override
  public void run() {
    if (isEnabled()) {
      final Shell shell = UiPlugin.getDefault().getCurrentWorkbenchWindow().getShell();
      try {
        ModelResource modelResource = ModelUtil.getModelResource(this.selectedModel, false);
        if (modelResource != null) {

          boolean cancelled = openEditorIfNeeded(modelResource);

          if (cancelled) {
            return;
          }
          final Resource resource = modelResource.getEmfResource();

          executeInTransaction(resource, shell);
        }
      } catch (Exception e) {
        InternalModelerJdbcUiPluginConstants.Util.log(e);
        final String title =
            InternalModelerJdbcUiPluginConstants.Util.getString(
                "CostAnalysisAction.errorTitle"); //$NON-NLS-1$
        final String message =
            InternalModelerJdbcUiPluginConstants.Util.getString(
                "CostAnalysisAction.errorMessage"); //$NON-NLS-1$
        MessageDialog.openError(shell, title, message);
      }
    }
  }
  /**
   * Should only be called if current object and model are not <code>null</code>.
   *
   * @since 5.5.3
   */
  private boolean openEditorIfNeeded(ModelResource currentModel) {
    boolean openEditorCancelled = false;
    // we only need to worry about the readonly status if the file is not currently open,
    // and its underlying IResource is not read only

    if (currentModel == null) {

    } else if (!isEditorOpen(currentModel)
        && !currentModel.getResource().getResourceAttributes().isReadOnly()) {
      final IFile modelFile = (IFile) currentModel.getResource();
      Shell shell = UiPlugin.getDefault().getCurrentWorkbenchWindow().getShell();

      // may want to change these text strings eventually:
      if (MessageDialog.openQuestion(
          shell, ModelEditorManager.OPEN_EDITOR_TITLE, ModelEditorManager.OPEN_EDITOR_MESSAGE)) {
        // load and activate, not async (to prevent multiple dialogs from coming up):
        // Changed to use method that insures Object editor mode is on
        ModelEditorManager.openInEditMode(
            modelFile, true, UiConstants.ObjectEditor.IGNORE_OPEN_EDITOR);

      } else {
        openEditorCancelled = true;
      }
    }

    return openEditorCancelled;
  }
  // MyDefect : Refactored
  private void displayErrorMessage(ResourceRefactorCommand rrcCommand) {
    // if there are problems, use the common error dialog to report them
    if (rrcCommand.getPostExecuteMessages() != null
        && rrcCommand.getPostExecuteMessages().size() > 0) {
      RefactorCommandProcessorDialog rcpdDialog =
          new RefactorCommandProcessorDialog(
              UiPlugin.getDefault().getCurrentWorkbenchWindow().getShell(), rrcCommand);

      rcpdDialog.open();
    }
  }
  /**
   * Finds the visible VDB Editor for the supplied VDB
   *
   * <p>If an editor is NOT open for this vdb, then null is returned.
   *
   * @param vdb
   * @return the VdbEditor
   */
  public VdbEditor getVdbEditor(final IFile vdb) {
    final IWorkbenchWindow window = UiPlugin.getDefault().getCurrentWorkbenchWindow();

    if (window != null) {
      final IWorkbenchPage page = window.getActivePage();

      if (page != null) {
        VdbEditor editor = findEditorPart(page, vdb);
        if (editor != null) {
          return editor;
        }
      }
    }
    return null;
  }
 /**
  * Obtains the <code>IPreferenceStore</code> where this preference is being persisted.
  *
  * @return the preference store
  */
 private IPreferenceStore getPreferenceStore() {
   return UiPlugin.getDefault().getPreferenceStore();
 }
  @Override
  public void run(IAction action) {
    /*
     *  0. instantiate a move command
     *  1. instantiate the FileFolderMoveDialog
     *  2. 'init' it with:
     *      - the selection
     *      - the rename command
     *      - a ModelContainerSelectionValidator
     *
     *  3. if dlg is ok,
     *      a) set the destination (from the dialog) on the command
     *      a) execute the move command
     *      b) add the command to the UndoManager
     */

    // create the move command, set the resource on it
    ResourceMoveCommand rmcCommand = new ResourceMoveCommand();
    rmcCommand.setResource(resSelectedResource);
    rmcCommand.setImportHandler(new OrganizeImportHandlerDialog());

    // cleanup modified files before starting this operation
    boolean bContinue = doResourceCleanup();

    if (!bContinue) {
      return;
    }

    // check if anything wrong with dependents
    if (!checkDependentStatus(rmcCommand, resSelectedResource)) {
      return;
    }

    // create the dialog
    FileFolderMoveDialog ffmdDialog =
        new FileFolderMoveDialog(
            UiPlugin.getDefault().getCurrentWorkbenchWindow().getShell(),
            rmcCommand,
            resSelectedResource,
            new ModelExplorerContentProvider());

    // launch the dialog
    ffmdDialog.open();

    // if result is ok, finish the command and execute it
    if (ffmdDialog.getReturnCode() == Window.OK) {
      Object[] oSelectedObjects = ffmdDialog.getResult();

      // add the user's selected destination to the command
      this.dest = (IContainer) oSelectedObjects[0];
      rmcCommand.setDestination(this.dest);

      // Let's cache the auto-build and reset after.  We don't want auto-building before the
      // refactoring is complete
      boolean autoBuildOn = ModelerCore.getWorkspace().isAutoBuilding();
      if (autoBuildOn) {
        JobUtils.setAutoBuild(false);
      }

      // run it
      executeCommand(rmcCommand);

      if (autoBuildOn) {
        JobUtils.setAutoBuild(true);
      }

      // add the command to the Undo Manager (the manager will deal with whether
      //  the command can be undone or not)
      if (getStatus() != null && getStatus().getSeverity() < IStatus.ERROR) {
        getRefactorUndoManager().addCommand(rmcCommand);
      }

      // if there are problems, use the common error dialog to report them
      if (rmcCommand.getPostExecuteMessages() != null
          && rmcCommand.getPostExecuteMessages().size() > 0) {

        //                System.out.println( "[MoveRefactorAction.run] command has messages: " +
        // rmcCommand.getPostExecuteMessages().size() );  //$NON-NLS-1$

        RefactorCommandProcessorDialog rcpdDialog =
            new RefactorCommandProcessorDialog(
                UiPlugin.getDefault().getCurrentWorkbenchWindow().getShell(), rmcCommand);
        rcpdDialog.open();

      } else {
        //                 System.out.println( "[MoveRefactorAction.run] command has NO messages" );
        //   //$NON-NLS-1$
      }
    }
  }
 /**
  * Constructs a <code>NewSiblingAction</code> where a new sibling is created.
  *
  * @param theDescriptor the descriptor that determines the sibling type created
  */
 public NewSiblingAction(EObject sibling, Command theDescriptor) {
   super(UiPlugin.getDefault());
   this.sibling = sibling;
   setCommand(theDescriptor);
 }
 /**
  * Constructs a <code>NewSiblingAction</code> where no siblings are allowed. This action is not
  * enabled.
  */
 public NewSiblingAction() {
   super(UiPlugin.getDefault());
   configureNoneAllowedState();
 }
 /** @since 4.0 */
 public NewVdbWizard() {
   super(UiPlugin.getDefault(), TITLE, null);
   this.modelsForVdb = new ArrayList<IResource>();
 }
 public TableClipboardPasteAction() {
   super(UiPlugin.getDefault());
 }
 // MyDefect : Refactored
 private IWorkbenchPage getIWorkbenchPage() {
   // defect 16103 - rename partially fails (copy works, delete fails) while editor open.
   IWorkbench iwb = UiPlugin.getDefault().getWorkbench();
   return iwb.getActiveWorkbenchWindow().getActivePage();
 }
 // MyDefect : Refactored
 private FileFolderRenameDialog createFileFolderRenameDialog(ResourceRefactorCommand rrcCommand) {
   return new FileFolderRenameDialog(
       UiPlugin.getDefault().getCurrentWorkbenchWindow().getShell(),
       rrcCommand,
       resSelectedResource);
 }