001    package net.sourceforge.mp3dings.preferences;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: PrefPanel.java,v 1.15 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.BorderLayout;
023    import java.util.prefs.Preferences;
024    import javax.swing.BorderFactory;
025    import javax.swing.JPanel;
026    import javax.swing.JTextField;
027    import javax.swing.SwingUtilities;
028    import javax.swing.border.Border;
029    import javax.swing.border.EtchedBorder;
030    import net.sourceforge.mp3dings.Globals;
031    import net.sourceforge.mp3dings.MainFrame;
032    import net.sourceforge.mp3dings.Mp3dingsTableModel;
033    import net.sourceforge.mp3dings.PreferencesDialogue;
034    
035    /**
036     * Abstract Preference Dialogue Field.
037     * @author Moritz Ringler
038     * @version $Revision: 1.15 $ ($Date: 2006/09/25 17:34:42 $)
039     */
040    public abstract class PrefPanel extends JPanel{
041        protected Preferences prefs;
042        private MainFrame owner;
043        private String title;
044        private AbstractPrefModule parentModule;
045        private boolean removeAllowed = true;
046    
047        public String getName(){
048            return title;
049        }
050    
051        public void setName(String name){
052            title = name;
053        };
054    
055        protected PrefModule getParentModule(){
056            return parentModule;
057        }
058    
059        protected void setParentModule(AbstractPrefModule parent){
060            if (parentModule != null){
061                parentModule.remove(this);
062            }
063            parentModule = parent;
064        }
065    
066        protected PrefPanel(MainFrame owner, Preferences pref, String title){
067            this(pref, title);
068            this.owner = owner;
069        }
070    
071        protected PrefPanel(Preferences pref, String title){
072            this.title = title;
073            prefs = pref;
074            setLayout(new BorderLayout());
075            Border b = new EtchedBorder();
076            setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
077        }
078    
079        protected Preferences getPrefs(){
080            return prefs;
081        }
082    
083        protected int getNum(JTextField jtf){
084            try{
085                return Integer.parseInt(jtf.getText());
086            }
087            catch (NumberFormatException ex){
088                jtf.setText("0");
089                return 0;
090            }
091        }
092    
093        public boolean isRemoveAllowed(){
094            return removeAllowed;
095        }
096    
097        protected void setRemoveAllowed(boolean b){
098            removeAllowed = b;
099        }
100    
101        protected void selfDestruct(){
102            if(removeAllowed){
103            if(parentModule != null){
104                parentModule.remove(this);
105            }
106            SwingUtilities.updateComponentTreeUI(parentModule.getRootPane());
107            //The preference node doesn't have to be destroyed as
108            //PreferencesDialogue deletes all filters subtrees
109            //before Preferences are saved
110            }
111        }
112    
113    //
114        // /**Determines the allowed fields for all tabs that have the given type.**/
115        // protected String[][] getFieldArrays(int type,
116                // boolean includeOnlyEditableFields){
117                    // return getFieldArrays(new int[]{type},
118                                          // includeOnlyEditableFields);
119        // }
120    //
121        // /** Same as getFieldArrays(type, false). **/
122        // protected String[][] getFieldArrays(int type){
123            // return getFieldArrays(type, false);
124        // }
125    //
126    //
127        // /** Same as getFieldArrays(types, false). **/
128        // protected String[][] getFieldArrays(int[] types){
129            // return getFieldArrays(types, false);
130        // }
131    //
132        // /**Determines the allowed fields for all tabs that have
133         // * one of the given types.**/
134        // protected String[][] getFieldArrays(int[] types,
135                // boolean includeOnlyEditableFields){
136            // int numTabs                 = getTabNames().length;
137            // String[][] fieldArrays      = new String[numTabs][];
138            // Field[] allowedFields;
139            // for(int tab=0; tab<numTabs; tab++){
140                // allowedFields = getTabModel(tab).getAllowedFields();
141                // int j=0;
142                // //filter allowed fields
143                // LOOP_B: for(int ai=0; ai<allowedFields.length; ai++){
144                    // Field currentField = allowedFields[ai];
145                    // // check editable
146                    // if (includeOnlyEditableFields &&
147                        // !getTabModel(tab).getEditable(currentField)){
148                        // continue;
149                    // }
150                    // // check field type
151                    // int currentType = currentField.type();
152                    // for (int ti = 0; ti < types.length; ti++){
153                        // if (currentType == types[ti]){
154                            // allowedFields[j] = currentField;
155                            // j++;
156                            // continue LOOP_B;
157                        // }
158                    // }
159                // }
160                // fieldArrays[tab]        = new String[j+1];
161                // for (int i=0; i<j; i++){
162                    // fieldArrays[tab][i] = FIELDS.getNameForID(allowedFields[i]);
163                // }
164                // fieldArrays[tab][j]     = FIELDS.getNameForID(FIELDS.ALL_FIELDS);
165            // }
166            // return fieldArrays;
167        // }
168    
169        protected Mp3dingsTableModel getTabModel(int tab){
170            if(mainFrame() != null){
171                return mainFrame().getModel(tab);
172            } else {
173                return null;
174            }
175        }
176    
177        protected void setPrefs(Preferences p){
178            prefs = p;
179        }
180    
181        protected static String[] getTabNames(){
182            return Globals.getStringArray("TABS");
183        }
184    
185        protected synchronized MainFrame mainFrame(){
186            if (owner == null){
187                owner = (MainFrame) ((PreferencesDialogue) SwingUtilities.getAncestorOfClass(PreferencesDialogue.class, this)).getOwner();
188            }
189            return owner;
190        }
191    
192        abstract protected void save();
193    }