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.ui; |
17 | |
18 | import java.awt.event.ActionEvent; |
19 | import java.awt.event.ActionListener; |
20 | import java.io.File; |
21 | import java.io.FileFilter; |
22 | import java.util.Arrays; |
23 | import java.util.Iterator; |
24 | import java.util.List; |
25 | import java.util.Map; |
26 | import java.util.NoSuchElementException; |
27 | |
28 | import net.sf.jomic.comic.Conversion; |
29 | import net.sf.jomic.comic.CreateComicTask; |
30 | import net.sf.jomic.common.JomicTools; |
31 | import net.sf.jomic.tools.FileTools; |
32 | import net.sf.jomic.tools.LocaleTools; |
33 | import net.sf.jomic.tools.NaturalCaseInsensitiveOrderComparator; |
34 | import net.sf.jomic.tools.NestedTask; |
35 | import net.sf.jomic.tools.ProgressChangeListener; |
36 | import net.sf.jomic.tools.ProgressFrame; |
37 | import net.sf.jomic.tools.StringTools; |
38 | import net.sf.jomic.tools.SwingWorker; |
39 | import net.sf.jomic.tools.Task; |
40 | |
41 | import org.apache.commons.logging.Log; |
42 | import org.apache.commons.logging.LogFactory; |
43 | |
44 | /** |
45 | * SwingWorker to create a comic from a set of image files. |
46 | * |
47 | * @author Thomas Aglassinger |
48 | */ |
49 | public class CreateComicWorker extends SwingWorker implements ActionListener, ProgressChangeListener |
50 | { |
51 | private Conversion conversion; |
52 | private FileFilter createComicFileFilter; |
53 | private Task createComicTask; |
54 | private FileTools fileTools; |
55 | private boolean isCreateComicForEachFolder; |
56 | private boolean isOpenAfterCreation; |
57 | private JomicApplication jomicApplication; |
58 | private JomicTools jomicTools; |
59 | private LocaleTools localeTools; |
60 | private Log logger; |
61 | private String oldProgressMessage; |
62 | private ProgressFrame progressFrame; |
63 | private File sourceDir; |
64 | private StringTools stringTools; |
65 | private File targetDir; |
66 | |
67 | public CreateComicWorker(File newSourceDir, File newTargetDir, Conversion newConversion, |
68 | boolean newCreateComicForEachFolder, boolean newOpenAfterCreation) { |
69 | super(); |
70 | logger = LogFactory.getLog(CreateComicWorker.class); |
71 | fileTools = FileTools.instance(); |
72 | jomicTools = JomicTools.instance(); |
73 | localeTools = LocaleTools.instance(); |
74 | stringTools = StringTools.instance(); |
75 | jomicApplication = JomicApplication.instance(); |
76 | |
77 | sourceDir = newSourceDir; |
78 | targetDir = newTargetDir; |
79 | conversion = newConversion; |
80 | isCreateComicForEachFolder = newCreateComicForEachFolder; |
81 | isOpenAfterCreation = newOpenAfterCreation; |
82 | createComicTask = null; |
83 | progressFrame = new ProgressFrame(); |
84 | createComicFileFilter = new CreateComicFileFilter(); |
85 | } |
86 | |
87 | private File getTargetFile(String targetName) { |
88 | String comicFormat = conversion.getComicFormat(); |
89 | File result; |
90 | |
91 | // TODO: Simplify code by adding Conversion.getComicFormatSuffix(). |
92 | if (comicFormat.equals(Conversion.COMIC_FORMAT_CBZ)) { |
93 | result = new File(targetDir, targetName + ".cbz"); |
94 | } else if (comicFormat.equals(Conversion.COMIC_FORMAT_PDF)) { |
95 | result = new File(targetDir, targetName + ".pdf"); |
96 | } else { |
97 | assert false : "comicFormat=" + comicFormat; |
98 | result = null; |
99 | } |
100 | return result; |
101 | } |
102 | |
103 | public void actionPerformed(ActionEvent event) { |
104 | try { |
105 | assert event != null; |
106 | |
107 | if (logger.isInfoEnabled()) { |
108 | logger.info("cancel creation"); |
109 | } |
110 | interrupt(); |
111 | } catch (Throwable error) { |
112 | jomicTools.showError(event, error); |
113 | } |
114 | } |
115 | |
116 | public Object construct() { |
117 | File firstTargetFile = null; |
118 | |
119 | progressFrame.addActionListener(this); |
120 | progressFrame.setTitle(localeTools.getMessage("progress.create.title")); |
121 | progressFrame.setNote(localeTools.getMessage("progress.create.lookingUpSourceFiles")); |
122 | progressFrame.setVisible(true); |
123 | try { |
124 | File[] sourceFiles = fileTools.listFilesRecursively(sourceDir, createComicFileFilter); |
125 | |
126 | if (sourceFiles.length == 0) { |
127 | String errorMessage = localeTools.getMessage( |
128 | "errors.folderMustContainImages", stringTools.sourced(sourceDir)); |
129 | |
130 | throw new NoSuchElementException(errorMessage); |
131 | } |
132 | if (isCreateComicForEachFolder) { |
133 | Map folderMap = fileTools.getFolderMap(sourceFiles); |
134 | |
135 | assert !folderMap.isEmpty(); |
136 | Map folderNames = fileTools.getFolderNames(folderMap); |
137 | Map flattenedFolderNames = fileTools.getFlattenedFolderNames(folderNames); |
138 | |
139 | assert !flattenedFolderNames.isEmpty(); |
140 | Task[] createComicTasks = new Task[flattenedFolderNames.size()]; |
141 | int taskIndex = 0; |
142 | Iterator targetNameRider = flattenedFolderNames.entrySet().iterator(); |
143 | |
144 | while (targetNameRider.hasNext()) { |
145 | Map.Entry targetNameEntry = (Map.Entry) targetNameRider.next(); |
146 | String targetName = (String) targetNameEntry.getKey(); |
147 | File targetFile = getTargetFile(targetName); |
148 | File taskSourceDir = (File) targetNameEntry.getValue(); |
149 | String taskSourceDirPath = taskSourceDir.getPath(); |
150 | List taskSourceFileList = (List) folderMap.get(taskSourceDirPath); |
151 | |
152 | assert taskSourceFileList != null; |
153 | File[] taskSourceFiles = (File[]) taskSourceFileList.toArray(new File[0]); |
154 | |
155 | createComicTasks[taskIndex] = createCreateComicTask(targetFile, taskSourceFiles); |
156 | if (firstTargetFile == null) { |
157 | firstTargetFile = targetFile; |
158 | } |
159 | taskIndex += 1; |
160 | } |
161 | assert taskIndex > 0; |
162 | createComicTask = new NestedTask(createComicTasks); |
163 | } else { |
164 | File targetFile = getTargetFile(sourceDir.getName()); |
165 | |
166 | createComicTask = createCreateComicTask(targetFile, sourceFiles); |
167 | firstTargetFile = targetFile; |
168 | } |
169 | createComicTask.addProgressChangeListener(this); |
170 | progressFrame.setMaximum(createComicTask.getMaxProgress()); |
171 | createComicTask.start(); |
172 | if (isOpenAfterCreation && (firstTargetFile != null) && firstTargetFile.exists()) { |
173 | jomicApplication.open(null, firstTargetFile); |
174 | } |
175 | } catch (Exception error) { |
176 | progressFrame.setVisible(false); |
177 | jomicTools.showError(progressFrame, "errors.cannotCreateComic", error); |
178 | } finally { |
179 | if (createComicTask != null) { |
180 | createComicTask.removeProgressChangeListener(this); |
181 | } |
182 | progressFrame.removeActionListener(this); |
183 | progressFrame.dispose(); |
184 | } |
185 | return firstTargetFile; |
186 | } |
187 | |
188 | public void progressChanged(Task source) { |
189 | String progressMessage = source.getProgressMessage(); |
190 | |
191 | if (!progressMessage.equals(oldProgressMessage)) { |
192 | progressFrame.setNote(progressMessage); |
193 | oldProgressMessage = progressMessage; |
194 | } |
195 | progressFrame.setProgress(source.getProgress()); |
196 | } |
197 | |
198 | private Task createCreateComicTask(File targetFile, File[] sourceFiles) { |
199 | Task result; |
200 | String[] sourceNames = fileTools.getRelativePaths(sourceDir, sourceFiles); |
201 | |
202 | Arrays.sort(sourceFiles, new NaturalCaseInsensitiveOrderComparator()); |
203 | result = new CreateComicTask(sourceDir, sourceNames, targetFile, conversion); |
204 | return result; |
205 | } |
206 | } |