ingo@524: package de.intevation.flys.client.shared.model;
ingo@524:
ingo@524: import java.io.Serializable;
ingo@524:
ingo@2438: import java.util.ArrayList;
felix@1435: import java.util.LinkedHashMap;
ingo@524: import java.util.List;
ingo@524:
ingo@524:
ingo@524: /**
felix@1435: * Data Model for list of themes (shown facets).
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@2438: public List getActiveThemes() {
ingo@2438: List active = new ArrayList();
ingo@2438: List all = getThemes();
ingo@2438:
ingo@2438: if (all == null || all.isEmpty()) {
ingo@2438: return active;
ingo@2438: }
ingo@2438:
ingo@2438: for (Theme theme: all) {
ingo@2438: if (theme.getActive() == 1) {
ingo@2438: active.add(theme);
ingo@2438: }
ingo@2438: }
ingo@2438:
ingo@2438: return active;
ingo@2438: }
ingo@2438:
ingo@2438:
ingo@524: public int getThemeCount() {
ingo@524: return themes.size();
ingo@524: }
ingo@524:
ingo@524:
ingo@524: /**
felix@1473: * Returns (first) theme of which the artifact has given uuid, null if none
felix@1473: * found.
felix@1473: * @param uuid Artifacts identifier for which to search theme.
felix@1473: * @return theme of which getArtifact() equals given uuid.
felix@1473: */
felix@1473: public Theme getTheme(String uuid) {
felix@1473: for (Theme theme: themes) {
felix@1473: if (theme.getArtifact().equals(uuid)) {
felix@1473: return theme;
felix@1473: }
felix@1473: }
felix@1473: return null;
felix@1473: }
felix@1473:
felix@1473:
felix@1473: /**
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: }
felix@1435:
felix@1435:
felix@1435: /**
felix@1435: * Create a map from index to description of facets that have a given name.
felix@1435: * Only visible facets are taken into account.
felix@1435: * @param facetName name to match against facets whose info to put in map.
felix@1435: * @return mapping of index to description
felix@1435: */
felix@1435: public LinkedHashMap toMapIndexDescription(String facetName) {
felix@1435: int count = getThemeCount();
felix@1435: LinkedHashMap valueMap = new LinkedHashMap();
felix@1437: for (int i = 0; i <= count; i++) {
felix@1435: Theme theme = getThemeAt(i + 1);
felix@1435:
felix@1435: if (theme == null || theme.getVisible() == 0) {
felix@1435: continue;
felix@1435: }
felix@1435:
felix@1435: if (theme.getFacet().equals(facetName)) {
felix@1435: valueMap.put(String.valueOf(theme.getIndex()),
felix@1435: theme.getDescription());
felix@1435: }
felix@1435: }
felix@1435: return valueMap;
felix@1435: }
felix@1435:
felix@1435:
felix@1435: public LinkedHashMap
felix@1435: toMapArtifactUUIDDescription(String facetName
felix@1435: ) {
felix@1435: int count = getThemeCount();
felix@1435: LinkedHashMap valueMap = new LinkedHashMap();
felix@1437: for (int i = 0; i <= count; i++) {
felix@1435: Theme theme = getThemeAt(i + 1);
felix@1435:
felix@1435: if (theme == null || theme.getVisible() == 0) {
felix@1435: continue;
felix@1435: }
felix@1435:
felix@1435: if (theme.getFacet().equals(facetName)) {
felix@1435: valueMap.put(theme.getArtifact(),
felix@1435: theme.getDescription());
felix@1435: }
felix@1435: }
felix@1435: return valueMap;
felix@1435: }
ingo@524: }
ingo@524: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :