/**
  * Convert multiline text to array of {@code IMakeTarget}s. Each line is interpreted as one
  * separate make command.
  *
  * @param multilineText - input text.
  * @param container - container where the targets will belong.
  * @return resulting array of {@code IMakeTarget}s.
  */
 private static IMakeTarget[] prepareMakeTargetsFromString(
     String multilineText, IContainer container) {
   if (container != null) {
     String[] lines = multilineText.split("[\n\r]"); // $NON-NLS-1$
     List<IMakeTarget> makeTargets = new ArrayList<IMakeTarget>(lines.length);
     for (String command : lines) {
       command = command.trim();
       if (command.length() > 0) {
         String name = command;
         String buildCommand = command;
         String buildTarget = null;
         String defaultBuildCommand =
             MakeTargetDndUtil.getProjectBuildCommand(container.getProject());
         if (command.startsWith(defaultBuildCommand + " ")) { // $NON-NLS-1$
           buildCommand = defaultBuildCommand;
           buildTarget = command.substring(defaultBuildCommand.length() + 1).trim();
           name = buildTarget;
         }
         try {
           makeTargets.add(
               MakeTargetDndUtil.createMakeTarget(name, buildTarget, buildCommand, container));
         } catch (CoreException e) {
           // Ignore failed targets
           MakeUIPlugin.log(e);
         }
       }
     }
     return makeTargets.toArray(new IMakeTarget[makeTargets.size()]);
   }
   return null;
 }
  /**
   * Creates make targets array from filenames array. These will be loose targets not connected to
   * global make target list managed by MakeTargetManager
   *
   * @param filenames - array of filenames. Each filename expected to be an actual file otherwise a
   *     user gets a warning popup.
   * @param dropContainer - a container where the targets are being created.
   * @param shell - a shell to display warnings to user. If null, no warnings are displayed.
   * @return array of make targets.
   */
  private static IMakeTarget[] prepareMakeTargetsFromFiles(
      String[] filenames, IContainer dropContainer, Shell shell) {
    List<IMakeTarget> makeTargetsList = new ArrayList<IMakeTarget>(filenames.length);

    int errorCount = 0;
    int nonFileCount = 0;
    for (String filepath : filenames) {
      IPath path = new Path(filepath);
      File file = path.toFile();
      if (file.isFile()) {
        String name = path.lastSegment();
        try {
          String buildCommand =
              MakeTargetDndUtil.getProjectBuildCommand(dropContainer.getProject());
          makeTargetsList.add(
              MakeTargetDndUtil.createMakeTarget(name, filepath, buildCommand, dropContainer));
        } catch (CoreException e) {
          errorCount++;
          MakeUIPlugin.log(e);
        }
      } else {
        nonFileCount++;
      }
    }

    if (shell != null) {
      if (errorCount > 0) {
        MessageDialog.openError(
            shell,
            MakeUIPlugin.getResourceString("MakeTargetDnD.title.createError"), // $NON-NLS-1$
            MakeUIPlugin.getResourceString("MakeTargetDnD.message.createError")); // $NON-NLS-1$
      }
      if (nonFileCount > 0) {
        MessageDialog.openInformation(
            shell,
            MakeUIPlugin.getResourceString("MakeTargetDnD.title.createInfo"), // $NON-NLS-1$
            MakeUIPlugin.getResourceString(
                "MakeTargetDnD.message.createNonFileTargetAttempt")); //$NON-NLS-1$
      }
    }

    return makeTargetsList.toArray(new IMakeTarget[makeTargetsList.size()]);
  }
  /**
   * Combined operation of creating make targets in Make Target View from multiline text. The method
   * will ask a confirmation if user tries to drop more then 1 target to prevent easily made mistake
   * of unintended copying of old contents of the clipboard.
   *
   * @param multilineText - input make target commands in textual form.
   * @param dropContainer - container where add the targets.
   * @param operation - operation such as copying or moving. Must be a {@link
   *     org.eclipse.swt.dnd.DND} operation.
   * @param shell - a shell to display progress of operation to user.
   * @see DND#DROP_NONE
   * @see DND#DROP_COPY
   * @see DND#DROP_MOVE
   * @see DND#DROP_LINK
   */
  public static void createMultilineTargetsUI(
      String multilineText, IContainer dropContainer, int operation, Shell shell) {

    IMakeTarget[] makeTargets = prepareMakeTargetsFromString(multilineText, dropContainer);
    boolean confirmed = true;
    if (makeTargets.length > 1) {
      String title =
          MakeUIPlugin.getResourceString(
              "MakeTargetDnD.title.createFromTextConfirm"); //$NON-NLS-1$
      String question =
          MessageFormat.format(
              MakeUIPlugin.getResourceString(
                  "MakeTargetDnD.message.createFromTextConfirm"), //$NON-NLS-1$
              new Object[] {new Integer(makeTargets.length)});

      String topTargets = ""; // $NON-NLS-1$
      for (int i = 0; i < makeTargets.length; i++) {
        // limit dimensions of the confirm dialog
        final int HEIGHT_LIMIT = 20;
        final int LENGTH_LIMIT = 200;
        if (i > HEIGHT_LIMIT) {
          topTargets = topTargets + "..."; // $NON-NLS-1$
          break;
        }
        String name = makeTargets[i].getName();
        if (name.length() > LENGTH_LIMIT) {
          name = name.substring(0, LENGTH_LIMIT - 3) + "..."; // $NON-NLS-1$
        }
        topTargets = topTargets + name + "\n"; // $NON-NLS-1$
      }

      confirmed = MessageDialog.openConfirm(shell, title, question + topTargets);
    }
    if (confirmed) {
      MakeTargetDndUtil.copyTargets(makeTargets, dropContainer, operation, shell);
    }
  }
 /**
  * Creates make targets from array of filenames in Make Target View. Each file will be a separate
  * target in the view.
  *
  * @param filenames - array of filenames. Each filename expected to be an actual file otherwise a
  *     user gets a warning popup.
  * @param dropContainer - a container where the targets are being created.
  * @param operation - drop/paste operation.
  * @param shell - a shell to display warnings to the user.
  */
 public static void createFileTargetsUI(
     String[] filenames, IContainer dropContainer, int operation, Shell shell) {
   IMakeTarget[] makeTargets = prepareMakeTargetsFromFiles(filenames, dropContainer, shell);
   MakeTargetDndUtil.copyTargets(makeTargets, dropContainer, operation, shell);
 }