View Javadoc

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  
24  import sk.baka.ambient.ActionsEnum;
25  import sk.baka.ambient.commons.SimpleBus;
26  import android.util.Log;
27  
28  /***
29   * Groups given controllers together and allows group operations over them.
30   * 
31   * @author Martin Vysny
32   */
33  public final class ControllerGroup {
34  	/***
35  	 * Creates new group controller.
36  	 */
37  	public ControllerGroup() {
38  		super();
39  	}
40  
41  	private final List<AbstractController> controllers = new ArrayList<AbstractController>();
42  	private final List<ActionsEnum> actions = new ArrayList<ActionsEnum>();
43  
44  	/***
45  	 * Registers a controller to this group.
46  	 * 
47  	 * @param controller
48  	 *            the controller to register.
49  	 * @param action
50  	 *            action which triggers visibility of this controller.
51  	 */
52  	public void add(final AbstractController controller,
53  			final ActionsEnum action) {
54  		controllers.add(controller);
55  		actions.add(action);
56  	}
57  
58  	/***
59  	 * Destroys all controllers. Exceptions are catched and logged.
60  	 */
61  	public void destroy() {
62  		for (final AbstractController c : controllers) {
63  			try {
64  				c.destroy();
65  			} catch (Exception ex) {
66  				Log.e(ControllerGroup.class.getSimpleName(),
67  						"Error destroying controller: " + ex.getMessage(), ex);
68  			}
69  		}
70  		controllers.clear();
71  		actions.clear();
72  	}
73  
74  	/***
75  	 * Hides all other controllers and flips the visibility of given controller.
76  	 * 
77  	 * @param controller
78  	 *            the controller to show/hide. If <code>null</code> then all
79  	 *            controllers are hidden.
80  	 * @param visibility
81  	 *            set this visibility for the controller. If <code>null</code>
82  	 *            then the visibility is flipped.
83  	 */
84  	public void flipVisibility(final AbstractController controller,
85  			final Boolean visibility) {
86  		for (final AbstractController c : controllers) {
87  			if (c != controller) {
88  				c.hide();
89  			} else {
90  				if (visibility == null) {
91  					c.flipVisibility();
92  				} else {
93  					c.setVisibility(visibility);
94  				}
95  			}
96  		}
97  	}
98  
99  	/***
100 	 * Returns the visible controller.
101 	 * 
102 	 * @return index of the visible controller or <code>-1</code> if no such
103 	 *         controller exists.
104 	 */
105 	public int getVisibleControllerIndex() {
106 		for (int i = 0; i < controllers.size(); i++) {
107 			if (controllers.get(i).isVisible())
108 				return i;
109 		}
110 		return -1;
111 	}
112 
113 	/***
114 	 * Returns the visible controller.
115 	 * 
116 	 * @return index of the visible controller or <code>-1</code> if no such
117 	 *         controller exists.
118 	 */
119 	public AbstractController getVisibleController() {
120 		for (int i = 0; i < controllers.size(); i++) {
121 			final AbstractController c = controllers.get(i);
122 			if (c.isVisible()) {
123 				return c;
124 			}
125 		}
126 		return null;
127 	}
128 
129 	/***
130 	 * Returns action which triggers visibility of this controller.
131 	 * 
132 	 * @param controller
133 	 *            the controller, may be <code>null</code>.
134 	 * @return the action or <code>null</code> if no such controller exists or
135 	 *         it has no action attached.
136 	 */
137 	public ActionsEnum getActionForController(
138 			final AbstractController controller) {
139 		final int index = controllers.indexOf(controller);
140 		if (index < 0) {
141 			return null;
142 		}
143 		return actions.get(index);
144 	}
145 
146 	/***
147 	 * Unregisters all controllers from given bus.
148 	 * 
149 	 * @param bus
150 	 *            the bus to unregister controllers from.
151 	 */
152 	public void unregister(final SimpleBus bus) {
153 		for (final AbstractController c : controllers) {
154 			bus.removeHandler(c);
155 		}
156 	}
157 
158 	/***
159 	 * Registers all controllers from given bus. Invokes
160 	 * {@link AbstractController#update(sk.baka.ambient.commons.Interval)} on
161 	 * all controllers.
162 	 * 
163 	 * @param bus
164 	 *            the bus to unregister controllers from.
165 	 */
166 	public void registerAndUpdate(final SimpleBus bus) {
167 		for (final AbstractController c : controllers) {
168 			bus.addHandler(c);
169 			c.update(null);
170 		}
171 	}
172 
173 	/***
174 	 * Returns controller present at given index. Returns <code>null</code> if
175 	 * the index is invalid.
176 	 * 
177 	 * @param index
178 	 *            the index
179 	 * @return controller instance or <code>null</code>.
180 	 */
181 	public AbstractController get(int index) {
182 		return ((index < 0) || (index >= controllers.size())) ? null
183 				: controllers.get(index);
184 	}
185 
186 	/***
187 	 * Returns first controller with given class (or subclass of given class).
188 	 * 
189 	 * @param <T>
190 	 *            the controller type
191 	 * @param clazz
192 	 *            the controller class
193 	 * @return non-<code>null</code> controller instance.
194 	 * @throws IllegalArgumentException
195 	 *             if no such controller exists.
196 	 */
197 	public <T extends AbstractController> T getController(final Class<T> clazz) {
198 		for (final AbstractController c : controllers) {
199 			if (clazz.isInstance(c)) {
200 				return clazz.cast(c);
201 			}
202 		}
203 		throw new IllegalArgumentException("No such controller: "
204 				+ clazz.getName());
205 	}
206 
207 	public void zoom(boolean zoom) {
208 		for (final AbstractController c : controllers) {
209 			c.zoom(zoom);
210 		}
211 	}
212 }