/**
   * Adds the specified files to the list of import data.
   *
   * @param files The files to import.
   */
  private void insertFiles(Map<File, StatusLabel> files) {
    if (files == null || files.size() == 0) {
      statusLabel.setText("No files to import.");
      return;
    }
    components = new HashMap<File, FileImportComponent>();
    totalFiles = files.size();
    String text = "Importing " + totalFiles + " file";
    if (totalFiles > 1) text += "s";
    statusLabel.setText(text);

    Entry entry;
    Iterator i = files.entrySet().iterator();
    FileImportComponent c;
    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    int index = 0;
    p.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    File f;
    DatasetData d = dataset;
    Object node = refNode;
    if (folderAsContainer) {
      node = null;
      d = new DatasetData();
      d.setName(file.getName());
    }
    while (i.hasNext()) {
      entry = (Entry) i.next();
      f = (File) entry.getKey();
      c = new FileImportComponent(f, folderAsContainer, browsable, group, singleGroup);
      if (f.isFile()) {
        c.setLocation(data, d, node);
        c.setParent(this);
      }
      c.showContainerLabel(showContainerLabel);
      c.setType(getType());
      attachListeners(c);
      c.setStatusLabel((StatusLabel) entry.getValue());
      if (index % 2 == 0) c.setBackground(UIUtilities.BACKGROUND_COLOUR_EVEN);
      else c.setBackground(UIUtilities.BACKGROUND_COLOUR_ODD);
      components.put((File) entry.getKey(), c);
      p.add(c);
      index++;
    }
    removeAll();
    pane = EditorUtil.createTaskPane("");
    pane.setCollapsed(false);
    setNumberOfImport();

    IconManager icons = IconManager.getInstance();
    pane.setIcon(icons.getIcon(IconManager.DIRECTORY));
    Font font = pane.getFont();
    pane.setFont(font.deriveFont(font.getStyle(), font.getSize() - 2));
    pane.add(p);
    double[][] size = {{TableLayout.FILL}, {TableLayout.PREFERRED}};
    setLayout(new TableLayout(size));
    add(pane, "0, 0");
    validate();
    repaint();
  }
 /**
  * Returns the import error object.
  *
  * @return See above.
  */
 public ImportErrorObject getImportErrorObject() {
   if (errorBox == null || !errorBox.isVisible()) return null;
   if (!errorBox.isEnabled()) return null;
   ImportErrorObject object = new ImportErrorObject(file, (ImportException) image);
   object.setReaderType(statusLabel.getReaderType());
   object.setUsedFiles(statusLabel.getUsedFiles());
   return object;
 }
 /**
  * Listens to property fired by the <code>StatusLabel</code>.
  *
  * @see PropertyChangeListener#propertyChange(PropertyChangeEvent)
  */
 public void propertyChange(PropertyChangeEvent evt) {
   String name = evt.getPropertyName();
   if (StatusLabel.FILES_SET_PROPERTY.equals(name)) {
     if (isCancelled()) {
       statusLabel.setText(CANCEL_TEXT);
       busyLabel.setBusy(false);
       busyLabel.setVisible(false);
       return;
     }
     Map<File, StatusLabel> files = (Map<File, StatusLabel>) evt.getNewValue();
     insertFiles((Map<File, StatusLabel>) evt.getNewValue());
     firePropertyChange(IMPORT_FILES_NUMBER_PROPERTY, null, files.size());
   } else if (StatusLabel.FILE_IMPORT_STARTED_PROPERTY.equals(name)) {
     StatusLabel sl = (StatusLabel) evt.getNewValue();
     if (sl == statusLabel && busyLabel != null) {
       busyLabel.setBusy(true);
       busyLabel.setVisible(true);
       cancelButton.setVisible(sl.isCancellable());
     }
   } else if (StatusLabel.CANCELLABLE_IMPORT_PROPERTY.equals(name)) {
     StatusLabel sl = (StatusLabel) evt.getNewValue();
     cancelButton.setVisible(sl.isCancellable());
   } else if (StatusLabel.FILE_IMPORTED_PROPERTY.equals(name)) {
     Object[] results = (Object[]) evt.getNewValue();
     File f = (File) results[0];
     if (f.getAbsolutePath().equals(file.getAbsolutePath())) {
       setStatus(false, results[1]);
       if (f.isFile()) {
         if (hasImportFailed()) firePropertyChange(IMPORT_STATUS_CHANGE_PROPERTY, null, FAILURE);
         else if (isCancelled()) firePropertyChange(IMPORT_STATUS_CHANGE_PROPERTY, null, PARTIAL);
         else firePropertyChange(IMPORT_STATUS_CHANGE_PROPERTY, null, SUCCESS);
       }
     }
   } else if (StatusLabel.FILE_RESET_PROPERTY.equals(name)) {
     file = (File) evt.getNewValue();
     fileNameLabel.setText(file.getName());
   } else if (ThumbnailLabel.BROWSE_PLATE_PROPERTY.equals(name)) {
     firePropertyChange(BROWSE_PROPERTY, evt.getOldValue(), evt.getNewValue());
   } else if (StatusLabel.CONTAINER_FROM_FOLDER_PROPERTY.equals(name)) {
     containerFromFolder = (DataObject) evt.getNewValue();
     if (containerFromFolder instanceof DatasetData) {
       containerLabel.setText(TEXT_IMPORTED);
       browseButton.setText(((DatasetData) containerFromFolder).getName());
       containerObject = containerFromFolder;
     } else if (containerFromFolder instanceof ScreenData) {
       containerLabel.setText(TEXT_IMPORTED);
       browseButton.setText(((ScreenData) containerFromFolder).getName());
       containerObject = containerFromFolder;
     }
   } else if (StatusLabel.NO_CONTAINER_PROPERTY.equals(name)) {
     containerLabel.setText("");
     noContainer = true;
   } else if (StatusLabel.DEBUG_TEXT_PROPERTY.equals(name)) {
     firePropertyChange(name, evt.getOldValue(), evt.getNewValue());
   }
 }
 /**
  * Replaces the initial status label.
  *
  * @param label The value to replace.
  */
 void setStatusLabel(StatusLabel label) {
   statusLabel = label;
   statusLabel.addPropertyChangeListener(this);
   buildGUI();
   revalidate();
   repaint();
 }
 /**
  * Indicates that the file will not be imported.
  *
  * @param fire Pass <code>true</code> to fire a property, <code>false</code> otherwise.
  */
 private void cancel(boolean fire) {
   if (busyLabel.isBusy() && !statusLabel.isCancellable()) return;
   String s = CANCEL_TEXT;
   if (file.isDirectory()) {
     busyLabel.setBusy(true);
     busyLabel.setVisible(true);
     s += " waiting on scanning to finish";
   } else {
     busyLabel.setBusy(false);
     busyLabel.setVisible(false);
   }
   statusLabel.setText(s);
   statusLabel.markedAsCancel();
   cancelButton.setEnabled(false);
   cancelButton.setVisible(false);
   if (image == null && file.isFile())
     firePropertyChange(IMPORT_STATUS_CHANGE_PROPERTY, null, PARTIAL);
   if (fire) firePropertyChange(CANCEL_IMPORT_PROPERTY, null, this);
 }
  /**
   * Sets the result of the import.
   *
   * @param status Flag indicating the status of the import.
   * @param image The image.
   */
  public void setStatus(boolean status, Object image) {
    this.image = image;
    busyLabel.setBusy(false);
    busyLabel.setVisible(false);
    cancelButton.setVisible(false);
    importCount++;
    if (parent != null) parent.increaseNumberOfImport();
    if (image instanceof ImageData) {
      ImageData img = (ImageData) image;
      Exception error = null;
      try {
        img.getDefaultPixels();
      } catch (Exception e) {
        error = e;
        toReImport = true;
      }
      if (error != null) {
        exception = error;
        fileNameLabel.setForeground(ERROR_COLOR);
        resultLabel.setVisible(false);
        errorButton.setToolTipText(UIUtilities.formatExceptionForToolTip(error));
        errorButton.setVisible(true);
        errorBox.setVisible(true);
        errorBox.addChangeListener(this);
        deleteButton.setVisible(true);
        deleteButton.addActionListener(this);
      } else {
        imageLabel.setData(img);
        resultLabel.setText(VIEW_TEXT);
        resultLabel.setForeground(UIUtilities.HYPERLINK_COLOR);
        if (browsable) resultLabel.setToolTipText(ThumbnailLabel.IMAGE_LABEL_TOOLTIP);
        else imageLabel.setToolTipText("");
        resultLabel.setVisible(true);
        fileNameLabel.addMouseListener(adapter);
        resultLabel.addMouseListener(adapter);
        addMouseListener(adapter);
        showContainerLabel = (dataset != null || containerFromFolder != null);
        if (noContainer) {
          browseButton.setVisible(false);
          containerLabel.setVisible(false);
        } else {
          browseButton.setVisible(showContainerLabel);
          containerLabel.setVisible(showContainerLabel);
        }
        groupLabel.setVisible(!singleGroup);
      }
    } else if (image instanceof ThumbnailData) {
      ThumbnailData thumbnail = (ThumbnailData) image;
      groupLabel.setVisible(!singleGroup);
      if (thumbnail.isValidImage()) {
        imageLabel.setData(thumbnail);

        statusLabel.setVisible(false);
        fileNameLabel.addMouseListener(adapter);
        addMouseListener(adapter);
        resultLabel.setText(VIEW_TEXT);
        resultLabel.setForeground(UIUtilities.HYPERLINK_COLOR);
        if (browsable) resultLabel.setToolTipText(ThumbnailLabel.IMAGE_LABEL_TOOLTIP);
        else imageLabel.setToolTipText("");
        resultLabel.setVisible(false);
        if (thumbnail.requirePyramid() != null && thumbnail.requirePyramid().booleanValue()) {
          imageLabel.setToolTipText(PYRAMID_TEXT);
          resultLabel.setVisible(true);
          resultLabel.addMouseListener(adapter);
        }
        showContainerLabel = (dataset != null || containerFromFolder != null);
        if (noContainer) {
          browseButton.setVisible(false);
          containerLabel.setVisible(false);
        } else {
          browseButton.setVisible(showContainerLabel);
          containerLabel.setVisible(showContainerLabel);
        }
      } else {
        statusLabel.setVisible(false);
        fileNameLabel.setForeground(ERROR_COLOR);
        resultLabel.setText(IMAGE_CREATION_ERROR_TEXT);
        resultLabel.setToolTipText(UIUtilities.formatExceptionForToolTip(thumbnail.getError()));
        resultLabel.setVisible(true);
        errorButton.setVisible(false);
        errorBox.setVisible(false);
        groupLabel.setVisible(!singleGroup);
      }
    } else if (image instanceof PlateData) {
      imageLabel.setData((PlateData) image);
      statusLabel.setVisible(false);
      groupLabel.setVisible(!singleGroup);
      if (browsable) {
        resultLabel.setText(BROWSE_TEXT);
        resultLabel.setForeground(UIUtilities.HYPERLINK_COLOR);
        resultLabel.setToolTipText(ThumbnailLabel.PLATE_LABEL_TOOLTIP);
        resultLabel.setVisible(true);
      }
      fileNameLabel.addMouseListener(adapter);
      resultLabel.addMouseListener(adapter);
      showContainerLabel = containerObject instanceof ScreenData;
      if (noContainer || !browsable) {
        browseButton.setVisible(false);
        containerLabel.setVisible(false);
      } else {
        browseButton.setVisible(showContainerLabel);
        containerLabel.setVisible(showContainerLabel);
      }
    } else if (image instanceof List) {
      statusLabel.setVisible(false);
      groupLabel.setVisible(!singleGroup);
      List list = (List) image;
      int m = list.size();
      imageLabel.setData(list.get(0));
      list.remove(0);
      ThumbnailLabel label = imageLabels.get(0);
      label.setVisible(true);
      label.setData(list.get(0));
      list.remove(0);
      if (list.size() > 0) {
        label = imageLabels.get(1);
        label.setVisible(true);
        label.setData(list.get(0));
        list.remove(0);
        int n = statusLabel.getSeriesCount() - m;
        if (n > 0) {
          label = imageLabels.get(2);
          Font f = label.getFont();
          label.setFont(f.deriveFont(f.getStyle(), f.getSize() - 2));
          label.setVisible(true);
          String value = "... " + n + " more";
          label.setText(value);
        }
      }
      resultLabel.setVisible(true);
      showContainerLabel = true;
      if (noContainer) {
        browseButton.setVisible(false);
        containerLabel.setVisible(false);
      } else {
        browseButton.setVisible(showContainerLabel);
        containerLabel.setVisible(showContainerLabel);
      }
      // control = resultLabel;
    } else if (image instanceof Boolean) {
      if (!statusLabel.isMarkedAsCancel()) {
        cancelButton.setVisible(false);
        if (statusLabel.isMarkedAsDuplicate()) {
          statusLabel.setVisible(false);
          setStatusText(StatusLabel.DUPLICATE);
        } else {
          statusLabel.setVisible(false);
          setStatusText(FILE_NOT_VALID_TEXT);
        }
      } else resultLabel.setText("");
    } else {
      if (!status) {
        statusLabel.setVisible(false);
        resultLabel.setToolTipText("");
        resultLabel.setEnabled(false);
        if (image == null) setStatusText(null);
        else if (image instanceof String) {
          setStatusText((String) image);
        } else if (image instanceof ImportException) {
          ImportException ie = (ImportException) image;
          fileNameLabel.setForeground(ERROR_COLOR);
          resultLabel.setVisible(false);
          toReImport = true;
          errorButton.setToolTipText(UIUtilities.formatExceptionForToolTip(ie));
          exception = ie;
          errorButton.setVisible(false);
          int s = ie.getStatus();
          if (s == ImportException.COMPRESSION) {
            resultLabel.setVisible(true);
            resultLabel.setText(COMPRESSION_ERROR_TEXT);
            resultLabel.setToolTipText(UIUtilities.formatExceptionForToolTip(ie));
          } else if (s == ImportException.MISSING_LIBRARY) {
            resultLabel.setVisible(true);
            resultLabel.setText(MISSING_LIB_ERROR_TEXT);
            resultLabel.setToolTipText(UIUtilities.formatExceptionForToolTip(ie));
          } else if (s == ImportException.FILE_ON_TAPE) {
            resultLabel.setVisible(true);
            resultLabel.setText(FILE_ON_TAPE_ERROR_TEXT);
            resultLabel.setToolTipText(UIUtilities.formatExceptionForToolTip(ie));
          } else if (s == ImportException.NO_SPACE) {
            resultLabel.setVisible(true);
            resultLabel.setText(NO_SPACE_ERROR_TEXT);
            resultLabel.setToolTipText(UIUtilities.formatExceptionForToolTip(ie));
          } else {
            errorButton.setVisible(true);
            errorBox.setVisible(true);
            errorBox.addChangeListener(this);
          }
          cancelButton.setVisible(false);
        }
      }
    }
    repaint();
  }
 /**
  * Sets the text of the {@link #resultLabel}.
  *
  * @param text The string to set.
  */
 private void setStatusText(String text) {
   if (text == null) text = "";
   text = text.trim();
   if (text.length() == 0) resultLabel.setText(statusLabel.getErrorText());
   else resultLabel.setText(text);
 }
  /** Initializes the components. */
  private void initComponents() {
    reimportedLabel = new JLabel("Reimported");
    reimportedLabel.setVisible(false);
    showContainerLabel = true;
    adapter =
        new MouseAdapter() {

          /**
           * Views the image.
           *
           * @see MouseListener#mousePressed(MouseEvent)
           */
          public void mousePressed(MouseEvent e) {
            if (e.getClickCount() == 1) {
              ViewImage evt;
              int plugin = ImporterAgent.runAsPlugin();
              if (image instanceof ThumbnailData) {
                ThumbnailData data = (ThumbnailData) image;
                EventBus bus = ImporterAgent.getRegistry().getEventBus();
                if (data.getImage() != null) {
                  evt =
                      new ViewImage(
                          new SecurityContext(group.getId()),
                          new ViewImageObject(data.getImage()),
                          null);
                  evt.setPlugin(plugin);
                  bus.post(evt);
                }
              } else if (image instanceof ImageData) {
                ImageData data = (ImageData) image;
                EventBus bus = ImporterAgent.getRegistry().getEventBus();
                if (data != null) {
                  evt =
                      new ViewImage(
                          new SecurityContext(group.getId()), new ViewImageObject(data), null);
                  evt.setPlugin(plugin);
                  bus.post(evt);
                }
              } else if (image instanceof PlateData) {
                firePropertyChange(BROWSE_PROPERTY, null, image);
              }
            }
          }
        };

    setLayout(new FlowLayout(FlowLayout.LEFT));
    busyLabel = new JXBusyLabel(SIZE);
    busyLabel.setVisible(false);
    busyLabel.setBusy(false);

    cancelButton = new JLabel("Cancel");
    cancelButton.setForeground(UIUtilities.HYPERLINK_COLOR);
    cancelButton.addMouseListener(
        new MouseAdapter() {

          /**
           * Browses the object the image.
           *
           * @see MouseListener#mousePressed(MouseEvent)
           */
          public void mousePressed(MouseEvent e) {
            Object src = e.getSource();
            if (e.getClickCount() == 1 && src instanceof JLabel) {
              cancel(true);
            }
          }
        });
    cancelButton.setVisible(true);

    browseButton = new JLabel(BROWSE_CONTAINER_TEXT);
    if (browsable) {
      browseButton.setToolTipText(BROWSE_CONTAINER_TOOLTIP);
      browseButton.setForeground(UIUtilities.HYPERLINK_COLOR);
      browseButton.addMouseListener(
          new MouseAdapter() {

            /**
             * Browses the object the image.
             *
             * @see MouseListener#mousePressed(MouseEvent)
             */
            public void mousePressed(MouseEvent e) {
              Object src = e.getSource();
              if (e.getClickCount() == 1 && src instanceof JLabel) {
                browse();
              }
            }
          });
    }

    browseButton.setVisible(false);

    containerLabel = new JLabel();
    containerLabel.setVisible(false);
    groupLabel = new JLabel("Group: " + group.getName());
    groupLabel.setVisible(false);

    namePane = new JPanel();
    namePane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
    IconManager icons = IconManager.getInstance();
    Icon icon;
    if (file.isFile()) icon = icons.getIcon(IconManager.IMAGE);
    else icon = icons.getIcon(IconManager.DIRECTORY);
    imageLabel = new ThumbnailLabel(icon);
    imageLabel.addPropertyChangeListener(this);
    imageLabels = new ArrayList<ThumbnailLabel>();
    ThumbnailLabel label;
    for (int i = 0; i < NUMBER; i++) {
      label = new ThumbnailLabel();
      label.setVisible(false);
      imageLabels.add(label);
    }
    fileNameLabel = new JLabel(file.getName());
    namePane.add(imageLabel);
    Iterator<ThumbnailLabel> j = imageLabels.iterator();
    while (j.hasNext()) {
      namePane.add(j.next());
    }
    namePane.add(Box.createHorizontalStrut(4));
    namePane.add(fileNameLabel);
    namePane.add(Box.createHorizontalStrut(10));
    resultLabel = new JLabel();
    // control = busyLabel;
    errorBox = new JCheckBox("Mark to Send");
    errorBox.setOpaque(false);
    errorBox.setToolTipText("Mark the file to send to the development " + "team.");
    errorBox.setVisible(false);
    errorBox.setSelected(true);
    errorButton = new JButton("Failed");
    errorButton.setForeground(ERROR_COLOR);
    errorButton.addMouseListener(
        new MouseAdapter() {

          /**
           * Displays the error dialog at the specified location.
           *
           * @see MouseAdapter#mouseReleased(MouseEvent)
           */
          public void mouseReleased(MouseEvent e) {
            showError(e.getPoint());
          }
        });
    errorButton.setVisible(false);

    statusLabel = new StatusLabel();
    statusLabel.addPropertyChangeListener(this);
    deleteButton = new JButton(icons.getIcon(IconManager.DELETE));
    deleteButton.setActionCommand("" + DELETE_ID);
    deleteButton.setToolTipText("Delete the image");
    UIUtilities.unifiedButtonLookAndFeel(deleteButton);
    deleteButton.setVisible(false);
    image = null;
  }
 /**
  * Returns <code>true</code> if the import has been cancelled, <code>false</code> otherwise.
  *
  * @return See above.
  */
 public boolean isCancelled() {
   return statusLabel.isMarkedAsCancel();
 }