Ejemplo n.º 1
0
  /**
   * Exports an archive to a given file.
   *
   * @param archive The archive to export
   * @param file The file to which the archive will be exported. If this is null, the user will be
   *     prompted for a file.
   */
  protected void _exportArchiveToFile(IMArchive archive, File file) {
    // Prompt the user for a file name if necessary
    while (file == null) {
      // Show the Save dialog
      this._fileChooser.setDialogTitle("Export IM Archive");
      if (this._fileChooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) return;

      // Get file, adjusting the extension if necessary
      file = this._fileChooser.getSelectedFile();
      if (!file.getName().toLowerCase().endsWith(".json"))
        file = new File(file.getPath() + ".json");

      // Check for overwriting
      if (file.exists())
        if (JOptionPane.showConfirmDialog(
                this,
                "File already exists. Overwrite?",
                "Confirm Overwrite",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE)
            != JOptionPane.YES_OPTION) file = null;
    }

    // Prepare and execute task
    TaskWorker taskWorker = new TaskWorker(new ExportArchiveToFileTask(archive, file));
    Object result = taskWorker.runTask();

    if (result instanceof Exception) {
      this._showErrorMessage("Could not export archive:\n" + ((Exception) result).getMessage());
      return;
    }
  }
Ejemplo n.º 2
0
  /** Executes the Import Archive From File command. */
  protected void _doImportArchiveFromFile() {
    this._fileChooser.setDialogTitle("Import IM Archive");
    if (this._fileChooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) return;
    File file = this._fileChooser.getSelectedFile();

    // Prepare and execute task
    TaskWorker taskWorker = new TaskWorker(new LoadArchiveTask(file));
    Object result = taskWorker.runTask();

    if (result instanceof Exception) {
      this._showErrorMessage("Could not import archive:\n" + ((Exception) result).getMessage());
      return;
    }

    this._finishImport((IMArchive) result);
  }
Ejemplo n.º 3
0
  /**
   * Prompts the user for the destination of recently imported archive data, and finishes up the
   * import.
   *
   * @param archive A temporary archive containing the imported data. It will be destroyed at the
   *     end of the function.
   */
  protected void _finishImport(IMArchive archive) {
    try {
      // Show the Import Finished dialog and allow the user to select
      // an action and a destination
      ImportFinishedDialog.Action action =
          this._importFinishedDialog.showDialog(
              archive, (this._archive != null) ? this._archive.getName() : null);
      if (action == ImportFinishedDialog.Action.CANCELLED) return;
      boolean merge = (action == ImportFinishedDialog.Action.MERGE_INTO);

      // Connect to the destination archive
      IMArchive destination;
      String destName = this._importFinishedDialog.resultArchiveName;

      if ((this._archive != null) && this._archive.getName().equals(destName))
        destination = this._archive;
      else destination = new IMArchive(destName);
      this._updateArchivesList();

      // Prepare and execute task
      TaskWorker taskWorker =
          new TaskWorker(
              new CopyOrMergeArchiveTask(
                  archive, destination, merge, this._importFinishedDialog.resultAccountingOnly));
      Object result = taskWorker.runTask();

      // Handle errors
      if (result instanceof Exception) {
        this._showErrorMessage(
            "Could not "
                + (merge ? "merge" : "copy")
                + " archive data:\n"
                + ((Exception) result).getMessage());
        return;
      }

      // Delete the source archive, if it was temporary
      if (archive.isTemporary()) {
        new TaskWorker(new DeleteArchiveTask(archive)).runTask();
      }

      if (this._importFinishedDialog.resultConnectAfter)
        if (destination != this._archive) this._setArchive(destination);
    } catch (Exception e) {
      this._showErrorMessage("Cannot import archive:\n" + e.getMessage());
    }
  }