JAL-2422 JAL-3551 unit tests updated for code changes
[jalview.git] / src / jalview / gui / AppJmolBinding.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.gui;
22
23 import java.awt.Container;
24 import java.io.File;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.util.Map;
28
29 import javax.swing.JComponent;
30
31 import org.jmol.api.JmolAppConsoleInterface;
32 import org.jmol.java.BS;
33 import org.openscience.jmol.app.jmolpanel.console.AppConsole;
34
35 import jalview.api.AlignmentViewPanel;
36 import jalview.api.structures.JalviewStructureDisplayI;
37 import jalview.bin.Cache;
38 import jalview.datamodel.PDBEntry;
39 import jalview.datamodel.SequenceI;
40 import jalview.ext.jmol.JalviewJmolBinding;
41 import jalview.io.DataSourceType;
42 import jalview.structure.StructureSelectionManager;
43
44 public class AppJmolBinding extends JalviewJmolBinding
45 {
46   public AppJmolBinding(AppJmol appJmol, StructureSelectionManager sSm,
47           PDBEntry[] pdbentry, SequenceI[][] sequenceIs,
48           DataSourceType protocol)
49   {
50     super(sSm, pdbentry, sequenceIs, protocol);
51     setViewer(appJmol);
52   }
53
54   @Override
55   public SequenceRenderer getSequenceRenderer(AlignmentViewPanel alignment)
56   {
57     return new SequenceRenderer(((AlignmentPanel) alignment).av);
58   }
59
60   @Override
61   public void sendConsoleEcho(String strEcho)
62   {
63     if (console != null)
64     {
65       console.sendConsoleEcho(strEcho);
66     }
67   }
68
69   @Override
70   public void sendConsoleMessage(String strStatus)
71   {
72     if (console != null && strStatus != null)
73     // && !strStatus.equals("Script completed"))
74     // should we squash the script completed string ?
75     {
76       console.sendConsoleMessage(strStatus);
77     }
78   }
79
80   @Override
81   public void showUrl(String url, String target)
82   {
83     try
84     {
85       jalview.util.BrowserLauncher.openURL(url);
86     } catch (Exception e)
87     {
88       Cache.log.error("Failed to launch Jmol-associated url " + url, e);
89       // TODO: 2.6 : warn user if browser was not configured.
90     }
91   }
92
93   @Override
94   public void refreshGUI()
95   {
96     // appJmolWindow.repaint();
97     javax.swing.SwingUtilities.invokeLater(new Runnable()
98     {
99       @Override
100       public void run()
101       {
102         JalviewStructureDisplayI theViewer = getViewer();
103         theViewer.updateTitleAndMenus();
104         ((JComponent) theViewer).revalidate();
105       }
106     });
107   }
108
109   @Override
110   public void notifyScriptTermination(String strStatus, int msWalltime)
111   {
112     // todo - script termination doesn't happen ?
113     // if (console != null)
114     // console.notifyScriptTermination(strStatus,
115     // msWalltime);
116   }
117
118   @Override
119   public void showUrl(String url)
120   {
121     showUrl(url, "jmol");
122   }
123
124   public void newJmolPopup(String menuName)
125   {
126     // jmolpopup = new JmolAwtPopup();
127     // jmolpopup.jpiInitialize((viewer), menuName);
128   }
129
130   @Override
131   public void selectionChanged(BS arg0)
132   {
133   }
134
135   @Override
136   public void showConsole(boolean b)
137   {
138     getViewer().showConsole(b);
139   }
140
141   @Override
142   protected JmolAppConsoleInterface createJmolConsole(
143           Container consolePanel, String buttonsToShow)
144   {
145     jmolViewer.setJmolCallbackListener(this);
146     return new AppConsole(jmolViewer, consolePanel, buttonsToShow);
147   }
148
149   @Override
150   protected void releaseUIResources()
151   {
152     setViewer(null);
153     closeConsole();
154   }
155
156   @Override
157   public void releaseReferences(Object svl)
158   {
159     if (svl instanceof SeqPanel)
160     {
161       getViewer().removeAlignmentPanel(((SeqPanel) svl).ap);
162     }
163   }
164
165   @Override
166   public Map<String, Object> getJSpecViewProperty(String arg0)
167   {
168     // TODO Auto-generated method stub
169     return null;
170   }
171
172   /**
173    * Overrides the default method to save a session to file, in order to
174    * guarantee it is done synchronously. Jmol command 'write STATE path' would
175    * execute asynchronously, so instead we get the state and write it directly
176    * here.
177    */
178   @Override
179   protected void saveSession(File f)
180   {
181     String state = jmolViewer.getStateInfo();
182     if (state != null)
183     {
184       try
185       {
186         FileWriter fw = new FileWriter(f);
187         fw.write(state);
188         fw.close();
189       } catch (IOException e)
190       {
191         Cache.log.error("Error writing Jmol state: " + e.toString());
192       }
193     }
194     else
195     {
196       Cache.log.error("Error requesting Jmol state to save");
197     }
198   }
199 }