Skip to content

Commit

Permalink
'#1866: Set an initial color based on name for new bookmarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
wladimirleite committed Sep 10, 2023
1 parent 0d240a1 commit 22f15d4
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 15 deletions.
2 changes: 2 additions & 0 deletions iped-api/src/main/java/iped/data/IBookmarks.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public interface IBookmarks extends Serializable {

Color getBookmarkColor(int bookmarkId);

Set<Color> getUsedColors();

void setBookmarkKeyStroke(int bookmarkId, KeyStroke key);

KeyStroke getBookmarkKeyStroke(int bookmarkId);
Expand Down
4 changes: 3 additions & 1 deletion iped-api/src/main/java/iped/data/IMultiBookmarks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Color> getUsedColors();

boolean isInReport(String bookmark);

void setInReport(String bookmark, boolean checked);
Expand Down
28 changes: 16 additions & 12 deletions iped-app/src/main/java/iped/app/ui/BookmarksManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -403,18 +405,20 @@ private void includeDuplicates(ArrayList<IItemId> 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);
}
}
Expand All @@ -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();
}
}

Expand All @@ -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();

}
Expand All @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Color> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions iped-engine/src/main/java/iped/engine/data/Bookmarks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -327,6 +328,10 @@ public Color getBookmarkColor(int bookmarkId) {
return bookmarkColors.get(bookmarkId);
}

public Set<Color> getUsedColors() {
return new HashSet<Color>(bookmarkColors.values());
}

public synchronized void setBookmarkKeyStroke(int bookmarkId, KeyStroke key) {
bookmarkKeyStrokes.put(bookmarkId, key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ public Color getBookmarkColor(String bookmarkName) {
return null;
}

public Set<Color> getUsedColors() {
Set<Color> usedColors = new HashSet<Color>();
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);
Expand Down

0 comments on commit 22f15d4

Please sign in to comment.