-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageFileManager.java
More file actions
145 lines (137 loc) · 5.56 KB
/
Copy pathImageFileManager.java
File metadata and controls
145 lines (137 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package photo_renamer;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
/**
* A manager for an ImageFile.
* Implements Observer in order to keep track of name changes to an Observable ImageFile.
*/
class ImageFileManager implements Observer {
/** A list of permissible ImageFile file types. */
private static final String[] EXTENSIONS = {".jpg", ".jpeg", ".png", ".bmp"};
/** The ImageFile object being renamed. */
private ImageFile managedImage;
/** The PhotoRenameLogger used to log the renaming events. */
private PhotoRenameLogger photoRenameLogger;
/** The ImageHistoryManager used to manage the history of ImageFiles. */
private ImageHistoryManager historyManager;
/**
* Construct a manager for observing an ImageFile.
*
* @param managedImage the ImageFile object being managed
* @param photoRenameLogger the PhotoRenameLogger used for logging rename events
* @param historyManager the ImageHistoryManager managing the history database
*/
@SuppressWarnings("unchecked")
ImageFileManager(ImageFile managedImage, PhotoRenameLogger photoRenameLogger, ImageHistoryManager historyManager)
throws IOException, ClassNotFoundException {
this.managedImage = managedImage;
this.photoRenameLogger = photoRenameLogger;
this.historyManager = historyManager;
}
/**
* Rename and modify an ImageFile to include the argued Tags.
*
* @param toApply the list of tags to apply to the managed image
*/
void applyTags(ArrayList<Tag> toApply) throws IOException {
// Record the old name for logging purposes.
String oldName = managedImage.name;
int extBeginning = oldName.lastIndexOf(".");
String oldExt = oldName.substring(extBeginning);
String newName = oldName.substring(0, extBeginning);
boolean shouldRename = false;
// Add each tag to the ImageFile Tag list, as well as the new name to be used.
for (Tag tag : toApply) {
if (!managedImage.tags.contains(tag) && !oldName.contains("@" + tag)) {
managedImage.tags.add(tag);
newName += " @" + tag.name;
shouldRename = true;
}
}
if (shouldRename) {
newName += oldExt;
managedImage.renameImage(newName);
}
}
/**
* Modify and rename the managed image to remove the argued Tags.
*
* @param toDelete the list of tags to delete from the managed image
*/
void deleteTags(ArrayList<Tag> toDelete) throws IOException {
// Record the old name and file extension.
String oldName = managedImage.name;
int extBeginning = oldName.lastIndexOf(".");
String oldExt = oldName.substring(extBeginning);
String newName = oldName.substring(0, extBeginning);
boolean shouldRename = false;
// Remove each tag from the ImageFile object and/or its old name.
for (Tag tag : toDelete) {
String removeTag = " @" + tag.name;
if (managedImage.tags.contains(tag)) {
managedImage.tags.remove(tag);
shouldRename = true;
}
if (oldName.contains(removeTag)) {
newName = newName.replace(removeTag, "");
shouldRename = true;
}
}
if (shouldRename) {
newName += oldExt;
managedImage.renameImage(newName);
}
}
/**
* Log rename events for the observed ImageFile and update the history database.
*
* @param o the ImageFile this ImageManager observes
* @param oldName the oldName of this ImageFile
*/
@Override
public void update(Observable o, Object oldName) {
try {
historyManager.toFile();
} catch (IOException e) {
JOptionPane.showMessageDialog(new JFrame(), "Failed to access history database!");
e.printStackTrace();
}
photoRenameLogger.logRename(oldName, o);
}
/**
* Return an ArrayList of ImageFiles located anywhere under a given directory.
*
* @param dir the root directory
* @param imageFiles the ArrayList of managedImages found
* @param historyManager the ImageHistoryManager being used
* @return the updated ArrayList of managedImages found
*/
static ArrayList<ImageFile> getImages(File dir, ArrayList<ImageFile> imageFiles, ImageHistoryManager historyManager)
throws IOException, ClassNotFoundException {
File[] files = dir.listFiles();
if (files != null) {
// Recursively search this directory for any images under it.
for (File sub : files) {
if (sub.isDirectory()) {
getImages(sub, imageFiles, historyManager);
} else {
String fileName = sub.getName().toLowerCase();
// Check for image file extensions and append the file if compliant.
for (String ext : EXTENSIONS) {
if (fileName.endsWith(ext)) {
ImageFile image = new ImageFile(sub.getName(), new ArrayList<>(), sub.getAbsolutePath());
image = historyManager.historicizeImage(image);
imageFiles.add(image);
break;
}
}
}
}
}
return imageFiles;
}
}