001    package net.sourceforge.mp3dings.preferences;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: ExternalApplicationModule.java,v 1.8 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.Container;
023    import java.awt.event.MouseAdapter;
024    import java.awt.event.MouseEvent;
025    import java.util.logging.Level;
026    import java.util.logging.Logger;
027    import java.util.prefs.BackingStoreException;
028    import java.util.prefs.Preferences;
029    import javax.swing.JButton;
030    import javax.swing.JOptionPane;
031    import javax.swing.JTextField;
032    import de.mospace.swing.MiniShell;
033    import de.mospace.swing.ProcessIOPane;
034    import net.sourceforge.mp3dings.Globals;
035    import net.sourceforge.mp3dings.utils.ExternalApplication;
036    
037    /**
038     * A Preference Module for External Applications
039     * @author Moritz Ringler
040     * @version $Revision: 1.8 $ ($Date: 2006/09/25 17:34:42 $)
041     */
042    public  class ExternalApplicationModule extends AbstractFilterModule{
043        private static final Logger logger = Logger.getLogger(ExternalApplicationModule.class.getPackage().getName());
044        private MiniShell console;
045    
046        public ExternalApplicationModule(MiniShell console){
047            super(Globals.getString("HELPER_APPLICATIONS"));
048            ((JButton) ((Container) getNonScrollingPart()).getComponent(0)).setText(
049                Globals.getString("NEW_APPLICATION"));
050            this.console = console;
051            builtInApps();
052            userApps();
053        }
054    
055        public PrefPanel getNewFilterPanel(){
056            int key = ExternalApplication.getUnusedId();
057            String name;
058            name =
059                JOptionPane.showInputDialog(this,
060                Globals.getString("QUERY_NEW_APP_NAME"));
061            return (name == null || name.equals(""))
062                ? null
063                : new ExternalApplicationPanel(
064                    ExternalApplication.getPrefNode(), name, key);
065        }
066    
067        public void remove(PrefPanel field){
068            if( ((ExternalApplicationPanel) field).isDeleteAllowed() ){
069                super.remove(field);
070            }
071        }
072    
073        private void builtInApps(){
074            Preferences pref = ExternalApplication.getPrefNode().node("builtin");
075            String desc = Globals.getString("PREFS_RUN_MP3_TEXT"
076                    +   ExternalApplication.PLAY);
077            ExternalApplicationPanel panel;
078    
079            /* play panel */
080            panel = new ExternalApplicationPanel(pref,
081                    Globals.getString("CONTEXT_MENU_PLAY"),
082                    ExternalApplication.PLAY, false);
083            panel.setDescription(desc);
084            if(panel.getExeInput().getPath().trim().equals("")){
085                panel.getExeInput().getTextfield().setText("C:\\Program Files\\Winamp\\winamp.exe %f");
086            }
087            addPanel(panel);
088    
089            /* enqueue panel */
090            desc = Globals.getString("PREFS_RUN_MP3_TEXT"
091                    +   ExternalApplication.ENQUEUE);
092            panel = new ExternalApplicationPanel(pref,
093                    Globals.getString("CONTEXT_MENU_ENQUEUE"),
094                    ExternalApplication.ENQUEUE, false);
095            panel.setDescription(desc);
096            if(panel.getExeInput().getPath().trim().equals("")){
097                panel.getExeInput().getTextfield().setText("C:\\Program Files\\Winamp\\winamp.exe /add %f");
098            }
099            addPanel(panel);
100    
101             /* system shell panel */
102            desc = Globals.getString("SYSTEM_SHELL_DESCRIPTION");
103            panel = new ExternalApplicationPanel(pref,
104                Globals.getString("SYSTEM_SHELL"),
105                ExternalApplication.SYSTEM_SHELL, false, false){
106                    public void save(){
107                        console.getProcessIOPane().setSystemShell(
108                                getExternalApplication().getExePath());
109                    }
110            };
111            JTextField pathinput = panel.getExeInput().getTextfield();
112            // manipulate model once model-view-relationship for ExternalApplication is established
113            pathinput.setText(console.getProcessIOPane().getSystemShell());
114            panel.setDescription(desc);
115            pathinput.addMouseListener(new MouseAdapter(){
116                public void mouseClicked(MouseEvent e){
117                    if(e.getClickCount() >= 2){
118                        JTextField tf = (JTextField) e.getSource();
119                        tf.setText(ProcessIOPane.guessSystemShell());
120                    }
121                }
122            });
123            addPanel(panel);
124        }
125    
126        private void userApps(){
127            Preferences pref = ExternalApplication.getPrefNode();
128            try {
129                String[] keys = pref.keys();
130                for(int i=0; i<keys.length; i++){
131                    try{
132                        addPanel(new ExternalApplicationPanel(
133                        pref, Integer.parseInt(keys[i])));
134                    } catch (NumberFormatException ex){
135                        logger.severe(ex.toString());
136                        Globals.logStackTrace(logger, Level.SEVERE, ex);
137                    }
138                }
139            } catch(BackingStoreException e) {
140                throw new RuntimeException(e);
141            }
142        }
143    }