2 VARNA is a tool for the automated drawing, visualization and annotation of the secondary structure of RNA, designed as a companion software for web servers and databases.
3 Copyright (C) 2008 Kevin Darty, Alain Denise and Yann Ponty.
4 electronic mail : Yann.Ponty@lri.fr
5 paper mail : LRI, bat 490 Universit� Paris-Sud 91405 Orsay Cedex France
7 This file is part of VARNA version 3.1.
8 VARNA version 3.1 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 VARNA version 3.1 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with VARNA version 3.1.
16 If not, see http://www.gnu.org/licenses.
18 package fr.orsay.lri.varna.controlers;
21 import java.awt.Color;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
25 import java.text.ParseException;
26 import java.util.ArrayList;
28 import javax.swing.JCheckBoxMenuItem;
29 import javax.swing.JColorChooser;
30 import javax.swing.JOptionPane;
32 import fr.orsay.lri.varna.VARNAPanel;
33 import fr.orsay.lri.varna.exceptions.ExceptionExportFailed;
34 import fr.orsay.lri.varna.exceptions.ExceptionJPEGEncoding;
35 import fr.orsay.lri.varna.exceptions.ExceptionNonEqualLength;
36 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
37 import fr.orsay.lri.varna.exceptions.ExceptionWritingForbidden;
38 import fr.orsay.lri.varna.interfaces.InterfaceVARNAListener;
39 import fr.orsay.lri.varna.models.VARNAConfig;
40 import fr.orsay.lri.varna.models.annotations.TextAnnotation;
41 import fr.orsay.lri.varna.models.rna.ModeleBase;
42 import fr.orsay.lri.varna.models.rna.ModeleBP;
43 import fr.orsay.lri.varna.models.rna.RNA;
44 import fr.orsay.lri.varna.views.VueBPThickness;
45 import fr.orsay.lri.varna.views.VueMenu;
48 * This listener controls menu items
53 public class ControleurMenu implements InterfaceVARNAListener,
56 private VARNAPanel _vp;
57 @SuppressWarnings("unused")
60 private String _color;
61 private Object _source;
64 * Creates the menu listener
69 public ControleurMenu(VARNAPanel _varnaPanel, VueMenu _vueMenu) {
72 _vp.getRNA().addVARNAListener(this);
75 public void actionPerformed(ActionEvent e) {
76 String[] temp = e.getActionCommand().split(",");
77 _source = e.getSource();
83 // selon l'option choisie dans le menu:
87 if (!optionRNADisplay())
89 if (!optionColorMap())
92 if (!optionBasePair())
99 if (!optionAnnotation())
100 if (!optionEditRNA())
101 _vp.errorDialog(new Exception("Uknown action command '"+_type+"'"));
104 private boolean optionEditRNA()
106 if (_type.equals("editallbps")) {
107 _vp.getVARNAUI().UIEditAllBasePairs();
109 else if (_type.equals("editallbases")) {
110 _vp.getVARNAUI().UIEditAllBases();
116 private boolean optionAnnotation() {
117 if (!_type.contains("annotation"))
119 // a partir du menu principale (gestion des annotations)
120 if (_type.equals("annotationsaddPosition")) {
121 _vp.getVARNAUI().UIAnnotationsAddPosition(_vp.getPopup().getSpawnPoint().x,_vp.getPopup().getSpawnPoint().y);
122 } else if (_type.equals("annotationsaddBase")) {
123 _vp.getVARNAUI().UIAnnotationsAddBase(_vp.getPopup().getSpawnPoint().x,_vp.getPopup().getSpawnPoint().y);
124 } else if (_type.equals("annotationsaddLoop")) {
125 _vp.getVARNAUI().UIAnnotationsAddLoop(_vp.getPopup().getSpawnPoint().x,_vp.getPopup().getSpawnPoint().y);
126 } else if (_type.equals("annotationsaddChemProb")) {
127 _vp.getVARNAUI().UIAnnotationsAddChemProb(_vp.getPopup().getSpawnPoint().x,_vp.getPopup().getSpawnPoint().y);
128 } else if (_type.equals("annotationsaddRegion")) {
129 _vp.getVARNAUI().UIAnnotationsAddRegion(_vp.getPopup().getSpawnPoint().x,_vp.getPopup().getSpawnPoint().y);
130 } else if (_type.equals("annotationsaddHelix")) {
131 _vp.getVARNAUI().UIAnnotationsAddHelix(_vp.getPopup().getSpawnPoint().x,_vp.getPopup().getSpawnPoint().y);
132 } else if (_type.equals("annotationsautohelices")) {
133 _vp.getVARNAUI().UIAutoAnnotateHelices();
134 } else if (_type.equals("annotationsautointerior")) {
135 _vp.getVARNAUI().UIAutoAnnotateInteriorLoops();
136 } else if (_type.equals("annotationsautoterminal")) {
137 _vp.getVARNAUI().UIAutoAnnotateTerminalLoops();
138 } else if (_type.equals("annotationsautohelices")) {
139 _vp.getVARNAUI().UIAutoAnnotateHelices();
140 } else if (_type.equals("annotationsremove")) {
141 _vp.getVARNAUI().UIAnnotationsRemove();
142 } else if (_type.equals("annotationsautoextremites")) {
143 _vp.getVARNAUI().UIAutoAnnotateStrandEnds();
144 } else if (_type.equals("annotationsedit")) {
145 _vp.getVARNAUI().UIAnnotationsEdit();
146 // a partir du menu selection (annotation la plus proche)
147 } else if (_type.equals("Selectionannotationremove")) {
148 _vp.getVARNAUI().UIAnnotationRemoveFromAnnotation(_vp.get_selectedAnnotation());
149 } else if (_type.equals("Selectionannotationedit")) {
150 _vp.getVARNAUI().UIAnnotationEditFromAnnotation(_vp.get_selectedAnnotation());
152 // a partir d'une structure(base, loop, helix) dans l'arn
153 // (annotation li� a la structure)
154 } else if (_type.endsWith("annotationadd")||_type.contains("annotationremove")||_type.contains("annotationedit"))
157 TextAnnotation.AnchorType type = trouverAncrage();
158 ArrayList<Integer> listeIndex = new ArrayList<Integer>();
162 listeIndex.add(_vp.getNearestBase());
164 if (_type.startsWith("loop1"))
165 listeIndex = _vp.getRNA().findLoopForward(_vp.getNearestBase());
166 else if (_type.startsWith("loop2"))
167 listeIndex = _vp.getRNA().findLoopBackward(_vp.getNearestBase());
169 listeIndex = _vp.getRNA().findLoop(_vp.getNearestBase());
172 listeIndex = _vp.getRNA().findHelix(_vp.getNearestBase());
175 if (_type.endsWith("annotationadd"))
176 { _vp.getVARNAUI().UIAnnotationAddFromStructure(type,listeIndex); }
177 else if (_type.contains("annotationremove"))
178 { _vp.getVARNAUI().UIAnnotationRemoveFromStructure(trouverAncrage(),listeIndex); }
179 else if (_type.contains("annotationedit"))
180 { _vp.getVARNAUI().UIAnnotationEditFromStructure(trouverAncrage(),listeIndex); }
182 } catch (Exception e2) {
183 e2.printStackTrace();
190 private TextAnnotation.AnchorType trouverAncrage() {
191 if (_type.contains("loop"))
192 return TextAnnotation.AnchorType.LOOP;
193 if (_type.contains("helix"))
194 return TextAnnotation.AnchorType.HELIX;
195 if (_type.contains("base"))
196 return TextAnnotation.AnchorType.BASE;
197 errorDialog(new Exception("probleme d'identification de l'ancrage"));
198 return TextAnnotation.AnchorType.POSITION;
201 private boolean option5prime() {
205 private boolean option3prime() {
209 private boolean optionBulge() {
213 private boolean optionStem() {
217 private boolean optionHelix() {
221 private boolean colorBases() {
222 // System.out.println(_type);
223 ArrayList<Integer> listBase = new ArrayList<Integer>();
224 String phrase = "Choose new " + _type;
225 if (_color.equals("InnerColor")) {
226 phrase += " inner color";
227 Color c = JColorChooser.showDialog(_vp, phrase,
228 VARNAConfig.BASE_INNER_COLOR_DEFAULT);
230 listBase = listSwitchType(_type);
231 for (int i = 0; i < listBase.size(); i++) {
232 _vp.getRNA().get_listeBases().get(listBase.get(i))
233 .getStyleBase().setBaseInnerColor(c);
237 } else if (_color.equals("OutlineColor")) {
238 phrase += " outline color";
239 Color c = JColorChooser.showDialog(_vp, phrase,
240 VARNAConfig.BASE_OUTLINE_COLOR_DEFAULT);
242 listBase = listSwitchType(_type);
243 for (int i = 0; i < listBase.size(); i++) {
244 _vp.getRNA().get_listeBases().get(listBase.get(i))
245 .getStyleBase().setBaseOutlineColor(c);
249 } else if (_color.equals("NameColor")) {
250 phrase += " name color";
251 Color c = JColorChooser.showDialog(_vp, phrase,
252 VARNAConfig.BASE_NAME_COLOR_DEFAULT);
254 listBase = listSwitchType(_type);
255 for (int i = 0; i < listBase.size(); i++) {
256 _vp.getRNA().get_listeBases().get(listBase.get(i))
257 .getStyleBase().setBaseNameColor(c);
261 } else if (_color.equals("NumberColor")) {
262 phrase += " number color";
263 Color c = JColorChooser.showDialog(_vp, phrase,
264 VARNAConfig.BASE_NUMBER_COLOR_DEFAULT);
266 listBase = listSwitchType(_type);
267 for (int i = 0; i < listBase.size(); i++) {
268 _vp.getRNA().get_listeBases().get(listBase.get(i))
269 .getStyleBase().setBaseNumberColor(c);
273 } else if (_color.equals("BPColor")) {
274 phrase += " base-pair color";
275 Color c = JColorChooser.showDialog(_vp, phrase,
276 VARNAConfig.BASE_NUMBER_COLOR_DEFAULT);
278 listBase = listSwitchType(_type);
279 for (int i = 0; i < listBase.size(); i++)
281 for (ModeleBP msbp:_vp.getRNA().getBPsAt(listBase.get(i)))
284 msbp.getStyle().setCustomColor(c);
290 } else if (_color.equals("BPColor")) {
291 phrase += " base-pair color";
292 Color c = JColorChooser.showDialog(_vp, phrase,
293 VARNAConfig.BASE_NUMBER_COLOR_DEFAULT);
295 listBase = listSwitchType(_type);
296 for (int i = 0; i < listBase.size(); i++) {
297 ModeleBase mb = _vp.getRNA().get_listeBases().get(
299 if (mb.getElementStructure() != -1) {
300 mb.getStyleBP().getStyle().setCustomColor(c);
305 } else if (_color.equals("BPThickness")) {
306 listBase = listSwitchType(_type);
307 // System.out.println(listBase.size());
308 ArrayList<ModeleBP> styleBPs = new ArrayList<ModeleBP>();
309 for (int i = 0; i < listBase.size(); i++) {
310 ModeleBase mb = _vp.getRNA().get_listeBases().get(
312 if (mb.getElementStructure() != -1) {
313 styleBPs.add(mb.getStyleBP());
316 VueBPThickness vbpt = new VueBPThickness(_vp, styleBPs);
317 if (JOptionPane.showConfirmDialog(_vp, vbpt.getPanel(),
318 "Set base pair(s) thickness", JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION) {
319 vbpt.restoreThicknesses();
327 private ArrayList<Integer> listSwitchType(String _type) {
328 if (_type.equals("helix"))
329 return _vp.getRNA().findHelix(_vp.getNearestBase());
330 if (_type.equals("current")) {
331 return _vp.getSelectionIndices();
333 if (_type.equals("allBases")) {
334 return _vp.getRNA().findAll();
336 if (_type.equals("loop1")) {
337 return _vp.getRNA().findLoopForward(_vp.getNearestBase());
339 if (_type.equals("loop2")) {
340 return _vp.getRNA().findLoopBackward(_vp.getNearestBase());
342 if (_type.equals("stem"))
343 return _vp.getRNA().findStem(_vp.getNearestBase());
344 if (_type.equals("base")) {
345 ArrayList<Integer> list = new ArrayList<Integer>();
346 list.add(_vp.getNearestBase());
349 if (_type.equals("basepair") || _type.equals("bpcolor")
350 || _type.equals("bp")) {
351 ArrayList<Integer> list = new ArrayList<Integer>();
352 int i = _vp.getNearestBase();
354 ModeleBase mb = _vp.getRNA().get_listeBases().get(i);
355 int j = mb.getElementStructure();
356 if (mb.getElementStructure() != -1) {
362 if (_type.equals("5'"))
363 return _vp.getRNA().findNonPairedBaseGroup(_vp.getNearestBase());
364 if (_type.equals("3'"))
365 return _vp.getRNA().findNonPairedBaseGroup(_vp.getNearestBase());
366 if (_type.equals("bulge"))
367 return _vp.getRNA().findNonPairedBaseGroup(_vp.getNearestBase());
368 if (_type.equals("all"))
369 return _vp.getRNA().findAll();
370 return new ArrayList<Integer>();
373 private boolean optionLoop() {
377 private boolean optionBase() {
378 if (_type.equals("baseChar")) {
379 _vp.getVARNAUI().UISetBaseCharacter();
386 private boolean optionBasePair() {
387 if (_type.equals("basepair")) {
388 _vp.getVARNAUI().UIEditBasePair();
390 } else if (_type.equals("bpcolor")) {
391 _vp.getVARNAUI().UIColorBasePair();
393 } else if (_type.equals("thickness")) {
394 _vp.getVARNAUI().UIThicknessBasePair();
402 private boolean optionView() {
403 if (_type.equals("background")) {
404 _vp.getVARNAUI().UISetBackground();
405 } else if (_type.equals("shownc")) {
406 _vp.getVARNAUI().UIToggleShowNCBP();
407 } else if (_type.equals("showbackbone")) {
408 _vp.getVARNAUI().UIToggleDrawBackbone();
409 } else if (_type.equals("shownp")) {
410 _vp.getVARNAUI().UIToggleShowNonPlanar();
411 } else if (_type.equals("spaceBetweenBases")) {
412 _vp.getVARNAUI().UISetSpaceBetweenBases();
413 } else if (_type.equals("bpheightincrement")) {
414 _vp.getVARNAUI().UISetBPHeightIncrement();
415 } else if (_type.equals("borderSize")) {
416 _vp.getVARNAUI().UISetBorder();
417 } else if (_type.startsWith("zoom")) {
418 if (_type.equals("zoom")) {
419 _vp.getVARNAUI().UICustomZoom();
421 String factor = _type.substring("zoom".length());
422 double pc = Integer.parseInt(factor);
424 _vp.setZoom(new Double(pc));
427 } else if (_type.equals("rotation")) {
428 _vp.getVARNAUI().UIGlobalRotation();
429 } else if (_type.equals("rescale")) {
430 _vp.getVARNAUI().UIGlobalRescale();
437 private boolean optionTitle() {
438 if (_type.equals("titleDisplay")) {
439 _vp.getVARNAUI().UISetTitleFont();
440 } else if (_type.equals("setTitle")) {
441 _vp.getVARNAUI().UISetTitle();
442 } else if (_type.equals("titleColor")) {
443 _vp.getVARNAUI().UISetTitleColor();
450 private boolean optionColorMap() {
451 if (_type.equals("toggleshowcolormap")) {
452 _vp.getVARNAUI().UIToggleColorMap();
453 } else if (_type.equals("colormapcaption")) {
454 _vp.getVARNAUI().UISetColorMapCaption();
455 } else if (_type.equals("colormapstyle")) {
456 _vp.getVARNAUI().UISetColorMapStyle();
457 } else if (_type.equals("colormaploadvalues")) {
458 _vp.getVARNAUI().UILoadColorMapValues();
459 } else if (_type.equals("colormapvalues")) {
460 _vp.getVARNAUI().UISetColorMapValues();
466 private boolean optionRNADisplay() {
467 // les options d'affichages generales
468 if (_type.equals("gaspin")) {
469 _vp.getVARNAUI().UIToggleGaspinMode();
470 } else if (_type.equals("backbone")) {
471 _vp.getVARNAUI().UISetBackboneColor();
472 } else if (_type.equals("bonds")) {
473 Color c = JColorChooser.showDialog(_vp, "Choose new bonds color",
474 _vp.getBackground());
476 _vp.setDefaultBPColor(c);
479 } else if (_type.equals("basecolorforBP")) {
480 if (_source != null) {
481 if (_source instanceof JCheckBoxMenuItem) {
482 JCheckBoxMenuItem check = (JCheckBoxMenuItem) _source;
483 _vp.setUseBaseColorsForBPs(check.getState());
487 } else if (_type.equals("bpstyle")) {
488 _vp.getVARNAUI().UISetBPStyle();
489 } else if (_type.equals("specialbasecolored")) {
490 _vp.getVARNAUI().UIToggleColorSpecialBases();
491 } else if (_type.equals("showwarnings")) {
492 _vp.getVARNAUI().UIToggleShowWarnings();
493 } else if (_type.equals("dashbasecolored")) {
494 _vp.getVARNAUI().UIToggleColorGapsBases();
495 } else if (_type.equals("numPeriod")) {
496 _vp.getVARNAUI().UISetNumPeriod();
497 } else if (_type.equals("eachKind")) {
498 if (_vp.getRNA().get_listeBases() != null) {
499 _vp.getVARNAUI().UIBaseTypeColor();
501 _vp.emitWarning("No base");
503 } else if (_type.equals("eachCouple")) {
504 if (_vp.getRNA().get_listeBases() != null
505 && _vp.getRNA().get_listeBases().size() != 0) {
506 _vp.getVARNAUI().UIBasePairTypeColor();
508 _vp.emitWarning("No base");
510 } else if (_type.equals("eachBase")) {
511 if (_vp.getRNA().get_listeBases() != null
512 && _vp.getRNA().get_listeBases().size() != 0) {
513 _vp.getVARNAUI().UIBaseAllColor();
515 _vp.emitWarning("No base");
517 } else if (_type.equals("specialBasesColor")) {
518 _vp.getVARNAUI().UIPickSpecialBasesColor();
519 } else if (_type.equals("dashBasesColor")) {
520 _vp.getVARNAUI().UIPickGapsBasesColor();
526 private boolean optionImport() {
527 if (_type.equals("userInput")) {
529 _vp.getVARNAUI().UIManualInput();
530 } catch (ParseException e1) {
532 } catch (ExceptionNonEqualLength e2) {
535 } else if (_type.equals("file")) {
537 _vp.getVARNAUI().UIFile();
538 } catch (ExceptionNonEqualLength e1) {
541 } else if (_type.equals("print")) {
542 _vp.getVARNAUI().UIPrint();
543 } else if (_type.equals("about")) {
544 _vp.getVARNAUI().UIAbout();
550 private boolean optionRedraw() {
551 if (_type.equals("reset")) {
552 _vp.getVARNAUI().UIReset();
553 } else if (_type.equals("circular")) {
554 _vp.getVARNAUI().UICircular();
555 } else if (_type.equals("radiate")) {
556 _vp.getVARNAUI().UIRadiate();
557 } else if (_type.equals("naview")) {
558 _vp.getVARNAUI().UINAView();
559 } else if (_type.equals("varnaview")) {
560 _vp.getVARNAUI().UIVARNAView();
561 } else if (_type.equals("motifview")) {
562 _vp.getVARNAUI().UIMOTIFView();
563 } else if (_type.equals("line")) {
564 _vp.getVARNAUI().UILine();
565 } else if (_type.equals("flat")) {
566 _vp.getVARNAUI().UIToggleFlatExteriorLoop();
572 private boolean optionExport() {
573 if (_type.equals("saveas")) {
575 _vp.getVARNAUI().UISaveAs();
576 } catch (ExceptionExportFailed e1) {
578 } catch (ExceptionPermissionDenied e1) {
581 } else if (_type.equals("dbn")) {
583 _vp.getVARNAUI().UISaveAsDBN();
584 } catch (ExceptionExportFailed e) {
586 } catch (ExceptionPermissionDenied e) {
589 } else if (_type.equals("bpseq")) {
591 _vp.getVARNAUI().UISaveAsBPSEQ();
592 } catch (ExceptionExportFailed e) {
594 } catch (ExceptionPermissionDenied e) {
597 } else if (_type.equals("ct")) {
599 _vp.getVARNAUI().UISaveAsCT();
600 } catch (ExceptionExportFailed e) {
602 } catch (ExceptionPermissionDenied e) {
605 } else if (_type.equals("eps")) {
607 _vp.getVARNAUI().UIExportEPS();
608 } catch (ExceptionWritingForbidden e1) {
610 } catch (ExceptionExportFailed e) {
613 } else if (_type.equals("tikz")) {
615 _vp.getVARNAUI().UIExportTIKZ();
616 } catch (ExceptionWritingForbidden e1) {
618 } catch (ExceptionExportFailed e) {
621 } else if (_type.equals("xfig")) {
623 _vp.getVARNAUI().UIExportXFIG();
624 } catch (ExceptionWritingForbidden e1) {
626 } catch (ExceptionExportFailed e) {
629 } else if (_type.equals("svg")) {
631 _vp.getVARNAUI().UIExportSVG();
632 } catch (ExceptionWritingForbidden e1) {
634 } catch (ExceptionExportFailed e) {
637 } else if (_type.equals("jpeg")) {
639 _vp.getVARNAUI().UIExportJPEG();
640 } catch (ExceptionJPEGEncoding e1) {
642 } catch (ExceptionExportFailed e1) {
645 } else if (_type.equals("png")) {
647 _vp.getVARNAUI().UIExportPNG();
648 } catch (ExceptionExportFailed e1) {
657 * Return the extension of a file, it means the string after the last dot of
662 * @return <code>null</code> if the file name have no dot<br>
663 * <code>ext</code> if the file name contains a dot
665 public String getExtension(File f) {
666 String s = f.getName();
667 return getExtension(s);
671 * Return the extension of a string, it means the string after the last dot
675 * The strnig o the path
676 * @return <code>null</code> if the path have no dot<br>
677 * <code>ext</code> if the path contains a dot
679 public String getExtension(String s) {
681 int i = s.lastIndexOf('.');
683 if (i > 0 && i < s.length() - 1) {
684 ext = s.substring(i + 1).toLowerCase();
690 * Open an error message dialog with the exception message
693 * The <code>Exception</code>
695 public void errorDialog(Exception e1) {
696 if (_vp.isErrorsOn())
697 JOptionPane.showMessageDialog(_vp, e1.getMessage(), "VARNA Error",
698 JOptionPane.ERROR_MESSAGE);
701 public void onStructureRedrawn() {
702 // TODO Auto-generated method stub
706 public void onWarningEmitted(String s) {
707 if (_vp.isErrorsOn())
708 JOptionPane.showMessageDialog(_vp,s, "VARNA Warning",
709 JOptionPane.ERROR_MESSAGE);
712 public void onLoad(String path) {
713 // TODO Auto-generated method stub
717 public void onLoaded() {
718 // TODO Auto-generated method stub
722 public void onUINewStructure(VARNAConfig v, RNA r) {
723 // TODO Auto-generated method stub
727 public void onZoomLevelChanged() {
728 // TODO Auto-generated method stub
732 public void onTranslationChanged() {
733 // TODO Auto-generated method stub