private void runLanguageFilterDialog() {
    final NetworkLibrary library = NetworkLibrary.Instance();

    final List<String> allLanguageCodes = library.languageCodes();
    Collections.sort(allLanguageCodes, new ZLLanguageUtil.CodeComparator());
    final Collection<String> activeLanguageCodes = library.activeLanguageCodes();
    final CharSequence[] languageNames = new CharSequence[allLanguageCodes.size()];
    final boolean[] checked = new boolean[allLanguageCodes.size()];

    for (int i = 0; i < allLanguageCodes.size(); ++i) {
      final String code = allLanguageCodes.get(i);
      languageNames[i] = ZLLanguageUtil.languageName(code);
      checked[i] = activeLanguageCodes.contains(code);
    }

    final DialogInterface.OnMultiChoiceClickListener listener =
        new DialogInterface.OnMultiChoiceClickListener() {
          public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            checked[which] = isChecked;
          }
        };
    final ZLResource dialogResource = ZLResource.resource("dialog");
    final AlertDialog dialog =
        new AlertDialog.Builder(this)
            .setMultiChoiceItems(languageNames, checked, listener)
            .setTitle(
                dialogResource.getResource("languageFilterDialog").getResource("title").getValue())
            .setPositiveButton(
                dialogResource.getResource("button").getResource("ok").getValue(),
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                    final TreeSet<String> newActiveCodes =
                        new TreeSet<String>(new ZLLanguageUtil.CodeComparator());
                    for (int i = 0; i < checked.length; ++i) {
                      if (checked[i]) {
                        newActiveCodes.add(allLanguageCodes.get(i));
                      }
                    }
                    library.setActiveLanguageCodes(newActiveCodes);
                    library.synchronize();
                    NetworkView.Instance().fireModelChanged();
                  }
                })
            .create();
    dialog.show();
  }
Esempio n. 2
0
public abstract class BasketItem extends NetworkCatalogItem {
  private long myGeneration = 0;

  private final ZLStringListOption myBooksInBasketOption;
  private final Map<String, NetworkBookItem> myBooks =
      Collections.synchronizedMap(new HashMap<String, NetworkBookItem>());

  protected BasketItem(INetworkLink link) {
    super(
        link,
        NetworkLibrary.resource().getResource("basket").getValue(),
        NetworkLibrary.resource().getResource("basketSummaryEmpty").getValue(),
        new UrlInfoCollection<UrlInfo>(),
        Accessibility.ALWAYS,
        FLAGS_DEFAULT & ~FLAGS_GROUP);
    myBooksInBasketOption = new ZLStringListOption(Link.getSiteName(), "Basket", null);
  }

  public void addItem(NetworkBookItem book) {
    myBooks.put(book.Id, book);
  }

  @Override
  public CharSequence getSummary() {
    final int size = bookIds().size();
    if (size == 0) {
      return super.getSummary();
    } else {
      final Money basketCost = cost();
      if (basketCost != null) {
        return NetworkLibrary.resource()
            .getResource("basketSummary")
            .getValue()
            .replace("%0", String.valueOf(size))
            .replace("%1", basketCost.toString());
      } else {
        return NetworkLibrary.resource()
            .getResource("basketSummaryCountOnly")
            .getValue()
            .replace("%0", String.valueOf(size));
      }
    }
  }

  @Override
  public boolean canBeOpened() {
    return !bookIds().isEmpty();
  }

  @Override
  public String getStringId() {
    return "@Basket:" + Link.getSiteName();
  }

  public long getGeneration() {
    return myGeneration;
  }

  public final void add(NetworkBookItem book) {
    List<String> ids = bookIds();
    if (!ids.contains(book.Id)) {
      ids = new ArrayList<String>(ids);
      ids.add(book.Id);
      myBooksInBasketOption.setValue(ids);
      addItem(book);
      ++myGeneration;
      NetworkLibrary.Instance().fireModelChangedEvent(NetworkLibrary.ChangeListener.Code.SomeCode);
    }
  }

  public final void remove(NetworkBookItem book) {
    List<String> ids = bookIds();
    if (ids.contains(book.Id)) {
      ids = new ArrayList<String>(ids);
      ids.remove(book.Id);
      myBooksInBasketOption.setValue(ids);
      myBooks.remove(book);
      ++myGeneration;
      NetworkLibrary.Instance().fireModelChangedEvent(NetworkLibrary.ChangeListener.Code.SomeCode);
    }
  }

  public final void clear() {
    myBooksInBasketOption.setValue(null);
    myBooks.clear();
    ++myGeneration;
    NetworkLibrary.Instance().fireModelChangedEvent(NetworkLibrary.ChangeListener.Code.SomeCode);
  }

  public final boolean contains(NetworkBookItem book) {
    return bookIds().contains(book.Id);
  }

  public List<String> bookIds() {
    return myBooksInBasketOption.getValue();
  }

  public NetworkBookItem getBook(String id) {
    return myBooks.get(id);
  }

  protected boolean isFullyLoaded() {
    synchronized (myBooks) {
      for (String id : bookIds()) {
        final NetworkBookItem b = myBooks.get(id);
        if (b == null) {
          return false;
        }
      }
    }
    return true;
  }

  private Money cost() {
    Money sum = Money.ZERO;
    synchronized (myBooks) {
      for (String id : bookIds()) {
        final NetworkBookItem b = myBooks.get(id);
        if (b == null) {
          return null;
        }
        final BookBuyUrlInfo info = b.buyInfo();
        if (info == null) {
          return null;
        }
        if (b.getStatus() == NetworkBookItem.Status.CanBePurchased) {
          if (info.Price == null) {
            return null;
          }
          sum = sum.add(info.Price);
        }
      }
    }
    return sum;
  }
}