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.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Component;
26 import java.awt.Dimension;
27 import java.awt.datatransfer.DataFlavor;
28 import java.awt.datatransfer.Transferable;
29 import java.awt.dnd.DnDConstants;
30 import java.awt.dnd.DropTarget;
31 import java.awt.dnd.DropTargetAdapter;
32 import java.awt.dnd.DropTargetDropEvent;
33 import java.awt.event.ComponentEvent;
34 import java.awt.event.MouseAdapter;
35 import java.awt.event.MouseEvent;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.List;
42 import javax.swing.DefaultListModel;
43 import javax.swing.DefaultListSelectionModel;
44 import javax.swing.JLabel;
45 import javax.swing.JList;
46 import javax.swing.JPanel;
47 import javax.swing.JScrollPane;
48 import javax.swing.ListModel;
49 import javax.swing.ListSelectionModel;
50 import javax.swing.event.ListSelectionEvent;
51 import javax.swing.event.ListSelectionListener;
53 import fr.orsay.lri.varna.VARNAPanel;
54 import fr.orsay.lri.varna.components.ReorderableJList;
55 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
56 import fr.orsay.lri.varna.exceptions.ExceptionNAViewAlgorithm;
57 import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength;
58 import fr.orsay.lri.varna.models.FullBackup;
59 import fr.orsay.lri.varna.models.VARNAConfig;
60 import fr.orsay.lri.varna.models.rna.RNA;
62 import jalview.datamodel.SequenceI;
63 import jalview.ext.varna.JalviewVarnaBinding;
64 import jalview.structure.AtomSpec;
65 import jalview.util.MessageManager;
67 public class AppVarnaBinding extends JalviewVarnaBinding
71 // remove unused (commented out) fields?
72 // protected JPanel _tools = new JPanel();
74 // private JPanel _input = new JPanel();
76 // private JPanel _strPanel = new JPanel();
78 // private JTextField _str = new JTextField();
80 // private JTextField _seq = new JTextField();
82 // private JLabel _strLabel = new JLabel(
83 // MessageManager.getString("label.str"));
85 // private JButton _updateButton = new JButton(
86 // MessageManager.getString("action.update"));
88 // private JButton _deleteButton = new JButton(
89 // MessageManager.getString("action.delete"));
91 // private JButton _duplicateButton = new JButton(
92 // MessageManager.getString("action.snapshot"));
94 protected JPanel _listPanel = new JPanel();
96 private ReorderableJList _sideList = null;
98 private static String errorOpt = "error";
100 @SuppressWarnings("unused")
101 private boolean _error;
103 private Color _backgroundColor = Color.white;
105 private static int _nextID = 1;
107 @SuppressWarnings("unused")
108 private int _algoCode;
110 private BackupHolder _rnaList;
115 public AppVarnaBinding()
121 * Constructs the VARNAPanel and an (empty) selection list of structures to
126 DefaultListModel<FullBackup> dlm = new DefaultListModel<FullBackup>();
128 int marginTools = 40;
130 DefaultListSelectionModel m = new DefaultListSelectionModel();
131 m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
132 m.setLeadAnchorNotificationEnabled(false);
134 _sideList = new ReorderableJList();
135 _sideList.setModel(dlm);
136 _sideList.addMouseListener(new MouseAdapter()
139 public void mouseClicked(MouseEvent e) {
140 AppVarnaBinding.this.mouseClicked(e);
143 _sideList.setSelectionModel(m);
144 _sideList.setPreferredSize(new Dimension(100, 0));
145 _sideList.addListSelectionListener(new ListSelectionListener()
147 public void valueChanged(ListSelectionEvent evt)
149 changeSelectedStructure_actionPerformed(evt);
152 _rnaList = new BackupHolder(dlm, _sideList);
156 vp = new VARNAPanel("0", ".");
157 } catch (ExceptionNonEqualLength e)
161 vp.setPreferredSize(new Dimension(400, 400));
163 JScrollPane listScroller = new JScrollPane(_sideList);
164 listScroller.setPreferredSize(new Dimension(150, 0));
166 vp.setBackground(_backgroundColor);
168 // MC commented out stuff not added to panel - remove?
169 // Font textFieldsFont = Font.decode("MonoSpaced-PLAIN-12");
171 // _seq.setFont(textFieldsFont);
172 // if (!rnaList.isEmpty())
174 // _seq.setText(rnaList.get(0).getSeq());
177 // _updateButton.addActionListener(new ActionListener()
179 // public void actionPerformed(ActionEvent e)
181 // FullBackup sel = (FullBackup) _sideList.getSelectedValue();
182 // sel.rna.setSequence("A");
186 // _strLabel.setPreferredSize(new Dimension(marginTools, 15));
187 // _strLabel.setHorizontalTextPosition(JLabel.LEFT);
188 // _str.setFont(textFieldsFont);
189 // _strPanel.setLayout(new BorderLayout());
190 // _strPanel.add(_strLabel, BorderLayout.WEST);
191 // _strPanel.add(_str, BorderLayout.CENTER);
193 // _input.setLayout(new GridLayout(1, 0));
194 // _input.add(_strPanel);
196 // JPanel goPanel = new JPanel();
197 // goPanel.setLayout(new BorderLayout());
199 // _tools.setLayout(new BorderLayout());
200 // _tools.add(_input, BorderLayout.CENTER);
201 // _tools.add(goPanel, BorderLayout.EAST);
203 // goPanel.add(_updateButton, BorderLayout.CENTER);
205 // JPanel ops = new JPanel();
206 // ops.setLayout(new GridLayout(1, 2));
207 // ops.add(_deleteButton);
208 // ops.add(_duplicateButton);
210 JLabel j = new JLabel(
211 MessageManager.getString("label.structures_manager"),
213 _listPanel.setLayout(new BorderLayout());
215 _listPanel.add(j, BorderLayout.NORTH);
216 _listPanel.add(listScroller, BorderLayout.CENTER);
218 new DropTarget(vp, new DropTargetAdapter() {
220 public void drop(DropTargetDropEvent dtde)
222 AppVarnaBinding.this.drop(dtde);
227 public JPanel getListPanel()
233 * Returns the currently selected RNA, or null if none selected
237 public RNA getSelectedRNA()
239 int selectedIndex = _sideList.getSelectedIndex();
240 if (selectedIndex < 0)
244 FullBackup selected = _rnaList.getElementAt(selectedIndex);
249 * Substitute currently selected RNA with the edited one
253 public void updateSelectedRNA(RNA rnaEdit)
259 public static String generateDefaultName()
261 return "User file #" + _nextID++;
264 public String[][] getParameterInfo()
268 // Parameter Name Kind of Value Description,
269 { "sequenceDBN", "String", "A raw RNA sequence" },
270 { "structureDBN", "String",
271 "An RNA structure in dot bracket notation (DBN)" },
272 { errorOpt, "boolean", "To show errors" }, };
276 @SuppressWarnings("unused")
277 private Color getSafeColor(String col, Color def)
282 result = Color.decode(col);
283 } catch (Exception e)
287 result = Color.getColor(col, def);
288 } catch (Exception e2)
296 public VARNAPanel get_varnaPanel()
301 public void set_varnaPanel(VARNAPanel surface)
306 public void drop(DropTargetDropEvent dtde)
310 Transferable tr = dtde.getTransferable();
311 DataFlavor[] flavors = tr.getTransferDataFlavors();
312 for (int i = 0; i < flavors.length; i++)
314 if (flavors[i].isFlavorJavaFileListType())
316 dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
317 Object ob = tr.getTransferData(flavors[i]);
318 if (ob instanceof List)
320 List list = (List) ob;
321 for (int j = 0; j < list.size(); j++)
323 Object o = list.get(j);
325 if (dtde.getSource() instanceof DropTarget)
327 DropTarget dt = (DropTarget) dtde.getSource();
328 Component c = dt.getComponent();
329 if (c instanceof VARNAPanel)
331 String path = o.toString();
332 VARNAPanel varnaPanel = (VARNAPanel) c;
335 FullBackup bck = VARNAPanel.importSession(path);
336 _rnaList.add(bck.config, bck.rna, bck.name, true);
337 } catch (ExceptionLoadingFailed e3)
340 Collection<RNA> mdls = fr.orsay.lri.varna.factories.RNAFactory
344 r.drawRNA(varnaPanel.getConfig());
345 String name = r.getName();
348 name = path.substring(path
349 .lastIndexOf(File.separatorChar) + 1);
353 name += " (Model " + mn++ + ")";
355 _rnaList.add(varnaPanel.getConfig().clone(), r, name, true);
362 // If we made it this far, everything worked.
363 dtde.dropComplete(true);
367 // Hmm, the user must not have dropped a file list
369 } catch (Exception e)
377 private class BackupHolder
379 private DefaultListModel<FullBackup> _rnalist;
381 private List<RNA> _rnas = new ArrayList<RNA>();
385 public BackupHolder(DefaultListModel<FullBackup> rnaList, JList l)
391 public void add(VARNAConfig c, RNA r, String name)
393 add(c, r, name, false);
397 * Adds an entry to the end of the selection list and (optionally) sets it
405 public void add(VARNAConfig c, RNA r, String name, boolean select)
409 _l.removeSelectionInterval(0, _rnalist.size());
413 name = generateDefaultName();
415 FullBackup bck = new FullBackup(c, r, name);
417 _rnalist.addElement(bck);
420 _l.setSelectedIndex(0);
424 public FullBackup getElementAt(int i)
426 return _rnalist.getElementAt(i);
430 public void mouseClicked(MouseEvent e)
432 if (e.getClickCount() == 2)
434 int index = _sideList.locationToIndex(e.getPoint());
435 ListModel<FullBackup> dlm = _sideList.getModel();
436 // FullBackup item = dlm.getElementAt(index);
438 _sideList.ensureIndexIsVisible(index);
440 * TODO Object newName = JOptionPane.showInputDialog( this,
441 * "Specify a new name for this RNA", "Rename RNA",
442 * JOptionPane.QUESTION_MESSAGE, (Icon)null, null, item.toString()); if
443 * (newName!=null) { item.name = newName.toString();
444 * this._sideList.repaint(); }
450 public String[] getPdbFile()
456 public void releaseReferences(Object svl)
461 public void updateColours(Object source)
466 public void componentHidden(ComponentEvent e)
471 public void componentMoved(ComponentEvent e)
476 public void componentResized(ComponentEvent e)
481 public void componentShown(ComponentEvent e)
486 public void highlightAtoms(List<AtomSpec> atoms)
491 public boolean isListeningFor(SequenceI seq)
497 * Returns the path to a temporary file containing a representation of the
498 * state of the Varna display, or null on any error
505 public String getStateInfo(RNA rna)
513 * we have to show the RNA we want to save in the viewer; get the currently
514 * displayed model first so we can restore it
516 FullBackup sel = (FullBackup) _sideList.getSelectedValue();
518 FullBackup model = null;
519 ListModel models = _sideList.getModel();
520 for (int i = 0; i < models.getSize(); i++)
522 model = (FullBackup) models.getElementAt(i);
523 if (model.rna == rna)
536 vp.showRNA(model.rna, model.config);
541 temp = File.createTempFile("varna", null);
543 String filePath = temp.getAbsolutePath();
547 * restore the previous display
549 vp.showRNA(sel.rna, sel.config);
552 } catch (IOException e)
558 public int getSelectedIndex()
560 return _sideList.getSelectedIndex();
564 * Switch the Varna display to the structure selected in the left hand panel
568 protected void changeSelectedStructure_actionPerformed(ListSelectionEvent evt)
570 if (!evt.getValueIsAdjusting())
572 showSelectedStructure();
579 protected void showSelectedStructure()
581 FullBackup sel = (FullBackup) _sideList.getSelectedValue();
584 vp.showRNA(sel.rna, sel.config);
589 * Set and display the selected item in the list of structures
593 public void setSelectedIndex(final int selectedRna)
596 * note this does nothing if, say, selecting item 3 when only 1 has been
599 _sideList.setSelectedIndex(selectedRna);
600 // TODO ? need a worker thread to get this to happen properly
604 * Add an RNA structure to the selection list
608 public void addStructure(RNA rna)
610 VARNAConfig config = vp.getConfig().clone();
611 addStructure(rna, config);
618 protected void addStructure(final RNA rna, final VARNAConfig config)
620 drawRna(rna, config);
621 _rnaList.add(config, rna, rna.getName());
628 protected void drawRna(final RNA rna, final VARNAConfig config)
632 rna.drawRNA(rna.getDrawMode(), config);
633 } catch (ExceptionNAViewAlgorithm e)
635 // only throwable for draw mode = 3 NAView
636 System.err.println("Error drawing RNA: " + e.getMessage());