001    /**
002     * iFish - An iRiver iHP jukebox database creation tool
003     *
004     * Copyright (C) 2009 Richard "Shred" Körber
005     *   http://ifish.shredzone.org
006     *
007     * This program is free software: you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published by
009     * the Free Software Foundation, either version 3 of the License, or
010     * (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, see <http://www.gnu.org/licenses/>.
019     */
020    package net.shredzone.ifish.actions;
021    
022    import java.awt.BorderLayout;
023    import java.awt.Color;
024    import java.awt.FlowLayout;
025    import java.awt.event.ActionEvent;
026    import java.awt.event.ActionListener;
027    import java.awt.event.KeyEvent;
028    import java.util.prefs.Preferences;
029    
030    import javax.swing.JButton;
031    import javax.swing.JDialog;
032    import javax.swing.JPanel;
033    import javax.swing.KeyStroke;
034    
035    import net.shredzone.ifish.IFish;
036    import net.shredzone.ifish.IFishPane;
037    import net.shredzone.ifish.IFishPrefs;
038    import net.shredzone.ifish.gui.PrefsPane;
039    import net.shredzone.ifish.i18n.L;
040    import net.shredzone.ifish.pool.ImgPool;
041    import net.shredzone.jshred.swing.JHeadline;
042    import net.shredzone.jshred.swing.SwingUtils;
043    
044    /**
045     * Shows the Preferences window.
046     *
047     * @author  Richard Körber &lt;dev@shredzone.de&gt;
048     * @version $Id: PrefsAction.java 291 2009-04-28 21:29:27Z shred $
049     */
050    public class PrefsAction extends IFishAction {
051      private static final long serialVersionUID = 3256727277357250868L;
052      protected final static Preferences prefs =
053        java.util.prefs.Preferences.userNodeForPackage( PrefsAction.class );
054      
055      private JDialog dialog;
056      private JButton jbOK;
057      private JButton jbCancel;
058      private boolean confirmed = false;
059      
060      /**
061       * Create a new PrefsAction.
062       *
063       * @param   fish        Related IFishPane
064       */
065      public PrefsAction( IFishPane fish ) {
066        super(
067          fish,
068          L.tr("action.prefs"),
069          ImgPool.get("prefs.png"),
070          L.tr("action.prefs.tt"),
071          KeyStroke.getKeyStroke( KeyEvent.VK_F8, 0 )
072        );
073      }
074      
075      /**
076       * The action implementation itself. Do not call directly.
077       */
078      @Override
079      protected void action() {
080        if( dialog!=null ) {
081          //--- Already open? ---
082          // Then bring it to front
083          dialog.setVisible( true );
084          dialog.toFront();
085        }else {
086          PrefsPane pPrefs = new PrefsPane( fish );
087          
088          //--- Create Dialog ---
089          JPanel jpDialog = new JPanel( new BorderLayout() );
090          {
091            //--- Title ---
092            JHeadline jhHeader = new JHeadline(
093                L.tr("a.prefs.title"),
094                L.tr("a.prefs.desc"),
095                ImgPool.get("prefs_large.png"),
096                new Color( 0xE36700 )
097            );
098            jpDialog.add( jhHeader, BorderLayout.NORTH );
099            
100            //--- Preferences Pane ---
101            jpDialog.add( pPrefs, BorderLayout.CENTER );
102    
103            //--- OK and Cancel Button ---
104            JPanel jpOKCancel = new JPanel( new FlowLayout( FlowLayout.TRAILING ) );
105            {
106              ActionListener listener = new DialogActionListener();
107              
108              jbOK = new JButton( L.tr("a.prefs.ok"), ImgPool.get("accept.png") );
109              jbOK.addActionListener( listener );
110              jpOKCancel.add( jbOK );
111    
112              jbCancel = new JButton( L.tr("a.prefs.cancel"), ImgPool.get("cancel.png") );
113              jbCancel.addActionListener( listener );
114              jpOKCancel.add( jbCancel );
115            }
116            jpDialog.add( jpOKCancel, BorderLayout.SOUTH );
117          }
118          
119          //--- Open the Prefs modal dialog ---
120          dialog = new JDialog(
121            SwingUtils.getComponentFrame( fish ),
122            L.tr("a.prefs.title"),
123            true
124          );
125          dialog.getContentPane().add( jpDialog );
126          
127          SwingUtils.setConfirmKey( dialog, jbOK );
128          SwingUtils.setCancelKey( dialog, jbCancel );
129          
130          //--- Set the proper size ---
131          int width  = prefs.getInt( "width", -1 );
132          int height = prefs.getInt( "height", -1 );
133          if( width>=0 && height>=0 ) {
134            dialog.setSize( width, height );
135          }else {
136            dialog.pack();
137          }
138          
139          //--- Open the dialog ---
140          dialog.setLocationRelativeTo( dialog.getOwner() );
141          dialog.setVisible( true );
142          
143          //--- Remember the size ---
144          prefs.putInt( "width" , dialog.getWidth() );
145          prefs.putInt( "height", dialog.getHeight() );
146          
147          //--- Dispose the dialog ---
148          dialog.dispose();
149          dialog = null;
150          
151          //--- Save the prefs ---
152          if( confirmed ) {
153            final IFishPrefs fishprefs = fish.getPrefs();
154            final String oldPath = fishprefs.getDirectory();
155            pPrefs.guiToPrefs();
156            final String newPath = fishprefs.getDirectory();
157            if(! oldPath.equals(newPath) ) {
158              // Base directory has been changed. Reload the database!
159              IFishAction readaction = fish.getAction( IFishPane.ACTION_READDATABASE );
160              readaction.perform();
161            }
162          }
163        }
164      }
165    
166    /*----------------------------------------------------------------------------*/
167      
168      /**
169       * ActionListener waiting for OK or Cancel to be pressed.
170       */
171      private class DialogActionListener implements ActionListener {
172        @Override
173        public void actionPerformed( ActionEvent e ) {
174          Object src = e.getSource();
175          confirmed = ( src == jbOK );
176          if( dialog!=null ) dialog.setVisible( false );
177        }
178      }
179    
180    }
181