From: gmungoc Date: Thu, 31 Oct 2019 15:45:51 +0000 (+0000) Subject: JAL-3468 truncate feature description to 40 chars in tooltip/menu item X-Git-Tag: Develop-2_11_2_0-d20201215~58^2~7^2~7 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=586b87fca07c53c935ffe67827e498eb80fad4bf;p=jalview.git JAL-3468 truncate feature description to 40 chars in tooltip/menu item --- diff --git a/src/jalview/gui/JvSwingUtils.java b/src/jalview/gui/JvSwingUtils.java index d6090e2..7a5dde6 100644 --- a/src/jalview/gui/JvSwingUtils.java +++ b/src/jalview/gui/JvSwingUtils.java @@ -96,7 +96,7 @@ public final class JvSwingUtils } return (enclose ? "" : "") - + "

" + + "

" + ttext + "

" + ((enclose ? "" : "")); } diff --git a/src/jalview/gui/PopupMenu.java b/src/jalview/gui/PopupMenu.java index 415054e..187090b 100644 --- a/src/jalview/gui/PopupMenu.java +++ b/src/jalview/gui/PopupMenu.java @@ -88,6 +88,11 @@ import javax.swing.JRadioButtonMenuItem; public class PopupMenu extends JPopupMenu implements ColourChangeListener { /* + * maximum length of feature description to include in popup menu item text + */ + private static final int FEATURE_DESC_MAX = 40; + + /* * true for ID Panel menu, false for alignment panel menu */ private final boolean forIdPanel; @@ -808,7 +813,8 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener /** * A helper method to add one menu item whose action is to show details for one - * feature + * feature. The menu text includes feature description, but this may be + * truncated. * * @param details * @param seqName @@ -819,36 +825,36 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener { int start = sf.getBegin(); int end = sf.getEnd(); - String desc = null; - if (start == end) - { - desc = String.format("%s %d", sf.getType(), start); - } - else + StringBuilder desc = new StringBuilder(); + desc.append(sf.getType()).append(" ").append(String.valueOf(start)); + if (start != end) { - desc = String.format("%s %d-%d", sf.getType(), start, end); + desc.append("-").append(String.valueOf(end)); } - String tooltip = desc; String description = sf.getDescription(); if (description != null) { + desc.append(" "); description = StringUtils.stripHtmlTags(description); - if (description.length() > 12) - { - desc = desc + " " + description.substring(0, 12) + ".."; - } - else + + /* + * truncate overlong descriptions unless they contain an href + * (as truncation could leave corrupted html) + */ + boolean hasLink = description.indexOf("a href") > -1; + if (description.length() > FEATURE_DESC_MAX && !hasLink) { - desc = desc + " " + description; + description = description.substring(0, FEATURE_DESC_MAX) + "..."; } - tooltip = tooltip + " " + description; + desc.append(description); } - if (sf.getFeatureGroup() != null) + String featureGroup = sf.getFeatureGroup(); + if (featureGroup != null) { - tooltip = tooltip + (" (" + sf.getFeatureGroup() + ")"); + desc.append(" (").append(featureGroup).append(")"); } - JMenuItem item = new JMenuItem(desc); - item.setToolTipText(tooltip); + String htmlText = JvSwingUtils.wrapTooltip(true, desc.toString()); + JMenuItem item = new JMenuItem(htmlText); item.addActionListener(new ActionListener() { @Override diff --git a/src/jalview/io/SequenceAnnotationReport.java b/src/jalview/io/SequenceAnnotationReport.java index 5a55b5b..df28ea3 100644 --- a/src/jalview/io/SequenceAnnotationReport.java +++ b/src/jalview/io/SequenceAnnotationReport.java @@ -46,6 +46,8 @@ import java.util.Map; */ public class SequenceAnnotationReport { + private static final int MAX_DESCRIPTION_LENGTH = 40; + private static final String COMMA = ","; private static final String ELLIPSIS = "..."; @@ -64,7 +66,7 @@ public class SequenceAnnotationReport * Comparator to order DBRefEntry by Source + accession id (case-insensitive), * with 'Primary' sources placed before others, and 'chromosome' first of all */ - private static Comparator comparator = new Comparator() + private static Comparator comparator = new Comparator<>() { @Override @@ -206,6 +208,17 @@ public class SequenceAnnotationReport if (description != null && !description.equals(feature.getType())) { description = StringUtils.stripHtmlTags(description); + + /* + * truncate overlong descriptions unless they contain an href + * (as truncation could leave corrupted html) + */ + boolean hasLink = description.indexOf("a href") > -1; + if (description.length() > MAX_DESCRIPTION_LENGTH && !hasLink) + { + description = description.substring(0, MAX_DESCRIPTION_LENGTH) + + ELLIPSIS; + } sb.append("; ").append(description); } @@ -376,7 +389,7 @@ public class SequenceAnnotationReport if (sequence.getDescription() != null) { tmp = sequence.getDescription(); - sb.append("
").append(tmp); + sb.append(tmp); maxWidth = Math.max(maxWidth, tmp.length()); } SequenceI ds = sequence;