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