JAL-4047 JAL-4948 show count of each dbref authority and 'first' reference
[jalview.git] / src / jalview / javascript / JsSelectionSender.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.bin.JalviewLite;
25 import jalview.datamodel.ColumnSelection;
26 import jalview.datamodel.HiddenColumns;
27 import jalview.datamodel.SequenceGroup;
28 import jalview.structure.SelectionSource;
29
30 public class JsSelectionSender extends JSFunctionExec
31         implements jalview.structure.SelectionListener, JsCallBack
32 {
33
34   AlignFrame _af;
35
36   String _listener;
37
38   public JsSelectionSender(JalviewLite jvlite, AlignFrame af,
39           String listener)
40   {
41     super(jvlite);
42     _af = af;
43     _listener = listener;
44   }
45
46   @Override
47   public void selection(SequenceGroup seqsel, ColumnSelection colsel,
48           HiddenColumns hidden, SelectionSource source)
49   {
50     // System.err.println("Testing selection event relay to
51     // jsfunction:"+_listener);
52     try
53     {
54       String setid = "";
55       AlignFrame src = _af;
56       if (source != null)
57       {
58         if (source instanceof jalview.appletgui.AlignViewport
59                 && ((jalview.appletgui.AlignViewport) source).applet.currentAlignFrame.viewport == source)
60         {
61           // should be valid if it just generated an event!
62           src = ((jalview.appletgui.AlignViewport) source).applet.currentAlignFrame;
63
64         }
65       }
66       String[] seqs = new String[] {};
67       String[] cols = new String[] {};
68       int strt = 0, end = (src == null) ? -1
69               : src.alignPanel.av.getAlignment().getWidth();
70       if (seqsel != null && seqsel.getSize() > 0)
71       {
72         seqs = new String[seqsel.getSize()];
73         for (int i = 0; i < seqs.length; i++)
74         {
75           seqs[i] = seqsel.getSequenceAt(i).getName();
76         }
77         if (strt < seqsel.getStartRes())
78         {
79           strt = seqsel.getStartRes();
80         }
81         if (end == -1 || end > seqsel.getEndRes())
82         {
83           end = seqsel.getEndRes();
84         }
85       }
86       if (colsel != null && !colsel.isEmpty())
87       {
88         if (end == -1)
89         {
90           end = colsel.getMax() + 1;
91         }
92         cols = new String[colsel.getSelected().size()];
93         for (int i = 0; i < cols.length; i++)
94         {
95           cols[i] = "" + (1 + colsel.getSelected().get(i).intValue());
96         }
97       }
98       else
99       {
100         if (seqsel != null && seqsel.getSize() > 0)
101         {
102           // send a valid range, otherwise we send the empty selection
103           cols = new String[2];
104           cols[0] = "" + (1 + strt) + "-" + (1 + end);
105         }
106         ;
107
108       }
109       System.err.println("Relaying selection to jsfunction:" + _listener);
110       executeJavascriptFunction(_listener,
111               new Object[]
112               { src, setid, jvlite.arrayToSeparatorList(seqs),
113                   jvlite.arrayToSeparatorList(cols) });
114     } catch (Exception ex)
115     {
116       System.err.println(
117               "Jalview Javascript exec error: Couldn't send selection message using function '"
118                       + _listener + "'");
119       ex.printStackTrace();
120       if (ex instanceof netscape.javascript.JSException)
121       {
122         System.err.println("Javascript Exception: "
123                 + ((netscape.javascript.JSException) ex).getCause()
124                         .toString());
125       }
126
127     }
128   }
129
130   @Override
131   public AlignFrame getAlignFrame()
132   {
133     return _af;
134   }
135
136   @Override
137   public String getListenerFunction()
138   {
139     return _listener;
140   }
141
142 }