/**
  * @see org.teiid.designer.core.workspace.ModelWorkspace#createModelProject(java.lang.String,
  *     java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
  * @since 4.0
  */
 @Override
 public ModelProject createModelProject(
     final String name, final IPath path, final IProgressMonitor monitor) throws CoreException {
   CoreArgCheck.isNotNull(name);
   // Check if project already exists
   if (findModelProject(name) != null) {
     throw new ModelWorkspaceException(
         ModelerCore.Util.getString(
             "ModelWorkspaceImpl.cannotCreateModelProject", name)); // $NON-NLS-1$
   }
   // Validate name
   final IWorkspace workspace = ModelerCore.getWorkspace();
   final IStatus status = workspace.validateName(name, IResource.PROJECT);
   if (!status.isOK()) {
     throw new ModelWorkspaceException(
         new ModelStatusImpl(status.getSeverity(), status.getCode(), status.getMessage()));
   }
   // Create new model project
   final IProject project = workspace.getRoot().getProject(name);
   final IProjectDescription desc = workspace.newProjectDescription(project.getName());
   desc.setLocation(path);
   desc.setNatureIds(MODEL_NATURES);
   final IWorkspaceRunnable op =
       new IWorkspaceRunnable() {
         @Override
         public void run(final IProgressMonitor monitor) throws CoreException {
           project.create(desc, monitor);
           project.open(monitor);
         }
       };
   workspace.run(op, monitor);
   return new ModelProjectImpl(project, this);
 }
 /**
  * Used to find a resource using a relative URI referenced to a known resource
  *
  * @see
  *     org.teiid.designer.core.container.ResourceFinder#findByWorkspaceUri(org.eclipse.emf.common.util.URI,
  *     org.eclipse.emf.ecore.resource.Resource)
  * @since 5.0.2
  */
 @Override
 public Resource findByWorkspaceUri(final URI theRelativeUri, final Resource knownResource) {
   try {
     // Defect 23396 - an NPE was thrown here because a relativeUri was provide (i.e.
     // BooksDatatypes.xsd) which resulted
     // in a NULL model Resource or null corresponding resource.
     ModelResource mr = ModelerCore.getModelEditor().findModelResource(knownResource);
     if (mr != null && mr.getCorrespondingResource() != null) {
       IPath thePath =
           mr.getCorrespondingResource()
               .getFullPath()
               .removeLastSegments(1)
               .append(theRelativeUri.toString());
       IResource iResrc = ModelerCore.getWorkspace().getRoot().findMember(thePath);
       if (iResrc instanceof IFile) {
         ModelResource modelResource =
             ModelerCore.getModelEditor().findModelResource((IFile) iResrc);
         if (modelResource != null) {
           return modelResource.getEmfResource();
         }
       }
     }
   } catch (ModelWorkspaceException theException) {
     ModelerCore.Util.log(theException);
   }
   return null;
 }
 /**
  * Validates the destination file if it is read-only and additionally the source file if both are
  * read-only. Returns true if both files could be made writeable.
  *
  * @param source source file
  * @param destination destination file
  * @param shell ui context for the validation
  * @return boolean <code>true</code> both files could be made writeable. <code>false</code> either
  *     one or both files were not made writeable
  */
 boolean validateEdit(IFile source, IFile destination, Shell shell) {
   if (destination.isReadOnly()) {
     IWorkspace workspace = ModelerCore.getWorkspace();
     IStatus status;
     if (source.isReadOnly())
       status = workspace.validateEdit(new IFile[] {source, destination}, shell);
     else status = workspace.validateEdit(new IFile[] {destination}, shell);
     return status.isOK();
   }
   return true;
 }
  void handleViewModelLocationBrowse() {
    final IContainer folder =
        WidgetUtil.showFolderSelectionDialog(
            ModelerCore.getWorkspace().getRoot(),
            new ModelingResourceFilter(),
            new ModelProjectSelectionStatusValidator());

    if (folder != null && sourceModelContainerText != null) {
      // viewModelContainerText.setText(folder.getFullPath().makeRelative().toString());
      this.importManager.setViewModelLocation(folder);
      this.viewModelContainerText.setText(folder.getFullPath().makeRelative().toString());
    }

    notifyChanged();
  }
 /** Compute the non-java resources contained in this java project. */
 private Object[] computeNonModelResources() {
   IProject[] projects = ModelerCore.getWorkspace().getRoot().getProjects();
   int length = projects.length;
   Object[] nonModelResourcesTemp = null;
   int index = 0;
   for (int i = 0; i < length; i++) {
     IProject project = projects[i];
     if (!ModelerCore.hasModelNature(project)) {
       if (nonModelResourcesTemp == null) {
         nonModelResourcesTemp = new Object[length];
       }
       nonModelResourcesTemp[index++] = project;
     }
   }
   if (index == 0) return NO_NON_MODEL_RESOURCES;
   if (index < length) {
     System.arraycopy(nonModelResources, 0, nonModelResources = new Object[index], 0, index);
   }
   return nonModelResourcesTemp;
 }
  /**
   * Return the new name to be given to the target resource.
   *
   * @return java.lang.String
   * @param context IVisualPart
   */
  protected String queryNewResourceName(final IResource resource) {
    final IWorkspace workspace = ModelerCore.getWorkspace();
    final IPath prefix = resource.getFullPath().removeLastSegments(1);
    IInputValidator validator =
        new IInputValidator() {
          /**
           * {@inheritDoc}
           *
           * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
           */
          @Override
          public String isValid(String string) {
            if (resource.getName().equals(string)) {
              return UiConstants.Util.getString(
                  "RenameResourceAction.nameMustBeDifferent"); //$NON-NLS-1$
            }
            IStatus status = workspace.validateName(string, resource.getType());
            if (!status.isOK()) {
              return status.getMessage();
            }
            if (workspace.getRoot().exists(prefix.append(string))) {
              return UiConstants.Util.getString("RenameResourceAction.nameExists"); // $NON-NLS-1$
            }
            return null;
          }
        };

    InputDialog dialog =
        new InputDialog(
            getShell(),
            UiConstants.Util.getString("RenameResourceAction.inputDialogTitle"), // $NON-NLS-1$
            UiConstants.Util.getString("RenameResourceAction.inputDialogMessage"), // $NON-NLS-1$
            resource.getName(),
            validator);
    dialog.setBlockOnOpen(true);
    dialog.open();
    return dialog.getValue();
  }
  public static Collection getAllWorkspaceResources(final ResourceFilter filter) {
    // Collect all IResources within all IProjects
    final FileResourceCollectorVisitor visitor = new FileResourceCollectorVisitor(filter);
    final IProject[] projects = ModelerCore.getWorkspace().getRoot().getProjects();
    for (final IProject project : projects)
      try {
        project.accept(visitor);
      } catch (final CoreException e) {
        // do nothing
      }

    final Collection fileResources = visitor.getFileResourcesCollection();
    final Iterator itor = fileResources.iterator();
    while (itor.hasNext()) {
      final IFile fileResource = (IFile) itor.next();
      final IPath path = fileResource.getFullPath();
      // Do not process file names starting with '.' since these
      // are considered reserved for Eclipse specific files
      if (path.lastSegment().charAt(0) == '.') itor.remove();
    } // endwhile

    return fileResources;
  }
  @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$
      }
    }
  }
 /** Returns the workbench associated with this object. */
 @Override
 public IWorkspace getWorkspace() {
   return ModelerCore.getWorkspace();
 }
 /** Returns the workbench associated with this object. */
 private static IWorkspace getWorkspace() {
   return ModelerCore.getWorkspace();
 }