001    package net.sourceforge.mp3dings.preferences;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: ResizeModePanel.java,v 1.5 2006/11/14 09:44:08 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.GridBagLayout;
024    import java.awt.GridBagConstraints;
025    import java.awt.event.ActionListener;
026    import java.awt.event.ActionEvent;
027    import java.util.prefs.Preferences;
028    import javax.swing.ButtonGroup;
029    import javax.swing.JLabel;
030    import javax.swing.JPanel;
031    import javax.swing.JSpinner;
032    import javax.swing.SpinnerNumberModel;
033    import javax.swing.JRadioButton;
034    import net.sourceforge.mp3dings.Globals;
035    
036    /**
037     * The preference field that configures the resize mode of the JTables.
038     * @author Moritz Ringler
039     * @version $Revision: 1.5 $ ($Date: 2006/11/14 09:44:08 $)
040     */
041    public class ResizeModePanel extends PrefPanel{
042        JRadioButton[] button;
043        JSpinner maxWidth = new JSpinner(new SpinnerNumberModel(0, 0, 999, 1));
044    
045        public ResizeModePanel(Preferences pref,String name){
046            super(pref,name);
047            final int initialMode = prefs.getInt("ResizeMode", 0);
048            final int initialMaxWidth = prefs.getInt("MaximumColumnWidth", 0);
049    
050            maxWidth.setValue(new Integer(
051                (initialMaxWidth == Integer.MAX_VALUE)
052                    ? 0
053                    : initialMaxWidth
054            ));
055    
056            JLabel title = new JLabel(Globals.getString("PREFS_RESIZE_MODE"));
057            title.setFont(Globals.SANS_BOLD_12);
058            add(title, BorderLayout.NORTH);
059    
060            JPanel contents = new JPanel(new GridBagLayout());
061            GridBagConstraints gbc = new GridBagConstraints();
062    
063            ButtonGroup buttons    = new ButtonGroup();
064            String[] buttonLabel = Globals.getStringArray("PREFS_RESIZE_LABELS");
065            button     = new JRadioButton[3];
066            for (int i=0; i<button.length; i++){
067                button[i] = new JRadioButton(buttonLabel[i]);
068            }
069            button[initialMode].setSelected(true);
070            for(int i=0;i<button.length;i++){
071                buttons.add(button[i]);
072            }
073    
074            gbc.gridx = 0;
075            gbc.gridy = 0;
076            gbc.gridwidth = 1;
077            gbc.gridheight = 1;
078            gbc.fill = GridBagConstraints.BOTH;
079    
080            contents.add(button[0], gbc);
081    
082            gbc.gridx++;
083            contents.add(new JLabel(Globals.getString("PREFS_RESIZE_FIT_CELLS_LIMIT_TO") + " "), gbc);
084    
085            gbc.gridx++;
086            contents.add(maxWidth, gbc);
087    
088            gbc.gridx++;
089            contents.add(new JLabel(" px"), gbc);
090    
091            gbc.gridx = 0;
092            gbc.gridy++;
093    
094    
095            for(int i=1;i<button.length;i++){
096                contents.add(button[i], gbc);
097                gbc.gridy++;
098            }
099            add(contents, BorderLayout.NORTH);
100    
101            maxWidth.setEnabled(initialMode == 0);
102            ActionListener al = new ActionListener(){
103                public void actionPerformed(ActionEvent e){
104                    updateMaxWidth();
105                }
106            };
107            for(int i=0; i<button.length; i++){
108                button[i].addActionListener(al);
109            }
110        }
111    
112        private void updateMaxWidth(){
113            maxWidth.setEnabled(button[0].isSelected());
114        }
115    
116        private int getMode(){
117            for(int i=0; i<button.length; i++){
118                if(button[i].isSelected()){
119                    return i;
120                }
121            }
122            return 0;
123        }
124    
125        private int getMaxWidth(){
126            Number n = (Number) maxWidth.getValue();
127            int i = n.intValue();
128            return (i < 1) ? Integer.MAX_VALUE : i;
129        }
130    
131        public void save(){
132            prefs.putInt("MaximumColumnWidth", getMaxWidth());
133            prefs.putInt("ResizeMode",getMode());
134        }
135    }