public boolean updateNewsStates(Collection<StatesUpdateInfo> statesUpdateInfos) {
   boolean changed = false;
   for (StatesUpdateInfo info : statesUpdateInfos) {
     long newsId = info.getNewsReference().getId();
     if (info.getOldState() == null) {
       boolean itemRemoved = fNewsIds[INews.State.NEW.ordinal()].removeByElement(newsId);
       if (!itemRemoved) {
         EnumSet<State> remainingStates = EnumSet.allOf(INews.State.class);
         remainingStates.remove(INews.State.NEW);
         remainingStates.remove(info.getNewState());
         for (INews.State state : remainingStates) {
           if (fNewsIds[state.ordinal()].removeByElement(newsId)) itemRemoved = true;
         }
       }
       if (itemRemoved) {
         changed = true;
         fNewsIds[info.getNewState().ordinal()].add(newsId);
       }
     } else if (fNewsIds[info.getOldState().ordinal()].removeByElement(newsId)) {
       changed = true;
       fNewsIds[info.getNewState().ordinal()].add(newsId);
     }
   }
   return changed;
 }
 public List<NewsReference> removeNews(Set<INews.State> states) {
   List<NewsReference> newsRefs = getNews(states);
   for (INews.State state : states) {
     int index = state.ordinal();
     fNewsIds[index].clear();
   }
   return newsRefs;
 }
 public NewsContainer(Map<INews.State, Boolean> statesToSortedMap) {
   int length = INews.State.values().length;
   fNewsIds = new LongArrayList[length];
   for (int i = 0; i < fNewsIds.length; i++) {
     Boolean sort = statesToSortedMap.get(INews.State.getState(i));
     if (sort != null && sort.equals(Boolean.TRUE)) fNewsIds[i] = new SortedLongArrayList(0);
     else fNewsIds[i] = new LongArrayList(0);
   }
 }
  public int getNewsCount(Set<INews.State> states) {
    Assert.isNotNull(states, "states"); // $NON-NLS-1$

    int count = 0;

    for (INews.State state : states) {
      count += fNewsIds[state.ordinal()].size();
    }

    return count;
  }
  public List<NewsReference> getNews(Set<INews.State> states) {
    List<NewsReference> newsRefs = new ArrayList<NewsReference>(getNewsCount(states));

    for (INews.State state : states) {
      int index = state.ordinal();
      LongArrayList newsIds = fNewsIds[index];
      for (int i = 0, c = newsIds.size(); i < c; ++i) {
        long newsId = newsIds.get(i);
        Assert.isLegal(newsId != 0);
        newsRefs.add(new NewsReference(newsId));
      }
    }

    return newsRefs;
  }
Пример #6
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);
  }
  public Pair<Boolean, Boolean> setNews(Map<INews.State, List<NewsReference>> newsMap) {
    Assert.isNotNull(newsMap, "newsMap"); // $NON-NLS-1$

    boolean changed = false;
    boolean isNewNewsAdded = false;

    Set<INews.State> statesToReset = EnumSet.allOf(INews.State.class);
    statesToReset.removeAll(newsMap.keySet());

    /* Reset all ArrayLists whose state is not present in newsMap */
    for (INews.State state : statesToReset) {
      int index = state.ordinal();
      LongArrayList currentArrayList = fNewsIds[index];
      if (currentArrayList.size() > 0) {
        currentArrayList.clear();
        changed = true;
      }
    }

    /* For each Result */
    for (Map.Entry<INews.State, List<NewsReference>> mapEntry : newsMap.entrySet()) {
      List<NewsReference> news = mapEntry.getValue();
      INews.State state = mapEntry.getKey();

      Assert.isNotNull(news, "news"); // $NON-NLS-1$
      Assert.isNotNull(state, "state"); // $NON-NLS-1$

      long[] newArray = new long[news.size()];

      /* Fill Bucket */
      for (int i = 0; i < news.size(); i++) {
        newArray[i] = news.get(i).getId();
      }

      int index = state.ordinal();
      LongArrayList currentArrayList = fNewsIds[index];

      if (state.equals(INews.State.NEW)) {
        /* Check for added *new* News */

        if (newArray.length > currentArrayList.size()) isNewNewsAdded = true;
        else {
          for (long value : newArray) {
            if (currentArrayList.indexOf(value) < 0) {
              isNewNewsAdded = true;
              break;
            }
          }
        }
        if (isNewNewsAdded || (newArray.length != currentArrayList.size())) {
          changed = true;
          currentArrayList.setAll(newArray);
        }
      } else {
        if (!currentArrayList.elementsEqual(newArray)) {
          currentArrayList.setAll(newArray);
          changed = true;
        }
      }
    }

    return Pair.create(changed, isNewNewsAdded);
  }
  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());
      }
    }
  }