import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Vector;
+import java.util.stream.Collectors;
import jalview.api.AlignCalcWorkerI;
import jalview.bin.Console;
}
return false;
}
-}
+
+ // Method to get the key for a given provider value
+ public static String getProviderKey(String providerValue) {
+ for (Map.Entry<String, String> entry : Constants.STRUCTURE_PROVIDERS.entrySet()) {
+ if (entry.getValue().equals(providerValue)) {
+ return entry.getKey(); // Return the key (abbreviation) for the matching provider value
+ }
+ }
+ return null; // Return null if no match is found
+ }
+
+ public static String reduceLabelLength(String label) {
+ // Split the input by " | "
+ String[] parts = label.split(" \\| ");
+
+ // Map the full names to their abbreviations
+ String reducedLabel = Arrays.stream(parts)
+ .map(fullName -> Constants.STRUCTURE_PROVIDERS.entrySet().stream()
+ .filter(entry -> entry.getValue().equals(fullName))
+ .map(Map.Entry::getKey)
+ .findFirst()
+ .orElse(fullName)) // Use fullName if no abbreviation is found
+ .collect(Collectors.joining(" | "));
+
+ return reducedLabel; // Return the reduced label if abbreviations were applied
+ }
+}
\ No newline at end of file
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
+import jalview.analysis.AlignmentUtils;
import jalview.analysis.Conservation;
import jalview.analysis.TreeModel;
import jalview.api.AlignViewportI;
int offy;
private float threshold;
+
+ int labelLengthThreshold = 4;
String longestName;
BinaryNode highlightNode;
boolean applyToAllViews = false;
+
+ //Map to store label positions (BinaryNode -> List of bounding rectangles for the label)
+ private Map<BinaryNode, List<Rectangle>> labelBoundsMap = new HashMap<>();
/**
* Creates a new TreeCanvas object.
*/
public void drawNode(Graphics g, BinaryNode node, double chunk,
double wscale, int width, int offx, int offy)
- {
+ {
if (node == null)
{
return;
}
if (node.hasLabel())
{
- nodeLabel = node.getLabel() + " " + nodeLabel;
+ String label = node.getLabel();
+
+ if(label.length() > labelLengthThreshold) {
+
+ //label = AlignmentUtils.reduceLabelLength(label);
+ }
+
+ nodeLabel = label + " | " + nodeLabel;
+
+ // Split the nodeLabel by "|"
+ String[] lines = nodeLabel.split("\\|");
+
+ // Iterate over the lines and draw each line separately
+ String longestLabelString = "";
+ int i = 0;
+ for (i = 0; i < lines.length; i++) {
+ g.drawString(lines[i].trim(), xstart + 2, ypos - 2 - (i * fm.getHeight()));
+ if(longestLabelString.length() < lines[i].trim().length()) {
+ longestLabelString = lines[i].trim();
+ }
+ }
+
+ int labelWidth = fm.stringWidth(longestLabelString);
+ int labelHeight = fm.getHeight() * (i-1);
+
+ // Calculate the bounding box of the string
+ int xLabelPos = xstart + 2;
+ int yLabelPos = ypos - 2;
+ Rectangle labelBounds = new Rectangle(xLabelPos, yLabelPos - labelHeight, labelWidth, labelHeight);
+
+ // Add the bounding box to the map for this node (list allows multiple bounding boxes)
+ labelBoundsMap.computeIfAbsent(node, k -> new ArrayList<>()).add(labelBounds);
}
- if (!nodeLabel.equals(""))
+ else if (!nodeLabel.equals(""))
{
g.drawString(nodeLabel, xstart + 2, ypos - 2);
}
av.setCurrentTree(tree);
Object ob = findElement(evt.getX(), evt.getY());
+
+ // Get mouse coordinates
+ int mouseX = evt.getX();
+ int mouseY = evt.getY();
if (ob instanceof BinaryNode)
{
}
else
- {
+ {
+
if (highlightNode != null)
{
highlightNode = null;
setToolTipText(null);
repaint();
}
+
+ // Iterate through the map of label bounding boxes
+ for (Map.Entry<BinaryNode, List<Rectangle>> entry : labelBoundsMap.entrySet()) {
+ BinaryNode node = entry.getKey();
+ List<Rectangle> boundsList = entry.getValue();
+
+ // Check each bounding box for this node
+ for (Rectangle labelBounds : boundsList) {
+ if (labelBounds.contains(mouseX, mouseY)) {
+ // Show tooltip for this node's label
+ String nodeLabel = node.getDisplayName();
+ this.setToolTipText(nodeLabel);
+ repaint();
+ return; // Exit once we find a matching label
+ }
+ }
+ }
+ // Clear tooltip if no label is hovered
+ setToolTipText(null);
+ repaint();
+
}
}
gatherLabelsTo(groups.get(i), l);
if (!tp.isColumnWise())
{
- createSeqGroupFor(aps, l, col);
+ createSeqGroupFor(aps, l, col, groups.get(i).getLabel());
}
else
{
sb.append(" | ");
}
first = false;
+// if(labelsForNode.size()>1) {
+// String providerAbbreviation = AlignmentUtils.getProviderKey(label);
+// sb.append(providerAbbreviation);
+// }
sb.append(label);
+
}
binaryNode.setLabel(sb.toString());
}
}
public void createSeqGroupFor(AlignmentPanel[] aps, Vector<BinaryNode> l,
- Color col)
+ Color col, String label)
{
Vector<SequenceI> sequences = new Vector<>();
ColourSchemeI cs = null;
SequenceGroup _sg = new SequenceGroup(sequences, null, cs, true, true,
false, 0, av.getAlignment().getWidth() - 1);
-
- _sg.setName("JTreeGroup:" + _sg.hashCode());
+
+ if(label != null && !label.isEmpty()) {
+ label = AlignmentUtils.reduceLabelLength(label);
+ _sg.setName("JTreeGroup:" + label);
+ }
+ else {
+ _sg.setName("JTreeGroup:" + _sg.hashCode());
+ }
_sg.setIdColour(col);
for (int a = 0; a < aps.length; a++)