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.navigator;
22 
23 import java.awt.event.MouseAdapter;
24 import java.awt.event.MouseEvent;
25 import java.util.List;
26 
27 import net.sourceforge.atunes.gui.GuiUtils;
28 import net.sourceforge.atunes.gui.NavigationTableModel;
29 import net.sourceforge.atunes.model.IAudioObject;
30 import net.sourceforge.atunes.model.INavigationHandler;
31 import net.sourceforge.atunes.model.INavigationView;
32 import net.sourceforge.atunes.model.IOSManager;
33 import net.sourceforge.atunes.model.IPlayListHandler;
34 import net.sourceforge.atunes.model.ITable;
35 
36 /**
37  * The listener interface for receiving navigationTableMouse events.
38  */
39 public final class NavigationTableMouseListener extends MouseAdapter {
40 
41 	private NavigationController navigationController;
42 	private ITable navigationTable;
43 	private INavigationHandler navigationHandler;
44 	private IPlayListHandler playListHandler;
45 	private IOSManager osManager;
46 
47 	/**
48 	 * @param navigationController
49 	 */
setNavigationController( final NavigationController navigationController)50 	public void setNavigationController(
51 			final NavigationController navigationController) {
52 		this.navigationController = navigationController;
53 	}
54 
55 	/**
56 	 * @param navigationTable
57 	 */
setNavigationTable(final ITable navigationTable)58 	public void setNavigationTable(final ITable navigationTable) {
59 		this.navigationTable = navigationTable;
60 	}
61 
62 	/**
63 	 * @param navigationHandler
64 	 */
setNavigationHandler(final INavigationHandler navigationHandler)65 	public void setNavigationHandler(final INavigationHandler navigationHandler) {
66 		this.navigationHandler = navigationHandler;
67 	}
68 
69 	/**
70 	 * @param playListHandler
71 	 */
setPlayListHandler(final IPlayListHandler playListHandler)72 	public void setPlayListHandler(final IPlayListHandler playListHandler) {
73 		this.playListHandler = playListHandler;
74 	}
75 
76 	/**
77 	 * @param osManager
78 	 */
setOsManager(final IOSManager osManager)79 	public void setOsManager(final IOSManager osManager) {
80 		this.osManager = osManager;
81 	}
82 
83 	@Override
mouseClicked(final MouseEvent event)84 	public void mouseClicked(final MouseEvent event) {
85 		INavigationView currentView = this.navigationHandler.getCurrentView();
86 
87 		if (GuiUtils.isSecondaryMouseButton(this.osManager, event)) {
88 			this.navigationController.setPopupMenuCaller(this.navigationTable
89 					.getSwingComponent());
90 			int[] rowsSelected = this.navigationTable.getSelectedRows();
91 			int selected = this.navigationTable.rowAtPoint(event.getPoint());
92 			boolean found = false;
93 			int i = 0;
94 			while (!found && i < rowsSelected.length) {
95 				if (rowsSelected[i] == selected) {
96 					found = true;
97 				}
98 				i++;
99 			}
100 			if (!found) {
101 				this.navigationTable.getSelectionModel().setSelectionInterval(
102 						selected, selected);
103 			}
104 
105 			// Enable or disable actions of popup
106 			currentView.updateTablePopupMenuWithTableSelection(
107 					this.navigationTable, event);
108 
109 			// Show popup
110 			currentView.getTablePopupMenu().show(
111 					this.navigationController.getPopupMenuCaller(),
112 					event.getX(), event.getY());
113 		} else {
114 			if (event.getClickCount() == 2) {
115 				int[] selRow = this.navigationTable.getSelectedRows();
116 				List<IAudioObject> songs = ((NavigationTableModel) this.navigationTable
117 						.getModel()).getAudioObjectsAt(selRow);
118 				if (songs != null && songs.size() >= 1) {
119 					this.playListHandler.addToVisiblePlayList(songs);
120 				}
121 			}
122 		}
123 	}
124 }
125