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