|
1 | 1 | package com.kodedu.service; |
2 | 2 |
|
3 | | -import com.kodedu.config.StoredConfigBean; |
4 | | -import com.kodedu.controller.ApplicationController; |
5 | | -import com.kodedu.helper.IOHelper; |
6 | | -import com.kodedu.other.Current; |
7 | | -import com.kodedu.service.ui.FileBrowseService; |
8 | | -import javafx.application.Platform; |
9 | 3 | import javafx.stage.DirectoryChooser; |
10 | 4 | import javafx.stage.FileChooser; |
11 | | -import org.slf4j.Logger; |
12 | | -import org.slf4j.LoggerFactory; |
13 | | -import org.springframework.beans.factory.annotation.Autowired; |
14 | | -import org.springframework.stereotype.Component; |
15 | 5 |
|
16 | 6 | import java.io.File; |
17 | | -import java.nio.file.Files; |
18 | | -import java.nio.file.FileSystems; |
19 | 7 | import java.nio.file.Path; |
20 | | -import java.util.List; |
21 | | -import java.util.Objects; |
22 | 8 | import java.util.Optional; |
23 | | -import java.util.concurrent.CompletableFuture; |
24 | 9 | import java.util.function.Supplier; |
25 | | -import java.util.stream.Collectors; |
26 | | -import java.util.stream.Stream; |
27 | | - |
28 | | -import static java.util.Arrays.asList; |
29 | 10 |
|
30 | 11 | /** |
31 | 12 | * Created by usta on 25.12.2014. |
32 | 13 | */ |
33 | | -@Component |
34 | | -public class DirectoryService { |
35 | | - |
36 | | - private final ApplicationController controller; |
37 | | - private final FileBrowseService fileBrowser; |
38 | | - private final Current current; |
39 | | - private final PathResolverService pathResolver; |
40 | | - private final StoredConfigBean storedConfigBean; |
41 | | - |
42 | | - private final Logger logger = LoggerFactory.getLogger(DirectoryService.class); |
43 | | - |
44 | | - private Optional<Path> workingDirectory = Optional.of(IOHelper.getPath(System.getProperty("user.home"))); |
45 | | - private Optional<File> initialDirectory = Optional.empty(); |
46 | | - |
47 | | - private Supplier<Path> pathSaveSupplier; |
48 | | - private final FileWatchService fileWatchService; |
49 | | - private final ThreadService threadService; |
50 | | - private final PathMapper pathMapper; |
51 | | - |
52 | | - |
53 | | - @Autowired |
54 | | - public DirectoryService(final ApplicationController controller, final FileBrowseService fileBrowser, final Current current, PathResolverService pathResolver, StoredConfigBean storedConfigBean, FileWatchService fileWatchService, ThreadService threadService, PathMapper pathMapper) { |
55 | | - this.controller = controller; |
56 | | - this.fileBrowser = fileBrowser; |
57 | | - this.current = current; |
58 | | - this.pathResolver = pathResolver; |
59 | | - this.storedConfigBean = storedConfigBean; |
60 | | - this.fileWatchService = fileWatchService; |
61 | | - this.threadService = threadService; |
62 | | - this.pathMapper = pathMapper; |
63 | | - |
64 | | - pathSaveSupplier = () -> { |
65 | | - final FileChooser chooser = newFileChooser("Save Document"); |
66 | | - chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Asciidoc", "*.adoc", "*.asciidoc", "*.asc", "*.ad", "*.txt", "*.*")); |
67 | | - chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Markdown", "*.md", "*.markdown", "*.txt", "*.*")); |
68 | | - File file = chooser.showSaveDialog(null); |
69 | | - return Objects.nonNull(file) ? file.toPath() : null; |
70 | | - }; |
71 | | - |
72 | | - } |
73 | | - |
74 | | - public DirectoryChooser newDirectoryChooser(String title) { |
75 | | - DirectoryChooser directoryChooser = new DirectoryChooser(); |
76 | | - directoryChooser.setTitle(title); |
77 | | - initialDirectory.ifPresent(file -> { |
78 | | - directoryChooser.setInitialDirectory(getChooserInitialDirectory(file)); |
79 | | - }); |
80 | | - return directoryChooser; |
81 | | - } |
82 | | - |
83 | | - public FileChooser newFileChooser(String title) { |
84 | | - final FileChooser fileChooser = new FileChooser(); |
85 | | - fileChooser.setTitle(title); |
86 | | - initialDirectory.ifPresent(file -> { |
87 | | - fileChooser.setInitialDirectory(getChooserInitialDirectory(file)); |
88 | | - }); |
89 | | - |
90 | | - return fileChooser; |
91 | | - } |
92 | | - |
93 | | - /** |
94 | | - * Resolve a file to a valid base browsing directory |
95 | | - * @param referenceFile The base directory or a child file that should be used as a browsing base path |
96 | | - * @return A valid directory's File object |
97 | | - */ |
98 | | - private static File getChooserInitialDirectory(File referenceFile) { |
99 | | - // Search for an existing directory |
100 | | - while (referenceFile != null){ |
101 | | - if(Files.isDirectory(referenceFile.toPath())) { |
102 | | - return referenceFile; |
103 | | - } |
104 | | - referenceFile = referenceFile.getParentFile(); |
105 | | - } |
106 | | - |
107 | | - // No parent file could be found or referenceFile |
108 | | - // Default to returning a path to the first drive available |
109 | | - return FileSystems.getDefault().getRootDirectories().iterator().next().toFile(); |
110 | | - } |
111 | | - |
112 | | - public Path workingDirectory() { |
113 | | - return workingDirectory.orElseGet(this::workingDirectorySupplier); |
114 | | - } |
115 | | - |
116 | | - private Path workingDirectorySupplier() { |
117 | | - |
118 | | - if (!Platform.isFxApplicationThread()) { |
119 | | - final CompletableFuture<Path> completableFuture = new CompletableFuture<>(); |
120 | | - completableFuture.runAsync(() -> { |
121 | | - threadService.runActionLater(() -> { |
122 | | - try { |
123 | | - Path path = workingDirectorySupplier(); |
124 | | - completableFuture.complete(path); |
125 | | - } catch (Exception e) { |
126 | | - completableFuture.completeExceptionally(e); |
127 | | - } |
128 | | - }); |
129 | | - }, threadService.executor()); |
130 | | - return completableFuture.join(); |
131 | | - } |
132 | | - |
133 | | - final DirectoryChooser directoryChooser = newDirectoryChooser("Select working directory"); |
134 | | - final File file = directoryChooser.showDialog(null); |
135 | | - |
136 | | - workingDirectory = Optional.ofNullable(file.toPath()); |
137 | | - |
138 | | - workingDirectory.ifPresent(fileBrowser::browse); |
139 | | - |
140 | | - return Objects.nonNull(file) ? file.toPath() : null; |
141 | | - } |
142 | | - |
143 | | - public Path currentPath() { |
144 | | - return current.currentPath().orElseGet(pathSaveSupplier); |
145 | | - } |
146 | | - |
147 | | - public Supplier<Path> getPathSaveSupplier() { |
148 | | - return pathSaveSupplier; |
149 | | - } |
150 | | - |
151 | | - public void setPathSaveSupplier(Supplier<Path> pathSaveSupplier) { |
152 | | - this.pathSaveSupplier = pathSaveSupplier; |
153 | | - } |
154 | | - |
155 | | - public void setWorkingDirectory(Optional<Path> workingDirectory) { |
156 | | - this.workingDirectory = workingDirectory; |
157 | | - } |
158 | | - |
159 | | - public Optional<Path> getWorkingDirectory() { |
160 | | - return workingDirectory; |
161 | | - } |
162 | | - |
163 | | - public Optional<File> getInitialDirectory() { |
164 | | - return initialDirectory; |
165 | | - } |
166 | | - |
167 | | - public void setInitialDirectory(Optional<File> initialDirectory) { |
168 | | - this.initialDirectory = initialDirectory; |
169 | | - } |
170 | | - |
171 | | - public void askWorkingDir() { |
172 | | - DirectoryChooser directoryChooser = this.newDirectoryChooser("Select Working Directory"); |
173 | | - File selectedDir = directoryChooser.showDialog(null); |
174 | | - if (Objects.nonNull(selectedDir)) { |
175 | | - changeWorkigDir(selectedDir.toPath()); |
176 | | - } |
177 | | - } |
178 | | - |
179 | | - public void changeWorkigDir(Path path) { |
180 | | - if (Objects.isNull(path)) |
181 | | - return; |
182 | | - |
183 | | - pathMapper.addRootPath(path); |
184 | | - |
185 | | - storedConfigBean.setWorkingDirectory(path.toString()); |
186 | | - |
187 | | - this.setWorkingDirectory(Optional.of(path)); |
188 | | - fileBrowser.browse(path); |
189 | | - this.setInitialDirectory(Optional.ofNullable(path.toFile())); |
190 | | - controller.getStage().setTitle(String.format("AsciidocFX - %s", path)); |
191 | | - |
192 | | - } |
193 | | - |
194 | | - public void goUp() { |
195 | | - workingDirectory.map(Path::getParent).ifPresent(this::changeWorkigDir); |
196 | | - } |
197 | | - |
198 | | - public void refreshWorkingDir() { |
199 | | - workingDirectory.ifPresent(this::changeWorkigDir); |
200 | | - } |
201 | | - |
202 | | - public String interPath() { |
203 | | - return interPath(currentParentOrWorkdir()); |
204 | | - } |
205 | | - |
206 | | - public String interPath(Path path) { |
207 | | - |
208 | | - try { |
209 | | - Path subpath = path.subpath(0, path.getNameCount()); |
210 | | - return subpath.toString().replace('\\', '/'); |
211 | | - } catch (Exception e) { |
212 | | - return "."; |
213 | | - } |
214 | | - } |
215 | | - |
216 | | - public Path getSaveOutputPath(FileChooser.ExtensionFilter extensionFilter, boolean askPath) { |
217 | | - |
218 | | - if (!Platform.isFxApplicationThread()) { |
219 | | - final CompletableFuture<Path> completableFuture = new CompletableFuture<>(); |
220 | | - |
221 | | - completableFuture.runAsync(() -> { |
222 | | - threadService.runActionLater(() -> { |
223 | | - try { |
224 | | - Path outputPath = getSaveOutputPath(extensionFilter, askPath); |
225 | | - completableFuture.complete(outputPath); |
226 | | - } catch (Exception e) { |
227 | | - completableFuture.completeExceptionally(e); |
228 | | - } |
229 | | - }); |
230 | | - }, threadService.executor()); |
231 | | - |
232 | | - return completableFuture.join(); |
233 | | - } |
234 | | - |
235 | | - boolean isNew = current.currentTab().isNew(); |
236 | | - |
237 | | - if (isNew) { |
238 | | - controller.saveDoc(); |
239 | | - } |
240 | | - |
241 | | - final Path currentTabPath = current.currentPath().get(); |
242 | | - final Path currentTabPathDir = currentTabPath.getParent(); |
243 | | - String tabText = current.getCurrentTabText().replace("*", "").trim(); |
244 | | - tabText = tabText.contains(".") ? tabText.substring(0, tabText.lastIndexOf(".")) : tabText; |
245 | | - |
246 | | - if (!askPath) { |
247 | | - return currentTabPathDir.resolve(extensionFilter.getExtensions().get(0).replace("*", tabText)); |
248 | | - } |
249 | | - |
250 | | - final FileChooser fileChooser = this.newFileChooser(String.format("Save %s file", extensionFilter.getDescription())); |
251 | | - fileChooser.getExtensionFilters().addAll(extensionFilter); |
252 | | - File file = fileChooser.showSaveDialog(null); |
253 | | - |
254 | | - if (Objects.isNull(file)) { |
255 | | - return currentTabPathDir.resolve(extensionFilter.getExtensions().get(0).replace("*", tabText)); |
256 | | - } |
257 | | - |
258 | | - return file.toPath(); |
259 | | - } |
260 | | - |
261 | | - public Path findPathInCurrentOrWorkDir(String uri) { |
262 | | - |
263 | | - Optional<Path> lookUpFile = pathMapper.lookUpFile(uri); |
264 | | - |
265 | | - if (lookUpFile.isPresent()) { |
266 | | - return lookUpFile.get(); |
267 | | - } |
268 | | - |
269 | | - Optional<Path> inCurrentParent = Optional.empty(); |
270 | | - Optional<Path> inRoot = Optional.empty(); |
| 14 | +public interface DirectoryService { |
271 | 15 |
|
272 | | - try { |
273 | | - inCurrentParent = Optional.ofNullable(current.currentTab().getPath().getParent()); |
274 | | - } catch (Exception e) { |
275 | | - //no-op |
276 | | - } |
| 16 | + public DirectoryChooser newDirectoryChooser(String title); |
277 | 17 |
|
278 | | - try { |
279 | | - if (inCurrentParent.isPresent()) { |
280 | | - inRoot = inCurrentParent.map(Path::getRoot); |
281 | | - } else { |
282 | | - inRoot = workingDirectory.map(Path::getRoot); |
283 | | - } |
284 | | - } catch (Exception e) { |
285 | | - //no-op |
286 | | - } |
| 18 | + public FileChooser newFileChooser(String title); |
287 | 19 |
|
288 | | - return Stream.<Optional<Path>>of(inCurrentParent, workingDirectory, inRoot) |
289 | | - .filter(Optional::isPresent) |
290 | | - .map(Optional::get) |
291 | | - .map(path -> path.resolve(uri)) |
292 | | - .filter(Files::exists) |
293 | | - .findFirst() |
294 | | - .orElseGet(() -> null); |
| 20 | + public Path workingDirectory(); |
295 | 21 |
|
296 | | - } |
| 22 | + public Path currentPath(); |
297 | 23 |
|
298 | | - public Path findPathInPublic(String finalUri) { |
299 | | - List<String> uris = asList(finalUri, finalUri.replaceFirst("/", "")).stream().collect(Collectors.toList()); |
300 | | - Path result = null; |
301 | | - for (String uri : uris) { |
302 | | - Path configPath = controller.getConfigPath().resolve("public"); |
303 | | - Path resolve = configPath.resolve(uri); |
304 | | - if (Files.exists(resolve)) { |
305 | | - result = resolve; |
306 | | - break; |
307 | | - } |
308 | | - } |
309 | | - return result; |
310 | | - } |
| 24 | + public Supplier<Path> getPathSaveSupplier(); |
311 | 25 |
|
312 | | - public Path findPathInWorkdirOrLookup(Path uri) { |
| 26 | + public void setPathSaveSupplier(Supplier<Path> pathSaveSupplier); |
313 | 27 |
|
314 | | - Optional<Path> lookUpFile = pathMapper.lookUpFile(uri); |
| 28 | + public void setWorkingDirectory(Optional<Path> workingDirectory); |
315 | 29 |
|
316 | | - if (lookUpFile.isPresent()) { |
317 | | - return lookUpFile.get(); |
318 | | - } |
| 30 | + public Optional<Path> getWorkingDirectory(); |
319 | 31 |
|
320 | | - Optional<Path> optional = current.currentPath().map(Path::getParent); |
| 32 | + public Optional<File> getInitialDirectory(); |
321 | 33 |
|
322 | | - if (optional.isPresent()) { |
323 | | - Path resolve = optional.get().resolve(uri); |
| 34 | + public void setInitialDirectory(Optional<File> initialDirectory); |
324 | 35 |
|
325 | | - if (Files.exists(resolve)) { |
326 | | - return resolve; |
327 | | - } |
328 | | - } |
| 36 | + public void askWorkingDir(); |
329 | 37 |
|
330 | | - Path workingDirectory = workingDirectory(); |
331 | | - Path resolve = workingDirectory.resolve(uri); |
| 38 | + public void changeWorkigDir(Path path); |
332 | 39 |
|
333 | | - if (Files.exists(resolve)) { |
334 | | - return resolve; |
335 | | - } |
| 40 | + public void goUp(); |
336 | 41 |
|
337 | | - if (optional.isPresent()) { |
338 | | - Path currentParent = optional.get(); |
| 42 | + public void refreshWorkingDir(); |
339 | 43 |
|
340 | | - while (true) { |
| 44 | + public String interPath(); |
341 | 45 |
|
342 | | - if (Objects.nonNull(currentParent)) { |
343 | | - Path candidate = currentParent.resolve(uri); |
344 | | - if (Files.exists(candidate)) { |
345 | | - return candidate; |
346 | | - } else { |
347 | | - currentParent = currentParent.getParent(); |
348 | | - } |
349 | | - } else { |
350 | | - break; |
351 | | - } |
| 46 | + public String interPath(Path path); |
352 | 47 |
|
353 | | - } |
| 48 | + public Path getSaveOutputPath(FileChooser.ExtensionFilter extensionFilter, boolean askPath); |
354 | 49 |
|
355 | | - } |
| 50 | + public Path findPathInCurrentOrWorkDir(String uri); |
356 | 51 |
|
357 | | - return null; |
| 52 | + public Path findPathInPublic(String finalUri); |
358 | 53 |
|
359 | | - } |
| 54 | + public Path findPathInWorkdirOrLookup(Path uri); |
360 | 55 |
|
361 | | - public Path currentParentOrWorkdir() { |
362 | | - return current.currentPath().map(Path::getParent).orElse(this.workingDirectory()); |
363 | | - } |
| 56 | + public Path currentParentOrWorkdir(); |
364 | 57 | } |
0 commit comments