6734200f1ac4c82f2c42504b289ea171c886dd01
[jalview.git] / src / jalview / gui / AppJmolBinding.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.gui;
19
20 import java.awt.Container;
21 import java.util.BitSet;
22
23 import jalview.api.AlignmentViewPanel;
24 import jalview.bin.Cache;
25 import jalview.datamodel.PDBEntry;
26 import jalview.datamodel.SequenceI;
27 import jalview.structure.StructureSelectionManager;
28
29 import org.jmol.api.JmolAppConsoleInterface;
30 import org.jmol.api.JmolViewer;
31 import org.jmol.popup.JmolPopup;
32 import org.openscience.jmol.app.jmolpanel.AppConsole;
33
34 public class AppJmolBinding extends jalview.ext.jmol.JalviewJmolBinding
35 {
36
37   /**
38    * 
39    */
40   private AppJmol appJmolWindow;
41
42   public AppJmolBinding(AppJmol appJmol, StructureSelectionManager sSm,
43           PDBEntry[] pdbentry, SequenceI[][] sequenceIs, String[][] chains,
44           String protocol)
45   {
46     super(sSm, pdbentry, sequenceIs, chains, protocol);
47     appJmolWindow = appJmol;
48   }
49
50   FeatureRenderer fr = null;
51
52   @Override
53   public jalview.api.FeatureRenderer getFeatureRenderer(
54           AlignmentViewPanel alignment)
55   {
56     AlignmentPanel ap = (alignment == null) ? appJmolWindow.ap
57             : (AlignmentPanel) alignment;
58     if (ap.av.showSequenceFeatures)
59     {
60       if (fr == null)
61       {
62         fr = ap.cloneFeatureRenderer();
63       }
64       else
65       {
66         ap.updateFeatureRenderer(fr);
67       }
68     }
69
70     return fr;
71   }
72
73   @Override
74   public jalview.api.SequenceRenderer getSequenceRenderer(
75           AlignmentViewPanel alignment)
76   {
77     return new SequenceRenderer(((AlignmentPanel) alignment).av);
78   }
79
80   public void sendConsoleEcho(String strEcho)
81   {
82     if (console != null)
83     {
84       console.sendConsoleEcho(strEcho);
85     }
86   }
87
88   public void sendConsoleMessage(String strStatus)
89   {
90     if (console != null && strStatus != null)
91     // && !strStatus.equals("Script completed"))
92     // should we squash the script completed string ?
93     {
94       console.sendConsoleMessage(strStatus);
95     }
96   }
97
98   @Override
99   public void showUrl(String url, String target)
100   {
101     try
102     {
103       jalview.util.BrowserLauncher.openURL(url);
104     } catch (Exception e)
105     {
106       Cache.log.error("Failed to launch Jmol-associated url " + url, e);
107       // TODO: 2.6 : warn user if browser was not configured.
108     }
109   }
110
111   @Override
112   public void refreshGUI()
113   {
114     // appJmolWindow.repaint();
115     javax.swing.SwingUtilities.invokeLater(new Runnable()
116     {
117       public void run()
118       {
119         appJmolWindow.updateTitleAndMenus();
120         appJmolWindow.revalidate();
121       }
122     });
123   }
124
125   public void updateColours(Object source)
126   {
127     AlignmentPanel ap = (AlignmentPanel) source, topap;
128     // ignore events from panels not used to colour this view
129     if (!appJmolWindow.isUsedforcolourby(ap))
130       return;
131     if (!isLoadingFromArchive())
132     {
133       colourBySequence(ap.av.getShowSequenceFeatures(), ap);
134     }
135   }
136
137   public void notifyScriptTermination(String strStatus, int msWalltime)
138   {
139     // todo - script termination doesn't happen ?
140     // if (console != null)
141     // console.notifyScriptTermination(strStatus,
142     // msWalltime);
143   }
144
145   public void showUrl(String url)
146   {
147     showUrl(url, "jmol");
148   }
149
150   public void newJmolPopup(boolean translateLocale, String menuName,
151           boolean asPopup)
152   {
153
154     jmolpopup = new JmolPopup();
155     jmolpopup.initialize(viewer, translateLocale, menuName, asPopup);
156   }
157
158   public void selectionChanged(BitSet arg0)
159   {
160     // TODO Auto-generated method stub
161
162   }
163
164   public void refreshPdbEntries()
165   {
166     // TODO Auto-generated method stub
167
168   }
169
170   public void showConsole(boolean b)
171   {
172     appJmolWindow.showConsole(b);
173   }
174
175   /**
176    * add the given sequences to the mapping scope for the given pdb file handle
177    * 
178    * @param pdbFile
179    *          - pdbFile identifier
180    * @param seq
181    *          - set of sequences it can be mapped to
182    */
183   public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
184   {
185     for (int pe = 0; pe < pdbentry.length; pe++)
186     {
187       if (pdbentry[pe].getFile().equals(pdbFile))
188       {
189         addSequence(pe, seq);
190       }
191     }
192   }
193
194   @Override
195   protected JmolAppConsoleInterface createJmolConsole(JmolViewer viewer2,
196           Container consolePanel, String buttonsToShow)
197   {
198     return new AppConsole(viewer, consolePanel, buttonsToShow);
199   }
200
201   @Override
202   protected void releaseUIResources()
203   {
204     appJmolWindow = null;
205     if (console != null)
206     {
207       try
208       {
209         console.setVisible(false);
210       } catch (Error e)
211       {
212       } catch (Exception x)
213       {
214       }
215       ;
216       console = null;
217     }
218
219   }
220
221   @Override
222   public void releaseReferences(Object svl)
223   {
224     if (svl instanceof SeqPanel)
225     {
226       appJmolWindow.removeAlignmentPanel(((SeqPanel) svl).ap);
227
228     }
229     ;
230   }
231 }