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.modules.context.youtube;
22 
23 import java.awt.Component;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.util.ArrayList;
27 import java.util.List;
28 
29 import javax.swing.JMenuItem;
30 
31 import net.sourceforge.atunes.kernel.modules.context.AbstractContextPanelContent;
32 import net.sourceforge.atunes.kernel.modules.context.ContextTable;
33 import net.sourceforge.atunes.kernel.modules.context.ContextTableAction;
34 import net.sourceforge.atunes.kernel.modules.internetsearch.SearchFactory;
35 import net.sourceforge.atunes.kernel.modules.webservices.youtube.YoutubeService;
36 import net.sourceforge.atunes.model.IContextHandler;
37 import net.sourceforge.atunes.model.IUnknownObjectChecker;
38 import net.sourceforge.atunes.model.IVideoEntry;
39 import net.sourceforge.atunes.utils.I18nUtils;
40 
41 /**
42  * Content to show videos from Youtube
43  *
44  * @author alex
45  *
46  */
47 public class YoutubeContent extends
48 		AbstractContextPanelContent<YoutubeDataSource> {
49 
50 	private static final long serialVersionUID = 5041098100868186051L;
51 
52 	private ContextTable youtubeResultTable;
53 
54 	private final JMenuItem moreResults;
55 
56 	private final JMenuItem openYoutube;
57 
58 	private YoutubeService youtubeService;
59 
60 	private IContextHandler contextHandler;
61 
62 	private IUnknownObjectChecker unknownObjectChecker;
63 
64 	/**
65 	 * @param unknownObjectChecker
66 	 */
setUnknownObjectChecker( final IUnknownObjectChecker unknownObjectChecker)67 	public void setUnknownObjectChecker(
68 			final IUnknownObjectChecker unknownObjectChecker) {
69 		this.unknownObjectChecker = unknownObjectChecker;
70 	}
71 
72 	/**
73 	 * Default constructor
74 	 */
YoutubeContent()75 	public YoutubeContent() {
76 		this.moreResults = new JMenuItem(
77 				I18nUtils.getString("SEE_MORE_RESULTS"));
78 		this.moreResults.addActionListener(new ActionListener() {
79 
80 			@Override
81 			public void actionPerformed(final ActionEvent e) {
82 				searchMoreResultsInYoutube();
83 			}
84 		});
85 		this.openYoutube = new JMenuItem(I18nUtils.getString("GO_TO_YOUTUBE"));
86 		this.openYoutube.addActionListener(new ActionListener() {
87 
88 			@Override
89 			public void actionPerformed(final ActionEvent e) {
90 				openYoutube();
91 			}
92 		});
93 	}
94 
95 	@Override
getContentName()96 	public String getContentName() {
97 		return I18nUtils.getString("YOUTUBE_VIDEOS");
98 	}
99 
100 	@Override
updateContentFromDataSource(final YoutubeDataSource source)101 	public void updateContentFromDataSource(final YoutubeDataSource source) {
102 		((YoutubeResultTableModel) this.youtubeResultTable.getModel())
103 				.setEntries(source.getVideos());
104 		this.moreResults.setEnabled(true);
105 		this.openYoutube.setEnabled(true);
106 	}
107 
108 	@Override
clearContextPanelContent()109 	public void clearContextPanelContent() {
110 		super.clearContextPanelContent();
111 		((YoutubeResultTableModel) this.youtubeResultTable.getModel())
112 				.setEntries(null);
113 		this.moreResults.setEnabled(false);
114 		this.openYoutube.setEnabled(false);
115 	}
116 
117 	@Override
getComponent()118 	public Component getComponent() {
119 		// Create components
120 		this.youtubeResultTable = getBeanFactory().getBean(ContextTable.class);
121 		this.youtubeResultTable.setModel(new YoutubeResultTableModel());
122 		this.youtubeResultTable.addContextRowPanel(getBeanFactory().getBean(
123 				YoutubeResultsTableCellRendererCode.class));
124 
125 		ContextTableAction<IVideoEntry> action = getBeanFactory().getBean(
126 				OpenYoutubeVideoAction.class);
127 		List<ContextTableAction<?>> list = new ArrayList<ContextTableAction<?>>();
128 		list.add(action);
129 
130 		this.youtubeResultTable.setRowActions(list);
131 		return this.youtubeResultTable;
132 	}
133 
134 	@Override
getOptions()135 	public List<Component> getOptions() {
136 		List<Component> options = new ArrayList<Component>();
137 		options.add(this.moreResults);
138 		options.add(this.openYoutube);
139 		return options;
140 	}
141 
142 	/**
143 	 * Searches for more results of the last search
144 	 *
145 	 * @return
146 	 */
searchMoreResultsInYoutube()147 	private void searchMoreResultsInYoutube() {
148 		String searchString = this.youtubeService
149 				.getSearchForAudioObject(this.contextHandler
150 						.getCurrentAudioObject());
151 		if (searchString.length() > 0) {
152 			final List<IVideoEntry> result = this.youtubeService
153 					.searchInYoutube(
154 							this.contextHandler.getCurrentAudioObject()
155 									.getArtist(this.unknownObjectChecker),
156 							searchString,
157 							this.youtubeResultTable.getRowCount() + 1);
158 			((YoutubeResultTableModel) this.youtubeResultTable.getModel())
159 					.addEntries(result);
160 		}
161 	}
162 
163 	/**
164 	 * Opens a web browser to show youtube results
165 	 */
openYoutube()166 	protected void openYoutube() {
167 		getDesktop().openSearch(
168 				SearchFactory.getSearchForName("YouTube"),
169 				this.youtubeService.getSearchForAudioObject(this.contextHandler
170 						.getCurrentAudioObject()));
171 	}
172 
173 	/**
174 	 * @param youtubeService
175 	 */
setYoutubeService(final YoutubeService youtubeService)176 	public void setYoutubeService(final YoutubeService youtubeService) {
177 		this.youtubeService = youtubeService;
178 	}
179 
180 	/**
181 	 * @param contextHandler
182 	 */
setContextHandler(final IContextHandler contextHandler)183 	public void setContextHandler(final IContextHandler contextHandler) {
184 		this.contextHandler = contextHandler;
185 	}
186 }
187