001    package net.sourceforge.mp3dings.preferences;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: AbstractFilterModule.java,v 1.11 2006/09/25 17:34:42 ringler Exp $
006     *
007     * This program is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU General Public License
009     * as published by the Free Software Foundation; either version 2
010     * of the License, or (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, write to the Free Software
019     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020     */
021    
022    import java.awt.CardLayout;
023    import java.awt.Component;
024    import java.awt.Container;
025    import java.awt.GridBagConstraints;
026    import java.awt.GridBagLayout;
027    import java.awt.GridLayout;
028    import java.awt.Insets;
029    import java.awt.event.ActionEvent;
030    import java.awt.event.ActionListener;
031    import javax.swing.BorderFactory;
032    import javax.swing.DefaultListCellRenderer;
033    import javax.swing.JButton;
034    import javax.swing.JList;
035    import javax.swing.JPanel;
036    import javax.swing.JScrollPane;
037    import javax.swing.ListSelectionModel;
038    import javax.swing.SwingUtilities;
039    import javax.swing.event.ListSelectionEvent;
040    import javax.swing.event.ListSelectionListener;
041    import net.sourceforge.mp3dings.Globals;
042    
043    /**
044     * A Preference Module for filters.
045     * @author Moritz Ringler
046     * @version $Revision: 1.11 $ ($Date: 2006/09/25 17:34:42 $)
047     */
048    public abstract class AbstractFilterModule extends AbstractPrefModule{
049        Container nonScrollingPart;
050    
051        private JList filterList = new JList(myPanels);
052        private Container filterView;
053    
054        public AbstractFilterModule(String name){
055            super(name);
056            setLayout(new GridBagLayout());
057            GridBagConstraints gbc = new GridBagConstraints();
058    
059            gbc.fill = GridBagConstraints.BOTH;
060            gbc.gridx = 0;
061            gbc.gridy = 0;
062            gbc.gridwidth = 4;
063            gbc.gridheight = 1;
064            gbc.weightx = 1.0;
065            gbc.weighty = .0;
066            add(getTitle(), gbc);
067    
068            gbc.gridx = 0;
069            gbc.gridy = 1;
070            gbc.gridwidth = 1;
071            gbc.gridheight = 1;
072            gbc.ipadx = 2;
073            gbc.weighty = 0.;
074            gbc.weightx = 0.7;
075            gbc.fill = GridBagConstraints.BOTH;
076            gbc.anchor = GridBagConstraints.CENTER;
077            gbc.insets = new Insets(5,5,5,5);
078            add(new JScrollPane(filterList), gbc);
079    
080            gbc.gridx = 1;
081            gbc.gridheight = 1;
082            gbc.weighty = 0.;
083            gbc.weightx = 1.0;
084            gbc.fill = GridBagConstraints.NONE;
085            add(getNonScrollingPart(), gbc);
086    
087            gbc.gridx = 0;
088            gbc.gridy = 2;
089            gbc.gridwidth = 2;
090            gbc.gridheight = 1;
091            gbc.weightx = 1.0;
092            gbc.weighty = 0.7;
093            gbc.fill = GridBagConstraints.BOTH;
094    
095            //JScrollPane jsp = new JScrollPane();
096            filterView = new JPanel(new CardLayout());
097            ((JPanel) filterView).setBorder(BorderFactory.
098            createMatteBorder(5,5,5,5,((JPanel) filterView).getBackground()));
099            add(filterView, gbc);
100            //filterView = jsp.getViewport();
101    
102    
103    
104            filterList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
105            filterList.addListSelectionListener(new ListSelectionListener(){
106                public void valueChanged(ListSelectionEvent e){
107                    if(filterList.getSelectedValue() != null){
108                        CardLayout layout = (CardLayout) filterView.getLayout();
109                        layout.show(filterView, ((PrefPanel) filterList.getSelectedValue()).getName());
110                    }
111                }
112            });
113            filterList.setCellRenderer(new DefaultListCellRenderer(){
114                public Component getListCellRendererComponent(JList list,
115                        Object value, int index, boolean isSelected,
116                        boolean cellHasFocus){
117                    return super.getListCellRendererComponent(list,
118                            ((PrefPanel) value).getName(), index, isSelected,
119                            cellHasFocus);
120                }
121    
122            });
123            filterList.setFixedCellWidth(256);
124        }
125    
126        public void addPanel(PrefPanel p){
127            super.addPanel(p);
128            filterView.add(p, p.getName());
129        }
130    
131        public void remove(PrefPanel p){
132            filterView.remove(p);
133            int selection = filterList.getMaxSelectionIndex();
134            filterList.clearSelection();
135            myPanels.remove(p);
136            if(selection >= 0){
137                int listsize = filterList.getModel().getSize();
138                if (listsize > 0){
139                    filterList.setSelectedIndex((listsize > selection)? selection : listsize -1);
140                }
141            }
142            filterList.repaint();
143        }
144    
145        /** This method must return a Container to which other
146        * Components can be added.
147        **/
148        public Component getNonScrollingPart(){
149            if (nonScrollingPart == null){
150    
151                /* new Filter button */
152                JButton button =
153                        new JButton(Globals.getString("NEW"));
154                button.addActionListener(new ActionListener(){
155                    public void actionPerformed(ActionEvent e){
156                        PrefPanel panel = getNewFilterPanel();
157                        if(panel != null){
158                            addPanel(panel);
159                            SwingUtilities.updateComponentTreeUI(getRootPane());
160                        }
161                    }
162                });
163                nonScrollingPart = new JPanel(new GridLayout(0,1,0,2));
164                nonScrollingPart.add(button);
165    
166                /* remove Filter button */
167                button = new JButton(Globals.getString("DELETE"));
168                button.addActionListener(new ActionListener(){
169                    public void actionPerformed(ActionEvent e){
170                        if(filterList.getSelectedValue() != null){
171                            PrefPanel pp = (PrefPanel) filterList.getSelectedValue();
172                            if(pp.isRemoveAllowed()){
173                                remove(pp);
174                            }
175                        }
176                    }
177                });
178                nonScrollingPart.add(button);
179    
180            }
181            return nonScrollingPart;
182        }
183    
184        public abstract PrefPanel getNewFilterPanel();
185    }