/*
   * (non-Javadoc)
   *
   * @see org.eclipse.jface.action.Action#run()
   */
  @Override
  protected void doRun() {
    RepositoryNode node =
        (RepositoryNode) ((IStructuredSelection) getSelection()).getFirstElement();
    Item item = node.getObject().getProperty().getItem();
    if (item == null) {
      return;
    }
    URL url = null;
    // String extension = null;
    if (item instanceof DocumentationItem) {
      DocumentationItem documentationItem = (DocumentationItem) item;
      // if (documentationItem.getExtension() != null) {
      // extension = documentationItem.getExtension();
      // }
      IFile file =
          LinkDocumentationHelper.getTempFile(
              documentationItem.getName(), documentationItem.getExtension());
      try {
        documentationItem.getContent().setInnerContentToFile(file.getLocation().toFile());
        url = file.getLocationURI().toURL();
      } catch (Exception e) {
        showErrorMessage();
        return;
      }

    } else if (item instanceof LinkDocumentationItem) { // link documenation
      LinkDocumentationItem linkDocItem = (LinkDocumentationItem) item;
      if (!LinkUtils.validateLink(linkDocItem.getLink())) {
        showErrorMessage();
        return;
      }
      // if (linkDocItem.getExtension() != null) {
      // extension = linkDocItem.getExtension();
      // }
      String uri = linkDocItem.getLink().getURI();

      if (LinkUtils.isRemoteFile(uri)) {
        try {
          url = new URL(uri);
        } catch (MalformedURLException e) {
          //
        }
      } else if (LinkUtils.existedFile(uri)) {
        try {
          url = new File(uri).toURL();
        } catch (MalformedURLException e) {
          //
        }
      }
    }
    openedByBrowser(item, url);
    // progress(item, extension);

  }
  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.jface.action.Action#run()
   */
  protected void doRun() {
    RepositoryNode node =
        (RepositoryNode) ((IStructuredSelection) getSelection()).getFirstElement();

    final Item item = node.getObject().getProperty().getItem();
    if (item == null) {
      return;
    }
    String initialFileName = null;
    String initialExtension = null;
    if (item instanceof DocumentationItem) {
      DocumentationItem documentationItem = (DocumentationItem) item;
      initialFileName = documentationItem.getName();
      if (documentationItem.getExtension() != null) {
        initialExtension = documentationItem.getExtension();
      }
    } else if (item instanceof LinkDocumentationItem) { // link documenation
      LinkDocumentationItem linkDocItem = (LinkDocumentationItem) item;

      if (!LinkUtils.validateLink(linkDocItem.getLink())) {
        MessageDialog.openError(
            Display.getCurrent().getActiveShell(),
            Messages.getString("ExtractDocumentationAction.fileErrorTitle"), // $NON-NLS-1$
            Messages.getString("ExtractDocumentationAction.fileErrorMessages")); // $NON-NLS-1$
        return;
      }

      initialFileName = linkDocItem.getName();
      if (linkDocItem.getExtension() != null) {
        initialExtension = linkDocItem.getExtension();
      }
    }
    if (initialFileName != null) {
      FileDialog fileDlg = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);

      if (initialExtension != null) {
        initialFileName = initialFileName + LinkUtils.DOT + initialExtension; // $NON-NLS-1$
        fileDlg.setFilterExtensions(
            new String[] {"*." + initialExtension, "*.*"}); // $NON-NLS-1$ //$NON-NLS-2$
      }
      fileDlg.setFileName(initialFileName);
      String filename = fileDlg.open();
      if (filename != null) {
        final File file = new File(filename);

        ProgressDialog progressDialog =
            new ProgressDialog(Display.getCurrent().getActiveShell()) {

              @Override
              public void run(IProgressMonitor monitor)
                  throws InvocationTargetException, InterruptedException {
                try {
                  if (item instanceof DocumentationItem) {
                    DocumentationItem documentationItem = (DocumentationItem) item;
                    documentationItem.getContent().setInnerContentToFile(file);
                  } else if (item instanceof LinkDocumentationItem) { // link documenation
                    LinkDocumentationItem linkDocItem = (LinkDocumentationItem) item;
                    ByteArray byteArray = LinkDocumentationHelper.getLinkItemContent(linkDocItem);
                    if (byteArray != null) {
                      byteArray.setInnerContentToFile(file);
                    }
                  }

                } catch (IOException ioe) {
                  MessageBoxExceptionHandler.process(ioe);
                }
              }
            };
        try {
          progressDialog.executeProcess();
        } catch (InvocationTargetException e) {
          ExceptionHandler.process(e);
        } catch (InterruptedException e) {
          // Nothing to do
        }
      }
    }
  }