source: trunk/src/org/expeditee/gui/management/ResourceUtil.java@ 1447

Last change on this file since 1447 was 1447, checked in by bnemhaus, 5 years ago

When dnd'ing a or copy/pasting a image that is external to expeditee (not a sub file of expedite.home) then that image is imported into the expeditee file system with a randomly generated name. A users FolderSettings are used to determine location to import image too.

File size: 4.4 KB
Line 
1package org.expeditee.gui.management;
2
3import java.io.File;
4import java.io.IOException;
5import java.nio.file.Path;
6import java.nio.file.Paths;
7import java.util.List;
8
9import org.expeditee.core.Image;
10import org.expeditee.gui.DisplayController;
11import org.expeditee.gui.Frame;
12import org.expeditee.gui.FrameIO;
13import org.expeditee.items.Text;
14
15public class ResourceUtil {
16
17 private static final char PATTERN_START_TWO = '{';
18 private static final char PATTERN_START_ONE = '$';
19 private static final char PATTERN_END = '}';
20 private static final char ESCAPE = '\\';
21
22 public static final String CURRENT_FRAMESET_FLAG = "${CURRENT_FRAMESET}";
23 public static final String CURRENT_USER = "${CURRENT_USER}";
24
25 /**
26 * Attempts to relativise a image path using expeditee.home (FrameIO.PARENT_FOLDER).
27 * @param path The path to attempt to relativise
28 * @return Returns the most relativised path if one exists, otherwise returns the ref parameter passed in.
29 */
30 public static Path relativiseImagePath(Path path) {
31 Path expediteeHome = Paths.get(FrameIO.PARENT_FOLDER);
32 if (path.startsWith(expediteeHome)) {
33 return expediteeHome.relativize(path);
34 } else {
35 return path;
36 }
37 }
38
39 public static Path resolveImagePath(Path path, Frame context) {
40 List<String> directories = ResourceManager.images.getDirectories(context);
41 directories.add(FrameIO.PARENT_FOLDER);
42
43 for (String dir: directories) {
44 Path canditate = Paths.get(dir).resolve(path);
45 if (canditate.toFile().exists()) {
46 return canditate;
47 }
48 }
49
50 return null;
51 }
52
53 public static Path getImageSaveFileLocation(Frame context) {
54 List<String> directories = ResourceManager.images.getDirectories(context);
55 if (directories.isEmpty()) {
56 return Paths.get(FrameIO.PARENT_FOLDER).toAbsolutePath();
57 } else {
58 return Paths.get(directories.get(0));
59 }
60 }
61
62 public static Text newImageWithName(Image img, String name) throws IOException {
63 ResourceManager.images.getDirectories();// TODO: save this image out to the first directory in list instead of to IMAGES_PATH
64 File out = new File(FrameIO.IMAGES_PATH + name);
65 out.mkdirs();
66 img.writeToDisk("png", out);
67 Text item = DisplayController.getCurrentFrame().createNewText("@i: " + out.getName());
68 return item;
69 }
70
71 /**
72 * Utility function that transforms a input string to a output string.
73 * Any occurances of 'pattern' are replaced with 'replacement'.
74 * \ is used as the escape key.
75 * @param input The raw input
76 * @param pattern The pattern to replace.
77 * This will be prepended by in ${ and appended by } in the input string.
78 * @param replacement The replacement content to use.
79 * @return The transformed string.
80 */
81 protected static String substitute(String input, String pattern, String replacement) {
82 StringBuilder outputSB = new StringBuilder();
83 StringBuilder inputSB = new StringBuilder(input.replace("\\", "\\\\") + ResourceUtil.ESCAPE);
84 StringBuilder patternSB = new StringBuilder();
85 boolean inPattern = false;
86 boolean patternStartPossible = true;
87
88 int index = 0;
89 int length = inputSB.length() - 1;
90 while (index < length) {
91 char c = inputSB.charAt(index);
92 boolean startingPattern = patternStartPossible &&
93 c == ResourceUtil.PATTERN_START_TWO &&
94 outputSB.charAt(outputSB.length() - 1) == ResourceUtil.PATTERN_START_ONE;
95 boolean endingPattern = c == ResourceUtil.PATTERN_END;
96
97 if (c == ResourceUtil.ESCAPE && !inPattern) {
98 index++;
99 c = inputSB.charAt(index);
100 outputSB.append(c);
101 if (c == PATTERN_START_ONE) {
102 patternStartPossible = false;
103 }
104 } else if (inPattern && endingPattern) {
105 String patternFound = patternSB.toString();
106 if (("${" + patternFound + "}").equals(pattern)) {
107 outputSB.append(replacement);
108 } else {
109 outputSB.append("" + ResourceUtil.PATTERN_START_ONE + ResourceUtil.PATTERN_START_TWO);
110 outputSB.append(patternFound);
111 outputSB.append(ResourceUtil.PATTERN_END);
112 }
113 inPattern = false;
114 patternSB = new StringBuilder();
115 } else if (!inPattern && startingPattern) {
116 outputSB.deleteCharAt(outputSB.length() - 1);
117 inPattern = true;
118 } else if (inPattern) {
119 patternSB.append(c);
120 } else {
121 outputSB.append(c);
122 patternStartPossible = true;
123 }
124
125 index++;
126 }
127
128 if (patternSB.length() > 0) {
129 outputSB.append("${");
130 }
131 outputSB.append(patternSB);
132 return outputSB.toString();
133 }
134}
Note: See TracBrowser for help on using the repository browser.