Example #1
0
  /**
   * Returns the new name to be given to the target resource.
   *
   * @param resource the resource to query status on
   * @return the new name
   */
  protected String queryNewResourceName(final File resource) {
    final IWorkspace workspace = ResourcesPlugin.getWorkspace();
    final IPath prefix = new Path(resource.getAbsolutePath()).removeLastSegments(1);

    IInputValidator validator =
        new IInputValidator() {

          /*
           * (non-Javadoc)
           *
           * @see
           * org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String
           * )
           */
          public String isValid(String string) {
            if (new Path(resource.getName())
                .removeFileExtension()
                .toFile()
                .getName()
                .equals(string)) {
              return Messages.getString("RenameResourceAction.nameExists"); // $NON-NLS-1$
            }

            IPath newPath = new Path(string);

            IStatus status =
                workspace.validateName(
                    newPath.toFile().getName(),
                    resource.isFile() ? IResource.FILE : IResource.FOLDER);

            if (!status.isOK()) {
              return status.getMessage();
            }

            IPath fullPath = prefix.append(string);

            if (fullPath.toFile().exists()) {
              return Messages.getString("RenameResourceAction.nameExists"); // $NON-NLS-1$
            }
            return null;
          }
        };

    InputDialog dialog =
        new InputDialog(
            getShell(),
            Messages.getString("RenameResourceAction.inputDialogTitle"), // $NON-NLS-1$
            Messages.getString("RenameResourceAction.inputDialogMessage"), // $NON-NLS-1$
            new Path(resource.getName()).toFile().getName(),
            validator);

    dialog.setBlockOnOpen(true);
    int result = dialog.open();
    if (result == Window.OK) {
      IPath newPath = new Path(dialog.getValue());

      return newPath.toFile().getName();
    }
    return null;
  }
  /** The behaviour of the action. Checks if the name entered by the user is valid. */
  public void run() {
    ISelection selection = viewer.getSelection();
    if (selection.isEmpty()) {
      this.setEnabled(false);
    }
    TreeObject object = (TreeObject) ((IStructuredSelection) selection).getFirstElement();
    if (object instanceof DocumentTreeObject) {

      String oldValue = object.getValue().toString();

      IInputValidator validator = new TextValidator(oldValue);
      InputDialog dialog =
          new InputDialog(
              parent.getShell(),
              Properties.getProperty("rename"),
              Properties.getProperty("new_name"),
              oldValue,
              validator);
      dialog.setBlockOnOpen(true); // waits for user enter a name

      int res = dialog.open(); // opens the rename dialog

      if (res == InputDialog.OK) {
        String newValue = dialog.getValue();

        String newExtension = GUIUtil.getFileExtension(newValue);
        String oldExtension = GUIUtil.getFileExtension(oldValue);

        if (!newExtension.equalsIgnoreCase(oldExtension)) {
          newValue += oldExtension;
        }

        if (!newValue.equalsIgnoreCase(oldValue)) {
          if (!ProjectManagerController.getInstance().renameDocument(oldValue, newValue)) {
            MessageDialog.openError(
                parent.getShell(),
                Properties.getProperty("error_while_renaming"),
                Properties.getProperty("can_not_rename"));
          }
          try {
            GUIManager.getInstance().refreshViews();
          } catch (TargetException e) {
            MessageDialog.openError(
                this.parent.getShell(),
                Properties.getProperty("error_while_reloading_project"),
                e.getMessage());
            e.printStackTrace();
          }
        }
      }
    }
  }
  /**
   * 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();
  }