1 /*
2  * aTunes
3  * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors
4  *
5  * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
6  *
7  * http://www.atunes.org
8  * http://sourceforge.net/projects/atunes
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20 
21 package net.sourceforge.atunes.kernel.actions;
22 
23 import java.awt.event.ActionEvent;
24 import java.lang.reflect.ParameterizedType;
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 import javax.swing.Icon;
29 
30 import net.sourceforge.atunes.model.IAudioObject;
31 import net.sourceforge.atunes.model.ITreeObject;
32 import net.sourceforge.atunes.model.ITreeObjectsSource;
33 import net.sourceforge.atunes.utils.Logger;
34 import net.sourceforge.atunes.utils.StringUtils;
35 
36 /**
37  * An action called after selecting nodes of a tree
38  * @author alex
39  *
40  * @param <T>
41  */
42 public abstract class AbstractActionOverSelectedTreeObjects<T extends ITreeObject<? extends IAudioObject>> extends CustomAbstractAction {
43 
44 	private static final long serialVersionUID = -2396109319433549043L;
45 
46 	private ITreeObjectsSource treeObjectsSource;
47 
48 	private final Class<?> clazz;
49 
50 	/**
51 	 * @param name
52 	 */
AbstractActionOverSelectedTreeObjects(final String name)53 	public AbstractActionOverSelectedTreeObjects(final String name) {
54 		super(name);
55 		clazz = (Class<?>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
56 	}
57 
58 	/**
59 	 * @param name
60 	 * @param icon
61 	 */
AbstractActionOverSelectedTreeObjects(final String name, final Icon icon)62 	public AbstractActionOverSelectedTreeObjects(final String name, final Icon icon) {
63 		super(name, icon);
64 		clazz = (Class<?>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
65 	}
66 
67 	/**
68 	 * @param treeObjectsSource
69 	 */
setTreeObjectsSource(final ITreeObjectsSource treeObjectsSource)70 	public void setTreeObjectsSource(final ITreeObjectsSource treeObjectsSource) {
71 		this.treeObjectsSource = treeObjectsSource;
72 	}
73 
74 	/**
75 	 * Returns if preprocess is needed
76 	 *
77 	 * Default implementation does not need preprocess
78 	 *
79 	 * @return
80 	 */
isPreprocessNeeded()81 	protected boolean isPreprocessNeeded() {
82 		return false;
83 	}
84 
85 	/**
86 	 * Given a tree object performs a preprocess returning tree object to
87 	 * include in list or null if given tree object must be excluded from list
88 	 *
89 	 * Default implementation returns the same object
90 	 *
91 	 * @param treeObject
92 	 * @return
93 	 */
preprocessObject(final T treeObject)94 	protected T preprocessObject(final T treeObject) {
95 		return treeObject;
96 	}
97 
executeAction(List<T> objects)98 	protected abstract void executeAction(List<T> objects);
99 
100 	@Override
executeAction()101 	protected final void executeAction() {
102 		// Use executionAction(List<T> objects)
103 	}
104 
105 	@SuppressWarnings("unchecked")
106 	@Override
actionPerformed(final ActionEvent e)107 	public final void actionPerformed(final ActionEvent e) {
108 		Logger.debug("Executing action: ", this.getClass().getName());
109 
110 		if (this.treeObjectsSource == null) {
111 			throw new IllegalArgumentException(StringUtils.getString("No treeObjectsSource for action: ", this.getClass().getName()));
112 		}
113 
114 		List<ITreeObject<? extends IAudioObject>> treeObjects = treeObjectsSource.getSelectedTreeObjects();
115 
116 		if (treeObjects == null || treeObjects.isEmpty()) {
117 			return;
118 		}
119 
120 		List<T> selectedTreeObjects = new ArrayList<T>();
121 
122 		for (ITreeObject<? extends IAudioObject> ao : treeObjects) {
123 			if (clazz.isAssignableFrom(ao.getClass())) {
124 				if (isPreprocessNeeded()) {
125 					T processedTreeObject = preprocessObject((T) ao);
126 					if (processedTreeObject != null) {
127 						selectedTreeObjects.add(processedTreeObject);
128 					}
129 				} else {
130 					selectedTreeObjects.add((T)ao);
131 				}
132 			}
133 		}
134 
135 		// Call to perform action
136 		executeAction(selectedTreeObjects);
137 	}
138 }
139