JAL-1517 fix copyright for 2.8.2
[jalview.git] / src / jalview / appletgui / RotatableCanvas.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.appletgui;
22
23 import java.util.*;
24
25 import java.awt.*;
26 import java.awt.event.*;
27
28 import jalview.api.RotatableCanvasI;
29 import jalview.datamodel.*;
30 import jalview.math.*;
31 import jalview.util.*;
32
33 public class RotatableCanvas extends Panel implements MouseListener,
34         MouseMotionListener, KeyListener, RotatableCanvasI
35 {
36   RotatableMatrix idmat = new RotatableMatrix(3, 3);
37
38   RotatableMatrix objmat = new RotatableMatrix(3, 3);
39
40   RotatableMatrix rotmat = new RotatableMatrix(3, 3);
41
42   String tooltip;
43
44   int toolx, tooly;
45
46   // RubberbandRectangle rubberband;
47
48   boolean drawAxes = true;
49
50   int omx = 0;
51
52   int mx = 0;
53
54   int omy = 0;
55
56   int my = 0;
57
58   Image img;
59
60   Graphics ig;
61
62   Dimension prefsize;
63
64   float centre[] = new float[3];
65
66   float width[] = new float[3];
67
68   float max[] = new float[3];
69
70   float min[] = new float[3];
71
72   float maxwidth;
73
74   float scale;
75
76   int npoint;
77
78   Vector points;
79
80   float[][] orig;
81
82   float[][] axes;
83
84   int startx;
85
86   int starty;
87
88   int lastx;
89
90   int lasty;
91
92   int rectx1;
93
94   int recty1;
95
96   int rectx2;
97
98   int recty2;
99
100   float scalefactor = 1;
101
102   AlignViewport av;
103
104   boolean showLabels = false;
105
106   public RotatableCanvas(AlignViewport av)
107   {
108     this.av = av;
109   }
110
111   public void showLabels(boolean b)
112   {
113     showLabels = b;
114     repaint();
115   }
116
117   public void setPoints(Vector points, int npoint)
118   {
119     this.points = points;
120     this.npoint = npoint;
121     PaintRefresher.Register(this, av.getSequenceSetId());
122
123     prefsize = getPreferredSize();
124     orig = new float[npoint][3];
125
126     for (int i = 0; i < npoint; i++)
127     {
128       SequencePoint sp = (SequencePoint) points.elementAt(i);
129       for (int j = 0; j < 3; j++)
130       {
131         orig[i][j] = sp.coord[j];
132       }
133     }
134     // Initialize the matrices to identity
135
136     for (int i = 0; i < 3; i++)
137     {
138       for (int j = 0; j < 3; j++)
139       {
140         if (i != j)
141         {
142           idmat.addElement(i, j, 0);
143           objmat.addElement(i, j, 0);
144           rotmat.addElement(i, j, 0);
145         }
146         else
147         {
148           idmat.addElement(i, j, 0);
149           objmat.addElement(i, j, 0);
150           rotmat.addElement(i, j, 0);
151         }
152       }
153     }
154
155     axes = new float[3][3];
156     initAxes();
157
158     findCentre();
159     findWidth();
160
161     scale = findScale();
162
163     // System.out.println("Scale factor = " + scale);
164
165     addMouseListener(this);
166     addKeyListener(this);
167     // if (getParent() != null) {
168     // getParent().addKeyListener(this);
169     // }
170     addMouseMotionListener(this);
171
172     // Add rubberband
173     // rubberband = new RubberbandRectangle(this);
174     // rubberband.setActive(true);
175     // rubberband.addListener(this);
176   }
177
178   /*
179    * public boolean handleSequenceSelectionEvent(SequenceSelectionEvent evt) {
180    * redrawneeded = true; repaint(); return true; }
181    * 
182    * public void removeNotify() { controller.removeListener(this);
183    * super.removeNotify(); }
184    */
185
186   public void initAxes()
187   {
188     for (int i = 0; i < 3; i++)
189     {
190       for (int j = 0; j < 3; j++)
191       {
192         if (i != j)
193         {
194           axes[i][j] = 0;
195         }
196         else
197         {
198           axes[i][j] = 1;
199         }
200       }
201     }
202   }
203
204   public void findWidth()
205   {
206     max = new float[3];
207     min = new float[3];
208
209     max[0] = (float) -1e30;
210     max[1] = (float) -1e30;
211     max[2] = (float) -1e30;
212
213     min[0] = (float) 1e30;
214     min[1] = (float) 1e30;
215     min[2] = (float) 1e30;
216
217     for (int i = 0; i < 3; i++)
218     {
219       for (int j = 0; j < npoint; j++)
220       {
221         SequencePoint sp = (SequencePoint) points.elementAt(j);
222         if (sp.coord[i] >= max[i])
223         {
224           max[i] = sp.coord[i];
225         }
226         if (sp.coord[i] <= min[i])
227         {
228           min[i] = sp.coord[i];
229         }
230       }
231     }
232
233     // System.out.println("xmax " + max[0] + " min " + min[0]);
234     // System.out.println("ymax " + max[1] + " min " + min[1]);
235     // System.out.println("zmax " + max[2] + " min " + min[2]);
236
237     width[0] = Math.abs(max[0] - min[0]);
238     width[1] = Math.abs(max[1] - min[1]);
239     width[2] = Math.abs(max[2] - min[2]);
240
241     maxwidth = width[0];
242
243     if (width[1] > width[0])
244     {
245       maxwidth = width[1];
246     }
247     if (width[2] > width[1])
248     {
249       maxwidth = width[2];
250     }
251
252     // System.out.println("Maxwidth = " + maxwidth);
253   }
254
255   public float findScale()
256   {
257     int dim, width, height;
258     if (getSize().width != 0)
259     {
260       width = getSize().width;
261       height = getSize().height;
262     }
263     else
264     {
265       width = prefsize.width;
266       height = prefsize.height;
267     }
268
269     if (width < height)
270     {
271       dim = width;
272     }
273     else
274     {
275       dim = height;
276     }
277
278     return (float) (dim * scalefactor / (2 * maxwidth));
279   }
280
281   public void findCentre()
282   {
283     // Find centre coordinate
284     findWidth();
285
286     centre[0] = (max[0] + min[0]) / 2;
287     centre[1] = (max[1] + min[1]) / 2;
288     centre[2] = (max[2] + min[2]) / 2;
289
290     // System.out.println("Centre x " + centre[0]);
291     // System.out.println("Centre y " + centre[1]);
292     // System.out.println("Centre z " + centre[2]);
293   }
294
295   public Dimension getPreferredSize()
296   {
297     if (prefsize != null)
298     {
299       return prefsize;
300     }
301     else
302     {
303       return new Dimension(400, 400);
304     }
305   }
306
307   public Dimension getMinimumSize()
308   {
309     return getPreferredSize();
310   }
311
312   public void update(Graphics g)
313   {
314     paint(g);
315   }
316
317   public void paint(Graphics g)
318   {
319     if (points == null)
320     {
321       g.setFont(new Font("Verdana", Font.PLAIN, 18));
322       g.drawString(MessageManager.getString("label.calculating_pca") + "....", 20, getSize().height / 2);
323     }
324     else
325     {
326
327       // Only create the image at the beginning -
328       if ((img == null) || (prefsize.width != getSize().width)
329               || (prefsize.height != getSize().height))
330       {
331         prefsize.width = getSize().width;
332         prefsize.height = getSize().height;
333
334         scale = findScale();
335
336         // System.out.println("New scale = " + scale);
337         img = createImage(getSize().width, getSize().height);
338         ig = img.getGraphics();
339
340       }
341
342       drawBackground(ig, Color.black);
343       drawScene(ig);
344       if (drawAxes == true)
345       {
346         drawAxes(ig);
347       }
348
349       if (tooltip != null)
350       {
351         ig.setColor(Color.red);
352         ig.drawString(tooltip, toolx, tooly);
353       }
354
355       g.drawImage(img, 0, 0, this);
356     }
357   }
358
359   public void drawAxes(Graphics g)
360   {
361
362     g.setColor(Color.yellow);
363     for (int i = 0; i < 3; i++)
364     {
365       g.drawLine(getSize().width / 2, getSize().height / 2,
366               (int) (axes[i][0] * scale * max[0] + getSize().width / 2),
367               (int) (axes[i][1] * scale * max[1] + getSize().height / 2));
368     }
369   }
370
371   public void drawBackground(Graphics g, Color col)
372   {
373     g.setColor(col);
374     g.fillRect(0, 0, prefsize.width, prefsize.height);
375   }
376
377   public void drawScene(Graphics g)
378   {
379     // boolean darker = false;
380
381     int halfwidth = getSize().width / 2;
382     int halfheight = getSize().height / 2;
383
384     for (int i = 0; i < npoint; i++)
385     {
386       SequencePoint sp = (SequencePoint) points.elementAt(i);
387       int x = (int) ((float) (sp.coord[0] - centre[0]) * scale) + halfwidth;
388       int y = (int) ((float) (sp.coord[1] - centre[1]) * scale)
389               + halfheight;
390       float z = sp.coord[1] - centre[2];
391
392       if (av.getSequenceColour(sp.sequence) == Color.black)
393       {
394         g.setColor(Color.white);
395       }
396       else
397       {
398         g.setColor(av.getSequenceColour(sp.sequence));
399       }
400
401       if (av.getSelectionGroup() != null)
402       {
403         if (av.getSelectionGroup().getSequences(null)
404                 .contains(((SequencePoint) points.elementAt(i)).sequence))
405         {
406           g.setColor(Color.gray);
407         }
408       }
409       if (z < 0)
410       {
411         g.setColor(g.getColor().darker());
412       }
413
414       g.fillRect(x - 3, y - 3, 6, 6);
415       if (showLabels)
416       {
417         g.setColor(Color.red);
418         g.drawString(
419                 ((SequencePoint) points.elementAt(i)).sequence.getName(),
420                 x - 3, y - 4);
421       }
422     }
423   }
424
425   public Dimension minimumsize()
426   {
427     return prefsize;
428   }
429
430   public Dimension preferredsize()
431   {
432     return prefsize;
433   }
434
435   public void keyTyped(KeyEvent evt)
436   {
437   }
438
439   public void keyReleased(KeyEvent evt)
440   {
441   }
442
443   public void keyPressed(KeyEvent evt)
444   {
445     if (evt.getKeyCode() == KeyEvent.VK_UP)
446     {
447       scalefactor = (float) (scalefactor * 1.1);
448       scale = findScale();
449     }
450     else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
451     {
452       scalefactor = (float) (scalefactor * 0.9);
453       scale = findScale();
454     }
455     else if (evt.getKeyChar() == 's')
456     {
457       System.err.println("DEBUG: Rectangle selection"); // log.debug
458       if (rectx2 != -1 && recty2 != -1)
459       {
460         rectSelect(rectx1, recty1, rectx2, recty2);
461
462       }
463     }
464     repaint();
465   }
466
467   public void printPoints()
468   {
469     for (int i = 0; i < npoint; i++)
470     {
471       SequencePoint sp = (SequencePoint) points.elementAt(i);
472       Format.print(System.out, "%5d ", i);
473       for (int j = 0; j < 3; j++)
474       {
475         Format.print(System.out, "%13.3f  ", sp.coord[j]);
476       }
477       System.out.println();
478     }
479   }
480
481   public void mouseClicked(MouseEvent evt)
482   {
483   }
484
485   public void mouseEntered(MouseEvent evt)
486   {
487   }
488
489   public void mouseExited(MouseEvent evt)
490   {
491   }
492
493   public void mouseReleased(MouseEvent evt)
494   {
495   }
496
497   public void mousePressed(MouseEvent evt)
498   {
499     int x = evt.getX();
500     int y = evt.getY();
501
502     mx = x;
503     my = y;
504
505     omx = mx;
506     omy = my;
507
508     startx = x;
509     starty = y;
510
511     rectx1 = x;
512     recty1 = y;
513
514     rectx2 = -1;
515     recty2 = -1;
516
517     SequenceI found = findPoint(x, y);
518
519     if (found != null)
520     {
521       // TODO: applet PCA is not associatable with multi-panels - only parent
522       // view
523       if (av.getSelectionGroup() != null)
524       {
525         av.getSelectionGroup().addOrRemove(found, true);
526         av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
527       }
528       else
529       {
530         av.setSelectionGroup(new SequenceGroup());
531         av.getSelectionGroup().addOrRemove(found, true);
532         av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
533
534       }
535       PaintRefresher.Refresh(this, av.getSequenceSetId());
536       av.sendSelection();
537     }
538     repaint();
539   }
540
541   public void mouseMoved(MouseEvent evt)
542   {
543     SequenceI found = findPoint(evt.getX(), evt.getY());
544     if (found == null)
545     {
546       tooltip = null;
547     }
548     else
549     {
550       tooltip = found.getName();
551       toolx = evt.getX();
552       tooly = evt.getY();
553     }
554     repaint();
555   }
556
557   public void mouseDragged(MouseEvent evt)
558   {
559     mx = evt.getX();
560     my = evt.getY();
561
562     rotmat.setIdentity();
563
564     rotmat.rotate((float) (my - omy), 'x');
565     rotmat.rotate((float) (mx - omx), 'y');
566
567     for (int i = 0; i < npoint; i++)
568     {
569       SequencePoint sp = (SequencePoint) points.elementAt(i);
570       sp.coord[0] -= centre[0];
571       sp.coord[1] -= centre[1];
572       sp.coord[2] -= centre[2];
573
574       // Now apply the rotation matrix
575       sp.coord = rotmat.vectorMultiply(sp.coord);
576
577       // Now translate back again
578       sp.coord[0] += centre[0];
579       sp.coord[1] += centre[1];
580       sp.coord[2] += centre[2];
581     }
582
583     for (int i = 0; i < 3; i++)
584     {
585       axes[i] = rotmat.vectorMultiply(axes[i]);
586     }
587     omx = mx;
588     omy = my;
589
590     paint(this.getGraphics());
591   }
592
593   public void rectSelect(int x1, int y1, int x2, int y2)
594   {
595     // boolean changedSel = false;
596     for (int i = 0; i < npoint; i++)
597     {
598       SequencePoint sp = (SequencePoint) points.elementAt(i);
599       int tmp1 = (int) ((sp.coord[0] - centre[0]) * scale + (float) getSize().width / 2.0);
600       int tmp2 = (int) ((sp.coord[1] - centre[1]) * scale + (float) getSize().height / 2.0);
601
602       if (tmp1 > x1 && tmp1 < x2 && tmp2 > y1 && tmp2 < y2)
603       {
604         if (av != null)
605         {
606           if (!av.getSelectionGroup().getSequences(null)
607                   .contains(sp.sequence))
608           {
609             av.getSelectionGroup().addSequence(sp.sequence, true);
610           }
611         }
612       }
613     }
614   }
615
616   public SequenceI findPoint(int x, int y)
617   {
618
619     int halfwidth = getSize().width / 2;
620     int halfheight = getSize().height / 2;
621
622     int found = -1;
623
624     for (int i = 0; i < npoint; i++)
625     {
626
627       SequencePoint sp = (SequencePoint) points.elementAt(i);
628       int px = (int) ((float) (sp.coord[0] - centre[0]) * scale)
629               + halfwidth;
630       int py = (int) ((float) (sp.coord[1] - centre[1]) * scale)
631               + halfheight;
632
633       if (Math.abs(px - x) < 3 && Math.abs(py - y) < 3)
634       {
635         found = i;
636       }
637     }
638     if (found != -1)
639     {
640       return ((SequencePoint) points.elementAt(found)).sequence;
641     }
642     else
643     {
644       return null;
645     }
646   }
647
648 }