001    package net.sourceforge.mp3dings.preferences;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: ColorChooserPanel.java,v 1.8 2006/09/22 16:25:53 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.awt.GridLayout;
024    import java.awt.Insets;
025    import java.util.MissingResourceException;
026    import java.util.prefs.Preferences;
027    import javax.swing.BorderFactory;
028    import javax.swing.JLabel;
029    import javax.swing.JPanel;
030    import javax.swing.border.EmptyBorder;
031    import net.sourceforge.mp3dings.Globals;
032    import net.sourceforge.mp3dings.MainFrame;
033    import de.mospace.swing.LabeledColorButton;
034    
035    /**
036     * A Preference field for customizing the colors of some GUI elements.
037     * Package: mp3dings.preferences
038     * File: $Id: ColorChooserPanel.java,v 1.8 2006/09/22 16:25:53 ringler Exp $
039     * Description: Preference Module: Container for PrefPanels
040     * Copyright: Copyright (c) 2003
041     * Company:
042     * @author Moritz Ringler
043     * @version $Revision: 1.8 $ ($Date: 2006/09/22 16:25:53 $)
044     */
045    
046    public class ColorChooserPanel extends PrefPanel{
047        int n = 6;
048        int nBg = 4;
049        LabeledColorButton[] colorButton = new LabeledColorButton[n];
050    
051        /** Creates a new instance of this class.
052         * @param pref the preferences node to load current settings from
053         * @param name the display name of this field
054         **/
055        public ColorChooserPanel(MainFrame owner,Preferences pref,String name){
056            super(owner, pref,name);
057            JPanel   myPane      = new JPanel();
058            JLabel   text        = new JLabel(Globals.getString("PREFS_COLOR_TEXT"));
059            text.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
060            String[] colorLabels = Globals.getStringArray("PREFS_COLOR_LABELS");
061            if(colorLabels == null || colorLabels.length < n){
062                throw new MissingResourceException("Not enough color labels",
063                    Globals.LR.getClass().getName(),
064                    "PREFS_COLOR_LABELS");
065            }
066            for (int i = 0; i < nBg; i++){
067                colorButton[i] = new LabeledColorButton(colorLabels[i],
068                        Globals.getColor(i));
069            }
070            for (int i=nBg; i< n; i++){
071                colorButton[i] = new LabeledColorButton(colorLabels[i],
072                        Globals.getColor(i), true);
073            }
074            myPane.setLayout(new GridLayout((n+1)/2,4));
075            myPane.setBorder(new EmptyBorder(5,0,0,0));
076            EmptyBorder eb = new EmptyBorder(0,5,0,5);
077            Insets is0505 = new Insets(0,5,0,5);
078            for(int i=0; i<n; i++){
079                JLabel jl = new JLabel(colorLabels[i]);
080                jl.setHorizontalAlignment(JLabel.RIGHT);
081                jl.setBorder(eb);
082                colorButton[i].setMargin(is0505);
083                myPane.add(jl);
084                myPane.add(colorButton[i]);
085    
086            }
087            JLabel title = new JLabel(Globals.getString("PREFS_COLORS"));
088            title.setFont(Globals.SANS_BOLD_12);
089            add(title, BorderLayout.NORTH);
090            add(text, BorderLayout.SOUTH);
091            add(myPane, BorderLayout.CENTER);
092        }
093    
094        /** Gets the color from the ith colorButton as an RGB int value. **/
095        private int getColor(int i){
096            return colorButton[i].getRGB();
097        }
098    
099        /** Updates and saves the application colors.**/
100        public void save(){
101            int[] colors = new int[n];
102            for (int i=0; i<n; i++){
103                colors[i] = getColor(i);
104            }
105            Globals.setColors(colors);
106        }
107    }