Package org.jampa.model.radio

Source Code of org.jampa.model.radio.AbstractRadioItem

/*
* Jampa
* Copyright (C) 2008-2009 J. Devauchelle and contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*/

package org.jampa.model.radio;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.jampa.controllers.Controller;
import org.jampa.model.SortMode;
import org.jampa.model.comparators.RadioItemComparator;
import org.jampa.preferences.PreferenceConstants;

public abstract class AbstractRadioItem implements IRadioItem {
 
  protected static SortMode _sortMode = SortMode.getValueOf(Controller.getInstance().getPreferenceStore().getString(PreferenceConstants.RADIOVIEW_SORT_MODE));;
 
  protected IRadioItem _parent = null;
  protected String _name = null;
  protected List<IRadioItem> _itemList = null;

  public AbstractRadioItem(IRadioItem parent, String name) {
    _parent = parent;
    _name = name;
  }
 
  public String getName() {
    return _name;
  }
 
  public abstract boolean isFavorite();
 
  public void setFavorite(boolean value) {}
 
  public boolean hasChildren() {
    return ((_itemList != null) && (_itemList.size() > 0));
  }
 
  public List<IRadioItem> getChildren() {
    Collections.sort(_itemList, new RadioItemComparator(_sortMode));
    return _itemList;
  }
 
  public boolean hasChildren(boolean onlyFavorites) {
    if (onlyFavorites) {
      if ((_itemList != null) && (_itemList.size() > 0)) {
       
        boolean stop = false;
        Iterator<IRadioItem> iter = _itemList.iterator();
        while ((!stop) &&
            (iter.hasNext())) {
          stop = iter.next().isFavorite();
        }
        return stop;
       
      } else {
        return false;
      }
    } else {
      return this.hasChildren();
    }
  }
 
  public List<IRadioItem> getChildren(boolean onlyFavorites) {
    if (onlyFavorites) {
     
      IRadioItem item;
      List<IRadioItem> tmpList = new ArrayList<IRadioItem>();
      Iterator<IRadioItem> iter = _itemList.iterator();
      while (iter.hasNext()) {
        item = iter.next();
        if ((item.isFavorite()) ||
            (item.hasChildren(true))) {
          tmpList.add(item);
        }
      }
      Collections.sort(tmpList, new RadioItemComparator(_sortMode));
      return tmpList;
     
    } else {
      return this.getChildren();
    }
  }
 
  public boolean hasParent() {
    return (_parent != null);
  }
 
  public IRadioItem getParent() {
    return _parent;
  }
 
  public void setParent(IRadioItem newParent){
    _parent = newParent;
  }
 
  public SortMode getSortMode() {
    return _sortMode;
  }
 
  public void setSortMode(SortMode value) {
    _sortMode = value;
    Controller.getInstance().getPreferenceStore().setValue(PreferenceConstants.RADIOVIEW_SORT_MODE, _sortMode.getValue());
  }

}
TOP

Related Classes of org.jampa.model.radio.AbstractRadioItem

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.