Feature rendering in groups
authoramwaterhouse <Andrew Waterhouse>
Thu, 16 Mar 2006 15:13:43 +0000 (15:13 +0000)
committeramwaterhouse <Andrew Waterhouse>
Thu, 16 Mar 2006 15:13:43 +0000 (15:13 +0000)
src/jalview/appletgui/FeatureRenderer.java
src/jalview/appletgui/FeatureSettings.java

index 21c401a..0b504be 100755 (executable)
@@ -45,7 +45,6 @@ public class FeatureRenderer
 \r
     String [] renderOrder;\r
 \r
-    boolean drawText = true;\r
     FontMetrics fm;\r
     int charOffset;\r
 \r
@@ -85,11 +84,11 @@ public class FeatureRenderer
 \r
       return new Color( ((Integer)currentColour).intValue() );\r
     }\r
+\r
     /**\r
      * This is used by the Molecule Viewer to get the accurate colour\r
      * of the rendered sequence\r
      */\r
-\r
     boolean overview = false;\r
 \r
     int white = Color.white.getRGB();\r
@@ -152,12 +151,12 @@ public class FeatureRenderer
         return;\r
 \r
 \r
-      if(g!=null)\r
-        fm = g.getFontMetrics();\r
-\r
       if (av.featuresDisplayed == null || renderOrder==null)\r
        {\r
          findAllFeatures();\r
+         if(av.featuresDisplayed.size()<1)\r
+           return;\r
+\r
          sequenceFeatures = seq.getSequenceFeatures();\r
          sfSize = sequenceFeatures.length;\r
        }\r
@@ -171,11 +170,15 @@ public class FeatureRenderer
       {\r
         spos = lastSequence.findPosition(start);\r
         epos = lastSequence.findPosition(end);\r
+        if(g!=null)\r
+          fm = g.getFontMetrics();\r
       }\r
       String type;\r
       for(int renderIndex=0; renderIndex<renderOrder.length; renderIndex++)\r
        {\r
         type =  renderOrder[renderIndex];\r
+        if(!av.featuresDisplayed.containsKey(type))\r
+          continue;\r
 \r
         // loop through all features in sequence to find\r
         // current feature to render\r
@@ -184,7 +187,10 @@ public class FeatureRenderer
           if (!sequenceFeatures[sfindex].type.equals(type))\r
             continue;\r
 \r
-          if (featureGroups != null\r
+          if(sequenceFeatures[sfindex]==null)\r
+          {\r
+          }\r
+          else  if (featureGroups != null\r
               && sequenceFeatures[sfindex].featureGroup != null\r
               &&\r
               featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)\r
@@ -285,6 +291,7 @@ public class FeatureRenderer
     void findAllFeatures()\r
     {\r
       av.featuresDisplayed = new Hashtable();\r
+      Vector allfeatures = new Vector();\r
       for (int i = 0; i < av.alignment.getHeight(); i++)\r
       {\r
         SequenceFeature [] features = av.alignment.getSequenceAt(i).getSequenceFeatures();\r
@@ -299,18 +306,19 @@ public class FeatureRenderer
           {\r
             av.featuresDisplayed.put(features[index].getType(),\r
                                   new Integer( getColour(features[index].getType()).getRGB()) );\r
+            allfeatures.addElement(features[index].getType());\r
           }\r
           index++;\r
         }\r
       }\r
 \r
-      renderOrder = new String[av.featuresDisplayed.size()];\r
-      Enumeration en = av.featuresDisplayed.keys();\r
-      int i = 0;\r
+      renderOrder = new String[allfeatures.size()];\r
+      Enumeration en = allfeatures.elements();\r
+      int i = allfeatures.size()-1;\r
       while(en.hasMoreElements())\r
       {\r
         renderOrder[i] = en.nextElement().toString();\r
-        i++;\r
+        i--;\r
       }\r
     }\r
 \r
@@ -337,26 +345,26 @@ public class FeatureRenderer
 \r
     public void setFeaturePriority(Object [][] data)\r
     {\r
-      // The feature table will display high priority\r
-      // features at the top, but theses are the ones\r
-      // we need to render last, so invert the data\r
-      av.featuresDisplayed.clear();\r
-\r
-      renderOrder = new String[data.length];\r
+        // The feature table will display high priority\r
+        // features at the top, but theses are the ones\r
+        // we need to render last, so invert the data\r
+        if(av.featuresDisplayed!=null)\r
+          av.featuresDisplayed.clear();\r
 \r
-      if(data.length>0)\r
-      for(int i=0; i<data.length; i++)\r
-      {\r
-       String type = data[i][0].toString();\r
-       setColour(type, (Color)data[i][1]);\r
-       if( ((Boolean)data[i][2]).booleanValue() )\r
-       {\r
-         av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));\r
-       }\r
+        renderOrder = new String[data.length];\r
 \r
-       renderOrder[i] = type;\r
-      }\r
+        if (data.length > 0)\r
+          for (int i = 0; i < data.length; i++)\r
+          {\r
+            String type = data[i][0].toString();\r
+            setColour(type, (Color) data[i][1]);\r
+            if ( ( (Boolean) data[i][2]).booleanValue())\r
+            {\r
+              av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));\r
+            }\r
 \r
+            renderOrder[data.length - i - 1] = type;\r
+          }\r
     }\r
 \r
     Hashtable featureColours = new Hashtable();\r
index 20e4a4d..6710ed4 100755 (executable)
@@ -27,13 +27,14 @@ import java.awt.event.*;
 public class FeatureSettings extends Panel implements ItemListener,\r
     MouseListener, MouseMotionListener\r
 {\r
-  final FeatureRenderer fr;\r
-  final AlignmentPanel ap;\r
-  final AlignViewport av;\r
-  final Frame frame;\r
+  FeatureRenderer fr;\r
+  AlignmentPanel ap;\r
+  AlignViewport av;\r
+  Frame frame;\r
   Panel groupPanel;\r
   Panel featurePanel = new Panel();\r
   ScrollPane scrollPane;\r
+  boolean alignmentHasFeatures = false;\r
 \r
   public FeatureSettings(AlignViewport av, final AlignmentPanel ap)\r
   {\r
@@ -46,91 +47,106 @@ public class FeatureSettings extends Panel implements ItemListener,
     this.setLayout(new BorderLayout());\r
     scrollPane = new ScrollPane();\r
     scrollPane.add(featurePanel);\r
-    add(scrollPane, BorderLayout.CENTER);\r
+    if (alignmentHasFeatures)\r
+      add(scrollPane, BorderLayout.CENTER);\r
+\r
     if(groupPanel!=null)\r
     {\r
       groupPanel.setLayout(\r
-          new GridLayout(groupPanel.getComponentCount() / 4, 4));\r
+          new GridLayout( fr.featureGroups.size() / 4 + 1, 4));\r
       groupPanel.validate();\r
 \r
       add(groupPanel, BorderLayout.NORTH);\r
     }\r
     frame = new Frame();\r
     frame.add(this);\r
-    int height = featurePanel.getComponentCount()*50 ;\r
-    if (height>400)\r
-      height = 400;\r
+    int height = featurePanel.getComponentCount()*50 +30;\r
+\r
+    height = Math.max(100, height);\r
+    height = Math.min(400, height);\r
 \r
-    jalview.bin.JalviewLite.addFrame(frame, "Feature Settings", 250,\r
+    jalview.bin.JalviewLite.addFrame(frame, "Feature Settings", 280,\r
                                      height);\r
   }\r
 \r
+  public void paint(Graphics g)\r
+  {\r
+    g.setColor(Color.black);\r
+    g.drawString("No Features added to this alignment!!", 10, 20);\r
+  }\r
+\r
   void setTableData()\r
   {\r
+    alignmentHasFeatures = false;\r
+\r
     if(fr.featureGroups==null)\r
       fr.featureGroups = new Hashtable();\r
-    else\r
-      fr.featureGroups.clear();\r
 \r
     Vector allFeatures = new Vector();\r
+    Vector allGroups = new Vector();\r
     SequenceFeature[] tmpfeatures;\r
+    String group;\r
+\r
 \r
     for (int i = 0; i < av.alignment.getHeight(); i++)\r
     {\r
       if (av.alignment.getSequenceAt(i).getSequenceFeatures() == null)\r
         continue;\r
 \r
+      alignmentHasFeatures = true;\r
+\r
       tmpfeatures = av.alignment.getSequenceAt(i).getSequenceFeatures();\r
       int index = 0;\r
       while (index < tmpfeatures.length)\r
       {\r
-        if(tmpfeatures[index].getFeatureGroup()!=null\r
-           && tmpfeatures[index].getFeatureGroup().length()>0\r
-           && !fr.featureGroups.containsKey(tmpfeatures[index].getFeatureGroup()))\r
+        if(tmpfeatures[index].getFeatureGroup()!=null)\r
         {\r
-          fr.featureGroups.put(tmpfeatures[index].getFeatureGroup(), new Boolean(true));\r
-          if(groupPanel==null)\r
-          {\r
-            groupPanel = new Panel();\r
-          }\r
-\r
-          Checkbox check = new Checkbox(tmpfeatures[index].getFeatureGroup(), true);\r
-          check.setFont(new Font("Serif", Font.BOLD, 12));\r
-          check.addItemListener(this);\r
-          groupPanel.add(check);\r
+          group = tmpfeatures[index].featureGroup;\r
+          if(!allGroups.contains(group))\r
+           {\r
+             allGroups.addElement(group);\r
+\r
+             boolean visible = true;\r
+             if (fr.featureGroups.containsKey(group))\r
+             {\r
+               visible = ( (Boolean) fr.featureGroups.get(group)).booleanValue();\r
+             }\r
+\r
+             fr.featureGroups.put(group, new Boolean(visible));\r
+\r
+             if (groupPanel == null)\r
+             {\r
+               groupPanel = new Panel();\r
+             }\r
+\r
+             Checkbox check = new Checkbox(group, visible);\r
+             check.setFont(new Font("Serif", Font.BOLD, 12));\r
+             check.addItemListener(this);\r
+             groupPanel.add(check);\r
+           }\r
         }\r
 \r
         if (!allFeatures.contains(tmpfeatures[index].getType()))\r
         {\r
             allFeatures.addElement(tmpfeatures[index].getType());\r
         }\r
-\r
-\r
-        index ++;\r
+         index ++;\r
       }\r
     }\r
 \r
-    resetTable();\r
-    featurePanel.setLayout(new GridLayout(allFeatures.size(), 2, 10,5));\r
-    featurePanel.validate();\r
+    resetTable(false);\r
   }\r
 \r
-  void resetTable()\r
+ //This routine adds and removes checkboxes depending on\r
+ //Group selection states\r
+  void resetTable(boolean groupsChanged)\r
   {\r
-    featurePanel.removeAll();\r
-    Enumeration groups = fr.featureGroups.keys();\r
     SequenceFeature [] tmpfeatures;\r
-    String group, type;\r
-    Vector checksAdded = new Vector();\r
-\r
-    while(groups.hasMoreElements())\r
-    {\r
-     group = groups.nextElement().toString();\r
-     if ( !( (Boolean) fr.featureGroups.get(group)).booleanValue())\r
-       continue;\r
+    String group=null, type;\r
+    Vector visibleChecks = new Vector();\r
 \r
      for (int i = 0; i < av.alignment.getHeight(); i++)\r
-       {\r
+     {\r
          if (av.alignment.getSequenceAt(i).getSequenceFeatures() == null)\r
            continue;\r
 \r
@@ -138,28 +154,105 @@ public class FeatureSettings extends Panel implements ItemListener,
          int index = 0;\r
          while (index < tmpfeatures.length)\r
          {\r
-           if (tmpfeatures[index].getFeatureGroup() != null &&\r
-               tmpfeatures[index].getFeatureGroup().equals(group))\r
+           group = tmpfeatures[index].featureGroup;\r
+\r
+           if (group==null || fr.featureGroups.get(group)==null ||\r
+               ((Boolean) fr.featureGroups.get(group)).booleanValue())\r
            {\r
              type = tmpfeatures[index].getType();\r
-             if(!checksAdded.contains(type))\r
+             if(!visibleChecks.contains(type) )\r
              {\r
-               checksAdded.addElement(type);\r
-               Checkbox check = new Checkbox(type, true);\r
-               check.addMouseListener(this);\r
-               check.addMouseMotionListener(this);\r
-               check.setBackground(fr.getColour(type));\r
-               check.addItemListener(this);\r
-               featurePanel.add(check);\r
+               visibleChecks.addElement(type);\r
              }\r
            }\r
            index++;\r
          }\r
      }\r
-   }\r
+\r
+       Component[] comps;\r
+       int cSize = featurePanel.getComponentCount();\r
+       Checkbox check;\r
+       //This will remove any checkboxes which shouldn't be\r
+       //visible\r
+       for (int i = 0; i < cSize; i++)\r
+       {\r
+         comps = featurePanel.getComponents();\r
+         check = (Checkbox) comps[i];\r
+         if (!visibleChecks.contains(check.getLabel()))\r
+         {\r
+           featurePanel.remove(i);\r
+           cSize --;\r
+           i--;\r
+         }\r
+       }\r
+\r
+       if(fr.renderOrder!=null)\r
+       {\r
+         //First add the checks in the previous render order,\r
+         //in case the window has been closed and reopened\r
+         for(int ro=fr.renderOrder.length-1; ro>-1; ro--)\r
+         {\r
+              String item = fr.renderOrder[ro];\r
+\r
+              if(!visibleChecks.contains(item))\r
+                continue;\r
+\r
+              visibleChecks.removeElement(item);\r
+\r
+              addCheck(false, item);\r
+         }\r
+       }\r
+\r
+       // now add checkboxes which should be visible,\r
+       // if they have not already been added\r
+       Enumeration en = visibleChecks.elements();\r
+       while(en.hasMoreElements())\r
+       {\r
+         addCheck(groupsChanged, en.nextElement().toString());\r
+       }\r
+\r
+\r
+   featurePanel.setLayout(new GridLayout(featurePanel.getComponentCount(), 1, 10,5));\r
    featurePanel.validate();\r
+\r
+\r
    if(scrollPane!=null)\r
      scrollPane.validate();\r
+\r
+   itemStateChanged(null);\r
+  }\r
+\r
+  void addCheck(boolean groupsChanged, String type)\r
+  {\r
+    boolean addCheck;\r
+    Component [] comps = featurePanel.getComponents();\r
+    Checkbox check;\r
+    addCheck = true;\r
+    for (int i = 0; i < featurePanel.getComponentCount(); i++)\r
+    {\r
+      check = (Checkbox) comps[i];\r
+      if (check.getLabel().equals(type))\r
+      {\r
+        addCheck = false;\r
+        break;\r
+      }\r
+    }\r
+\r
+    if (addCheck)\r
+    {\r
+      boolean selected = false;\r
+      if (groupsChanged || av.featuresDisplayed.containsKey(type))\r
+      {\r
+        selected = true;\r
+      }\r
+\r
+      check = new Checkbox(type, selected);\r
+      check.addMouseListener(this);\r
+      check.addMouseMotionListener(this);\r
+      check.setBackground(fr.getColour(type));\r
+      check.addItemListener(this);\r
+      featurePanel.add(check);\r
+    }\r
   }\r
 \r
   public void itemStateChanged(ItemEvent evt)\r
@@ -175,7 +268,7 @@ public class FeatureSettings extends Panel implements ItemListener,
         if (ap.overviewPanel != null)\r
           ap.overviewPanel.updateOverviewImage();\r
 \r
-        resetTable();\r
+        resetTable(true);\r
         return;\r
       }\r
     }\r
@@ -188,9 +281,6 @@ public class FeatureSettings extends Panel implements ItemListener,
       for (int i = 0; i < cSize; i++)\r
       {\r
         Checkbox check = (Checkbox) comps[i];\r
-        if(!check.getState())\r
-          continue;\r
-\r
         tmp[tmpSize][0] = check.getLabel();\r
         tmp[tmpSize][1] = fr.getColour(check.getLabel());\r
         tmp[tmpSize][2] = new Boolean(check.getState());\r
@@ -226,7 +316,6 @@ public class FeatureSettings extends Panel implements ItemListener,
 \r
     int height = evt.getY()+evt.getComponent().getLocation().y;\r
 \r
-\r
     if(height > this.getSize().height)\r
      {\r
        comp = featurePanel.getComponent(featurePanel.getComponentCount()-1);\r
@@ -245,7 +334,9 @@ public class FeatureSettings extends Panel implements ItemListener,
     if(comp!=null && comp instanceof Checkbox)\r
       target = (Checkbox)comp;\r
 \r
-    if (selectedCheck != null && target != null && selectedCheck != target)\r
+    if (   selectedCheck != null\r
+        && target != null\r
+        && selectedCheck != target)\r
     {\r
       int targetIndex = -1;\r
       for(int i=0; i<featurePanel.getComponentCount(); i++)\r
@@ -254,7 +345,6 @@ public class FeatureSettings extends Panel implements ItemListener,
           { targetIndex = i; break; }\r
       }\r
 \r
-\r
       featurePanel.remove(selectedCheck);\r
       featurePanel.add(selectedCheck, targetIndex);\r
       featurePanel.validate();\r
@@ -262,9 +352,24 @@ public class FeatureSettings extends Panel implements ItemListener,
     }\r
   }\r
 \r
+  public void setUserColour(String feature, Color col)\r
+  {\r
+    fr.setColour(feature, col);\r
+    featurePanel.removeAll();\r
+    resetTable(false);\r
+    ap.repaint();\r
+  }\r
 \r
   public void mouseEntered(MouseEvent evt){}\r
   public void mouseExited(MouseEvent evt){}\r
-  public void mouseClicked(MouseEvent evt){}\r
+  public void mouseClicked(MouseEvent evt)\r
+  {\r
+    Checkbox check = (Checkbox) evt.getSource();\r
+    if(evt.getClickCount()>1)\r
+    {\r
+       new UserDefinedColours(this, check.getLabel(),\r
+                              fr.getColour(check.getLabel()));\r
+    }\r
+  }\r
   public void mouseMoved(MouseEvent evt){}\r
 }\r