001    /**
002     * iFish - An iRiver iHP jukebox database creation tool
003     *
004     * Copyright (C) 2009 Richard "Shred" Körber
005     *   http://ifish.shredzone.org
006     *
007     * This program is free software: you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published by
009     * the Free Software Foundation, either version 3 of the License, or
010     * (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
019     */
020    package net.shredzone.ifish.gui;
021    
022    import net.shredzone.ifish.*;
023    import net.shredzone.ifish.actions.*;
024    import net.shredzone.ifish.db.*;
025    import net.shredzone.jshred.swing.*;
026    import javax.swing.*;
027    import javax.swing.event.ListSelectionEvent;
028    import javax.swing.event.ListSelectionListener;
029    
030    import java.awt.*;
031    import java.awt.event.*;
032    import java.util.prefs.*;
033    
034    /**
035     * This panel allows to see the database in a table, and play selected
036     * files from it.
037     *
038     * @author    Richard Körber &lt;dev@shredzone.de&gt;
039     * @version   $Id: DatabasePane.java 291 2009-04-28 21:29:27Z shred $
040     */
041    public class DatabasePane extends JPanel implements PlaylistSource, ActionListener, ListSelectionListener {
042      private static final long serialVersionUID = 3688501091243669812L;
043      private final IFishPane fish;
044      private final IFishPrefs prefs;
045      private JSortedTable jtDatabase;
046      private NaviDbTableModel model;
047      private SortableTableModelProxy proxy;
048      
049      /**
050       * Create a new DatabasePane.
051       *
052       * @param   fish        IFishPane reference
053       */
054      public DatabasePane( IFishPane fish ) {
055        this.fish = fish;
056        this.prefs = fish.getPrefs();
057        
058        setLayout( new BorderLayout() );
059          
060        jtDatabase = new JSortedTable();
061        jtDatabase.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
062        jtDatabase.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
063        jtDatabase.getSelectionModel().addListSelectionListener( this );
064        add( new JScrollPane( jtDatabase ), BorderLayout.CENTER );
065        
066        //--- React on double clicks ---
067        jtDatabase.addMouseListener( new MouseAdapter() {
068          @Override
069          public void mouseClicked( MouseEvent e ) {
070            if( e.getClickCount()==2 ) {
071              IFishAction action = DatabasePane.this.fish.getAction( IFishPane.ACTION_PLAYER_PLAY );
072              if( action.isEnabled() ) {
073                action.perform();
074              }
075            }
076          }
077        });
078      }
079      
080      /**
081       * Set the new TableModel to be shown.
082       *
083       * @param   model     NaviDbTableModel to be shown
084       */
085      public void setModel( NaviDbTableModel model ) {
086        this.model = model;
087        proxy = new SortableTableModelProxy( model );
088        jtDatabase.setModel( proxy );
089        SwingUtils.spreadColumns( jtDatabase, 300 );
090      }
091      
092      /**
093       * Get all selected entries
094       *
095       * @return    Array of selected entries
096       */
097      @Override
098      public Entry[] getSelectedEntries() {
099        int[] rows = jtDatabase.getSelectedRows();
100        if( rows.length>=0 ) {
101          Entry[] result = new Entry[rows.length];
102          for( int ix=0; ix<rows.length; ix++ ) {
103            result[ix] = model.getEntryAt( proxy.mapRow( rows[ix] ) );
104          }
105          return result;
106        }else {
107          return new Entry[0];
108        }
109      }
110    
111      /**
112       * Internal ActionListener implementation, do not use.
113       *
114       * @param   e         ActionEvent
115       */
116      @Override
117      public void actionPerformed( ActionEvent e ) {
118        Object src = e.getSource();
119      }
120    
121      /**
122       * Internal ListSelectionListener implementation, do not use.
123       * 
124       * @param   e         ListSelectionEvent
125       */
126      @Override
127      public void valueChanged( ListSelectionEvent e ) {
128        fish.setPlaylistSource( this );
129      }
130      
131    }