Skip to content

Commit 97598d5

Browse files
committed
#1207: Fix to short bookmark names
1 parent 5893a37 commit 97598d5

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

iped-app/src/main/java/iped/app/ui/bookmarks/BookmarkCellRenderer.java

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ public class BookmarkCellRenderer {
1919
private static final RenderingHints renderingHints;
2020

2121
private String[] names;
22+
private String[] shortNames;
2223
private Color[] colors, foregrounds;
2324
private final Map<Integer, ClippedString[]> clippedNamesMemo = new HashMap<Integer, ClippedString[]>();
2425
private int maxSpacing;
2526
private String lastStr;
27+
private boolean lastShowShortNames;
2628

2729
static {
2830
Map<Key, Object> hints = new HashMap<Key, Object>();
@@ -44,13 +46,19 @@ public class BookmarkCellRenderer {
4446
}
4547

4648
void setBookmark(String str, Color color) {
47-
boolean strChanged = false;
48-
if (!str.equals(lastStr)) {
49+
boolean showShortNames = BookmarksManager.showShortBookmarksNames();
50+
boolean settingChanged = (showShortNames != lastShowShortNames);
51+
boolean strChanged = false;
52+
if (!str.equals(lastStr) || settingChanged) {
4953
strChanged = true;
5054
if (names == null || names.length != 1) {
5155
names = new String[1];
56+
shortNames = new String[1];
5257
}
53-
lastStr = names[0] = str;
58+
names[0] = str;
59+
shortNames[0] = transformBookmarkName(str, showShortNames);
60+
lastStr = str;
61+
lastShowShortNames = showShortNames;
5462
}
5563
if (colors == null || colors.length < names.length) {
5664
colors = new Color[1];
@@ -65,22 +73,29 @@ void setBookmark(String str, Color color) {
6573
}
6674

6775
public void setBookmarks(IMultiBookmarks bookmarks, String str) {
68-
boolean strChanged = false;
69-
if (!str.equals(lastStr)) {
76+
boolean showShortNames = BookmarksManager.showShortBookmarksNames();
77+
boolean settingChanged = (showShortNames != lastShowShortNames);
78+
boolean strChanged = false;
79+
if (!str.equals(lastStr) || settingChanged) {
7080
strChanged = true;
7181
if (str.indexOf(" | ") < 0) {
7282
if (names == null || names.length != 1) {
7383
names = new String[] { str };
84+
shortNames = new String[] { str };
7485
} else {
7586
names[0] = str;
87+
shortNames[0] = str;
7688
}
7789
} else {
7890
names = str.split(" \\| ");
91+
shortNames = new String[names.length];
7992
}
8093
for (int i = 0; i < names.length; i++) {
8194
names[i] = names[i].replace("||", "|");
95+
shortNames[i] = transformBookmarkName(names[i], showShortNames);
8296
}
8397
lastStr = str;
98+
lastShowShortNames = showShortNames;
8499
}
85100
if (colors == null || colors.length < names.length) {
86101
colors = new Color[names.length];
@@ -125,21 +140,12 @@ private synchronized ClippedString[] getClippedNames(FontMetrics fm, Graphics2D
125140
cs = new ClippedString[names.length];
126141
int x = 0;
127142
for (int i = 0; i < names.length; i++) {
128-
String s = names[i].trim();
143+
String s;
129144
if (BookmarksManager.showShortBookmarksNames()) {
130-
StringBuilder displayName = new StringBuilder();
131-
int ancestorsCount = BookmarkTree.countAncestors(s);
132-
for (int j = 0; j < ancestorsCount; j++) {
133-
displayName.append(Bookmarks.PATH_SEPARATOR_DISPLAY);
134-
}
135-
if (displayName.length() > 0) {
136-
displayName.append(" ");
137-
}
138-
displayName.append(BookmarkTree.getNameFromPath(s));
139-
s = displayName.toString();
145+
s = shortNames[i].trim();
140146
}
141147
else {
142-
s = BookmarkTree.displayPath(s);
148+
s = BookmarkTree.displayPath(names[i].trim());
143149
}
144150
Rectangle2D rc = fm.getStringBounds(s, g);
145151
int rw = (int) rc.getWidth() + 6;
@@ -224,4 +230,24 @@ private class ClippedString {
224230
this.w = w;
225231
}
226232
}
233+
234+
/**
235+
* Transforms a bookmark name to short format if needed
236+
*/
237+
private String transformBookmarkName(String name, boolean useShortNames) {
238+
if (!useShortNames) {
239+
return name;
240+
}
241+
242+
StringBuilder displayName = new StringBuilder();
243+
int ancestorsCount = BookmarkTree.countAncestors(name);
244+
for (int j = 0; j < ancestorsCount; j++) {
245+
displayName.append(Bookmarks.PATH_SEPARATOR_DISPLAY);
246+
}
247+
if (displayName.length() > 0) {
248+
displayName.append(" ");
249+
}
250+
displayName.append(BookmarkTree.getNameFromPath(name));
251+
return displayName.toString();
252+
}
227253
}

0 commit comments

Comments
 (0)