View Javadoc

1   package com.melloware.jukes.gui.view.component;
2   
3   import java.awt.event.ActionEvent;
4   import java.awt.event.KeyAdapter;
5   import java.awt.event.KeyEvent;
6   import java.awt.event.KeyListener;
7   import java.beans.PropertyChangeEvent;
8   import java.beans.PropertyChangeListener;
9   
10  import javax.swing.AbstractListModel;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  
15  import com.jgoodies.uif.action.ActionManager;
16  import com.melloware.jukes.file.Playlist;
17  import com.melloware.jukes.gui.tool.Actions;
18  import com.melloware.jukes.gui.tool.Resources;
19  import com.melloware.jukes.gui.view.PlaylistPanel;
20  
21  /**
22   * Mode used for displaying playlists  in a JList.
23   * <p>
24   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
25   * @author Emil A. Lefkof III <info@melloware.com>
26   * @version 4.0
27   */
28  public final class PlaylistListModel
29      extends AbstractListModel
30      implements PropertyChangeListener {
31  
32      private static final Log LOG = LogFactory.getLog(PlaylistListModel.class);
33      private final Playlist playlist;
34      private final PlaylistListModel model;
35      private final PlaylistPanel panel;
36      
37      /**
38       * KeyAdapter to deselect all on CTrl+D and delete items with DEL key.
39       */
40      private transient final KeyListener keyTypedListener = new KeyAdapter() {
41          public void keyPressed(final KeyEvent event) {
42              if ((event.getKeyCode() == KeyEvent.VK_DELETE) && (model.getSize() > 0)) {
43                  LOG.debug("Delete pressed");
44                  panel.putClientProperty(Resources.EDITOR_COMPONENT, panel);
45                  final ActionEvent action = new ActionEvent(panel, 1, "");
46                  ActionManager.get(Actions.PLAYLIST_REMOVE_TRACK_ID).actionPerformed(action);
47              } else if ((event.isControlDown()) && (event.getKeyCode() == KeyEvent.VK_D)) {
48                  LOG.debug("Deselect All");
49                  panel.clearSelection();
50              } else {
51                  super.keyPressed(event);
52              }
53          }
54      };
55  
56      /**
57      * Constructs a <code>PlaylistPanel</code> for the given module.
58      *
59      * @param aPlaylist  provides the playlist class needed for display
60      */
61      public PlaylistListModel(Playlist aPlaylist, PlaylistPanel aPanel) {
62          super();
63          LOG.debug("PlaylistListModel created.");
64          this.playlist = aPlaylist;
65          this.playlist.addPropertyChangeListener(this);
66          this.panel = aPanel;
67          this.model = this;
68      }
69  
70      /* (non-Javadoc)
71       * @see javax.swing.ListModel#getElementAt(int)
72       */
73      public Object getElementAt(int aIndex) {
74          Object returnValue;
75  
76          if (aIndex < this.playlist.getList().size()) {
77              returnValue = this.playlist.getList().get(aIndex);
78          } else {
79              returnValue = null;
80          }
81          return returnValue;
82      }
83  
84      /**
85       * Gets the keyTypedListener.
86       * <p>
87       * @return Returns the keyTypedListener.
88       */
89      public KeyListener getKeyTypedListener() {
90          return this.keyTypedListener;
91      }
92  
93      /* (non-Javadoc)
94       * @see javax.swing.ListModel#getSize()
95       */
96      public int getSize() {
97          return this.playlist.size();
98      }
99  
100     /**
101      * Whenever the master playlist changes update this view.
102      */
103     public void propertyChange(PropertyChangeEvent evt) {
104         LOG.debug("Playlist changed");
105         fireContentsChanged(this, 0, getSize());
106     }
107 
108 }