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
23
24
25
26
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
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
58
59
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
71
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
86
87
88
89 public KeyListener getKeyTypedListener() {
90 return this.keyTypedListener;
91 }
92
93
94
95
96 public int getSize() {
97 return this.playlist.size();
98 }
99
100
101
102
103 public void propertyChange(PropertyChangeEvent evt) {
104 LOG.debug("Playlist changed");
105 fireContentsChanged(this, 0, getSize());
106 }
107
108 }