From 22f15d4c5442a4bd1227c1a301fc4de22c885be5 Mon Sep 17 00:00:00 2001 From: tc-wleite Date: Sun, 10 Sep 2023 15:51:18 -0300 Subject: [PATCH] '#1866: Set an initial color based on name for new bookmarks. --- .../src/main/java/iped/data/IBookmarks.java | 2 ++ .../main/java/iped/data/IMultiBookmarks.java | 4 ++- .../java/iped/app/ui/BookmarksManager.java | 28 +++++++++++-------- .../ui/bookmarks/BookmarkColorsManager.java | 20 +++++++++++++ .../ui/bookmarks/BookmarkStandardColors.java | 3 +- .../main/java/iped/engine/data/Bookmarks.java | 5 ++++ .../java/iped/engine/data/MultiBookmarks.java | 8 ++++++ 7 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 iped-app/src/main/java/iped/app/ui/bookmarks/BookmarkColorsManager.java diff --git a/iped-api/src/main/java/iped/data/IBookmarks.java b/iped-api/src/main/java/iped/data/IBookmarks.java index 6563bb3f7b..5e614377a5 100644 --- a/iped-api/src/main/java/iped/data/IBookmarks.java +++ b/iped-api/src/main/java/iped/data/IBookmarks.java @@ -96,6 +96,8 @@ public interface IBookmarks extends Serializable { Color getBookmarkColor(int bookmarkId); + Set getUsedColors(); + void setBookmarkKeyStroke(int bookmarkId, KeyStroke key); KeyStroke getBookmarkKeyStroke(int bookmarkId); diff --git a/iped-api/src/main/java/iped/data/IMultiBookmarks.java b/iped-api/src/main/java/iped/data/IMultiBookmarks.java index 7b733a1979..ec8fc738f0 100644 --- a/iped-api/src/main/java/iped/data/IMultiBookmarks.java +++ b/iped-api/src/main/java/iped/data/IMultiBookmarks.java @@ -91,10 +91,12 @@ public interface IMultiBookmarks extends Serializable { void setBookmarkComment(String texto, String comment); - public Color getBookmarkColor(String bookmarkName); + Color getBookmarkColor(String bookmarkName); void setBookmarkColor(String bookmarkName, Color color); + Set getUsedColors(); + boolean isInReport(String bookmark); void setInReport(String bookmark, boolean checked); diff --git a/iped-app/src/main/java/iped/app/ui/BookmarksManager.java b/iped-app/src/main/java/iped/app/ui/BookmarksManager.java index 7d23517bc2..f63530d1ee 100644 --- a/iped-app/src/main/java/iped/app/ui/BookmarksManager.java +++ b/iped-app/src/main/java/iped/app/ui/BookmarksManager.java @@ -70,9 +70,11 @@ import org.slf4j.LoggerFactory; import iped.app.ui.bookmarks.BookmarkAndKey; +import iped.app.ui.bookmarks.BookmarkColorsManager; import iped.app.ui.bookmarks.BookmarkListRenderer; import iped.data.IItem; import iped.data.IItemId; +import iped.data.IMultiBookmarks; import iped.engine.data.IPEDMultiSource; import iped.engine.data.IPEDSource; import iped.engine.data.ItemId; @@ -403,18 +405,20 @@ private void includeDuplicates(ArrayList uniqueSelectedIds) { @Override public void actionPerformed(final ActionEvent evt) { - + IMultiBookmarks multiBookmarks = App.get().appCase.getMultiBookmarks(); if (evt.getSource() == newButton) { - String texto = newBookmark.getText().trim(); + String name = newBookmark.getText().trim(); String comment = comments.getText().trim(); - if (!texto.isEmpty() && !listModel.contains(new BookmarkAndKey(texto))) { - App.get().appCase.getMultiBookmarks().newBookmark(texto); - App.get().appCase.getMultiBookmarks().setBookmarkComment(texto, comment); + if (!name.isEmpty() && !listModel.contains(new BookmarkAndKey(name))) { + multiBookmarks.newBookmark(name); + multiBookmarks.setBookmarkComment(name, comment); + multiBookmarks.setBookmarkColor(name, BookmarkColorsManager.getInitialColor(multiBookmarks.getUsedColors(), name)); + multiBookmarks.saveState(); updateList(); } list.clearSelection(); for (int i = 0; i < listModel.size(); i++) { - if (listModel.get(i).getName().equalsIgnoreCase(texto)) { + if (listModel.get(i).getName().equalsIgnoreCase(name)) { list.setSelectedIndex(i); } } @@ -424,8 +428,8 @@ public void actionPerformed(final ActionEvent evt) { int idx = list.getSelectedIndex(); if (idx != -1) { String bookmarkName = list.getModel().getElementAt(idx).getName(); - App.get().appCase.getMultiBookmarks().setBookmarkComment(bookmarkName, comments.getText()); - App.get().appCase.getMultiBookmarks().saveState(); + multiBookmarks.setBookmarkComment(bookmarkName, comments.getText()); + multiBookmarks.saveState(); } } @@ -446,10 +450,10 @@ public void actionPerformed(final ActionEvent evt) { if (result == JOptionPane.YES_OPTION) { for (int index : list.getSelectedIndices()) { String bookmark = list.getModel().getElementAt(index).getName(); - App.get().appCase.getMultiBookmarks().delBookmark(bookmark); + multiBookmarks.delBookmark(bookmark); } updateList(); - App.get().appCase.getMultiBookmarks().saveState(); + multiBookmarks.saveState(); BookmarksController.get().updateUI(); } @@ -467,9 +471,9 @@ public void actionPerformed(final ActionEvent evt) { JOptionPane.showMessageDialog(dialog, Messages.getString("BookmarksManager.AlreadyExists")); return; } - App.get().appCase.getMultiBookmarks().renameBookmark(bookmark, newBookmark); + multiBookmarks.renameBookmark(bookmark, newBookmark); updateList(bookmark, newBookmark); - App.get().appCase.getMultiBookmarks().saveState(); + multiBookmarks.saveState(); BookmarksController.get().updateUI(); } } diff --git a/iped-app/src/main/java/iped/app/ui/bookmarks/BookmarkColorsManager.java b/iped-app/src/main/java/iped/app/ui/bookmarks/BookmarkColorsManager.java new file mode 100644 index 0000000000..78bc00ac5e --- /dev/null +++ b/iped-app/src/main/java/iped/app/ui/bookmarks/BookmarkColorsManager.java @@ -0,0 +1,20 @@ +package iped.app.ui.bookmarks; + +import java.awt.Color; +import java.util.Set; + +public class BookmarkColorsManager { + public static Color getInitialColor(Set usedColors, String name) { + int off = name.toLowerCase().hashCode() % BookmarkStandardColors.numStandardColors; + Color ret = BookmarkStandardColors.colors[off]; + for (int i = 0; i < BookmarkStandardColors.numStandardColors; i++) { + int idx = (off + i) % BookmarkStandardColors.numStandardColors; + Color c = BookmarkStandardColors.colors[idx]; + if (!usedColors.contains(c)) { + ret = c; + break; + } + } + return ret; + } +} diff --git a/iped-app/src/main/java/iped/app/ui/bookmarks/BookmarkStandardColors.java b/iped-app/src/main/java/iped/app/ui/bookmarks/BookmarkStandardColors.java index 2064154251..14aaf5468d 100644 --- a/iped-app/src/main/java/iped/app/ui/bookmarks/BookmarkStandardColors.java +++ b/iped-app/src/main/java/iped/app/ui/bookmarks/BookmarkStandardColors.java @@ -46,9 +46,8 @@ public class BookmarkStandardColors { colors[33] = new Color(218, 226, 240); colors[34] = new Color(243, 239, 248); colors[35] = new Color(230, 210, 226); - colors[36] = new Color(100, 100, 100); - colors[37] = new Color(160, 160, 160); + colors[37] = new Color(200, 200, 200); colors[38] = new Color(220, 220, 220); } } diff --git a/iped-engine/src/main/java/iped/engine/data/Bookmarks.java b/iped-engine/src/main/java/iped/engine/data/Bookmarks.java index 9ab6a39255..72c7b4301c 100644 --- a/iped-engine/src/main/java/iped/engine/data/Bookmarks.java +++ b/iped-engine/src/main/java/iped/engine/data/Bookmarks.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; +import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -327,6 +328,10 @@ public Color getBookmarkColor(int bookmarkId) { return bookmarkColors.get(bookmarkId); } + public Set getUsedColors() { + return new HashSet(bookmarkColors.values()); + } + public synchronized void setBookmarkKeyStroke(int bookmarkId, KeyStroke key) { bookmarkKeyStrokes.put(bookmarkId, key); } diff --git a/iped-engine/src/main/java/iped/engine/data/MultiBookmarks.java b/iped-engine/src/main/java/iped/engine/data/MultiBookmarks.java index ae2e3ec5b6..5f439dd599 100644 --- a/iped-engine/src/main/java/iped/engine/data/MultiBookmarks.java +++ b/iped-engine/src/main/java/iped/engine/data/MultiBookmarks.java @@ -201,6 +201,14 @@ public Color getBookmarkColor(String bookmarkName) { return null; } + public Set getUsedColors() { + Set usedColors = new HashSet(); + for (IBookmarks m : map.values()) { + usedColors.addAll(m.getUsedColors()); + } + return usedColors; + } + public void setBookmarkKeyStroke(String bookmarkName, KeyStroke key) { for (IBookmarks m : map.values()) m.setBookmarkKeyStroke(m.getBookmarkId(bookmarkName), key);