java side javascript callback enactors JAL-469
authorjprocter <Jim Procter>
Wed, 12 Jan 2011 16:59:00 +0000 (16:59 +0000)
committerjprocter <Jim Procter>
Wed, 12 Jan 2011 16:59:00 +0000 (16:59 +0000)
src/jalview/javascript/JsCallBack.java [new file with mode: 0644]
src/jalview/javascript/JsSelectionSender.java [new file with mode: 0644]
src/jalview/javascript/MouseOverListener.java [new file with mode: 0644]

diff --git a/src/jalview/javascript/JsCallBack.java b/src/jalview/javascript/JsCallBack.java
new file mode 100644 (file)
index 0000000..b34be76
--- /dev/null
@@ -0,0 +1,8 @@
+package jalview.javascript;
+
+public interface JsCallBack
+{
+  public jalview.appletgui.AlignFrame getAlignFrame();
+  public String getListenerFunction();
+  
+}
diff --git a/src/jalview/javascript/JsSelectionSender.java b/src/jalview/javascript/JsSelectionSender.java
new file mode 100644 (file)
index 0000000..d39dbe3
--- /dev/null
@@ -0,0 +1,136 @@
+package jalview.javascript;
+
+import java.util.ArrayList;
+import java.util.Enumeration;
+
+import jalview.appletgui.AlignFrame;
+import jalview.appletgui.AlignViewport;
+import jalview.bin.JalviewLite;
+import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.SequenceGroup;
+import jalview.structure.SelectionSource;
+import netscape.javascript.JSObject;
+
+public class JsSelectionSender implements
+        jalview.structure.SelectionListener, JsCallBack
+{
+  JalviewLite jvlite;
+
+  AlignFrame _af;
+
+  String _listener;
+
+  public JsSelectionSender(JalviewLite jvlite, AlignFrame af,
+          String listener)
+  {
+    this.jvlite = jvlite;
+    _af = af;
+    _listener = listener;
+  }
+
+  @Override
+  public void selection(SequenceGroup seqsel, ColumnSelection colsel,
+          SelectionSource source)
+  {
+    // System.err.println("Testing selection event relay to jsfunction:"+_listener);
+    try
+    {
+      String setid = "";
+      String viewid = "";
+      AlignFrame src = _af;
+      // filter events if necessary
+      if (source instanceof AlignViewport)
+      {
+        if (_af != null
+                && !_af.alignPanel.av.getSequenceSetId().equals(
+                        ((AlignViewport) source).getSequenceSetId()))
+        {
+          return;
+        }
+        jalview.appletgui.AlignmentPanel[] aps = jalview.appletgui.PaintRefresher
+                .getAssociatedPanels(setid = ((AlignViewport) source)
+                        .getSequenceSetId());
+        for (int a = 0; a < aps.length; a++)
+        {
+          if (aps[a].av == source)
+          {
+            src = aps[a].alignFrame;
+          }
+        }
+      }
+      else
+      {
+        // unhandled!
+        System.err.println("Unhandled selection source !");
+        return;
+      }
+      JSObject jsoWindow = JSObject.getWindow(jvlite);
+      String[] seqs = new String[]
+      {};
+      String[] cols = new String[]
+      {};
+      int strt = 0, end = src.alignPanel.av.getAlignment().getWidth();
+      if (seqsel != null && seqsel.getSize() > 0)
+      {
+        seqs = new String[seqsel.getSize()];
+        for (int i = 0; i < seqs.length; i++)
+        {
+          seqs[i] = seqsel.getSequenceAt(i).getName();
+        }
+        if (strt < seqsel.getStartRes())
+        {
+          strt = seqsel.getStartRes();
+        }
+        if (end > seqsel.getEndRes())
+        {
+          end = seqsel.getEndRes();
+        }
+      }
+      if (colsel != null && colsel.size() > 0)
+      {
+        cols = new String[colsel.getSelected().size()];
+        int d = 0, r = -1;
+        for (int i = 0; i < cols.length; i++)
+        {
+          cols[i] = ""+(1+((Integer)colsel.getSelected().elementAt(i)).intValue());
+        }
+      }
+      else
+      {
+        if (seqsel != null && seqsel.getSize() > 0)
+        {
+          // send a valid range, otherwise we send the empty selection
+          cols = new String[2];
+          cols[0] = "" + (1 + strt) + "-" + (1 + end);
+        }
+        ;
+
+      }
+      System.err.println("Relaying selection to jsfunction:" + _listener);
+      jsoWindow.call(
+              _listener,
+              new Object[]
+              { src, setid, jvlite.arrayToSeparatorList(seqs),
+                  jvlite.arrayToSeparatorList(cols) });
+    } catch (Exception ex)
+    {
+      System.err
+              .println("Jalview Javascript exec error: Couldn't send selection message using function '"
+                      + _listener + "'");
+      ex.printStackTrace();
+    }
+  }
+
+  @Override
+  public AlignFrame getAlignFrame()
+  {
+    return _af;
+  }
+
+  @Override
+  public String getListenerFunction()
+  {
+    return _listener;
+  }
+
+}
diff --git a/src/jalview/javascript/MouseOverListener.java b/src/jalview/javascript/MouseOverListener.java
new file mode 100644 (file)
index 0000000..05fc57e
--- /dev/null
@@ -0,0 +1,57 @@
+package jalview.javascript;
+
+import jalview.appletgui.AlignFrame;
+import jalview.bin.JalviewLite;
+import jalview.datamodel.SequenceI;
+import jalview.structure.VamsasListener;
+import netscape.javascript.JSObject;
+
+public class MouseOverListener implements VamsasListener,JsCallBack
+{
+  JalviewLite jvlite;
+  AlignFrame _af;
+  String _listener;
+  SequenceI last = null;
+
+  int i = -1;
+
+  public void mouseOver(SequenceI seq, int index)
+  {
+    if (seq != last || i != index)
+    {
+        // this should really be a trace message.
+        // Cache.log.debug("Mouse over " + v.getId() + " bound to "
+        // + seq + " at " + index);
+        last = seq;
+        i = index;
+        try {
+        JSObject js = JSObject.getWindow(jvlite);
+        js.call(_listener, new Object[] { _af, seq.getDisplayId(false), ""+(1+i)});
+        } catch (Exception ex)
+        {
+          System.err.println("JalviewLite javascript error: Couldn't send mouseOver with handler '"+_listener+"'");
+          ex.printStackTrace();
+      }
+    }
+  }
+  
+  public MouseOverListener(JalviewLite applet, AlignFrame af, String listener)
+  {
+    this.jvlite = applet;
+    _af = af;
+    _listener = listener;
+  }
+  @Override
+  public AlignFrame getAlignFrame()
+  {
+    return _af;
+  }
+
+  @Override
+  public String getListenerFunction()
+  {
+    return _listener;
+  }
+
+
+}