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  package sk.baka.ambient.activity.main;
19  
20  import java.util.List;
21  import java.util.Map;
22  
23  import sk.baka.ambient.AmbientApplication;
24  import sk.baka.ambient.R;
25  import sk.baka.ambient.ZoomEnum;
26  import sk.baka.ambient.collection.TrackMetadataBean;
27  import sk.baka.ambient.commons.Interval;
28  import sk.baka.ambient.views.gesturelist.GesturesListView;
29  import sk.baka.ambient.views.gesturelist.IGestureListViewListener;
30  import sk.baka.ambient.views.gesturelist.ModelHolder;
31  import sk.baka.ambient.views.gesturelist.TrackListClipboardObject;
32  import android.app.Activity;
33  import android.widget.LinearLayout.LayoutParams;
34  
35  /***
36   * The items in this controller are rendered as a list and can be selected.
37   * 
38   * @author Martin Vysny
39   */
40  public abstract class AbstractListController extends AbstractController
41  		implements IGestureListViewListener {
42  
43  	/***
44  	 * The list instance.
45  	 */
46  	public GesturesListView listView;
47  
48  	/***
49  	 * Creates new controller.
50  	 * 
51  	 * @param mainViewId
52  	 *            the view whose visibility is controlled.
53  	 * @param listViewId
54  	 *            the id of the {@link GesturesListView}
55  	 * @param mainActivity
56  	 *            the main activity instance.
57  	 */
58  	protected AbstractListController(final int mainViewId, int listViewId,
59  			Activity mainActivity) {
60  		super(mainViewId, mainActivity);
61  		final GesturesListView pv = ((GesturesListView) mainView
62  				.findViewById(listViewId));
63  		pv.listener = this;
64  		this.listView = pv;
65  	}
66  
67  	/***
68  	 * The color of highlighted items in the list.
69  	 */
70  	protected final int highlightColor = mainActivity.getResources().getColor(
71  			R.color.highlight);
72  
73  	/***
74  	 * Recomputes the {@link GesturesListView#getModel() model}. You don't have
75  	 * to call the {@link ModelHolder#notifyModified()} - it is called
76  	 * automatically after the method finishes.
77  	 */
78  	protected abstract void recomputeListItems();
79  
80  	public void highlightChanged(Interval highlight) {
81  		// nothing to do here
82  	}
83  
84  	/***
85  	 * Overrides default functionality - the {@link #recomputeListItems()} is
86  	 * invoked and the list view contents are updated.
87  	 */
88  	@Override
89  	public void update(final Interval target) {
90  		recomputeListItems();
91  		listView.getModel().highlight(target == null ? Interval.EMPTY : target);
92  		listView.getModel().notifyModified();
93  	}
94  
95  	public boolean canComputeItems() {
96  		return false;
97  	}
98  
99  	public boolean isReadOnly() {
100 		return true;
101 	}
102 
103 	public List<TrackMetadataBean> computeTracks(Interval highlight) {
104 		throw new UnsupportedOperationException("computeTracks");
105 	}
106 
107 	public void dropItems(List<TrackMetadataBean> tracks, int x, int y,
108 			int index) {
109 		throw new UnsupportedOperationException("dropItems");
110 	}
111 
112 	public Interval moveItems(Interval highlight, int delta) {
113 		throw new UnsupportedOperationException("moveItems");
114 	}
115 
116 	public Interval moveItemsByOne(Interval highlight, boolean down) {
117 		throw new UnsupportedOperationException("moveItemsByOne");
118 	}
119 
120 	public String getHint(Interval highlight) {
121 		throw new UnsupportedOperationException("getHint");
122 	}
123 
124 	public boolean isComputeTracksLong(Interval interval) {
125 		throw new UnsupportedOperationException("isComputeTracksLong");
126 	}
127 
128 	public boolean isComputeTracksOnlineOp(Interval interval) {
129 		throw new UnsupportedOperationException("isComputeTracksOnlineOp");
130 	}
131 
132 	public void removeItems(Interval remove) {
133 		throw new UnsupportedOperationException("removeItems");
134 	}
135 
136 	public boolean canHighlight() {
137 		return true;
138 	}
139 
140 	public void setClipboard(TrackListClipboardObject clipboard) {
141 		AmbientApplication.getInstance().setClipboard(clipboard);
142 	}
143 
144 	public Object getClipboard() {
145 		return AmbientApplication.getInstance().getClipboard();
146 	}
147 
148 	@Override
149 	public void destroy() {
150 		listView = null;
151 		super.destroy();
152 	}
153 
154 	@Override
155 	protected void performZoom(final Map<ZoomEnum, Integer> zoom) {
156 		listView.zoom(ZoomEnum.ListViewItems.getLevel(zoom));
157 	}
158 	
159 	/***
160 	 * Resizes the component handled by this controller.
161 	 * 
162 	 * @param delta
163 	 *            increase or decrease the height of the component by
164 	 *            <code>delta</code> pixels.
165 	 */
166 	public void resize(final int delta) {
167 		int newHeight = listView.getHeight() + delta;
168 		if (newHeight < 0) {
169 			newHeight = listView.getHeight();
170 		}
171 		final LayoutParams params = (LayoutParams) listView.getLayoutParams();
172 		params.height = newHeight;
173 		listView.requestLayout();
174 	}
175 }