View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
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, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.util.launcher;
21  
22  import java.util.Hashtable;
23  import java.util.Iterator;
24  
25  import org.xnap.XNap;
26  import org.xnap.util.Preferences;
27  
28  /***
29   * Manages {@link Player} objects.
30   */
31  public class PlayerManager
32  {
33  
34      //--- Constant(s) ---
35  
36      //--- Data field(s) ---
37  
38      private static Preferences prefs = Preferences.getInstance();
39      private static PlayerManager instance;
40      
41      private Player defaultPlayer;
42      private Hashtable playerByKey = new Hashtable();
43  
44      //--- Constructor(s) ---
45  
46      private PlayerManager() 
47      {
48  		add(new JMFPlayer());
49  		add(new XMMSPlayer());
50  		add(new NoatunPlayer());
51  		add(new ITunesPlayer());
52  		add(new DefaultPlayer("other", XNap.tr("Other"), ""));
53      }
54  
55      //--- Method(s) ---
56      
57      public static PlayerManager getInstance()
58      {
59  		if (instance == null) {
60  			synchronized (PlayerManager.class) {
61  				if (instance == null) {
62  					instance = new PlayerManager();
63  				}
64  			}
65  		}
66  		return instance;
67      }
68  
69      public void add(Player player)
70      {
71  		playerByKey.put(player.getKey(), player);
72      }
73  
74      /***
75       * Returns the player that is set in the preferences.
76       *
77       * @return null, if no valid player is set
78       */
79      public Player getDefaultPlayer()
80      {
81  		if (defaultPlayer != null) {
82  			if (defaultPlayer.getKey().equals(prefs.getPlayerType())) {
83  				return defaultPlayer;
84  			}
85  			defaultPlayer.stop();
86  		}
87  
88  		defaultPlayer = (Player)playerByKey.get(prefs.getPlayerType());
89  		if (defaultPlayer != null && defaultPlayer.isEditable()) {
90  			defaultPlayer.setCommand(prefs.getPlayerCommand());
91  		}
92  		return defaultPlayer;
93      }
94  
95      public Iterator iterator()
96      {
97  		return playerByKey.values().iterator();
98      }
99  
100     public void remove(Player player)
101     {
102 		playerByKey.remove(player.getKey());
103     }
104 
105 }