/*
   * @see org.eclipse.jface.dialogs.TrayDialog#createButtonBar(org.eclipse.swt.widgets.Composite)
   */
  @Override
  protected Control createButtonBar(Composite parent) {
    GridLayout layout = new GridLayout(1, false);
    layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
    layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
    layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
    layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);

    Composite buttonBar = new Composite(parent, SWT.NONE);
    buttonBar.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
    buttonBar.setLayout(layout);

    /* Status Label */
    fStatusLabel = new Link(buttonBar, SWT.NONE);
    applyDialogFont(fStatusLabel);
    fStatusLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
    if (StringUtils.isSet(fBookmark.getName())) fStatusLabel.setText(fBookmark.getName());

    /* Close */
    Button closeButton =
        createButton(buttonBar, IDialogConstants.CLOSE_ID, IDialogConstants.CLOSE_LABEL, false);
    closeButton.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            close();
          }
        });

    return buttonBar;
  }
  /*
   * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
   */
  @Override
  protected void configureShell(Shell shell) {
    super.configureShell(shell);

    if (StringUtils.isSet(fBookmark.getName()))
      shell.setText(NLS.bind(Messages.PreviewFeedDialog_PREVIEW_OF, fBookmark.getName()));
    else shell.setText(Messages.PreviewFeedDialog_PREVIEW);
  }
  private void showFeed(final IFeed feed) {
    if (feed != null && !fBrowser.getControl().isDisposed()) {
      List<INews> news = feed.getNewsByStates(INews.State.getVisible());
      int newsCount = news.size();
      if (news.size() > MAX_NEWS_SHOWN) news = news.subList(0, MAX_NEWS_SHOWN);

      /* Start HTML */
      StringBuilder html = new StringBuilder();
      html.append(
          "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"); //$NON-NLS-1$

      /* Windows only: Mark of the Web */
      if (Application.IS_WINDOWS) {
        html.append(IE_MOTW);
        html.append("\n"); // $NON-NLS-1$
      }

      /* Head */
      html.append("<html>\n  <head>\n"); // $NON-NLS-1$

      /* Append Base URI if available */
      URI base = (feed.getBase() != null) ? feed.getBase() : feed.getLink();
      if (base != null) {
        html.append("  <base href=\""); // $NON-NLS-1$
        html.append(base);
        html.append("\">"); // $NON-NLS-1$
      }

      /* Meta */
      html.append(
          "\n  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"); //$NON-NLS-1$

      /* CSS */
      try {
        StringWriter writer = new StringWriter();
        fLabelProvider.writeCSS(writer, false);
        html.append(writer.toString());
      } catch (IOException e) {
        /* Will Never Happen */
      }

      /* Open Body */
      html.append("  </head>\n  <body id=\"owlbody\">\n"); // $NON-NLS-1$

      /* Title */
      if (StringUtils.isSet(fBookmark.getName()))
        html.append("<div class=\"group\">")
            .append(fBookmark.getName())
            .append("</div>"); // $NON-NLS-1$ //$NON-NLS-2$

      /* Write News */
      for (INews item : news) {
        html.append(fLabelProvider.getText(item, false));
      }

      /* End HTML */
      html.append("\n  </body>\n</html>"); // $NON-NLS-1$

      /* Apply to Browser */
      fBrowser.getControl().setText(html.toString());

      /* Also Update Status */
      if (StringUtils.isSet(fBookmark.getName())) {
        StringBuilder str = new StringBuilder();
        if (feed.getHomepage() != null) {
          str.append(
              NLS.bind(
                  Messages.PreviewFeedDialog_FOUND_N_NEWS_HOMEPAGE,
                  newsCount,
                  fBookmark.getName()));
          fStatusLabel.addSelectionListener(
              new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                  new OpenInBrowserAction(new StructuredSelection(feed.getHomepage())).run();
                }
              });
        } else
          str.append(
              NLS.bind(Messages.PreviewFeedDialog_FOUND_N_NEWS, newsCount, fBookmark.getName()));

        fStatusLabel.setText(str.toString());
      }
    }
  }
  private void loadFeed() {

    /* Show Info that Feed is loading */
    if (fLoadedFeed == null || fLoadedFeed.getVisibleNews().isEmpty()) {
      if (StringUtils.isSet(fBookmark.getName()))
        showMessage(
            NLS.bind(Messages.PreviewFeedDialog_LOAD_FEED_N, fBookmark.getName()), false, true);
      else showMessage(Messages.PreviewFeedDialog_LOAD_FEED, false, true);
    }

    /* Load Feed in Background */
    JobRunner.runUIUpdater(
        new UIBackgroundJob(fBrowser.getControl()) {
          private IFeed feed;
          private Exception error;

          @Override
          protected void runInBackground(IProgressMonitor monitor) {

            /* First Check if a Feed was already provided */
            if (fLoadedFeed != null && !fLoadedFeed.getVisibleNews().isEmpty()) {
              feed = fLoadedFeed;
              return;
            }

            /* Otherwise Load Feed */
            try {

              /* Resolve Feed if existing */
              if (fFeedReference != null) feed = fFeedReference.resolve();

              /* Create Temporary Feed */
              if (feed == null || feed.getVisibleNews().isEmpty()) {
                feed =
                    Owl.getModelFactory()
                        .createFeed(null, fBookmark.getFeedLinkReference().getLink());

                /* Return if dialog closed */
                if (monitor.isCanceled()
                    || getShell().isDisposed()
                    || fBrowser.getControl().isDisposed()) return;

                /* Retrieve Stream */
                IProtocolHandler handler = Owl.getConnectionService().getHandler(feed.getLink());
                InputStream inS = handler.openStream(feed.getLink(), monitor, null);

                /* Return if dialog closed */
                if (monitor.isCanceled()
                    || getShell().isDisposed()
                    || fBrowser.getControl().isDisposed()) return;

                /* Interpret Feed */
                Owl.getInterpreter().interpret(inS, feed, null);
              }
            } catch (ConnectionException e) {
              error = e;
              Activator.safeLogError(e.getMessage(), e);
            } catch (ParserException e) {
              error = e;
              Activator.safeLogError(e.getMessage(), e);
            } catch (InterpreterException e) {
              error = e;
              Activator.safeLogError(e.getMessage(), e);
            }
          }

          @Override
          protected void runInUI(IProgressMonitor monitor) {
            if (feed != null && error == null) showFeed(feed);
            else if (error != null) {
              String errorMessage = CoreUtils.toMessage(error);
              if (StringUtils.isSet(errorMessage))
                showMessage(
                    NLS.bind(Messages.PreviewFeedDialog_UNABLE_LOAD_FEED, errorMessage),
                    true,
                    false);
            }
          }
        });
  }