1 /***
2 * Ambient - A music player for the Android platform
3 Copyright (C) 2007 Martin Vysny
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package sk.baka.ambient.activity.main;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24
25 import sk.baka.ambient.ActionsEnum;
26 import sk.baka.ambient.ZoomEnum;
27 import sk.baka.ambient.commons.SimpleBus;
28 import android.util.Log;
29
30 /***
31 * Groups given controllers together and allows group operations over them.
32 *
33 * @author Martin Vysny
34 */
35 public final class ControllerGroup {
36 /***
37 * Creates new group controller.
38 */
39 public ControllerGroup() {
40 super();
41 }
42
43 private final List<AbstractController> controllers = new ArrayList<AbstractController>();
44 private final List<ActionsEnum> actions = new ArrayList<ActionsEnum>();
45
46 /***
47 * Registers a controller to this group.
48 *
49 * @param controller
50 * the controller to register.
51 * @param action
52 * action which triggers visibility of this controller.
53 */
54 public void add(final AbstractController controller,
55 final ActionsEnum action) {
56 controllers.add(controller);
57 actions.add(action);
58 }
59
60 /***
61 * Destroys all controllers. Exceptions are catched and logged.
62 */
63 public void destroy() {
64 for (final AbstractController c : controllers) {
65 try {
66 c.destroy();
67 } catch (Exception ex) {
68 Log.e(ControllerGroup.class.getSimpleName(),
69 "Error destroying controller: " + ex.getMessage(), ex);
70 }
71 }
72 controllers.clear();
73 actions.clear();
74 }
75
76 /***
77 * Hides all other controllers and flips the visibility of given controller.
78 *
79 * @param controller
80 * the controller to show/hide. If <code>null</code> then all
81 * controllers are hidden.
82 * @param visibility
83 * set this visibility for the controller. If <code>null</code>
84 * then the visibility is flipped.
85 */
86 public void flipVisibility(final AbstractController controller,
87 final Boolean visibility) {
88 for (final AbstractController c : controllers) {
89 if (c != controller) {
90 c.hide();
91 } else {
92 if (visibility == null) {
93 c.flipVisibility();
94 } else {
95 c.setVisibility(visibility);
96 }
97 }
98 }
99 }
100
101 /***
102 * Returns the visible controller.
103 *
104 * @return index of the visible controller or <code>-1</code> if no such
105 * controller exists.
106 */
107 public int getVisibleControllerIndex() {
108 for (int i = 0; i < controllers.size(); i++) {
109 if (controllers.get(i).isVisible())
110 return i;
111 }
112 return -1;
113 }
114
115 /***
116 * Returns the visible controller.
117 *
118 * @return index of the visible controller or <code>-1</code> if no such
119 * controller exists.
120 */
121 public AbstractController getVisibleController() {
122 for (int i = 0; i < controllers.size(); i++) {
123 final AbstractController c = controllers.get(i);
124 if (c.isVisible()) {
125 return c;
126 }
127 }
128 return null;
129 }
130
131 /***
132 * Returns action which triggers visibility of this controller.
133 *
134 * @param controller
135 * the controller, may be <code>null</code>.
136 * @return the action or <code>null</code> if no such controller exists or
137 * it has no action attached.
138 */
139 public ActionsEnum getActionForController(
140 final AbstractController controller) {
141 final int index = controllers.indexOf(controller);
142 if (index < 0) {
143 return null;
144 }
145 return actions.get(index);
146 }
147
148 /***
149 * Unregisters all controllers from given bus.
150 *
151 * @param bus
152 * the bus to unregister controllers from.
153 */
154 public void unregister(final SimpleBus bus) {
155 for (final AbstractController c : controllers) {
156 bus.removeHandler(c);
157 }
158 }
159
160 /***
161 * Registers all controllers from given bus. Invokes
162 * {@link AbstractController#update(sk.baka.ambient.commons.Interval)} on
163 * all controllers.
164 *
165 * @param bus
166 * the bus to unregister controllers from.
167 */
168 public void registerAndUpdate(final SimpleBus bus) {
169 for (final AbstractController c : controllers) {
170 bus.addHandler(c);
171 c.update(null);
172 }
173 }
174
175 /***
176 * Returns controller present at given index. Returns <code>null</code> if
177 * the index is invalid.
178 *
179 * @param index
180 * the index
181 * @return controller instance or <code>null</code>.
182 */
183 public AbstractController get(int index) {
184 return ((index < 0) || (index >= controllers.size())) ? null
185 : controllers.get(index);
186 }
187
188 /***
189 * Returns first controller with given class (or subclass of given class).
190 *
191 * @param <T>
192 * the controller type
193 * @param clazz
194 * the controller class
195 * @return non-<code>null</code> controller instance.
196 * @throws IllegalArgumentException
197 * if no such controller exists.
198 */
199 public <T extends AbstractController> T getController(final Class<T> clazz) {
200 for (final AbstractController c : controllers) {
201 if (clazz.isInstance(c)) {
202 return clazz.cast(c);
203 }
204 }
205 throw new IllegalArgumentException("No such controller: "
206 + clazz.getName());
207 }
208
209 /***
210 * Zooms all controllers.
211 *
212 * @param zoom
213 * new zoom levels.
214 */
215 public void zoom(final Map<ZoomEnum,Integer> zoom) {
216 for (final AbstractController c : controllers) {
217 c.zoom(zoom);
218 }
219 }
220 }