001    package net.sourceforge.mp3dings.preferences;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: AbstractPrefModule.java,v 1.4 2006/11/23 17:38: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.Color;
023    import java.awt.Component;
024    import java.awt.Dimension;
025    import java.awt.Rectangle;
026    import java.util.Iterator;
027    import java.util.Vector;
028    import javax.swing.BorderFactory;
029    import javax.swing.JLabel;
030    import javax.swing.JPanel;
031    import javax.swing.Scrollable;
032    import javax.swing.SwingUtilities;
033    import javax.swing.tree.DefaultMutableTreeNode;
034    import javax.swing.tree.MutableTreeNode;
035    import net.sourceforge.mp3dings.Globals;
036    import net.sourceforge.mp3dings.MainFrame;
037    import net.sourceforge.mp3dings.PreferencesDialogue;
038    
039    /**
040     * A Preference Module, a container for similar PrefPanels.
041     * @author Moritz Ringler
042     * @version $Revision: 1.4 $ ($Date: 2006/11/23 17:38:42 $)
043     */
044    public class AbstractPrefModule extends JPanel implements Scrollable, PrefModule{
045        private String myName;
046        protected Vector myPanels = new Vector(); //must be a Vector
047        private MutableTreeNode myTreeNode = new DefaultMutableTreeNode(this);
048        private JLabel title;
049        private MainFrame owner;
050    
051        public AbstractPrefModule(String name){
052            super();
053            setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
054            setBackground(new Color(-1184257));
055            myName = name;
056        }
057    
058        public String toString(){
059            return myName;
060        }
061    
062        public boolean isSavable(){
063            return true;
064        }
065    
066        public void showInvalidPanel(){
067        }
068    
069        public void remove(PrefPanel p){
070            myPanels.remove(p);
071        }
072    
073        public void addPanel(PrefPanel p){
074            p.setParentModule(this);
075            myPanels.add(p);
076        }
077    
078        public void save(){
079            Iterator panelIt = myPanels.iterator();
080            while(panelIt.hasNext()){
081                ((PrefPanel) panelIt.next()).save();
082            }
083        }
084    
085        public Component getNonScrollingPart(){
086            return null;
087        }
088    
089        public synchronized Component getTitle(){
090            if(title == null){
091                title = new JLabel(myName);
092                title.setOpaque(true);
093                title.setBackground(Color.blue);
094                title.setFont(Globals.SANS_BOLD_12);
095                title.setForeground(Color.white);
096                title.setBorder(BorderFactory.createMatteBorder(5,5,5,5,Color.blue));
097            }
098            return title;
099        }
100    
101        public MutableTreeNode getTreeNode(){
102            return myTreeNode;
103        }
104    
105        public PrefPanel[] getPanels(){
106            return (PrefPanel[]) myPanels.toArray(new PrefPanel[]{});
107        }
108    
109        public Dimension getPreferredScrollableViewportSize(){
110            return getPreferredSize();
111        }
112    
113        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction){
114            return 10;
115        }
116    
117        public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction){
118            return 50;
119        }
120    
121        public boolean getScrollableTracksViewportWidth(){
122            return true;
123        }
124    
125        public boolean getScrollableTracksViewportHeight(){
126            return false;
127        }
128    
129        protected synchronized MainFrame mainFrame(){
130            if (owner == null){
131                owner = (MainFrame) ((PreferencesDialogue) SwingUtilities.getAncestorOfClass(PreferencesDialogue.class, this)).getOwner();
132            }
133            return owner;
134        }
135    
136        public Component getComponent(){
137            return this;
138        }
139    }