--- /dev/null
+package jalview.ext.treeviewer;
+
+import jalview.datamodel.SequenceI;
+import jalview.util.MessageManager;
+import jalview.viewmodel.AlignmentViewport;
+
+import java.awt.Dimension;
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Static class containing generic methods for communicating between the tree
+ * viewer frame and Jalview, currently includes adding the frame to the Jalview
+ * Desktop and triggering an association between an {@link AlignmentViewport}
+ * and {@link TreeFrameI}.
+ *
+ * @author kjvanderheide
+ *
+ */
+public final class TreeViewerUtils
+{
+ /**
+ * All tree viewers should be added to this map so that Jalview saves them to
+ * its project.
+ */
+ private static Map<TreeFrameI, TreeViewerBindingI> activeViews = new HashMap<>();
+
+ /**
+ * Adds the given tree frame to the Jalview {@link Desktop}.
+ *
+ * @param treeFrame
+ * @return the same tree frame but now as part of Jalview.
+ */
+ public static TreeFrameI addTreeViewFrameToJalview(
+ final TreeFrameI treeFrame, int width, int height)
+ {
+ treeFrame.setMinimumSize(new Dimension(width, height));
+ // aptxApp.setFont(Desktop.instance.getFont());
+ // aptxApp.getMainPanel().setFont(Desktop.instance.getFont());
+ String frameTitle = MessageManager.getString("label.aptx_title");
+ File treeFile = treeFrame.getTreePanel().getTreeFile();
+ if (treeFile != null)
+ {
+ frameTitle += MessageManager.formatMessage("label.of_x",
+ new String[]
+ { treeFile.getAbsolutePath() });
+ }
+ // Usually redundant as this is done when associating nodes as well but that
+ // step could be bypassed
+ activeViews.put(treeFrame, treeFrame.getViewBinding());
+
+ treeFrame.addFrameToJalview(frameTitle, true, width, height, true,
+ false);
+ treeFrame.getTreeControls().displayEntireTree();
+ return treeFrame;
+
+ }
+
+ /**
+ * Convience method for building the association between Jalview's alignment
+ * and the tree frame.
+ *
+ * @param treeFrame
+ * @param jalviewAlignViewport
+ * @param alignMappedToNodes
+ * @param nodesMappedToAlign
+ * @return
+ */
+ public static TreeViewerBindingI associateNodesWithJalviewSequences(
+ final TreeFrameI treeFrame,
+ final AlignmentViewport jalviewAlignViewport,
+ final Map<SequenceI, TreeNodeI> alignMappedToNodes,
+ final Map<TreeNodeI, SequenceI> nodesMappedToAlign)
+ {
+ TreeViewerBindingI treeBinding = new JalviewBinding(treeFrame,
+ jalviewAlignViewport,
+ alignMappedToNodes, nodesMappedToAlign);
+ jalviewAlignViewport.setCurrentExtTree(treeFrame.getTree());
+ activeViews.put(treeFrame, treeFrame.getViewBinding());
+
+ return treeBinding;
+ }
+
+ public static Map<TreeFrameI, TreeViewerBindingI> getActiveTreeViews()
+ {
+ return activeViews;
+ }
+
+}