1/*
2 * Copyright 2012 AndroidPlot.com
3 *
4 *    Licensed under the Apache License, Version 2.0 (the "License");
5 *    you may not use this file except in compliance with the License.
6 *    You may obtain a copy of the License at
7 *
8 *        http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *    Unless required by applicable law or agreed to in writing, software
11 *    distributed under the License is distributed on an "AS IS" BASIS,
12 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *    See the License for the specific language governing permissions and
14 *    limitations under the License.
15 */
16
17package com.androidplot.util;
18
19import java.util.List;
20
21/**
22 * Utility class providing additional element organization operations.
23 * @param 
24 */
25public class ListOrganizer<ElementType> implements ZIndexable<ElementType> {
26    private List<ElementType> list;
27
28    public ListOrganizer(List<ElementType> list) {
29        this.list = list;
30    }
31
32
33    public boolean moveToTop(ElementType element) {
34            if(list.remove(element)) {
35                list.add(list.size(), element);
36                return true;
37            } else {
38                return false;
39            }
40    }
41
42    public boolean moveAbove(ElementType objectToMove, ElementType reference) {
43        if(objectToMove == reference) {
44            throw new IllegalArgumentException("Illegal argument to moveAbove(A, B); A cannot be equal to B.");
45        }
46
47
48        list.remove(objectToMove);
49        int refIndex = list.indexOf(reference);
50        list.add(refIndex + 1, objectToMove);
51        return true;
52        //widgetOrder.remove(element);
53
54    }
55
56    public boolean moveBeneath(ElementType objectToMove, ElementType reference) {
57        if (objectToMove == reference) {
58            throw new IllegalArgumentException("Illegal argument to moveBeaneath(A, B); A cannot be equal to B.");
59        }
60
61        list.remove(objectToMove);
62        int refIndex = list.indexOf(reference);
63        list.add(refIndex, objectToMove);
64        return true;
65
66    }
67
68    public boolean moveToBottom(ElementType key) {
69
70        //int widgetIndex = widgetOrder.indexOf(key);
71        list.remove(key);
72        //list.add(list.size(), key);
73        list.add(0, key);
74        return true;
75        //widgetOrder.remove(key);
76    }
77
78    public boolean moveUp(ElementType key) {
79        int widgetIndex = list.indexOf(key);
80        if(widgetIndex == -1) {
81            // key not found:
82            return false;
83        }
84        if(widgetIndex >= list.size()-1) {
85            // already at the top:
86            return true;
87        }
88
89        ElementType widgetAbove = list.get(widgetIndex+1);
90        return moveAbove(key, widgetAbove);
91    }
92
93    public boolean moveDown(ElementType key) {
94        int widgetIndex = list.indexOf(key);
95        if(widgetIndex == -1) {
96            // key not found:
97            return false;
98        }
99        if(widgetIndex <= 0) {
100            // already at the bottom:
101            return true;
102        }
103
104        ElementType widgetBeneath = list.get(widgetIndex-1);
105        return moveBeneath(key, widgetBeneath);
106    }
107
108    @Override
109    public List<ElementType> elements() {
110        return list;
111    }
112
113    public void addToBottom(ElementType element) {
114        list.add(0, element);
115    }
116
117    public void addToTop(ElementType element) {
118        list.add(list.size(), element);
119    }
120}
121