Exemplo n.º 1
0
  /**
   * @param searchMarks The Set of <code>ISearchMark</code> to update the results in.
   * @param fromUserEvent Indicates whether to update the saved searches due to a user initiated
   *     event or an automatic one.
   */
  public void updateSavedSearches(Collection<ISearchMark> searchMarks, boolean fromUserEvent) {
    boolean firstUpdate = !fUpdatedOnce.get();

    fUpdatedOnce.set(true);
    IModelSearch modelSearch = Owl.getPersistenceService().getModelSearch();
    Set<SearchMarkEvent> events = new HashSet<SearchMarkEvent>(searchMarks.size());

    /* For each Search Mark */
    for (ISearchMark searchMark : searchMarks) {

      /* Return early if shutting down */
      if (Controller.getDefault().isShuttingDown()) return;

      /* Execute the search */
      List<SearchHit<NewsReference>> results =
          modelSearch.searchNews(searchMark.getSearchConditions(), searchMark.matchAllConditions());

      /* Fill Result into Map Buckets */
      Map<INews.State, List<NewsReference>> resultsMap =
          new EnumMap<INews.State, List<NewsReference>>(INews.State.class);

      Set<State> visibleStates = INews.State.getVisible();
      for (SearchHit<NewsReference> searchHit : results) {

        /* Return early if shutting down */
        if (Controller.getDefault().isShuttingDown()) return;

        INews.State state = (State) searchHit.getData(INews.STATE);
        if (visibleStates.contains(state)) {
          List<NewsReference> newsRefs = resultsMap.get(state);
          if (newsRefs == null) {
            newsRefs = new ArrayList<NewsReference>(results.size() / 3);
            resultsMap.put(state, newsRefs);
          }
          newsRefs.add(searchHit.getResult());
        }
      }

      /* Set Result */
      Pair<Boolean, Boolean> result = searchMark.setNewsRefs(resultsMap);
      boolean changed = result.getFirst();
      boolean newNewsAdded = result.getSecond();

      /* Create Event to indicate changed results if any */
      if (changed)
        events.add(
            new SearchMarkEvent(
                searchMark, null, true, !firstUpdate && !fromUserEvent && newNewsAdded));
    }

    /* Notify Listeners */
    if (!events.isEmpty() && !Controller.getDefault().isShuttingDown())
      DynamicDAO.getDAO(ISearchMarkDAO.class).fireNewsChanged(events);
  }
  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());
      }
    }
  }