JAL-2351 first refactoring and outline of read/write Jmol properties
[jalview.git] / src / jalview / gui / StructureViewerBase.java
index 91d7130..d7d01ca 100644 (file)
@@ -35,12 +35,13 @@ import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Vector;
 
 import javax.swing.JCheckBoxMenuItem;
+import javax.swing.JMenu;
 import javax.swing.JMenuItem;
-import javax.swing.JOptionPane;
 
 /**
  * Base class with common functionality for JMol, Chimera or other structure
@@ -52,6 +53,10 @@ import javax.swing.JOptionPane;
 public abstract class StructureViewerBase extends GStructureViewer
         implements Runnable, ViewSetProvider
 {
+  /*
+   * prefix for attributes on structure which are derived from Jalview features
+   */
+  public static final String NAMESPACE_PREFIX = "jv_";
 
   /**
    * list of sequenceSet ids associated with the view
@@ -552,4 +557,64 @@ public abstract class StructureViewerBase extends GStructureViewer
 
   abstract void showSelectedChains();
 
+  /**
+   * Send a command to the structure viewer to create residue attributes for
+   * Jalview features
+   */
+  abstract protected void sendFeaturesToViewer();
+
+  /**
+   * Query the structure viewer for its residue attribute names and add them as
+   * sub-items of the attributes menu. Names of the form "jv_*" are ignored as
+   * these originated from Jalview so no need to copy them back.
+   * 
+   * @param attributesMenu
+   */
+  protected void buildAttributesMenu(JMenu attributesMenu)
+  {
+    List<String> atts = getResidueAttributeNames();
+    if (atts == null)
+    {
+      return;
+    }
+    attributesMenu.removeAll();
+    Collections.sort(atts);
+    for (final String att : atts)
+    {
+      /*
+       * ignore 'jv_*' attributes, as these are Jalview features that have
+       * been transferred to residue attributes in Chimera!
+       */
+      if (!att.startsWith(StructureViewerBase.NAMESPACE_PREFIX))
+      {
+        JMenuItem menuItem = new JMenuItem(att);
+        menuItem.addActionListener(new ActionListener()
+        {
+          @Override
+          public void actionPerformed(ActionEvent e)
+          {
+            getResidueAttributes(att);
+          }
+        });
+        attributesMenu.add(menuItem);
+      }
+    }
+  }
+
+  /**
+   * Queries the structure viewer for residues with the given attribute, and
+   * creates sequence features in Jalview for the corresponding mapped positions
+   * 
+   * @param attName
+   */
+  protected abstract void getResidueAttributes(String attName);
+
+  /**
+   * Returns a list of residue attributes in the structure viewer, excluding any
+   * with prefix "jv_"
+   * 
+   * @return
+   */
+  protected abstract List<String> getResidueAttributeNames();
+
 }