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