• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanodash / 28444877373

30 Jun 2026 12:38PM UTC coverage: 28.035% (-0.01%) from 28.046%
28444877373

push

github

web-flow
Merge pull request #522 from knowledgepixels/feat/truncate-entity-link-labels

feat: truncate over-long entity labels in links, buttons, and breadcrumbs

1723 of 7007 branches covered (24.59%)

Branch coverage included in aggregate %.

3607 of 12005 relevant lines covered (30.05%)

4.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.28
src/main/java/com/knowledgepixels/nanodash/component/TitleBar.java
1
package com.knowledgepixels.nanodash.component;
2

3
import java.io.Serializable;
4
import java.util.ArrayList;
5
import java.util.List;
6

7
import org.apache.wicket.behavior.AttributeAppender;
8
import org.apache.wicket.markup.html.WebMarkupContainer;
9
import org.apache.wicket.markup.html.WebPage;
10
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
11
import org.apache.wicket.markup.html.panel.EmptyPanel;
12
import org.apache.wicket.markup.html.panel.Panel;
13
import org.apache.wicket.markup.repeater.Item;
14
import org.apache.wicket.markup.repeater.data.DataView;
15
import org.apache.wicket.markup.repeater.data.ListDataProvider;
16

17
import com.knowledgepixels.nanodash.NanodashPageRef;
18
import com.knowledgepixels.nanodash.NanodashPreferences;
19
import com.knowledgepixels.nanodash.Utils;
20
import com.knowledgepixels.nanodash.page.NanodashPage;
21
import com.knowledgepixels.nanodash.page.PublishPage;
22
import com.knowledgepixels.nanodash.page.QueryListPage;
23
import com.knowledgepixels.nanodash.page.SpaceListPage;
24
import com.knowledgepixels.nanodash.page.UserListPage;
25

26
/**
27
 * TitleBar is the top bar of the Nanodash application, which contains
28
 * navigation elements such as profile, my channel, users, connectors,
29
 * publish, query, and breadcrumb navigation.
30
 */
31
public class TitleBar extends Panel {
32

33
    private String highlight;
34

35
    /**
36
     * The breadcrumb/tab strip row (the grey band just below the nav bar). Holds
37
     * the breadcrumb links on the left and, optionally, the resource tab strip on
38
     * the right (see {@link #setTabs(ResourceTabs)}).
39
     */
40
    private WebMarkupContainer breadcrumbPath;
41

42
    /**
43
     * Constructs a TitleBar with the specified id, page, highlight element,
44
     * and an array of path references for breadcrumb navigation.
45
     *
46
     * @param id        the component id
47
     * @param page      the current Nanodash page
48
     * @param highlight the id of the element to highlight
49
     * @param pathRefs  an array of NanodashPageRef for breadcrumb navigation
50
     */
51
    public TitleBar(String id, NanodashPage page, String highlight, NanodashPageRef... pathRefs) {
52
        super(id);
9✔
53
        this.highlight = highlight;
9✔
54
        add(new ProfileItem("profile", page));
39✔
55
        // Centered title-bar message: the post-publish confirmation and/or the
56
        // always-on "you haven't published an introduction yet" warning.
57
        add(new JustPublishedMessagePanel("justPublishedMessage", page.getPageParameters()));
42✔
58

59
        createNavLink("users", UserListPage.class);
15✔
60
        createNavLink("connectors", SpaceListPage.class);
15✔
61
        createNavLink("publish", PublishPage.class).setVisible(!NanodashPreferences.get().isReadOnlyMode());
33!
62
        createNavLink("query", QueryListPage.class);
15✔
63

64
        breadcrumbPath = new WebMarkupContainer("breadcrumbpath");
18✔
65
        WebMarkupContainer breadcrumbLinks = new WebMarkupContainer("breadcrumblinks");
15✔
66
        List<CrumbPart> crumbParts = buildCrumbParts(pathRefs);
9✔
67
        if (!crumbParts.isEmpty()) {
9!
68
            CrumbPart first = crumbParts.get(0);
×
69
            breadcrumbLinks.add(first.ref().createComponent("firstpathelement", first.label()));
×
70
            // Getting serialization exception if not using 'new ArrayList<...>(...)' here:
71
            List<CrumbPart> moreParts = new ArrayList<CrumbPart>(crumbParts.subList(1, crumbParts.size()));
×
72
            breadcrumbLinks.add(new DataView<CrumbPart>("morepathelements", new ListDataProvider<CrumbPart>(moreParts)) {
×
73

74
                @Override
75
                protected void populateItem(Item<CrumbPart> item) {
76
                    CrumbPart part = item.getModelObject();
×
77
                    item.add(part.ref().createComponent("furtherpathelement", part.label()));
×
78
                }
×
79

80
            });
81
        } else {
×
82
            breadcrumbLinks.setVisible(false);
12✔
83
        }
84
        breadcrumbPath.add(breadcrumbLinks);
30✔
85
        // Tab strip placeholder (right side of the strip); replaced via setTabs().
86
        breadcrumbPath.add(new EmptyPanel("tabs").setVisible(false));
45✔
87
        // The strip shows when there is a breadcrumb to display; setTabs() also
88
        // forces it visible so a tab strip shows even without a breadcrumb.
89
        breadcrumbPath.setVisible(pathRefs.length > 0);
24!
90
        add(breadcrumbPath);
30✔
91
    }
3✔
92

93
    /**
94
     * One breadcrumb segment: the {@link NanodashPageRef} it links to and the
95
     * (possibly split) label text to show for it.
96
     */
97
    public record CrumbPart(NanodashPageRef ref, String label) implements Serializable {
27✔
98
    }
99

100
    /**
101
     * Flattens a breadcrumb path into the segments to render. Three transforms are
102
     * applied to each ref's label:
103
     * <ul>
104
     *   <li>For each non-root crumb, the part it shares with its parent label
105
     *       is stripped (e.g. parent "Knowledge Pixels", child "Knowledge
106
     *       Pixels Incubator" renders as "Incubator"). The shared part is the
107
     *       longest common character prefix, but only stripped when it covers
108
     *       (almost) all of the parent and ends on a non-letter/digit boundary
109
     *       in the child — this also catches singular/plural variations like
110
     *       parent "Nano Sessions", child "Nano Session #30" → "#30".</li>
111
     *   <li>For each non-root crumb, a word-level overlap where the child's
112
     *       leading word(s) restate the parent's trailing word(s) is stripped
113
     *       (e.g. parent "Abc Def Ghi", child "Ghi Jkl" renders as "Jkl"). The
114
     *       longest such parent-suffix/child-prefix overlap wins, matched on
115
     *       whole words case-insensitively, and never strips the whole child
116
     *       away.</li>
117
     *   <li>Only the leading segment of the remaining label is kept — the part
118
     *       before the first list/title separator ({@code ,}, {@code :},
119
     *       {@code ;}, {@code |}, or a spaced {@code -}) — so each path level
120
     *       renders as one concise crumb. E.g. "FIP.38.T.8 | FAIR Implementation
121
     *       Profile Training Session 8" renders as "FIP.38.T.8" and "3PFF: the
122
     *       Three Point FAIRification Framework" as "3PFF".</li>
123
     * </ul>
124
     * The parent-prefix comparison uses the original (un-simplified) labels.
125
     */
126
    static List<CrumbPart> buildCrumbParts(NanodashPageRef[] pathRefs) {
127
        List<CrumbPart> parts = new ArrayList<>();
12✔
128
        for (int i = 0; i < pathRefs.length; i++) {
24✔
129
            String label = pathRefs[i].getLabel();
15✔
130
            if (label == null) {
6!
131
                parts.add(new CrumbPart(pathRefs[i], null));
×
132
                continue;
×
133
            }
134
            if (i > 0) {
6✔
135
                String parentLabel = pathRefs[i - 1].getLabel();
21✔
136
                label = stripParentPrefix(parentLabel, label);
12✔
137
                label = stripParentOverlap(parentLabel, label);
12✔
138
            }
139
            // Show only the leading segment (before the first list/title separator)
140
            // so each path level renders as one concise crumb — e.g.
141
            // "FIP.38.T.8 | FAIR Implementation Profile Training Session 8" → "FIP.38.T.8",
142
            // "3PFF: the Three Point FAIRification Framework" → "3PFF".
143
            List<String> segments = splitLabel(label);
9✔
144
            if (!segments.isEmpty()) {
9!
145
                parts.add(new CrumbPart(pathRefs[i], truncateLabel(segments.get(0))));
42✔
146
            }
147
        }
148
        return parts;
6✔
149
    }
150

151
    /**
152
     * Truncates an over-long crumb label so a single crumb never blows up the
153
     * breadcrumb strip. Delegates to {@link Utils#truncateLinkLabel(String)} so
154
     * breadcrumbs and entity links share one truncation rule.
155
     */
156
    static String truncateLabel(String label) {
157
        return Utils.truncateLinkLabel(label);
9✔
158
    }
159

160
    /**
161
     * Splits a label on list/title separators — comma, colon, semicolon, pipe
162
     * (with or without surrounding spaces), or a space-surrounded hyphen — into
163
     * trimmed, non-empty segments. Returns the trimmed whole label if there is
164
     * no separator (so the result always has at least one element).
165
     */
166
    static List<String> splitLabel(String label) {
167
        List<String> segments = new ArrayList<>();
12✔
168
        if (label == null) return segments;
6!
169
        for (String s : label.split("\\s*[,;:]\\s+|\\s*\\|\\s*|\\s+-\\s+")) {
54✔
170
            String trimmed = s.trim();
9✔
171
            if (!trimmed.isEmpty()) segments.add(trimmed);
21!
172
        }
173
        if (segments.isEmpty()) {
9!
174
            String trimmed = label.trim();
×
175
            if (!trimmed.isEmpty()) segments.add(trimmed);
×
176
        }
177
        return segments;
6✔
178
    }
179

180
    /**
181
     * If the child label appears to restate the parent's "topic" at the start,
182
     * strips that shared prefix and returns the remainder. Otherwise returns
183
     * the child label unchanged.
184
     */
185
    private static String stripParentPrefix(String parent, String child) {
186
        if (parent == null || parent.isEmpty() || child == null) return child;
21!
187
        int lcp = 0;
6✔
188
        int max = Math.min(parent.length(), child.length());
18✔
189
        while (lcp < max && parent.charAt(lcp) == child.charAt(lcp)) lcp++;
36✔
190
        // Require a substantial match: at least 3 chars, and within 2 chars of
191
        // the full parent length (so things like "Sessions" vs "Session" still
192
        // match, but "Nanopublication Sessions" vs "Nano Session #30" doesn't).
193
        if (lcp < 3 || lcp < parent.length() - 2) return child;
33!
194
        // The boundary in the child must not fall mid-word, otherwise we'd be
195
        // chopping off a partial word like "Foo Bar" -> "Foo Baz Quux" -> "z Quux".
196
        if (lcp >= child.length() || Character.isLetterOrDigit(child.charAt(lcp))) return child;
27!
197
        String remainder = child.substring(lcp).replaceAll("^\\s+", "");
21✔
198
        if (remainder.isEmpty()) return child;
9!
199
        return remainder;
6✔
200
    }
201

202
    /**
203
     * If the child label's leading word(s) restate the parent label's trailing
204
     * word(s), strips that overlap and returns the remainder. E.g. parent
205
     * "Abc Def Ghi", child "Ghi Jkl" → "Jkl", or parent "Knowledge Pixels
206
     * Incubator Office Hours", child "Office Hour 24 June 2026" → "24 June 2026".
207
     * The overlap is matched on whole words, case-insensitively and tolerating
208
     * singular/plural variation ("Hours" ≈ "Hour"); the longest parent-suffix that
209
     * matches a child-prefix wins. Returns the child unchanged when there is no
210
     * overlap, or when the overlap would consume the whole child label (at least
211
     * one word is always kept).
212
     */
213
    static String stripParentOverlap(String parent, String child) {
214
        if (parent == null || parent.isEmpty() || child == null || child.isEmpty()) return child;
36!
215
        String[] parentWords = parent.trim().split("\\s+");
15✔
216
        String[] childWords = child.trim().split("\\s+");
15✔
217
        // Keep at least one child word, so the overlap can cover at most all but
218
        // the last child word.
219
        int maxK = Math.min(parentWords.length, childWords.length - 1);
24✔
220
        for (int k = maxK; k >= 1; k--) {
21✔
221
            boolean match = true;
6✔
222
            for (int j = 0; j < k; j++) {
21✔
223
                if (!wordsEquivalent(parentWords[parentWords.length - k + j], childWords[j])) {
39✔
224
                    match = false;
6✔
225
                    break;
3✔
226
                }
227
            }
228
            if (match) {
6✔
229
                return String.join(" ", java.util.Arrays.asList(childWords).subList(k, childWords.length));
27✔
230
            }
231
        }
232
        return child;
6✔
233
    }
234

235
    /**
236
     * Whether two breadcrumb words count as the same topic word: equal ignoring
237
     * case, or equal once a single trailing plural "s" is dropped from each (so
238
     * "Hours" matches "Hour", "Sessions" matches "Session").
239
     */
240
    private static boolean wordsEquivalent(String a, String b) {
241
        if (a.equalsIgnoreCase(b)) return true;
18✔
242
        return depluralize(a).equalsIgnoreCase(depluralize(b));
18✔
243
    }
244

245
    private static String depluralize(String word) {
246
        return word.length() > 1 && (word.endsWith("s") || word.endsWith("S"))
39!
247
                ? word.substring(0, word.length() - 1)
24✔
248
                : word;
3✔
249
    }
250

251
    /**
252
     * Places a resource tab strip (Content | About | Raw) on the right side of
253
     * the breadcrumb strip, and forces the strip visible so the tabs show even on
254
     * pages without a breadcrumb (e.g. user pages). Returns {@code this} for
255
     * fluent use at the {@code add(...)} call site.
256
     *
257
     * @param tabs the tab strip (its markup id must be {@code "tabs"})
258
     * @return this TitleBar
259
     */
260
    public TitleBar setTabs(ResourceTabs tabs) {
261
        breadcrumbPath.addOrReplace(tabs);
×
262
        breadcrumbPath.setVisible(true);
×
263
        return this;
×
264
    }
265

266
    private BookmarkablePageLink<Void> createNavLink(String id, Class<? extends WebPage> pageClass) {
267
        BookmarkablePageLink<Void> link = new BookmarkablePageLink<>(id, pageClass);
18✔
268
        if (id.equals(highlight)) {
15!
269
            link.add(new AttributeAppender("class", "selected"));
×
270
        }
271
        add(link);
27✔
272
        return link;
6✔
273
    }
274

275
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc