Skip to content

Commit

Permalink
'#1866: Memorize and reuse user color assignments for bookmark names.
Browse files Browse the repository at this point in the history
  • Loading branch information
wladimirleite committed Sep 12, 2023
1 parent 8224602 commit b157708
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions iped-app/src/main/java/iped/app/ui/BookmarksManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ public void actionPerformed(final ActionEvent evt) {
if (newColor != null && !newColor.equals(currentColor)) {
multiBookmarks.setBookmarkColor(currentName, newColor);
changed = true;
BookmarkColorsManager.storeNameToColor(currentName, newColor);
}

if (changed) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,56 @@
package iped.app.ui.bookmarks;

import java.awt.Color;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import iped.utils.IOUtil;

public class BookmarkColorsManager {
private static final String colorsMemoFile = System.getProperty("user.home") + "/.iped/bkmclr.dat";

private static Map<Integer, Color> colorsMemo = Collections.synchronizedMap(new LinkedHashMap<Integer, Color>());

private static final Map<Color, Color> backgroundToForeground = Collections
.synchronizedMap(new HashMap<Color, Color>());

static {
for (int i = 0; i < BookmarkStandardColors.numStandardColors; i++) {
Color foreground = i * 2 >= BookmarkStandardColors.numStandardColors ? Color.black : Color.white;
backgroundToForeground.put(BookmarkStandardColors.colors[i], foreground);
}
readColorsMemo();
}

private static int nameToKey(String name) {
return name.toLowerCase().hashCode();
}

public static void storeNameToColor(String name, Color color) {
colorsMemo.put(nameToKey(name), color);
writeColorsMemo();
}

public static Color getInitialColor(Set<Color> usedColors, String name) {
int off = name.toLowerCase().hashCode() % BookmarkStandardColors.numStandardColors;
int key = nameToKey(name);
if (colorsMemo.containsKey(key)) {
return colorsMemo.get(key);
}
int off = key % BookmarkStandardColors.numStandardColors;
Color ret = BookmarkStandardColors.colors[off];
for (int i = 0; i < BookmarkStandardColors.numStandardColors; i++) {
int idx = (off + i) % BookmarkStandardColors.numStandardColors;
Expand All @@ -41,4 +73,40 @@ static Color getForeground(Color background) {
}
return foreground;
}

private static synchronized void writeColorsMemo() {
ObjectOutputStream oos = null;
try {
File tmp = new File(colorsMemoFile + ".tmp");
tmp.deleteOnExit();
if (tmp.exists()) {
tmp.delete();
}
oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(tmp)));
oos.writeObject(colorsMemo);
oos.close();
Files.move(tmp.toPath(), Path.of(colorsMemoFile), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtil.closeQuietly(oos);
}
}

@SuppressWarnings("unchecked")
private static synchronized void readColorsMemo() {
ObjectInputStream ois = null;
try {
File in = new File(colorsMemoFile);
if (in.exists()) {
ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(in)));
colorsMemo = (Map<Integer, Color>) ois.readObject();
ois.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtil.closeQuietly(ois);
}
}
}

0 comments on commit b157708

Please sign in to comment.