2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import java.awt.Color;
24 import java.awt.Component;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.awt.event.ItemEvent;
28 import java.awt.event.ItemListener;
29 import java.beans.PropertyVetoException;
30 import java.io.BufferedReader;
32 import java.io.FileOutputStream;
33 import java.io.FileReader;
34 import java.io.IOException;
35 import java.io.PrintWriter;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Random;
39 import java.util.Vector;
41 import javax.swing.ButtonGroup;
42 import javax.swing.JCheckBoxMenuItem;
43 import javax.swing.JMenu;
44 import javax.swing.JMenuItem;
45 import javax.swing.JRadioButtonMenuItem;
46 import javax.swing.event.MenuEvent;
47 import javax.swing.event.MenuListener;
49 import jalview.api.AlignmentViewPanel;
50 import jalview.api.structures.JalviewStructureDisplayI;
51 import jalview.bin.Cache;
52 import jalview.bin.Console;
53 import jalview.datamodel.AlignmentI;
54 import jalview.datamodel.PDBEntry;
55 import jalview.datamodel.SequenceI;
56 import jalview.gui.JalviewColourChooser.ColourChooserListener;
57 import jalview.gui.StructureViewer.ViewerType;
58 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
59 import jalview.io.DataSourceType;
60 import jalview.io.JalviewFileChooser;
61 import jalview.io.JalviewFileView;
62 import jalview.jbgui.GStructureViewer;
63 import jalview.schemes.ColourSchemeI;
64 import jalview.schemes.ColourSchemes;
65 import jalview.structure.StructureMapping;
66 import jalview.structures.models.AAStructureBindingModel;
67 import jalview.util.BrowserLauncher;
68 import jalview.util.MessageManager;
69 import jalview.ws.dbsources.EBIAlfaFold;
70 import jalview.ws.dbsources.Pdb;
71 import jalview.ws.utils.UrlDownloadClient;
74 * Base class with common functionality for JMol, Chimera or other structure
80 public abstract class StructureViewerBase extends GStructureViewer
81 implements Runnable, ViewSetProvider
84 * names for colour options (additional to Jalview colour schemes)
88 BySequence, ByChain, ChargeCysteine, ByViewer
92 * Singleton list of all (open) instances of structureViewerBase
93 * TODO: JAL-3362 - review and adopt the swingJS-safe singleton pattern so each structure viewer base instance is kept to its own JalviewJS parent
95 private static List<JalviewStructureDisplayI> svbs = new ArrayList<>();
99 * @return list with all existing StructureViewers instance
101 public static List<JalviewStructureDisplayI> getAllStructureViewerBases()
103 List<JalviewStructureDisplayI> goodSvbs = new ArrayList<>();
104 for (JalviewStructureDisplayI s : svbs)
106 if (s != null && !goodSvbs.contains(s))
115 * list of sequenceSet ids associated with the view
117 protected List<String> _aps = new ArrayList<>();
120 * list of alignment panels to use for superposition
122 protected Vector<AlignmentViewPanel> _alignwith = new Vector<>();
125 * list of alignment panels that are used for colouring structures by aligned
128 protected Vector<AlignmentViewPanel> _colourwith = new Vector<>();
130 private String viewId = null;
132 private AlignmentPanel ap;
134 protected boolean alignAddedStructures = false;
136 protected volatile boolean _started = false;
138 protected volatile boolean addingStructures = false;
140 protected Thread worker = null;
142 protected boolean allChainsSelected = false;
144 protected JMenu viewSelectionMenu;
147 * set after sequence colouring has been applied for this structure viewer.
148 * used to determine if the final sequence/structure mapping has been
151 protected volatile boolean seqColoursApplied = false;
153 private IProgressIndicator progressBar = null;
155 private Random random = new Random();
158 * Default constructor
160 public StructureViewerBase()
168 * @return true if added structures should be aligned to existing one(s)
171 public boolean isAlignAddedStructures()
173 return alignAddedStructures;
179 * if added structures should be aligned to existing one(s)
182 public void setAlignAddedStructures(boolean alignAdded)
184 alignAddedStructures = alignAdded;
188 * called by the binding model to indicate when adding structures is happening
189 * or has been completed
191 * @param addingStructures
193 public synchronized void setAddingStructures(boolean addingStructures)
195 this.addingStructures = addingStructures;
201 * @return true if this Jmol instance is linked with the given alignPanel
203 public boolean isLinkedWith(AlignmentPanel ap2)
205 return _aps.contains(ap2.av.getSequenceSetId());
209 public boolean isUsedforaligment(AlignmentViewPanel ap2)
212 return (_alignwith != null) && _alignwith.contains(ap2);
216 public boolean isUsedForColourBy(AlignmentViewPanel ap2)
218 return (_colourwith != null) && _colourwith.contains(ap2);
223 * @return TRUE if the view is NOT being coloured by the alignment colours.
225 public boolean isColouredByViewer()
227 return !getBinding().isColourBySequence();
230 public String getViewId()
234 viewId = System.currentTimeMillis() + "." + this.hashCode();
239 protected void setViewId(String viewId)
241 this.viewId = viewId;
244 protected void buildActionMenu()
246 if (_alignwith == null)
248 _alignwith = new Vector<>();
250 if (_alignwith.size() == 0 && ap != null)
255 // TODO: refactor to allow concrete classes to register buttons for adding
257 // currently have to override to add buttons back in after they are cleared
259 for (Component c : viewerActionMenu.getMenuComponents())
261 if (c != alignStructs)
263 viewerActionMenu.remove((JMenuItem) c);
269 public AlignmentPanel getAlignmentPanel()
274 protected void setAlignmentPanel(AlignmentPanel alp)
280 public AlignmentPanel[] getAllAlignmentPanels()
282 AlignmentPanel[] t, list = new AlignmentPanel[0];
283 for (String setid : _aps)
285 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
288 t = new AlignmentPanel[list.length + panels.length];
289 System.arraycopy(list, 0, t, 0, list.length);
290 System.arraycopy(panels, 0, t, list.length, panels.length);
299 * set the primary alignmentPanel reference and add another alignPanel to the
300 * list of ones to use for colouring and aligning
304 public void addAlignmentPanel(AlignmentPanel nap)
306 if (getAlignmentPanel() == null)
308 setAlignmentPanel(nap);
310 if (!_aps.contains(nap.av.getSequenceSetId()))
312 _aps.add(nap.av.getSequenceSetId());
317 * remove any references held to the given alignment panel
322 public void removeAlignmentPanel(AlignmentViewPanel nap)
326 _alignwith.remove(nap);
327 _colourwith.remove(nap);
328 if (getAlignmentPanel() == nap)
330 setAlignmentPanel(null);
331 for (AlignmentPanel aps : getAllAlignmentPanels())
335 setAlignmentPanel(aps);
340 } catch (Exception ex)
343 if (getAlignmentPanel() != null)
349 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
351 addAlignmentPanel(nap);
352 if (!_alignwith.contains(nap))
358 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
360 if (_alignwith.contains(nap))
362 _alignwith.remove(nap);
366 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
367 boolean enableColourBySeq)
369 useAlignmentPanelForColourbyseq(nap);
370 getBinding().setColourBySequence(enableColourBySeq);
371 seqColour.setSelected(enableColourBySeq);
372 viewerColour.setSelected(!enableColourBySeq);
375 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
377 addAlignmentPanel(nap);
378 if (!_colourwith.contains(nap))
380 _colourwith.add(nap);
384 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
386 if (_colourwith.contains(nap))
388 _colourwith.remove(nap);
392 public abstract ViewerType getViewerType();
395 * add a new structure (with associated sequences and chains) to this viewer,
396 * retrieving it if necessary first.
402 * if true, new structure(s) will be aligned using associated
406 protected void addStructure(final PDBEntry pdbentry,
407 final SequenceI[] seqs, final String[] chains,
408 final IProgressIndicator alignFrame)
410 if (pdbentry.getFile() == null)
412 if (worker != null && worker.isAlive())
414 // a retrieval is in progress, wait around and add ourselves to the
416 new Thread(new Runnable()
421 while (worker != null && worker.isAlive() && _started)
425 Thread.sleep(100 + ((int) Math.random() * 100));
427 } catch (Exception e)
431 // and call ourselves again.
432 addStructure(pdbentry, seqs, chains, alignFrame);
438 // otherwise, start adding the structure.
439 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
441 { seqs }, new String[][] { chains });
442 addingStructures = true;
444 worker = new Thread(this);
449 protected boolean hasPdbId(String pdbId)
451 return getBinding().hasPdbId(pdbId);
455 * Returns a list of any viewer of the instantiated type. The list is
456 * restricted to those linked to the given alignment panel if it is not null.
458 protected List<StructureViewerBase> getViewersFor(AlignmentPanel alp)
460 return Desktop.instance.getStructureViewers(alp, this.getClass());
464 public void addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
465 String[] chains, final AlignmentViewPanel apanel, String pdbId)
468 * JAL-1742 exclude view with this structure already mapped (don't offer
469 * to align chain B to chain A of the same structure); code may defend
470 * against this possibility before we reach here
476 AlignmentPanel alignPanel = (AlignmentPanel) apanel; // Implementation error
479 useAlignmentPanelForSuperposition(alignPanel);
480 addStructure(pdbentry, seq, chains, alignPanel.alignFrame);
484 * Adds mappings for the given sequences to an already opened PDB structure,
485 * and updates any viewers that have the PDB file
492 public void addSequenceMappingsToStructure(SequenceI[] seq,
493 String[] chains, final AlignmentViewPanel alpanel,
496 AlignmentPanel apanel = (AlignmentPanel) alpanel;
498 // TODO : Fix multiple seq to one chain issue here.
500 * create the mappings
502 apanel.getStructureSelectionManager().setMapping(seq, chains,
503 pdbFilename, DataSourceType.FILE, getProgressIndicator());
506 * alert the FeatureRenderer to show new (PDB RESNUM) features
508 if (apanel.getSeqPanel().seqCanvas.fr != null)
510 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
511 // note - we don't do a refresh for structure here because we do it
512 // explicitly for all panels later on
513 apanel.paintAlignment(true, false);
517 * add the sequences to any other viewers (of the same type) for this pdb
520 // JBPNOTE: this looks like a binding routine, rather than a gui routine
521 for (StructureViewerBase viewer : getViewersFor(null))
523 AAStructureBindingModel bindingModel = viewer.getBinding();
524 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
526 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
528 bindingModel.addSequence(pe, seq);
529 viewer.addAlignmentPanel(apanel);
531 * add it to the set of alignments used for colouring structure by
534 viewer.useAlignmentPanelForColourbyseq(apanel);
535 viewer.buildActionMenu();
536 apanel.getStructureSelectionManager()
537 .sequenceColoursChanged(apanel);
545 public boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
546 final AlignmentViewPanel apanel, String pdbId)
548 String alreadyMapped = apanel.getStructureSelectionManager()
549 .alreadyMappedToFile(pdbId);
551 if (alreadyMapped == null)
556 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
560 void setChainMenuItems(List<String> chainNames)
562 chainMenu.removeAll();
563 if (chainNames == null || chainNames.isEmpty())
567 JMenuItem menuItem = new JMenuItem(
568 MessageManager.getString("label.all"));
569 menuItem.addActionListener(new ActionListener()
572 public void actionPerformed(ActionEvent evt)
574 allChainsSelected = true;
575 for (int i = 0; i < chainMenu.getItemCount(); i++)
577 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
579 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
582 showSelectedChains();
583 allChainsSelected = false;
587 chainMenu.add(menuItem);
589 for (String chain : chainNames)
591 menuItem = new JCheckBoxMenuItem(chain, true);
592 menuItem.addItemListener(new ItemListener()
595 public void itemStateChanged(ItemEvent evt)
597 if (!allChainsSelected)
599 showSelectedChains();
604 chainMenu.add(menuItem);
609 * Action on selecting one of Jalview's registered colour schemes
612 public void changeColour_actionPerformed(String colourSchemeName)
614 AlignmentI al = getAlignmentPanel().av.getAlignment();
615 ColourSchemeI cs = ColourSchemes.getInstance().getColourScheme(
616 colourSchemeName, getAlignmentPanel().av, al, null);
617 getBinding().colourByJalviewColourScheme(cs);
621 * Builds the colour menu
623 protected void buildColourMenu()
625 colourMenu.removeAll();
626 AlignmentI al = getAlignmentPanel().av.getAlignment();
629 * add colour by sequence, by chain, by charge and cysteine
631 colourMenu.add(seqColour);
632 colourMenu.add(chainColour);
633 colourMenu.add(chargeColour);
634 chargeColour.setEnabled(!al.isNucleotide());
637 * add all 'simple' (per-residue) colour schemes registered to Jalview
639 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
643 * add 'colour by viewer' (menu item text is set in subclasses)
645 viewerColour.setSelected(false);
646 viewerColour.addActionListener(new ActionListener()
649 public void actionPerformed(ActionEvent actionEvent)
651 viewerColour_actionPerformed();
654 colourMenu.add(viewerColour);
657 * add 'set background colour'
659 JMenuItem backGround = new JMenuItem();
661 .setText(MessageManager.getString("action.background_colour"));
662 backGround.addActionListener(new ActionListener()
665 public void actionPerformed(ActionEvent actionEvent)
667 background_actionPerformed();
670 colourMenu.add(backGround);
673 * add colour buttons to a group so their selection is
674 * mutually exclusive (background colour is a separate option)
676 itemGroup.add(seqColour);
677 itemGroup.add(chainColour);
678 itemGroup.add(chargeColour);
679 itemGroup.add(viewerColour);
683 * Construct menu items
685 protected void initMenus()
687 AAStructureBindingModel binding = getBinding();
689 seqColour = new JRadioButtonMenuItem();
690 seqColour.setText(MessageManager.getString("action.by_sequence"));
691 seqColour.setName(ViewerColour.BySequence.name());
692 seqColour.setSelected(binding.isColourBySequence());
693 seqColour.addActionListener(new ActionListener()
696 public void actionPerformed(ActionEvent actionEvent)
698 seqColour_actionPerformed();
702 chainColour = new JRadioButtonMenuItem();
703 chainColour.setText(MessageManager.getString("action.by_chain"));
704 chainColour.setName(ViewerColour.ByChain.name());
705 chainColour.addActionListener(new ActionListener()
708 public void actionPerformed(ActionEvent actionEvent)
710 chainColour_actionPerformed();
714 chargeColour = new JRadioButtonMenuItem();
715 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
716 chargeColour.setName(ViewerColour.ChargeCysteine.name());
717 chargeColour.addActionListener(new ActionListener()
720 public void actionPerformed(ActionEvent actionEvent)
722 chargeColour_actionPerformed();
726 viewerColour = new JRadioButtonMenuItem();
728 .setText(MessageManager.getString("label.colour_with_viewer"));
729 viewerColour.setToolTipText(MessageManager
730 .getString("label.let_viewer_manage_structure_colours"));
731 viewerColour.setName(ViewerColour.ByViewer.name());
732 viewerColour.setSelected(!binding.isColourBySequence());
734 if (_colourwith == null)
736 _colourwith = new Vector<>();
738 if (_alignwith == null)
740 _alignwith = new Vector<>();
743 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
744 MessageManager.getString("label.colour_by"), this, _colourwith,
748 public void itemStateChanged(ItemEvent e)
750 if (!seqColour.isSelected())
756 // update the viewer display now.
757 seqColour_actionPerformed();
761 viewMenu.add(seqColourBy);
763 final ItemListener handler = new ItemListener()
766 public void itemStateChanged(ItemEvent e)
768 if (_alignwith.isEmpty())
770 alignStructs.setEnabled(false);
771 alignStructs.setToolTipText(null);
775 alignStructs.setEnabled(true);
776 alignStructs.setToolTipText(MessageManager.formatMessage(
777 "label.align_structures_using_linked_alignment_views",
782 viewSelectionMenu = new ViewSelectionMenu(
783 MessageManager.getString("label.superpose_with"), this,
784 _alignwith, handler);
785 handler.itemStateChanged(null);
786 viewerActionMenu.add(viewSelectionMenu, 0);
787 viewerActionMenu.addMenuListener(new MenuListener()
790 public void menuSelected(MenuEvent e)
792 handler.itemStateChanged(null);
796 public void menuDeselected(MenuEvent e)
801 public void menuCanceled(MenuEvent e)
806 viewerActionMenu.setText(getViewerName());
807 helpItem.setText(MessageManager.formatMessage("label.viewer_help",
814 * Sends commands to the structure viewer to superimpose structures based on
815 * currently associated alignments. May optionally return an error message for
819 protected String alignStructsWithAllAlignPanels()
821 if (getAlignmentPanel() == null)
826 if (_alignwith.size() == 0)
828 _alignwith.add(getAlignmentPanel());
834 reply = getBinding().superposeStructures(_alignwith);
835 if (reply != null && !reply.isEmpty())
837 String text = MessageManager
838 .formatMessage("error.superposition_failed", reply);
839 statusBar.setText(text);
841 } catch (Exception e)
843 StringBuffer sp = new StringBuffer();
844 for (AlignmentViewPanel alignPanel : _alignwith)
846 sp.append("'" + alignPanel.getViewName() + "' ");
848 Console.info("Couldn't align structures with the " + sp.toString()
849 + "associated alignment panels.", e);
855 * Opens a colour chooser dialog, and applies the chosen colour to the
856 * background of the structure viewer
859 public void background_actionPerformed()
861 String ttl = MessageManager.getString("label.select_background_colour");
862 ColourChooserListener listener = new ColourChooserListener()
865 public void colourSelected(Color c)
867 getBinding().setBackgroundColour(c);
870 JalviewColourChooser.showColourChooser(this, ttl, null, listener);
874 public void viewerColour_actionPerformed()
876 if (viewerColour.isSelected())
878 // disable automatic sequence colouring.
879 getBinding().setColourBySequence(false);
884 public void chainColour_actionPerformed()
886 chainColour.setSelected(true);
887 getBinding().colourByChain();
891 public void chargeColour_actionPerformed()
893 chargeColour.setSelected(true);
894 getBinding().colourByCharge();
898 public void seqColour_actionPerformed()
900 AAStructureBindingModel binding = getBinding();
901 binding.setColourBySequence(seqColour.isSelected());
902 if (_colourwith == null)
904 _colourwith = new Vector<>();
906 if (binding.isColourBySequence())
908 if (!binding.isLoadingFromArchive())
910 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
912 // Make the currently displayed alignment panel the associated view
913 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
916 // Set the colour using the current view for the associated alignframe
917 for (AlignmentViewPanel alignPanel : _colourwith)
919 binding.colourBySequence(alignPanel);
921 seqColoursApplied = true;
926 public void pdbFile_actionPerformed()
928 // TODO: JAL-3048 not needed for Jalview-JS - save PDB file
929 JalviewFileChooser chooser = new JalviewFileChooser(
930 Cache.getProperty("LAST_DIRECTORY"));
932 chooser.setFileView(new JalviewFileView());
933 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
934 chooser.setToolTipText(MessageManager.getString("action.save"));
936 int value = chooser.showSaveDialog(this);
938 if (value == JalviewFileChooser.APPROVE_OPTION)
940 BufferedReader in = null;
943 // TODO: cope with multiple PDB files in view
944 in = new BufferedReader(
945 new FileReader(getBinding().getStructureFiles()[0]));
946 File outFile = chooser.getSelectedFile();
948 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
950 while ((data = in.readLine()) != null)
952 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
958 } catch (Exception ex)
960 ex.printStackTrace();
968 } catch (IOException e)
978 public void viewMapping_actionPerformed()
980 CutAndPasteTransfer cap = new CutAndPasteTransfer();
983 cap.appendText(getBinding().printMappings());
984 } catch (OutOfMemoryError e)
987 "composing sequence-structure alignments for display in text box.",
992 Desktop.addInternalFrame(cap,
993 MessageManager.getString("label.pdb_sequence_mapping"), 550,
997 protected abstract String getViewerName();
1000 * Configures the title and menu items of the viewer panel.
1003 public void updateTitleAndMenus()
1005 AAStructureBindingModel binding = getBinding();
1006 if (binding.hasFileLoadingError())
1011 setChainMenuItems(binding.getChainNames());
1013 this.setTitle(binding.getViewerTitle(getViewerName(), true));
1016 * enable 'Superpose with' if more than one mapped structure
1018 viewSelectionMenu.setEnabled(false);
1019 if (getBinding().getMappedStructureCount() > 1
1020 && getBinding().getSequence().length > 1)
1022 viewSelectionMenu.setEnabled(true);
1026 * Show action menu if it has any enabled items
1028 viewerActionMenu.setVisible(false);
1029 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
1031 if (viewerActionMenu.getItem(i).isEnabled())
1033 viewerActionMenu.setVisible(true);
1038 if (!binding.isLoadingFromArchive())
1040 seqColour_actionPerformed();
1045 public String toString()
1051 public boolean hasMapping()
1053 if (worker != null && (addingStructures || _started))
1057 if (getBinding() == null)
1059 if (_aps == null || _aps.size() == 0)
1061 // viewer has been closed, but we did at some point run.
1066 String[] pdbids = getBinding().getStructureFiles();
1072 for (String pdbid : pdbids)
1074 StructureMapping sm[] = getBinding().getSsm().getMapping(pdbid);
1075 if (sm != null && sm.length > 0 && sm[0] != null)
1080 // only return true if there is a mapping for every structure file we have
1082 if (p == 0 || p != pdbids.length)
1086 // and that coloring has been applied
1087 return seqColoursApplied;
1091 public void raiseViewer()
1097 public long startProgressBar(String msg)
1099 // TODO would rather have startProgress/stopProgress as the
1100 // IProgressIndicator interface
1101 long tm = random.nextLong();
1102 if (progressBar != null)
1104 progressBar.setProgressBar(msg, tm);
1110 public void stopProgressBar(String msg, long handle)
1112 if (progressBar != null)
1114 progressBar.setProgressBar(msg, handle);
1118 protected IProgressIndicator getProgressIndicator()
1123 protected void setProgressIndicator(IProgressIndicator pi)
1128 public void setProgressMessage(String message, long id)
1130 if (progressBar != null)
1132 progressBar.setProgressBar(message, id);
1137 public void showConsole(boolean show)
1139 // default does nothing
1143 * Show only the selected chain(s) in the viewer
1145 protected void showSelectedChains()
1147 List<String> toshow = new ArrayList<>();
1148 for (int i = 0; i < chainMenu.getItemCount(); i++)
1150 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
1152 JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
1153 if (item.isSelected())
1155 toshow.add(item.getText());
1159 getBinding().showChains(toshow);
1163 * Tries to fetch a PDB file and save to a temporary local file. Returns the
1164 * saved file path if successful, or null if not.
1166 * @param processingEntry
1169 protected String fetchPdbFile(PDBEntry processingEntry)
1171 String filePath = null;
1172 Pdb pdbclient = new Pdb();
1173 EBIAlfaFold afclient = new EBIAlfaFold();
1174 AlignmentI pdbseq = null;
1175 String pdbid = processingEntry.getId();
1176 long handle = System.currentTimeMillis()
1177 + Thread.currentThread().hashCode();
1180 * Write 'fetching PDB' progress on AlignFrame as we are not yet visible
1182 String msg = MessageManager.formatMessage("status.fetching_pdb",
1185 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1186 // long hdl = startProgressBar(MessageManager.formatMessage(
1187 // "status.fetching_pdb", new Object[]
1191 if (afclient.isValidReference(pdbid))
1193 pdbseq = afclient.getSequenceRecords(pdbid,
1194 processingEntry.getRetrievalUrl());
1198 if (processingEntry.hasRetrievalUrl())
1200 String safePDBId = java.net.URLEncoder.encode(pdbid, "UTF-8")
1201 .replace("%", "__");
1203 // retrieve from URL to new local tmpfile
1204 File tmpFile = File.createTempFile(safePDBId,
1205 "." + (PDBEntry.Type.MMCIF.toString().equals(
1206 processingEntry.getType().toString()) ? "cif"
1208 String fromUrl = processingEntry.getRetrievalUrl();
1209 UrlDownloadClient.download(fromUrl, tmpFile);
1211 // may not need this check ?
1212 String file = tmpFile.getAbsolutePath();
1215 pdbseq = EBIAlfaFold.importDownloadedStructureFromUrl(fromUrl,
1216 tmpFile, pdbid, null, null, null);
1221 pdbseq = pdbclient.getSequenceRecords(pdbid);
1224 } catch (Exception e)
1226 jalview.bin.Console.errPrintln(
1227 "Error retrieving PDB id " + pdbid + ": " + e.getMessage());
1230 msg = pdbid + " " + MessageManager.getString("label.state_completed");
1231 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1232 // stopProgressBar(msg, hdl);
1235 * If PDB data were saved and are not invalid (empty alignment), return the
1238 if (pdbseq != null && pdbseq.getHeight() > 0)
1240 // just use the file name from the first sequence's first PDBEntry
1241 filePath = new File(pdbseq.getSequenceAt(0).getAllPDBEntries()
1242 .elementAt(0).getFile()).getAbsolutePath();
1243 processingEntry.setFile(filePath);
1249 * If supported, saves the state of the structure viewer to a temporary file
1250 * and returns the file, else returns null
1254 public File saveSession()
1256 if (getBinding() == null)
1260 File session = getBinding().saveSession();
1261 long l = session.length();
1268 } catch (InterruptedException e)
1271 long nextl = session.length();
1277 } while (--wait > 0);
1281 private static boolean quitClose = false;
1283 public static void setQuitClose(boolean b)
1289 public boolean stillRunning()
1291 AAStructureBindingModel binding = getBinding();
1292 return binding != null && binding.isViewerRunning();
1296 * Close down this instance of Jalview's Chimera viewer, giving the user the
1297 * option to close the associated Chimera window (process). They may wish to
1298 * keep it open until they have had an opportunity to save any work.
1301 * if true, close any linked Chimera process; if false, prompt first
1304 public void closeViewer(boolean forceClose)
1306 AAStructureBindingModel binding = getBinding();
1311 String viewerName = getViewerName();
1313 int confirm = JvOptionPane.CANCEL_OPTION;
1314 if (QuitHandler.quitting())
1316 // already asked about closing external windows
1317 confirm = quitClose ? JvOptionPane.YES_OPTION
1318 : JvOptionPane.NO_OPTION;
1322 String prompt = MessageManager
1323 .formatMessage("label.confirm_close_viewer", new Object[]
1324 { binding.getViewerTitle(viewerName, false),
1326 prompt = JvSwingUtils.wrapTooltip(true, prompt);
1327 String title = MessageManager.getString("label.close_viewer");
1328 confirm = showCloseDialog(title, prompt);
1332 * abort closure if user hits escape or Cancel
1334 if (confirm == JvOptionPane.CANCEL_OPTION
1335 || confirm == JvOptionPane.CLOSED_OPTION)
1337 // abort possible quit handling if CANCEL chosen
1338 if (confirm == JvOptionPane.CANCEL_OPTION)
1342 // this is a bit futile
1343 this.setClosed(false);
1344 } catch (PropertyVetoException e)
1347 QuitHandler.abortQuit();
1351 forceClose = confirm == JvOptionPane.YES_OPTION;
1354 if (binding != null)
1356 binding.closeViewer(forceClose);
1358 setAlignmentPanel(null);
1361 _colourwith.clear();
1362 // TODO: check for memory leaks where instance isn't finalised because jmb
1363 // holds a reference to the window
1368 } catch (Throwable t)
1370 Console.info("Unexpected exception when deregistering structure viewer",t);
1375 private int showCloseDialog(final String title, final String prompt)
1377 int confirmResponse = JvOptionPane.CANCEL_OPTION;
1378 confirmResponse = JvOptionPane.showConfirmDialog(this, prompt,
1379 MessageManager.getString("label.close_viewer"),
1380 JvOptionPane.YES_NO_CANCEL_OPTION,
1381 JvOptionPane.WARNING_MESSAGE);
1382 return confirmResponse;
1386 public void showHelp_actionPerformed()
1392 String url = getBinding().getHelpURL();
1395 BrowserLauncher.openURL(url);
1399 catch (IOException ex)
1402 .println("Show " + getViewerName() + " failed with: "
1409 public boolean hasViewerActionsMenu()
1411 return viewerActionMenu != null && viewerActionMenu.isEnabled()
1412 && viewerActionMenu.getItemCount() > 0
1413 && viewerActionMenu.isVisible();