jmol update
[jalview.git] / src / jalview / gui / AppJmolBinding.java
1 /**
2  * 
3  */
4 package jalview.gui;
5
6 import java.awt.Container;
7 import java.awt.event.ComponentEvent;
8 import java.awt.event.ComponentListener;
9 import java.awt.event.ContainerEvent;
10 import java.awt.event.ContainerListener;
11 import java.util.BitSet;
12
13 import javax.swing.JPanel;
14
15 import jalview.bin.Cache;
16 import jalview.datamodel.PDBEntry;
17 import jalview.datamodel.SequenceI;
18
19 import org.jmol.api.JmolAppConsoleInterface;
20 import org.jmol.api.JmolViewer;
21 import org.jmol.popup.JmolPopup;
22 import org.openscience.jmol.app.jmolpanel.AppConsole;
23
24 public class AppJmolBinding extends jalview.ext.jmol.JalviewJmolBinding 
25 {
26
27   /**
28    * 
29    */
30   private AppJmol appJmolWindow;
31
32   public AppJmolBinding(AppJmol appJmol, PDBEntry[] pdbentry,
33           SequenceI[][] sequenceIs, String[][] chains, String protocol)
34   {
35     super(pdbentry, sequenceIs, chains, protocol);
36     appJmolWindow = appJmol;
37   }
38
39   FeatureRenderer fr = null;
40
41   @Override
42   public jalview.api.FeatureRenderer getFeatureRenderer()
43   {
44     if (appJmolWindow.ap.av.showSequenceFeatures)
45     {
46       if (fr == null)
47       {
48         fr = new FeatureRenderer(appJmolWindow.ap);
49       }
50
51       fr.transferSettings(appJmolWindow.ap.seqPanel.seqCanvas
52               .getFeatureRenderer());
53     }
54
55     return fr;
56   }
57
58   @Override
59   public jalview.api.SequenceRenderer getSequenceRenderer()
60   {
61     return new SequenceRenderer(appJmolWindow.ap.av);
62   }
63
64   public void sendConsoleEcho(String strEcho)
65   {
66     if (console != null)
67     {
68       console.sendConsoleEcho(strEcho);
69     }
70   }
71
72   public void sendConsoleMessage(String strStatus)
73   {
74     if (console != null && strStatus != null)
75     // && !strStatus.equals("Script completed"))
76     // should we squash the script completed string ?
77     {
78       console.sendConsoleMessage(strStatus);
79     }
80   }
81
82   @Override
83   public void showUrl(String url, String target)
84   {
85     try
86     {
87       jalview.util.BrowserLauncher.openURL(url);
88     } catch (Exception e)
89     {
90       Cache.log.error("Failed to launch Jmol-associated url " + url, e);
91       // TODO: 2.6 : warn user if browser was not configured.
92     }
93   }
94
95   @Override
96   public void refreshGUI()
97   {
98     // appJmolWindow.repaint();
99     javax.swing.SwingUtilities.invokeLater(new Runnable() {
100       public void run() {
101         appJmolWindow.updateTitleAndMenus();
102         appJmolWindow.revalidate();
103       }});
104   }
105
106   public void updateColours(Object source)
107   {
108     AlignmentPanel ap = (AlignmentPanel) source;
109     if (appJmolWindow.ap.alignFrame.getCurrentView() != ap.av)
110       return;
111
112     colourBySequence(ap.av.getShowSequenceFeatures(), ap.av.alignment);
113   }
114
115   public void notifyScriptTermination(String strStatus, int msWalltime)
116   {
117     // todo - script termination doesn't happen ?
118     //if (console != null)
119       //console.notifyScriptTermination(strStatus,
120       //        msWalltime);
121   }
122
123   public void showUrl(String url)
124   {
125     showUrl(url, "jmol");
126   }
127
128   public void newJmolPopup(boolean translateLocale, String menuName,
129           boolean asPopup)
130   {
131
132     jmolpopup = JmolPopup.newJmolPopup(viewer, translateLocale, menuName,
133             asPopup);
134   }
135
136   public void selectionChanged(BitSet arg0)
137   {
138     // TODO Auto-generated method stub
139
140   }
141
142   public void refreshPdbEntries()
143   {
144     // TODO Auto-generated method stub
145
146   }
147
148   public void showConsole(boolean b) {
149     appJmolWindow.showConsole(b);
150   }
151
152   /**
153    * add the given sequences to the mapping scope for the given pdb file handle
154    * 
155    * @param pdbFile
156    *          - pdbFile identifier
157    * @param seq
158    *          - set of sequences it can be mapped to
159    */
160   public void addSequenceForStructFile(String pdbFile, SequenceI[] seq)
161   {
162     for (int pe = 0; pe < pdbentry.length; pe++)
163     {
164       if (pdbentry[pe].getFile().equals(pdbFile))
165       {
166         addSequence(pe, seq);
167       }
168     }
169   }
170
171   @Override
172   protected JmolAppConsoleInterface createJmolConsole(JmolViewer viewer2,
173           Container consolePanel, String buttonsToShow)
174   {
175     return new AppConsole(viewer, consolePanel, buttonsToShow);
176   }
177
178   @Override
179   protected void releaseUIResources()
180   {
181     appJmolWindow = null;
182     if (console!=null) {
183       try {
184         console.setVisible(false);
185       } catch (Error e) {} catch (Exception x) {};
186       console=null;
187     }
188     
189   }
190 }