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 jalview.datamodel.SequenceI;
24 import jalview.ext.varna.JalviewVarnaBinding;
25 import jalview.structure.AtomSpec;
26 import jalview.util.MessageManager;
28 import java.awt.BorderLayout;
29 import java.awt.Color;
30 import java.awt.Component;
31 import java.awt.Dimension;
32 import java.awt.datatransfer.DataFlavor;
33 import java.awt.datatransfer.Transferable;
34 import java.awt.dnd.DnDConstants;
35 import java.awt.dnd.DropTarget;
36 import java.awt.dnd.DropTargetAdapter;
37 import java.awt.dnd.DropTargetDropEvent;
38 import java.awt.event.ComponentEvent;
39 import java.awt.event.MouseAdapter;
40 import java.awt.event.MouseEvent;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.List;
47 import javax.swing.DefaultListModel;
48 import javax.swing.DefaultListSelectionModel;
49 import javax.swing.JLabel;
50 import javax.swing.JList;
51 import javax.swing.JPanel;
52 import javax.swing.JScrollPane;
53 import javax.swing.ListModel;
54 import javax.swing.ListSelectionModel;
55 import javax.swing.event.ListSelectionEvent;
56 import javax.swing.event.ListSelectionListener;
58 import fr.orsay.lri.varna.VARNAPanel;
59 import fr.orsay.lri.varna.components.ReorderableJList;
60 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
61 import fr.orsay.lri.varna.exceptions.ExceptionNAViewAlgorithm;
62 import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength;
63 import fr.orsay.lri.varna.models.FullBackup;
64 import fr.orsay.lri.varna.models.VARNAConfig;
65 import fr.orsay.lri.varna.models.rna.RNA;
67 public class AppVarnaBinding extends JalviewVarnaBinding
71 protected JPanel _listPanel = new JPanel();
73 private ReorderableJList _sideList = null;
75 private static String errorOpt = "error";
77 @SuppressWarnings("unused")
78 private boolean _error;
80 private Color _backgroundColor = Color.white;
82 private static int _nextID = 1;
84 @SuppressWarnings("unused")
85 private int _algoCode;
87 private BackupHolder _rnaList;
92 public AppVarnaBinding()
98 * Constructs the VARNAPanel and an (empty) selection list of structures to
103 DefaultListModel<FullBackup> dlm = new DefaultListModel<FullBackup>();
105 int marginTools = 40;
107 DefaultListSelectionModel m = new DefaultListSelectionModel();
108 m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
109 m.setLeadAnchorNotificationEnabled(false);
111 _sideList = new ReorderableJList();
112 _sideList.setModel(dlm);
113 _sideList.addMouseListener(new MouseAdapter()
116 public void mouseClicked(MouseEvent e)
118 AppVarnaBinding.this.mouseClicked(e);
121 _sideList.setSelectionModel(m);
122 _sideList.setPreferredSize(new Dimension(100, 0));
123 _sideList.addListSelectionListener(new ListSelectionListener()
125 public void valueChanged(ListSelectionEvent evt)
127 changeSelectedStructure_actionPerformed(evt);
130 _rnaList = new BackupHolder(dlm, _sideList);
134 vp = new VARNAPanel("0", ".");
135 } catch (ExceptionNonEqualLength e)
139 vp.setPreferredSize(new Dimension(400, 400));
141 JScrollPane listScroller = new JScrollPane(_sideList);
142 listScroller.setPreferredSize(new Dimension(150, 0));
144 vp.setBackground(_backgroundColor);
146 JLabel j = new JLabel(
147 MessageManager.getString("label.structures_manager"),
149 _listPanel.setLayout(new BorderLayout());
151 _listPanel.add(j, BorderLayout.NORTH);
152 _listPanel.add(listScroller, BorderLayout.CENTER);
154 new DropTarget(vp, new DropTargetAdapter()
157 public void drop(DropTargetDropEvent dtde)
159 AppVarnaBinding.this.drop(dtde);
164 public JPanel getListPanel()
170 * Returns the currently selected RNA, or null if none selected
174 public RNA getSelectedRNA()
176 int selectedIndex = _sideList.getSelectedIndex();
177 if (selectedIndex < 0)
181 FullBackup selected = _rnaList.getElementAt(selectedIndex);
186 * Substitute currently selected RNA with the edited one
190 public void updateSelectedRNA(RNA rnaEdit)
196 public static String generateDefaultName()
198 return "User file #" + _nextID++;
201 public String[][] getParameterInfo()
204 // Parameter Name Kind of Value Description,
205 { "sequenceDBN", "String", "A raw RNA sequence" },
206 { "structureDBN", "String",
207 "An RNA structure in dot bracket notation (DBN)" },
208 { errorOpt, "boolean", "To show errors" }, };
212 @SuppressWarnings("unused")
213 private Color getSafeColor(String col, Color def)
218 result = Color.decode(col);
219 } catch (Exception e)
223 result = Color.getColor(col, def);
224 } catch (Exception e2)
232 public VARNAPanel get_varnaPanel()
237 public void set_varnaPanel(VARNAPanel surface)
242 public void drop(DropTargetDropEvent dtde)
246 Transferable tr = dtde.getTransferable();
247 DataFlavor[] flavors = tr.getTransferDataFlavors();
248 for (int i = 0; i < flavors.length; i++)
250 if (flavors[i].isFlavorJavaFileListType())
252 dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
253 Object ob = tr.getTransferData(flavors[i]);
254 if (ob instanceof List)
256 List list = (List) ob;
257 for (int j = 0; j < list.size(); j++)
259 Object o = list.get(j);
261 if (dtde.getSource() instanceof DropTarget)
263 DropTarget dt = (DropTarget) dtde.getSource();
264 Component c = dt.getComponent();
265 if (c instanceof VARNAPanel)
267 String path = o.toString();
268 VARNAPanel varnaPanel = (VARNAPanel) c;
271 FullBackup bck = VARNAPanel.importSession(path);
272 _rnaList.add(bck.config, bck.rna, bck.name, true);
273 } catch (ExceptionLoadingFailed e3)
276 Collection<RNA> mdls = fr.orsay.lri.varna.factories.RNAFactory
280 r.drawRNA(varnaPanel.getConfig());
281 String name = r.getName();
284 name = path.substring(
285 path.lastIndexOf(File.separatorChar) + 1);
289 name += " (Model " + mn++ + ")";
291 _rnaList.add(varnaPanel.getConfig().clone(), r, name,
293 // BH 2018 SwingJS clone of varnaPanel or its config will be the object itself, not a clone
300 // If we made it this far, everything worked.
301 dtde.dropComplete(true);
305 // Hmm, the user must not have dropped a file list
307 } catch (Exception e)
315 private class BackupHolder
317 private DefaultListModel<FullBackup> _rnalist;
319 private List<RNA> _rnas = new ArrayList<RNA>();
323 public BackupHolder(DefaultListModel<FullBackup> rnaList, JList l)
329 public void add(VARNAConfig c, RNA r, String name)
331 add(c, r, name, false);
335 * Adds an entry to the end of the selection list and (optionally) sets it
343 public void add(VARNAConfig c, RNA r, String name, boolean select)
347 _l.removeSelectionInterval(0, _rnalist.size());
351 name = generateDefaultName();
353 FullBackup bck = new FullBackup(c, r, name);
355 _rnalist.addElement(bck);
358 _l.setSelectedIndex(0);
362 public FullBackup getElementAt(int i)
364 return _rnalist.getElementAt(i);
368 public void mouseClicked(MouseEvent e)
370 if (e.getClickCount() == 2)
372 int index = _sideList.locationToIndex(e.getPoint());
373 ListModel<FullBackup> dlm = _sideList.getModel();
374 // FullBackup item = dlm.getElementAt(index);
376 _sideList.ensureIndexIsVisible(index);
378 * TODO Object newName = JvOptionPane.showInputDialog( this,
379 * "Specify a new name for this RNA", "Rename RNA",
380 * JvOptionPane.QUESTION_MESSAGE, (Icon)null, null, item.toString()); if
381 * (newName!=null) { item.name = newName.toString();
382 * this._sideList.repaint(); }
388 public String[] getStructureFiles()
394 public void releaseReferences(Object svl)
399 public void updateColours(Object source)
404 public void componentHidden(ComponentEvent e)
409 public void componentMoved(ComponentEvent e)
414 public void componentResized(ComponentEvent e)
419 public void componentShown(ComponentEvent e)
424 public void highlightAtoms(List<AtomSpec> atoms)
429 public boolean isListeningFor(SequenceI seq)
435 * Returns the path to a temporary file containing a representation of the
436 * state of the Varna display, or null on any error
443 public String getStateInfo(RNA rna)
451 * we have to show the RNA we want to save in the viewer; get the currently
452 * displayed model first so we can restore it
454 FullBackup sel = (FullBackup) _sideList.getSelectedValue();
456 FullBackup model = null;
457 ListModel models = _sideList.getModel();
458 for (int i = 0; i < models.getSize(); i++)
460 model = (FullBackup) models.getElementAt(i);
461 if (model.rna == rna)
474 vp.showRNA(model.rna, model.config);
479 temp = File.createTempFile("varna", null);
481 String filePath = temp.getAbsolutePath();
485 * restore the previous display
487 vp.showRNA(sel.rna, sel.config);
490 } catch (IOException e)
496 public int getSelectedIndex()
498 return _sideList.getSelectedIndex();
502 * Switch the Varna display to the structure selected in the left hand panel
506 protected void changeSelectedStructure_actionPerformed(
507 ListSelectionEvent evt)
509 if (!evt.getValueIsAdjusting())
511 showSelectedStructure();
518 protected void showSelectedStructure()
520 FullBackup sel = (FullBackup) _sideList.getSelectedValue();
523 vp.showRNA(sel.rna, sel.config);
528 * Set and display the selected item in the list of structures
532 public void setSelectedIndex(final int selectedRna)
535 * note this does nothing if, say, selecting item 3 when only 1 has been
538 _sideList.setSelectedIndex(selectedRna);
539 // TODO ? need a worker thread to get this to happen properly
543 * Add an RNA structure to the selection list
547 public void addStructure(RNA rna)
549 VARNAConfig config = vp.getConfig().clone(); // BH 2018 this will NOT be a clone in SwingJS
550 addStructure(rna, config);
557 protected void addStructure(final RNA rna, final VARNAConfig config)
559 drawRna(rna, config);
560 _rnaList.add(config, rna, rna.getName());
567 protected void drawRna(final RNA rna, final VARNAConfig config)
571 rna.drawRNA(rna.getDrawMode(), config);
572 } catch (ExceptionNAViewAlgorithm e)
574 // only throwable for draw mode = 3 NAView
575 System.err.println("Error drawing RNA: " + e.getMessage());