Java Rename File renameWallpaper(String new_file_name, String old_file_name, Map dimmensions, String basepath, List resolution_directories)

Here you can find the source of renameWallpaper(String new_file_name, String old_file_name, Map dimmensions, String basepath, List resolution_directories)

Description

reames a wallpaper and all its copies

License

Apache License

Parameter

Parameter Description
wallpaper Wallpaper object
dimmensions map that contains name(folder)/resolution of resized wallpaper
basepath path of folder where to look for subdirectories
resolution_directories directories for wallpapers resized to user resolutions

Declaration

public static boolean renameWallpaper(String new_file_name,
        String old_file_name, Map<String, Integer> dimmensions,
        String basepath, List<File> resolution_directories) 

Method Source Code

//package com.java2s;
/*//from   ww w  . ja  va 2  s  . co m
 *  Copyright 2010 demchuck.dima@gmail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.io.File;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    /**
     * reames a wallpaper and all its copies
     * @param wallpaper Wallpaper object
     * @param dimmensions map that contains name(folder)/resolution of resized wallpaper
     * @param basepath path of folder where to look for subdirectories
     * @param resolution_directories directories for wallpapers resized to user resolutions
     * @return
     */
    public static boolean renameWallpaper(String new_file_name,
            String old_file_name, Map<String, Integer> dimmensions,
            String basepath, List<File> resolution_directories) {
        Iterator<Entry<String, Integer>> entries = dimmensions.entrySet()
                .iterator();
        boolean ok_1 = true;
        while (entries.hasNext()) {
            Entry<String, Integer> e = entries.next();
            File old_file = new File(basepath + File.separator + e.getKey()
                    + File.separator + old_file_name);
            File new_file = new File(basepath + File.separator + e.getKey()
                    + File.separator + new_file_name);
            if (ok_1 && new_file.exists()) {
                //because new file is allways uniq, delete new_file if exists
                ok_1 = ok_1 && new_file.delete();
            }
            if (ok_1 && old_file.exists()) {
                ok_1 = ok_1 && old_file.renameTo(new_file);
            }
        }
        boolean ok_2 = true;
        if (ok_1) {
            for (File f : resolution_directories) {
                File old_file = new File(f, old_file_name);
                File new_file = new File(f, new_file_name);
                if (ok_2 && new_file.exists()) {
                    //because new file is allways uniq, delete new_file if exists
                    ok_2 = ok_2 && new_file.delete();
                }
                if (ok_2 && old_file.exists()) {
                    ok_2 = ok_2 && old_file.renameTo(new_file);
                }
            }
        }
        //reverting changes in case of an error
        if (!ok_1 || !ok_2) {
            entries = dimmensions.entrySet().iterator();
            while (entries.hasNext()) {
                Entry<String, Integer> e = entries.next();
                File old_file = new File(basepath + File.separator
                        + e.getKey() + File.separator + old_file_name);
                File new_file = new File(basepath + File.separator
                        + e.getKey() + File.separator + new_file_name);
                if (new_file.exists()) {
                    new_file.renameTo(old_file);//TODO: mb make some checking here
                }
            }
        }
        if (!ok_2) {
            for (File f : resolution_directories) {
                File old_file = new File(f, old_file_name);
                File new_file = new File(f, new_file_name);
                if (ok_2 && new_file.exists()) {
                    new_file.renameTo(old_file);//TODO: mb make some checking here
                }
            }
        }
        return ok_1 && ok_2;
    }
}

Related

  1. renameToBackupName(File file)
  2. renameToNextSequencedFile(String srcfile, String destfolder, String prefix, String suffix)
  3. renameToTempDir(File file, String newName)
  4. renameToTemporaryName(final File flFileToRename, final String strPrefix)
  5. renameToUpperCase(File dir)
  6. renameWithConfirm(String tmpFilename, String filename)