001    package net.sourceforge.mp3dings;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: FileToolbar.java 43 2007-10-20 17:39:59Z ringler $
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.GridBagConstraints;
023    import java.awt.GridBagLayout;
024    import java.awt.Rectangle;
025    import java.awt.BorderLayout;
026    import java.awt.Color;
027    import java.awt.Point;
028    import java.awt.Dimension;
029    import java.awt.Insets;
030    import java.awt.Toolkit;
031    import java.awt.Frame;
032    import java.awt.event.ActionEvent;
033    import java.awt.event.ActionListener;
034    import java.util.prefs.Preferences;
035    import javax.swing.BorderFactory;
036    import javax.swing.JButton;
037    import javax.swing.JLabel;
038    import javax.swing.JOptionPane;
039    import javax.swing.JPopupMenu;
040    import javax.swing.JMenuItem;
041    import javax.swing.JPanel;
042    import javax.swing.SwingConstants;
043    import javax.swing.SwingUtilities;
044    import javax.swing.event.TableModelEvent;
045    import javax.swing.event.TableModelListener;
046    import de.mospace.swing.icon.PopupIcon;
047    import net.sourceforge.mp3dings.utils.RenamingPattern;
048    import net.sourceforge.mp3dings.utils.RenamingPattern.*;
049    
050    /** The Toolbar of the mp3dings "Names" tab.
051     * @author Moritz Ringler
052     * @version $Revision: 43 $ ($Date: 2007-10-20 19:39:59 +0200 (Sa, 20 Okt 2007) $)
053     */
054    public final class FileToolbar extends JPanel implements TableModelListener{
055        private FileTableModel model;
056        private NewPatternBuilder patternBuilder;
057    
058        /* Name/files Toolbar */
059        public FileToolbar(FileTableModel ftm, DataTableModel data){
060            JButton bApplyPattern = new JButton();
061            JButton bMoveFiles = new JButton();
062            JButton bSelectPattern = new JButton();
063    
064            final Preferences pref = ftm.getPrefs().node("pattern");
065            model = ftm;
066            JLabel patternLabel = new JLabel();
067            /* Name/files Toolbar */
068            setBorder(BorderFactory.createEtchedBorder());
069            setBackground(Globals.getColor(Globals.COLOR_TOOLB_BG));
070    
071            data.addTableModelListener(this);
072    
073            /* fileButtons */
074            bApplyPattern.setText(Globals.getString("FILE_BUTTONS_APPLY_PATTERN_TEXT"));
075            bApplyPattern.setToolTipText(
076                Globals.getString("FILE_BUTTONS_APPLY_PATTERN_TOOLTIP"));
077            bApplyPattern.setMnemonic(
078                ((Integer) Globals.getObject("FILE_BUTTONS_APPLY_PATTERN_MNEMONIC"))
079                .intValue());
080            bApplyPattern.setMargin(Globals.INSETS_0505);
081            bApplyPattern.setFont(Globals.SERIF_0_10);
082            bMoveFiles.setText(
083                Globals.getString("FILE_BUTTONS_MOVE_FILES_TEXT"));
084            bMoveFiles.setToolTipText(
085                Globals.getString("FILE_BUTTONS_MOVE_FILES_TOOLTIP"));
086            bMoveFiles.setMnemonic(
087                ((Integer) Globals.getObject("FILE_BUTTONS_MOVE_FILES_MNEMONIC"))
088                .intValue());
089            bMoveFiles.setMargin(Globals.INSETS_0505);
090            bMoveFiles.setFont(Globals.SERIF_0_10);
091    
092            /* filename pattern box */
093            patternLabel.setText(Globals.getString("FILENAME_PATTERN_LABEL"));
094            patternLabel.setHorizontalTextPosition(SwingConstants.CENTER);
095            patternLabel.setHorizontalAlignment(SwingConstants.CENTER);
096            patternLabel.setFont(Globals.SERIF_0_10);
097            RenamingPattern initialPattern = null;
098            if(!ftm.patternStack.empty()){
099                initialPattern = RenamingPattern.newInstance(
100                        (RenamingPattern) model.patternStack.peek());
101            }
102            patternBuilder = new NewPatternBuilder(initialPattern, data);
103            bSelectPattern = new JButton(new PopupIcon());
104            bSelectPattern.setOpaque(true);
105            bSelectPattern.setForeground(Color.black);
106            bSelectPattern.setBackground(Color.white);
107            bSelectPattern.setBorder(BorderFactory.createCompoundBorder(
108                BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(2,2,2,2)));
109            bSelectPattern.addActionListener(new ActionListener(){
110                public void actionPerformed(ActionEvent e){
111                    showPatternSelectionPopup();
112                }
113            });
114            patternBuilder.add(bSelectPattern, BorderLayout.EAST);
115    
116           /* load last used setting */
117    
118            bApplyPattern.addActionListener(new ActionListener(){
119                public void actionPerformed(ActionEvent e){
120                    applyPatternPressed((e.getModifiers() & ActionEvent.SHIFT_MASK)
121                            == ActionEvent.SHIFT_MASK);
122                }
123            });
124            bMoveFiles.addActionListener(new ActionListener(){
125                public void actionPerformed(ActionEvent e){
126                    boolean overwrite = (e.getModifiers() & ActionEvent.SHIFT_MASK)
127                                            == ActionEvent.SHIFT_MASK;
128                    boolean rmParents = (e.getModifiers() & ActionEvent.CTRL_MASK)
129                                            == ActionEvent.CTRL_MASK;
130                    model.renameFiles(mainFrame(), overwrite, rmParents);
131                }
132            });
133    
134            /* Files Toolbar */
135    
136            setLayout(new GridBagLayout());
137            GridBagConstraints gbc = new GridBagConstraints();
138    
139            gbc.gridx = 0;
140            gbc.gridy = 0;
141            gbc.gridwidth = GridBagConstraints.RELATIVE;
142            gbc.gridheight = 1;
143            gbc.ipadx = 2;
144            gbc.weighty = 0.33;
145            gbc.weightx = 1;
146            gbc.fill = GridBagConstraints.BOTH;
147            gbc.insets = new Insets(10,5,0,5);
148            patternLabel.setOpaque(true);
149            patternLabel.setBackground(Globals.getColor(Globals.COLOR_PANEL_BG));
150            patternLabel.setBorder(BorderFactory.createEtchedBorder());
151            add(patternLabel,gbc);
152    
153            gbc.gridy = GridBagConstraints.RELATIVE;
154            gbc.gridheight = GridBagConstraints.REMAINDER;
155            gbc.insets = new Insets(0,5,10,5);
156            gbc.weighty = 0.67;
157            add(patternBuilder, gbc);
158    
159            gbc.insets = new Insets(0,10,0,10);
160            gbc.gridx = 1;
161            gbc.gridy = 1;
162            gbc.gridwidth = GridBagConstraints.REMAINDER;
163            gbc.gridheight = 1;
164            gbc.weightx = 0;
165            gbc.weighty = 0.5;
166            gbc.fill = GridBagConstraints.HORIZONTAL;
167            gbc.anchor = GridBagConstraints.CENTER;
168            add(bApplyPattern, gbc);
169    
170            gbc.gridy = 3;
171            add(bMoveFiles, gbc);
172            Dimension size = getPreferredSize();
173            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
174            screenSize.width -= 50;
175            if (size.width > screenSize.width) {
176                size.width = screenSize.width;
177            }
178            setPreferredSize(size);
179        }
180    
181        private void applyPatternPressed(boolean append){
182            RenamingPattern pattern = patternBuilder.getPattern();
183            /* test for .mp3 extension */
184            if(!pattern.getExample().toLowerCase().endsWith(".mp3")){
185                int value = JOptionPane.showConfirmDialog(mainFrame(),Globals.
186                getString("FILE_TABLE_CONFIRM_EXTENSION"));
187                if (value == JOptionPane.YES_OPTION){
188                    pattern.insert(pattern.getSize(), new ConstantBlock(".mp3"));
189                }
190            }
191    
192            model.applyPattern(mainFrame(), pattern, append);
193        }
194    
195        private Frame mainFrame(){
196            return (Frame) SwingUtilities.getAncestorOfClass(
197                    Frame.class,FileToolbar.this);
198        }
199    
200        public void tableChanged(TableModelEvent e){
201            /* do nothing, NewPatternBuilder automatically uses up-to-date fields */
202            ///* check if changeevent is a structure change */
203            //if(e.getFirstRow() == e.getLastRow() && e.getFirstRow() == TableModelEvent.HEADER_ROW){
204                //patternBuilder.fieldsChanged(((DataTableModel) e.getSource()).getFields());
205            //}
206        }
207    
208        private ActionListener patternSelectionListener = new ActionListener(){
209            public void actionPerformed(ActionEvent e){
210                Object src = e.getSource();
211                if(src instanceof PatternMenuItem){
212                    PatternMenuItem pmi = (PatternMenuItem) src;
213                    RenamingPattern selection = pmi.getPattern();
214                    patternBuilder.setPattern(selection);
215                }
216            }
217        };
218    
219        private void showPatternSelectionPopup(){
220            if(model.patternStack == null || model.patternStack.size() == 0){
221                return;
222            }
223            model.savePattern(patternBuilder.getPattern());
224            if(model.patternStack.size() <= 1){
225                return;
226            }
227            JPopupMenu popup = new JPopupMenu();
228            for(int i = model.patternStack.size() - 2; i >= 0; i--){
229                PatternMenuItem pmi = new PatternMenuItem((RenamingPattern) model.patternStack.get(i));
230                pmi.addActionListener(patternSelectionListener);
231                popup.add(pmi);
232            }
233            popup.setOpaque(true);
234            popup.setForeground(Color.black);
235            popup.setBackground(Color.white);
236            popup.pack();
237            Dimension d = popup.getPreferredSize();
238            Rectangle patternViewBounds = patternBuilder.getPatternViewBounds();
239            Point popupLocation = patternViewBounds.getLocation();
240            popupLocation.y += patternViewBounds.height;
241            SwingUtilities.convertPointFromScreen(popupLocation, this);
242            popup.setPopupSize(patternViewBounds.width, d.height);
243            popup.show(this, popupLocation.x, popupLocation.y);
244        }
245    
246        private static class PatternMenuItem extends JMenuItem{
247            protected final RenamingPattern pattern;
248    
249            public PatternMenuItem(RenamingPattern pattern){
250                super("<html>" + pattern.toString() +
251                      "<p><font color=\"silver\"><i>" + pattern.getExample() +
252                      "</i></font></html>");
253                this.pattern = pattern;
254                setOpaque(true);
255                setForeground(Color.black);
256                setBackground(Color.white);
257            }
258    
259            public RenamingPattern getPattern(){
260                return pattern;
261            }
262        }
263    
264    }