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; 021 022 import net.shredzone.ifish.db.*; 023 import net.shredzone.ifish.i18n.L; 024 import javax.swing.*; 025 import java.util.prefs.Preferences; 026 import java.io.*; 027 028 /** 029 * This SmartRenameCallback extension will honor the user's request 030 * whether to abort, ask, auto rename or ignore files with too lang 031 * filenames. 032 * 033 * @author Richard Körber <dev@shredzone.de> 034 * @version $Id: IFishRenameCallback.java 291 2009-04-28 21:29:27Z shred $ 035 */ 036 public class IFishRenameCallback extends SmartRenameCallback { 037 private final IFishPane fish; 038 private final IFishPrefs prefs; 039 040 public IFishRenameCallback( IFishPane fish ) { 041 this.fish = fish; 042 this.prefs = fish.getPrefs(); 043 } 044 045 /** 046 * Handle the directory names as desired by the user. 047 * 048 * @param base Base directory (jukebox mount point) 049 * @param directory Directory with the bad name 050 * @return Directory's new name, null if this directory is to be ignored 051 * @throws DatabaseException Directory could not be renamed 052 * @throws IOException An IO error occured during nenaming 053 */ 054 @Override 055 public File renameDirectory( File base, File directory ) 056 throws DatabaseException, IOException { 057 // 0=ignore, 1=abort, 2=rename, 3=ask 058 int rebuildDirType = prefs.getDirRename(); 059 if( rebuildDirType==0 ) // 0: Ignore 060 return null; 061 062 if( rebuildDirType==2 ) // 2: Rename 063 return super.renameDirectory( base, directory ); 064 065 // Everything else: abort 066 throw new DatabaseException( L.tr("rename.error.directory")+": "+directory ); 067 } 068 069 /** 070 * Handle the filenames as desired by the user. 071 * 072 * @param base Base directory (jukebox mount point) 073 * @param file File with the bad name 074 * @return New file name, null if this file is to be ignored 075 * @throws DatabaseException Directory could not be renamed 076 * @throws IOException An IO error occured during nenaming 077 */ 078 @Override 079 public File renameFile( File base, File file ) 080 throws DatabaseException, IOException { 081 int rebuildFileType = prefs.getFileRename(); 082 083 if( rebuildFileType==0 ) // 0: Ignore 084 return null; 085 086 if( rebuildFileType==2 ) // 2: Rename 087 return super.renameFile( base, file ); 088 089 if( rebuildFileType==3 ) { // 3: Ask 090 File attempt = file; 091 while( !FileEntry.isFileNameFitting( base, attempt ) ) { 092 Object[] args = new Object[] { 093 attempt.getName(), 094 new Integer( attempt.getName().length() ), 095 new Integer( 52 ) 096 }; 097 String message = java.text.MessageFormat.format( L.tr("rename.askme"), args ); 098 String proposal = JOptionPane.showInputDialog( fish, message, attempt.getName() ); 099 if( proposal==null ) { 100 return null; 101 } 102 attempt = new File( file.getParentFile(), proposal ); 103 } 104 105 //--- Rename the file --- 106 if( !file.renameTo( attempt ) ) { 107 // Could not be renamed?! 108 return super.renameFile( base, file ); 109 } 110 111 return attempt; 112 } 113 114 // Everything else: Abort 115 throw new DatabaseException( L.tr("rename.error.file")+": "+file ); 116 } 117 118 } 119