JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / jalview / javascript / MouseOverStructureListener.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.api.AlignmentViewPanel;
24 import jalview.api.FeatureRenderer;
25 import jalview.api.SequenceRenderer;
26 import jalview.appletgui.AlignFrame;
27 import jalview.bin.JalviewLite;
28 import jalview.datamodel.SequenceI;
29 import jalview.ext.jmol.JmolCommands;
30 import jalview.structure.AtomSpec;
31 import jalview.structure.StructureListener;
32 import jalview.structure.StructureMapping;
33 import jalview.structure.StructureSelectionManager;
34 import jalview.util.HttpUtils;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39 /**
40  * Propagate events involving PDB structures associated with sequences to a
41  * javascript function. Generally, the javascript handler is called with a
42  * series of arguments like (eventname, ... ). As of Jalview 2.7, the following
43  * different types of events are supported:
44  * <ul>
45  * <li>mouseover: javascript function called with arguments
46  * 
47  * <pre>
48  * ['mouseover',String(pdb file URI), String(pdb file chain ID), String(residue
49  * number moused over), String(atom index corresponding to residue)]
50  * </pre>
51  * 
52  * </li>
53  * <li>colourstruct: javascript function called with arguments
54  * 
55  * <pre>
56  * ['colourstruct',String(alignment view id),String(number of javascript message
57  * chunks to collect),String(length of first chunk in set of messages - or zero
58  * for null message)]
59  * </pre>
60  * 
61  * <br>
62  * The message contains a series of Jmol script commands that will colour
63  * structures according to their associated sequences in the current view. Use
64  * jalview
65  * .javascript.JalviewLiteJsApi.getJsMessage('colourstruct',String(alignment
66  * view id)) to retrieve successive chunks of the message.</li>
67  * </ul>
68  * 
69  * @author Jim Procter (jprocter)
70  * 
71  */
72 public class MouseOverStructureListener extends JSFunctionExec
73         implements JsCallBack, StructureListener
74 {
75
76   String _listenerfn;
77
78   String[] modelSet;
79
80   public MouseOverStructureListener(JalviewLite jalviewLite,
81           String listener, String[] modelList)
82   {
83     super(jalviewLite);
84     _listenerfn = listener;
85     modelSet = modelList;
86     if (modelSet != null)
87     {
88       for (int i = 0; i < modelSet.length; i++)
89       {
90         modelSet[i] = resolveModelFile(modelSet[i]);
91       }
92     }
93   }
94
95   /**
96    * Returns the first out of: file, file prefixed by document base, or file
97    * prefixed by codebase which can be resolved to a valid URL. If none can,
98    * returns the input parameter value.
99    * 
100    * @param file
101    */
102   public String resolveModelFile(String file)
103   {
104     // TODO reuse JalviewLite.LoadingThread.addProtocol instead
105     if (HttpUtils.isValidUrl(file))
106     {
107       return file;
108     }
109
110     String db = jvlite.getDocumentBase().toString();
111     db = db.substring(0, db.lastIndexOf("/"));
112     String docBaseFile = db + "/" + file;
113     if (HttpUtils.isValidUrl(docBaseFile))
114     {
115       return docBaseFile;
116     }
117
118     String cb = jvlite.getCodeBase() + file;
119     if (HttpUtils.isValidUrl(cb))
120     {
121       return cb;
122     }
123
124     return file;
125   }
126
127   @Override
128   public String[] getStructureFiles()
129   {
130     return modelSet;
131   }
132
133   public void mouseOverStructure(int atomIndex, String strInfo)
134   {
135
136     // StructureSelectionManager.getStructureSelectionManager().mouseOverStructure(atomIndex,
137     // chain, pdbfile)
138     // TODO Auto-generated method stub
139
140   }
141
142   @Override
143   public void highlightAtoms(List<AtomSpec> atoms)
144   {
145     for (AtomSpec atom : atoms)
146     {
147       try
148       {
149         // TODO is this right? StructureSelectionManager passes pdbFile as the
150         // field that is interpreted (in 2.8.2) as pdbId?
151         // JBPComment: yep - this is right! the Javascript harness uses the
152         // absolute pdbFile URI to locate the PDB file in the external viewer
153         executeJavascriptFunction(_listenerfn,
154                 new String[]
155                 { "mouseover", "" + atom.getPdbFile(), "" + atom.getChain(),
156                     "" + (atom.getPdbResNum()), "" + atom.getAtomIndex() });
157       } catch (Exception ex)
158       {
159         System.err.println("Couldn't execute callback with " + _listenerfn
160                 + " for atomSpec: " + atom);
161         ex.printStackTrace();
162       }
163     }
164   }
165
166   @Override
167   public synchronized void updateColours(Object srce)
168   {
169     final Object source = srce;
170     StructureSelectionManager ssm = StructureSelectionManager
171             .getStructureSelectionManager(jvlite);
172
173     if (JalviewLite.debug)
174     {
175       System.err.println(
176               this.getClass().getName() + " modelSet[0]: " + modelSet[0]);
177       ssm.reportMapping();
178     }
179
180     if (source instanceof jalview.api.AlignmentViewPanel)
181     {
182       SequenceI[][] sequence = new SequenceI[modelSet.length][];
183       for (int m = 0; m < modelSet.length; m++)
184       {
185         StructureMapping[] sm = ssm.getMapping(modelSet[m]);
186         if (sm != null && sm.length > 0)
187         {
188           sequence[m] = new SequenceI[sm.length];
189           for (int i = 0; i < sm.length; i++)
190           {
191             sequence[m][i] = sm[i].getSequence();
192           }
193         }
194         else
195         {
196           sequence[m] = new SequenceI[0];
197         }
198         // if (jvlite.debug)
199         // {
200         // System.err.println("Mapped '" + modelSet[m] + "' to "
201         // + sequence[m].length + " sequences.");
202         // }
203       }
204
205       SequenceRenderer sr = ((jalview.appletgui.AlignmentPanel) source)
206               .getSequenceRenderer();
207       FeatureRenderer fr = ((jalview.appletgui.AlignmentPanel) source).av
208               .isShowSequenceFeatures()
209                       ? new jalview.appletgui.FeatureRenderer(
210                               ((jalview.appletgui.AlignmentPanel) source).av)
211                       : null;
212       if (fr != null)
213       {
214         ((jalview.appletgui.FeatureRenderer) fr).transferSettings(
215                 ((jalview.appletgui.AlignmentPanel) source)
216                         .getFeatureRenderer());
217       }
218       ;
219
220       // Form a colour command from the given alignment panel for each distinct
221       // structure
222       ArrayList<String[]> ccomands = new ArrayList<>();
223       ArrayList<String> pdbfn = new ArrayList<>();
224       String[] colcommands = new JmolCommands().colourBySequence(ssm,
225               modelSet, sequence, sr, (AlignmentViewPanel) source);
226       if (colcommands == null)
227       {
228         return;
229       }
230       int sz = 0;
231       // for (jalview.structure.StructureMappingcommandSet ccset : colcommands)
232       for (String command : colcommands)
233       {
234         // sz += ccset.commands.length;
235         // ccomands.add(command); // ccset.commands);
236         // pdbfn.add(ccset.mapping);
237       }
238
239       String mclass, mhandle;
240       String ccomandset[] = new String[sz];
241       sz = 0;
242       for (String[] ccset : ccomands)
243       {
244         System.arraycopy(ccset, 0, ccomandset, sz, ccset.length);
245         sz += ccset.length;
246       }
247       if (jvlite.isJsMessageSetChanged(mclass = "colourstruct",
248               mhandle = ((jalview.appletgui.AlignmentPanel) source).av
249                       .getViewId(),
250               ccomandset))
251       {
252         jvlite.setJsMessageSet(mclass, mhandle, ccomandset);
253         // and notify javascript handler
254         String st[] = new String[] { "colourstruct",
255             "" + ((jalview.appletgui.AlignmentPanel) source).av.getViewId(),
256             "" + ccomandset.length, jvlite.arrayToSeparatorList(
257                     pdbfn.toArray(new String[pdbfn.size()])) };
258         try
259         {
260           executeJavascriptFunction(true, _listenerfn, st);
261         } catch (Exception ex)
262         {
263           System.err.println("Couldn't execute callback with " + _listenerfn
264                   + " using args { " + st[0] + ", " + st[1] + ", " + st[2]
265                   + "," + st[3] + "}"); // + ","+st[4]+"\n");
266           ex.printStackTrace();
267
268         }
269       }
270       /*
271        * new Thread(new Runnable() { public void run() { // and send to
272        * javascript handler String st[] = new String[0]; int i = 0; for (String
273        * colcommand : colcommands) { // do sync execution for each chunk try {
274        * executeJavascriptFunction( false, _listenerfn, st = new String[] {
275        * "colourstruct", "" + ((jalview.appletgui.AlignmentPanel) source).av
276        * .getViewId(), handle, "" }); } catch (Exception ex) {
277        * System.err.println("Couldn't execute callback with " + _listenerfn +
278        * " using args { " + st[0] + ", " + st[1] + ", " + st[2] + "," + st[3] +
279        * "\n"); ex.printStackTrace();
280        * 
281        * } } } }).start();
282        */
283     }
284
285   }
286
287   @Override
288   public AlignFrame getAlignFrame()
289   {
290     // associated with all alignframes, always.
291     return null;
292   }
293
294   @Override
295   public String getListenerFunction()
296   {
297     return _listenerfn;
298   }
299
300   @Override
301   public void releaseReferences(Object svl)
302   {
303
304     // TODO Auto-generated method stub
305
306   }
307
308   @Override
309   public boolean isListeningFor(SequenceI seq)
310   {
311     return true;
312   }
313
314 }