ingo@524: package de.intevation.flys.client.shared.model;
ingo@524:
ingo@524: import java.io.Serializable;
ingo@524:
ingo@524: import java.util.List;
ingo@524:
ingo@524:
ingo@524: /**
ingo@524: * @author Ingo Weinzierl
ingo@524: */
ingo@524: public class ThemeList implements Serializable {
ingo@524:
ingo@524: public List themes;
ingo@524:
ingo@524:
ingo@524: public ThemeList() {
ingo@524: }
ingo@524:
ingo@524:
ingo@524: public ThemeList(List themes) {
ingo@524: this.themes = themes;
ingo@524: }
ingo@524:
ingo@524:
ingo@524: public List getThemes() {
ingo@524: return themes;
ingo@524: }
ingo@524:
ingo@524:
ingo@524: public int getThemeCount() {
ingo@524: return themes.size();
ingo@524: }
ingo@524:
ingo@524:
ingo@524: /**
ingo@524: * Returns a theme at a specific position. NOTE: Themes start at position
ingo@524: * 1. So, take care in loops, that might start at index 0!
ingo@524: *
ingo@524: * @param pos The position of the desired theme.
ingo@524: *
ingo@524: * @return a theme.
ingo@524: */
ingo@524: public Theme getThemeAt(int pos) {
ingo@524: for (Theme theme: themes) {
ingo@524: if (theme.getPosition() == pos) {
ingo@524: return theme;
ingo@524: }
ingo@524: }
ingo@524:
ingo@524: return null;
ingo@524: }
ingo@526:
ingo@526:
ingo@526: public void removeTheme(Theme theme) {
ingo@526: if (theme != null) {
ingo@526: themes.remove(theme);
ingo@526: }
ingo@526: }
ingo@526:
ingo@526:
ingo@526: public void addTheme(Theme theme) {
ingo@526: if (theme != null) {
ingo@526: themes.add(theme);
ingo@526: }
ingo@526: }
ingo@528:
ingo@528:
ingo@528: /**
ingo@528: * Modifies the order of themes in this list and the position of the
ingo@528: * theme itself.
ingo@528: *
ingo@528: * @param theme The theme which position has to be modified.
ingo@528: * @param newPos The new position.
ingo@528: */
ingo@528: public void setThemePosition(Theme theme, int newPos) {
ingo@528: int count = getThemeCount();
ingo@528: int oldPos = theme.getPosition();
ingo@528:
ingo@528: if (newPos == oldPos || newPos > count || newPos < 1) {
ingo@528: return;
ingo@528: }
ingo@528:
ingo@528: boolean moveUp = newPos < oldPos;
ingo@528:
ingo@528: for (Theme aTheme: themes) {
ingo@528: int tmpPos = aTheme.getPosition();
ingo@528:
ingo@528: if (theme.equals(aTheme)) {
ingo@528: theme.setPosition(newPos);
ingo@528: }
ingo@528: else if (tmpPos >= newPos && tmpPos < oldPos && moveUp) {
ingo@528: aTheme.setPosition(tmpPos+1);
ingo@528: }
ingo@528: else if (tmpPos <= newPos && tmpPos > oldPos && !moveUp) {
ingo@528: aTheme.setPosition(tmpPos-1);
ingo@528: }
ingo@528: }
ingo@528: }
ingo@524: }
ingo@524: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :