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.views.gesturelist;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24
25 import sk.baka.ambient.collection.TrackMetadataBean;
26 import android.view.View;
27
28 /***
29 * The clipboard object holding a read-only list of arbitrary objects.
30 *
31 * @author Martin Vysny
32 */
33 public final class TrackListClipboardObject {
34
35 /***
36 * Creates new clipboard object.
37 *
38 * @param objects
39 * the object list, must not be <code>null</code>.
40 * @param targets
41 * possible view targets, must not be <code>null</code> nor
42 * empty.
43 */
44 public TrackListClipboardObject(
45 final List<? extends TrackMetadataBean> objects,
46 final List<? extends GesturesListView> targets) {
47 super();
48 if (targets.isEmpty())
49 throw new IllegalArgumentException("No tracks");
50 this.tracks = Collections
51 .unmodifiableList(new ArrayList<TrackMetadataBean>(objects));
52 this.targets = new ArrayList<GesturesListView>(targets);
53 }
54
55 /***
56 * Stored tracks. May return <code>null</code> if the tracklist was
57 * already garbage-collected.
58 */
59 private final List<TrackMetadataBean> tracks;
60
61 /***
62 * The allowed targets.
63 */
64 private final List<GesturesListView> targets;
65
66 /***
67 * Checks if given view is one of the possible targets of this object.
68 *
69 * @param view
70 * the view to check
71 * @return <code>true</code> if the view is an allowed paste target.
72 */
73 public boolean isTarget(final GesturesListView view) {
74 return targets.contains(view);
75 }
76
77 /***
78 * Retrieves stored tracks. May return empty collection if the tracklist was
79 * already garbage-collected.
80 *
81 * @return the tracklist, never <code>null</code>.
82 */
83 public List<TrackMetadataBean> getObjects() {
84 final List<TrackMetadataBean> result = tracks;
85 if (result != null)
86 return result;
87 return Collections.emptyList();
88 }
89
90 /***
91 * Retrieves the clipboard object from given view's
92 * {@link View#getTag() tag}.
93 *
94 * @param view
95 * the view to retrieve the object from
96 * @return object instance or <code>null</code> if there was invalid type
97 * or instance of object stored as the tag value.
98 */
99 public static TrackListClipboardObject fromView(final View view) {
100 if (view == null)
101 return null;
102 final Object tag = view.getTag();
103 return fromObject(tag);
104 }
105
106 /***
107 * Retrieves the clipboard object from given object.
108 *
109 * @param clipboard
110 * the object to retrieve the clipboard from
111 * @return object instance or <code>null</code> if invalid type of
112 * clipboard was provided.
113 */
114 public static TrackListClipboardObject fromObject(final Object clipboard) {
115 if (!(clipboard instanceof TrackListClipboardObject))
116 return null;
117 return (TrackListClipboardObject) clipboard;
118 }
119
120 /***
121 * Retrieves the clipboard from given object and checks if it is empty.
122 *
123 * @param view
124 * the view to check
125 * @return <code>true</code> if it is empty, <code>false</code>
126 * otherwise.
127 */
128 public static boolean isEmpty(final View view) {
129 final TrackListClipboardObject lco = fromView(view);
130 if (lco == null)
131 return true;
132 return lco.getObjects().isEmpty();
133 }
134
135 /***
136 * Retrieves the clipboard from given object and checks if it is empty.
137 *
138 * @param object
139 * the object to check
140 * @return <code>true</code> if it is empty, <code>false</code>
141 * otherwise.
142 */
143 public static boolean isEmpty(final Object object) {
144 final TrackListClipboardObject lco = fromObject(object);
145 if (lco == null)
146 return true;
147 return lco.getObjects().isEmpty();
148 }
149 }