JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / src / jalview / javascript / MouseOverListener.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.javascript;
22
23 import jalview.appletgui.AlignFrame;
24 import jalview.appletgui.AlignViewport;
25 import jalview.bin.JalviewLite;
26 import jalview.datamodel.SequenceI;
27 import jalview.structure.VamsasListener;
28 import jalview.structure.VamsasSource;
29
30 public class MouseOverListener extends JSFunctionExec implements
31         VamsasListener, JsCallBack
32 {
33   AlignFrame _af;
34
35   String _listener;
36
37   SequenceI last = null;
38
39   int i = -1;
40
41   @Override
42   public void mouseOverSequence(SequenceI seq, int index,
43           VamsasSource source)
44   {
45     if (seq != last || i != index)
46     {
47       // this should really be a trace message.
48       // Cache.log.debug("Mouse over " + v.getId() + " bound to "
49       // + seq + " at " + index);
50       last = seq;
51       i = index;
52       AlignFrame src = null;
53       try
54       {
55         if (source != null)
56         {
57           if (source instanceof AlignViewport
58                   && ((AlignViewport) source).applet.currentAlignFrame.viewport == source)
59           {
60             // should be valid if it just generated an event!
61             src = ((AlignViewport) source).applet.currentAlignFrame;
62
63           }
64           // TODO: ensure that if '_af' is specified along with a handler
65           // function, then only events from that alignFrame are sent to that
66           // function
67         }
68         executeJavascriptFunction(
69                 _listener,
70                 new Object[]
71                 { src, seq.getDisplayId(false), "" + (1 + i),
72                     "" + seq.findPosition(i) });
73       } catch (Exception ex)
74       {
75
76         System.err
77                 .println("JalviewLite javascript error: Couldn't send mouseOver with handler '"
78                         + _listener + "'");
79         if (ex instanceof netscape.javascript.JSException)
80         {
81           System.err.println("Javascript Exception: "
82                   + ((netscape.javascript.JSException) ex).getMessage());
83         }
84         ex.printStackTrace();
85       }
86     }
87   }
88
89   public MouseOverListener(JalviewLite applet, AlignFrame af,
90           String listener)
91   {
92     super(applet);
93     _af = af;
94     _listener = listener;
95   }
96
97   @Override
98   public AlignFrame getAlignFrame()
99   {
100     return _af;
101   }
102
103   @Override
104   public String getListenerFunction()
105   {
106     return _listener;
107   }
108
109 }