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.Disclist;
17  import com.melloware.jukes.gui.tool.Actions;
18  import com.melloware.jukes.gui.tool.Resources;
19  import com.melloware.jukes.gui.view.DisclistPanel;
20  
21  /**
22   * Mode used for displaying disclists 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 AZ Development 2009
27   */
28  public final class DisclistListModel extends AbstractListModel implements PropertyChangeListener {
29  
30     private static final Log LOG = LogFactory.getLog(DisclistListModel.class);
31     private final Disclist disclist;
32     private final DisclistListModel model;
33     private final DisclistPanel panel;
34  
35     /**
36      * KeyAdapter to deselect all on CTrl+D and delete items with DEL key.
37      */
38     private transient final KeyListener keyTypedListener = new KeyAdapter() {
39        @Override
40        public void keyPressed(final KeyEvent event) {
41           if ((event.getKeyCode() == KeyEvent.VK_DELETE) && (model.getSize() > 0)) {
42              LOG.debug("Delete pressed");
43              panel.putClientProperty(Resources.EDITOR_COMPONENT, panel);
44              final ActionEvent action = new ActionEvent(panel, 1, "");
45              ActionManager.get(Actions.DISCLIST_REMOVE_DISC_ID).actionPerformed(action);
46           } else if ((event.isControlDown()) && (event.getKeyCode() == KeyEvent.VK_D)) {
47              LOG.debug("Deselect All");
48              panel.clearSelection();
49           } else if (event.getKeyCode() == KeyEvent.VK_DOWN) {
50              disclist.getNext();
51           } else if (event.getKeyCode() == KeyEvent.VK_UP) {
52              disclist.getPrevious();
53           } else {
54              super.keyPressed(event);
55           }
56        }
57     };
58  
59     /**
60      * Constructs a <code>DisclistPanel</code> for the given module.
61      * @param aDisclist provides the disclist class needed for display
62      */
63     public DisclistListModel(Disclist aDisclist, DisclistPanel aPanel) {
64        super();
65        LOG.debug("DisclistListModel created.");
66        this.disclist = aDisclist;
67        this.disclist.addPropertyChangeListener(this);
68        this.panel = aPanel;
69        this.model = this;
70     }
71  
72     /*
73      * (non-Javadoc)
74      * @see javax.swing.ListModel#getElementAt(int)
75      */
76     public Object getElementAt(int aIndex) {
77        Object returnValue;
78  
79        if (aIndex < this.disclist.getDiscList().size()) {
80           returnValue = this.disclist.getDiscList().get(aIndex);
81        } else {
82           returnValue = null;
83        }
84        return returnValue;
85     }
86  
87     /**
88      * Gets the keyTypedListener.
89      * <p>
90      * @return Returns the keyTypedListener.
91      */
92     public KeyListener getKeyTypedListener() {
93        return this.keyTypedListener;
94     }
95  
96     /*
97      * (non-Javadoc)
98      * @see javax.swing.ListModel#getSize()
99      */
100    public int getSize() {
101       return this.disclist.size();
102    }
103 
104    /**
105     * Whenever the master disclist changes update this view.
106     */
107    public void propertyChange(PropertyChangeEvent evt) {
108       LOG.debug("Disclist changed");
109       fireContentsChanged(this, 0, getSize());
110    }
111 
112 }