JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / fr / orsay / lri / varna / controlers / ControleurVARNAPanelKeys.java
1 /*
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
6
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.
10
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.
14
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.
17  */
18 package fr.orsay.lri.varna.controlers;
19
20 import java.awt.Point;
21 import java.awt.event.FocusEvent;
22 import java.awt.event.FocusListener;
23 import java.awt.event.KeyEvent;
24 import java.awt.event.KeyListener;
25 import java.awt.event.MouseEvent;
26 import java.awt.event.MouseListener;
27 import java.awt.geom.Point2D;
28
29 import fr.orsay.lri.varna.VARNAPanel;
30 import fr.orsay.lri.varna.views.VueUI;
31
32
33 /**
34  * VARNAPanel Shortcuts Controller
35  * 
36  * @author darty
37  * 
38  */
39 public class ControleurVARNAPanelKeys implements KeyListener, FocusListener {
40
41         private VARNAPanel _vp;
42         
43
44         public ControleurVARNAPanelKeys(VARNAPanel vp) {
45                 _vp = vp;
46         }
47
48         public void mouseClicked(MouseEvent e) {
49         }
50
51         public void mouseEntered(MouseEvent e) {
52                 // prise du focus
53                 //_vp.requestFocus();
54                 
55         }
56
57         public void mouseExited(MouseEvent e) {
58         }
59
60         public void mousePressed(MouseEvent e) {
61         }
62
63         public void mouseReleased(MouseEvent e) {
64         }
65
66         public void keyPressed(KeyEvent e) {
67                 boolean controlDown = (e.getModifiersEx() & (KeyEvent.CTRL_DOWN_MASK)) == KeyEvent.CTRL_DOWN_MASK;
68                 boolean shiftDown = (e.getModifiersEx() & (KeyEvent.SHIFT_DOWN_MASK)) == KeyEvent.SHIFT_DOWN_MASK;
69                 boolean altDown = (e.getModifiersEx() & (KeyEvent.ALT_DOWN_MASK)) == KeyEvent.ALT_DOWN_MASK;
70                 VueUI ui = _vp.getVARNAUI();
71                 try {
72                         switch (e.getKeyCode()) {
73                         case (KeyEvent.VK_A):
74                                 if (controlDown) {
75                                         ui.UIAbout();
76                                 }
77                                 break;
78                         case (KeyEvent.VK_B):
79                                 if (controlDown) {
80                                         ui.UISetBorder();
81                                 }
82                                 else
83                                         if (altDown) {
84                                                 ui.UIToggleDrawBackbone();
85                                         }
86                                 break;
87                         case (KeyEvent.VK_C):
88                                 if (shiftDown && controlDown) {
89                                         ui.UISetColorMapCaption();
90                                 }
91                                 break;
92                         case (KeyEvent.VK_D):
93                                 if (controlDown) {
94                                         if (shiftDown) {
95                                                 ui.UIPickGapsBasesColor();
96                                         } else {
97                                                 ui.UIToggleColorGapsBases();
98                                         }
99                                 }
100                                 break;
101                         case (KeyEvent.VK_E):
102                                 if (controlDown) {
103                                         ui.UIToggleShowNonPlanar();
104                                 }
105                                 break;
106                         case (KeyEvent.VK_F):
107                                 if (controlDown) {
108                                         ui.UIToggleFlatExteriorLoop();
109                                 }
110                                 break;
111                         case (KeyEvent.VK_G):
112                                 if (controlDown) {
113                                         ui.UISetBackground();
114                                 }
115                                 else if (!shiftDown && altDown) {
116                                   ui.UIToggleGaspinMode();
117                                 }
118                                 break;
119                         case (KeyEvent.VK_H):
120                                 if (controlDown && !shiftDown) {
121                                         ui.UISetBPHeightIncrement();
122                                 }
123                                 else if (controlDown && shiftDown)
124                                 {
125                                         Point2D.Double p = _vp.getLastSelectedPosition();
126                                         ui.UIAnnotationsAddPosition((int)p.x,(int)p.y);
127                                 }
128                                 break;
129                         case (KeyEvent.VK_J):
130                                 if (controlDown) {
131                                         if (shiftDown) {
132                                                 ui.UIPickSpecialBasesColor();
133                                         } else {
134                                                 ui.UIToggleColorSpecialBases();
135                                         }
136                                 }
137                                 break;
138                         case (KeyEvent.VK_K):
139                                 if (controlDown && shiftDown) {
140                                         ui.UILoadColorMapValues();
141                                 }
142                                 else if (controlDown) {
143                                         ui.UISetBackboneColor();
144                                 }
145                                 break;
146                         case (KeyEvent.VK_L):
147                                 if (shiftDown && controlDown) {
148                                         ui.UIToggleColorMap();
149                                 } else if (controlDown)
150                                 {
151                                         ui.UISetColorMapStyle();
152                                 } else if (shiftDown)
153                                 {
154                                         ui.UISetColorMapValues();
155                                 } 
156                                 break;
157                         case (KeyEvent.VK_M):
158                                 if (controlDown) {
159                                         ui.UISetNumPeriod();
160                                 } else if (shiftDown && altDown) {
161                                         ui.UIToggleModifiable();
162                                 }
163                                 break;
164                         case (KeyEvent.VK_N):
165                                 if (controlDown) {
166                                         ui.UIManualInput();
167                                 }
168                                 break;
169                         case (KeyEvent.VK_O):
170                                 if (controlDown) {
171                                         ui.UIFile();
172                                 }
173                                 break;
174                         case (KeyEvent.VK_P):
175                                 if (controlDown && shiftDown) {
176                                         ui.UISetBPStyle();
177                                 }
178                                 else if (controlDown && !shiftDown) {
179                                         ui.UIPrint();
180                                 }
181                                 break;
182                         case (KeyEvent.VK_Q):
183                                 if      (controlDown && !shiftDown && !altDown) {
184                                         _vp.getVARNAUI().UIAutoAnnotateHelices();
185                                 }
186                                 else if (controlDown && shiftDown && !altDown) {
187                                         _vp.getVARNAUI().UIAutoAnnotateTerminalLoops();                                 
188                                 }
189                                 else if (!controlDown && shiftDown && altDown) {
190                                         _vp.getVARNAUI().UIAutoAnnotateInteriorLoops();                                 
191                                 }
192                                 else if (controlDown && !shiftDown && altDown) {
193                                         _vp.getVARNAUI().UIAutoAnnotateStrandEnds();                                    
194                                 }
195                                 break;
196                         case (KeyEvent.VK_R):
197                                 if (controlDown) {
198                                         if (shiftDown) {
199                                                 ui.UIReset();
200                                         } else {
201                                                 ui.UIGlobalRotation();
202                                         }
203                                 }
204                                 break;
205                         case (KeyEvent.VK_S):
206                                 if (controlDown) {
207                                         if (shiftDown) {
208                                                 ui.UISetSpaceBetweenBases();
209                                         } else {
210                                                 ui.UISaveAs();
211                                         }
212                                 }
213                                 break;
214                         case (KeyEvent.VK_T):
215                                 if (controlDown) {
216                                         if (shiftDown) {
217                                                 ui.UISetTitleFont();
218                                         } else if (altDown) {
219                                                 ui.UISetTitleColor();
220                                         } else {
221                                                 ui.UISetTitle();
222                                         }
223                                 }
224                                 break;
225                         case (KeyEvent.VK_U):
226                                 if (controlDown && !shiftDown && !altDown) {
227                                         _vp.getVARNAUI().UIBaseTypeColor();
228                                 } else if (!controlDown && shiftDown && !altDown) {
229                                         _vp.getVARNAUI().UIBasePairTypeColor();
230                                 } else if (!controlDown && !shiftDown && altDown) {
231                                         _vp.getVARNAUI().UIBaseAllColor();
232                                 }
233                                 break;
234                         case (KeyEvent.VK_W):
235                                 if (controlDown) {
236                                         ui.UIToggleShowNCBP();
237                                 }
238                                 break;
239                         case (KeyEvent.VK_X):
240                                 if (controlDown) {
241                                         ui.UIExport();
242                                 }
243                                 break;
244                         case (KeyEvent.VK_Y):
245                                 if (controlDown) {
246                                         ui.UIRedo();
247                                 }
248                         break;
249
250                         case (KeyEvent.VK_Z):
251                                 if (controlDown && !shiftDown) {
252                                         ui.UIUndo();
253                                 }
254                                 else if (controlDown && shiftDown) {
255                                         ui.UIRedo();
256                                 }
257                                 else if (!controlDown && !shiftDown) {
258                                         ui.UICustomZoom();
259                                 }
260                                 break;
261                         case (KeyEvent.VK_1):
262                                 if (controlDown) {
263                                         ui.UILine();
264                                 }
265                                 break;
266                         case (KeyEvent.VK_2):
267                                 if (controlDown) {
268                                         ui.UICircular();
269                                 }
270                                 break;
271                         case (KeyEvent.VK_3):
272                                 if (controlDown) {
273                                         ui.UIRadiate();
274                                 }
275                                 break;
276                         case (KeyEvent.VK_4):
277                                 if (controlDown) {
278                                         ui.UINAView();
279                                 }
280                                 break;
281                         case (KeyEvent.VK_5):
282                                 if (controlDown) {
283                                         ui.UIVARNAView();
284                                 }
285                                 break;
286                         case (KeyEvent.VK_6):
287                                 if (controlDown) {
288                                         ui.UIMOTIFView();
289                                 }
290                                 break;
291
292                         // Navigation control keys (Zoom in/out, arrow keys ...)
293                         case (KeyEvent.VK_DOWN):
294                                 if (_vp.getZoom() > 1) {
295                                         _vp.setTranslation(new Point(_vp.getTranslation().x,_vp.getTranslation().y-5));
296                                         _vp.checkTranslation();
297                                 }
298                                 break;
299                         case (KeyEvent.VK_UP):
300                                 if (_vp.getZoom() > 1) {
301                                         _vp.setTranslation(new Point(_vp.getTranslation().x,_vp.getTranslation().y+5));
302                                         _vp.checkTranslation();
303                                 }
304                                 break;
305                         case (KeyEvent.VK_LEFT):
306                                 if (_vp.getZoom() > 1) {
307                                         _vp.setTranslation(new Point(_vp.getTranslation().x+5,_vp.getTranslation().y));
308                                         _vp.checkTranslation();
309                                 }
310                                 break;
311                         case (KeyEvent.VK_RIGHT):
312                                 if (_vp.getZoom() > 1) {
313                                         _vp.setTranslation(new Point(_vp.getTranslation().x-5,_vp.getTranslation().y));
314                                         _vp.checkTranslation();
315                                 }
316                                 break;
317                         case (KeyEvent.VK_EQUALS):
318                         case (KeyEvent.VK_PLUS):
319                                 ui.UIZoomIn();
320                                 break;
321                         case (KeyEvent.VK_MINUS):
322                                 ui.UIZoomOut();
323                                 break;
324                         }
325                 } catch (Exception e1) {
326                         _vp.errorDialog(e1);
327                 }
328                 _vp.repaint();
329         }
330
331         /**
332          * if ((e.getKeyCode() == KeyEvent.VK_PLUS)||(e.getKeyChar() == '+')) {
333          * _vp.getVARNAUI().UIZoomIn(); } else if (e.getKeyCode() ==
334          * KeyEvent.VK_MINUS) { _vp.getVARNAUI().UIZoomOut(); } // 1 pour Redraw
335          * Radiate else if (e.getKeyChar() == KeyEvent.VK_1) {
336          * _vp.getVARNAUI().UIRadiate(); } // 2 pour Redraw Circular else if
337          * (e.getKeyChar() == KeyEvent.VK_2) { _vp.getVARNAUI().UICircular(); } // 3
338          * pour Redraw NAView else if (e.getKeyChar() == KeyEvent.VK_3) {
339          * _vp.getVARNAUI().UINAView(); }
340          * 
341          * // 4 for RNA on a line else if (e.getKeyChar() == KeyEvent.VK_4) {
342          * _vp.getVARNAUI().UILine(); } // 5 fun arn random coord else if
343          * (e.isControlDown() && e.getKeyChar() == KeyEvent.VK_9) { for (int i = 0;
344          * i < _vp.getRNA().get_listeBases().size(); i++) {
345          * _vp.getRNA().get_listeBases().get(i).set_coords( new
346          * Point2D.Double(_vp.getWidth() * Math.random(), _vp.getHeight() *
347          * Math.random())); _vp.getRNA().get_listeBases().get(i).set_center( new
348          * Point2D.Double(_vp.getWidth() / 2 Math.random(), _vp.getHeight() / 2
349          * Math.random())); } } // 6 fun random arn structure else if
350          * (e.isControlDown() & e.getKeyChar() == KeyEvent.VK_8) { try {
351          * _vp.drawRNA(_vp.getRNA().getListeBasesToString(), getRandomRNA(), _vp
352          * .getRNA().get_drawMode()); } catch (ExceptionNonEqualLength e1) {
353          * _vp.errorDialog(e1); } } _vp.repaint(); }
354          **/
355
356         public String getRandomRNA() {
357                 int pile = 0, j, i = 0;
358                 double l;
359                 String fun = "";
360                 while (i < 2000) {
361                         if (Math.random() > 0.5) {
362                                 j = 0;
363                                 l = Math.random() * 10;
364                                 while (j < l) {
365                                         fun += '.';
366                                         i++;
367                                         j++;
368                                 }
369                         } else {
370                                 if (Math.random() > 0.5 && pile > 0) {
371                                         j = 0;
372                                         l = Math.random() * 5;
373                                         while (j < l && pile > 0) {
374                                                 fun += ')';
375                                                 pile--;
376                                                 j++;
377                                                 i++;
378                                         }
379                                 } else {
380                                         j = 0;
381                                         l = Math.random() * 5;
382                                         while (j < l) {
383                                                 fun += '(';
384                                                 pile++;
385                                                 j++;
386                                                 i++;
387                                         }
388
389                                 }
390                         }
391                 }
392                 while (pile > 0) {
393                         fun += ')';
394                         pile--;
395                 }
396                 return fun;
397         }
398
399         public void keyReleased(KeyEvent e) {
400         }
401
402         public void keyTyped(KeyEvent e) {
403         }
404
405         public void focusGained(FocusEvent arg0) {
406                 _vp.repaint();
407         }
408
409         public void focusLost(FocusEvent arg0) {
410                 _vp.repaint();
411         }
412 }