/*
* 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.library;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jampa.model.SortMode;
import org.jampa.model.comparators.LibraryItemComparator;
public abstract class AbstractLibraryItem implements ILibraryItem {
protected Map<String, ILibraryItem> _list;
private ILibraryItem _parent;
protected String _name;
protected static SortMode _librarySortMode = SortMode.Ascending;
public AbstractLibraryItem(ILibraryItem parent, String name) {
_name = name;
_parent = parent;
_list = new Hashtable<String, ILibraryItem>();
}
public void addChild(String name, ILibraryItem child) {
_list.put(name, child);
}
public Object getParent() {
return _parent;
}
public boolean hasChildren() {
return _list.size() > 0;
}
public Map<String, ILibraryItem> getList() {
return _list;
}
public String getName() {
return _name;
}
public ILibraryItem getLibraryItemByName(String name) {
return _list.get(name);
}
public List<ILibraryItem> getChildrenList() {
ArrayList<ILibraryItem> tmpList = new ArrayList<ILibraryItem>();
Set<String> keys = _list.keySet();
Iterator<String> iter = keys.iterator();
ILibraryItem item;
while (iter.hasNext()) {
item = _list.get(iter.next());
tmpList.add(item);
}
Collections.sort(tmpList, new LibraryItemComparator(_librarySortMode));
return tmpList;
}
public Object[] getChildren() {
return getChildrenList().toArray();
}
public int getChildCount() {
int result = _list.size();
Set<String> keys = _list.keySet();
Iterator<String> iter = keys.iterator();
ILibraryItem item;
while (iter.hasNext()) {
item = _list.get(iter.next());
result = result + item.getChildCount();
}
return result;
}
public String getSortName() {
return _name;
}
public SortMode getLibrarySortMode() {
return _librarySortMode;
}
public void setLibrarySortMode(SortMode mode) {
_librarySortMode = mode;
}
}