| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.tools; |
| 17 | |
| 18 | import java.io.File; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Map; |
| 21 | |
| 22 | /** |
| 23 | * Task to obtain image sizes from a list of image files. |
| 24 | * |
| 25 | * @author Thomas Aglassinger |
| 26 | */ |
| 27 | public class CreateImagInfoMapTask extends AbstractTask |
| 28 | { |
| 29 | private long[] imageFileLengths; |
| 30 | private File[] imageFiles; |
| 31 | private Map imageInfoMap; |
| 32 | |
| 33 | public CreateImagInfoMapTask(File[] newImageFiles) { |
| 34 | this(); |
| 35 | assert newImageFiles != null; |
| 36 | assert newImageFiles.length > 0; |
| 37 | long totalImageFileLength = 0; |
| 38 | int imageFileCount = newImageFiles.length; |
| 39 | |
| 40 | imageFiles = newImageFiles; |
| 41 | imageFileLengths = new long[imageFileCount]; |
| 42 | for (int i = 0; i < imageFileCount; i += 1) { |
| 43 | imageFileLengths[i] = Math.max(0, imageFiles[i].length()); |
| 44 | totalImageFileLength += imageFileLengths[i]; |
| 45 | } |
| 46 | setMaxProgress(totalImageFileLength + 1); |
| 47 | } |
| 48 | |
| 49 | private CreateImagInfoMapTask() { |
| 50 | super(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get map where the key is an image file and the value is an <code>ImageInfo</code>. |
| 55 | * |
| 56 | * @see ImageInfo |
| 57 | */ |
| 58 | public Map getImageInfoMap() { |
| 59 | return imageInfoMap; |
| 60 | } |
| 61 | |
| 62 | public void start() |
| 63 | throws Exception { |
| 64 | setProgress(0); |
| 65 | imageInfoMap = new HashMap(); |
| 66 | for (int imageIndex = 0; imageIndex < imageFiles.length; imageIndex += 1) { |
| 67 | File imageFile = imageFiles[imageIndex]; |
| 68 | ImageInfo imageInfo = new ImageInfo(imageFile); |
| 69 | |
| 70 | imageInfoMap.put(imageFile, imageInfo); |
| 71 | setProgress(getProgress() + imageFileLengths[imageIndex]); |
| 72 | } |
| 73 | setProgress(getMaxProgress()); |
| 74 | } |
| 75 | } |