JAL-4158 first attempt at stashing colours for contact matrix groups and allowing...
[jalview.git] / src / jalview / gui / TreeCanvas.java
index 7af8940..29826f0 100755 (executable)
@@ -36,7 +36,9 @@ import java.awt.print.PageFormat;
 import java.awt.print.Printable;
 import java.awt.print.PrinterException;
 import java.awt.print.PrinterJob;
+import java.util.ArrayList;
 import java.util.BitSet;
+import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
@@ -51,8 +53,10 @@ import javax.swing.ToolTipManager;
 import jalview.analysis.Conservation;
 import jalview.analysis.TreeModel;
 import jalview.api.AlignViewportI;
+import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.BinaryNode;
 import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.ContactMatrixI;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceGroup;
@@ -993,6 +997,8 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
   void colourGroups(List<BinaryNode> groups)
   {
     AlignmentPanel[] aps = getAssociatedPanels();
+    List<BitSet> colGroups = new ArrayList<>();
+    Map<BitSet,Color> colors=new HashMap();
     for (int i = 0; i < groups.size(); i++)
     {
       Color col = new Color((int) (Math.random() * 255),
@@ -1003,7 +1009,25 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
       if (!tp.isColumnWise()) {
         createSeqGroupFor(aps, l, col);
       } else {
-        markColumnsFor(aps,l,col);
+        BitSet gp=createColumnGroupFor(l,col);
+
+        colGroups.add(gp);
+        colors.put(gp, col);
+      }
+    }
+    if (tp.isColumnWise())
+    {
+      AlignmentAnnotation aa = tp.getAssocAnnotation();
+      if (aa!=null) {
+        ContactMatrixI cm = av.getContactMatrix(aa);
+        if (cm!=null)
+        {
+          cm.updateGroups(colGroups);
+          for (BitSet gp:colors.keySet())
+          {
+            cm.setColorForGroup(gp, colors.get(gp));
+          }
+        }
       }
     }
 
@@ -1056,6 +1080,29 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
     }
     return false;
   }
+  
+  private BitSet createColumnGroupFor(Vector<BinaryNode> l,
+          Color col)
+  {
+    BitSet gp=new BitSet();
+    for (BinaryNode bn:l)
+    {
+      int colm=-1;
+      if (bn.element()!=null && bn.element()instanceof Integer)
+      { colm = (Integer)bn.element();
+      } else {
+        // parse out from nodename
+      try {
+        colm = Integer.parseInt(bn.getName().substring(bn.getName().indexOf("c")+1));
+      } catch (Exception e)
+      {
+        continue;
+      }
+      }
+      gp.set(colm);
+    }
+    return gp;
+  }
 
   private void markColumnsFor(AlignmentPanel[] aps, Vector<BinaryNode> l,
           Color col)