JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / jalview / appletgui / RotatableCanvas.js
1 Clazz.declarePackage ("jalview.appletgui");\r
2 Clazz.load (["awt2swing.Panel", "jalview.api.RotatableCanvasI", "java.awt.event.KeyListener", "$.MouseListener", "$.MouseMotionListener", "jalview.math.RotatableMatrix"], "jalview.appletgui.RotatableCanvas", ["jalview.appletgui.PaintRefresher", "jalview.datamodel.SequenceGroup", "jalview.util.Format", "$.MessageManager", "java.awt.Color", "$.Dimension", "$.Font"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.idmat = null;\r
5 this.objmat = null;\r
6 this.rotmat = null;\r
7 this.tooltip = null;\r
8 this.toolx = 0;\r
9 this.tooly = 0;\r
10 this.$drawAxes = true;\r
11 this.omx = 0;\r
12 this.mx = 0;\r
13 this.omy = 0;\r
14 this.my = 0;\r
15 this.img = null;\r
16 this.ig = null;\r
17 this.prefsize = null;\r
18 this.centre = null;\r
19 this.$width = null;\r
20 this.max = null;\r
21 this.min = null;\r
22 this.maxwidth = 0;\r
23 this.scale = 0;\r
24 this.npoint = 0;\r
25 this.points = null;\r
26 this.orig = null;\r
27 this.axes = null;\r
28 this.startx = 0;\r
29 this.starty = 0;\r
30 this.lastx = 0;\r
31 this.lasty = 0;\r
32 this.rectx1 = 0;\r
33 this.recty1 = 0;\r
34 this.rectx2 = 0;\r
35 this.recty2 = 0;\r
36 this.scalefactor = 1;\r
37 this.av = null;\r
38 this.$showLabels = false;\r
39 Clazz.instantialize (this, arguments);\r
40 }, jalview.appletgui, "RotatableCanvas", awt2swing.Panel, [java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.event.KeyListener, jalview.api.RotatableCanvasI]);\r
41 Clazz.prepareFields (c$, function () {\r
42 this.idmat =  new jalview.math.RotatableMatrix (3, 3);\r
43 this.objmat =  new jalview.math.RotatableMatrix (3, 3);\r
44 this.rotmat =  new jalview.math.RotatableMatrix (3, 3);\r
45 this.centre =  Clazz.newFloatArray (3, 0);\r
46 this.$width =  Clazz.newFloatArray (3, 0);\r
47 this.max =  Clazz.newFloatArray (3, 0);\r
48 this.min =  Clazz.newFloatArray (3, 0);\r
49 });\r
50 Clazz.makeConstructor (c$, \r
51 function (av) {\r
52 Clazz.superConstructor (this, jalview.appletgui.RotatableCanvas, []);\r
53 this.av = av;\r
54 }, "jalview.viewmodel.AlignmentViewport");\r
55 Clazz.defineMethod (c$, "showLabels", \r
56 function (b) {\r
57 this.$showLabels = b;\r
58 this.repaint ();\r
59 }, "~B");\r
60 Clazz.overrideMethod (c$, "setPoints", \r
61 function (points, npoint) {\r
62 this.points = points;\r
63 this.npoint = npoint;\r
64 jalview.appletgui.PaintRefresher.Register (this, this.av.getSequenceSetId ());\r
65 this.prefsize = this.getPreferredSize ();\r
66 this.orig =  Clazz.newFloatArray (npoint, 3, 0);\r
67 for (var i = 0; i < npoint; i++) {\r
68 var sp = points.elementAt (i);\r
69 for (var j = 0; j < 3; j++) {\r
70 this.orig[i][j] = sp.coord[j];\r
71 }\r
72 }\r
73 for (var i = 0; i < 3; i++) {\r
74 for (var j = 0; j < 3; j++) {\r
75 if (i != j) {\r
76 this.idmat.addElement (i, j, 0);\r
77 this.objmat.addElement (i, j, 0);\r
78 this.rotmat.addElement (i, j, 0);\r
79 } else {\r
80 this.idmat.addElement (i, j, 0);\r
81 this.objmat.addElement (i, j, 0);\r
82 this.rotmat.addElement (i, j, 0);\r
83 }}\r
84 }\r
85 this.axes =  Clazz.newFloatArray (3, 3, 0);\r
86 this.initAxes ();\r
87 this.findCentre ();\r
88 this.findWidth ();\r
89 this.scale = this.findScale ();\r
90 this.addMouseListener (this);\r
91 this.addKeyListener (this);\r
92 this.addMouseMotionListener (this);\r
93 }, "java.util.Vector,~N");\r
94 Clazz.defineMethod (c$, "initAxes", \r
95 function () {\r
96 for (var i = 0; i < 3; i++) {\r
97 for (var j = 0; j < 3; j++) {\r
98 if (i != j) {\r
99 this.axes[i][j] = 0;\r
100 } else {\r
101 this.axes[i][j] = 1;\r
102 }}\r
103 }\r
104 });\r
105 Clazz.defineMethod (c$, "findWidth", \r
106 function () {\r
107 this.max =  Clazz.newFloatArray (3, 0);\r
108 this.min =  Clazz.newFloatArray (3, 0);\r
109 this.max[0] = -1.0E30;\r
110 this.max[1] = -1.0E30;\r
111 this.max[2] = -1.0E30;\r
112 this.min[0] = 1e30;\r
113 this.min[1] = 1e30;\r
114 this.min[2] = 1e30;\r
115 for (var i = 0; i < 3; i++) {\r
116 for (var j = 0; j < this.npoint; j++) {\r
117 var sp = this.points.elementAt (j);\r
118 if (sp.coord[i] >= this.max[i]) {\r
119 this.max[i] = sp.coord[i];\r
120 }if (sp.coord[i] <= this.min[i]) {\r
121 this.min[i] = sp.coord[i];\r
122 }}\r
123 }\r
124 this.$width[0] = Math.abs (this.max[0] - this.min[0]);\r
125 this.$width[1] = Math.abs (this.max[1] - this.min[1]);\r
126 this.$width[2] = Math.abs (this.max[2] - this.min[2]);\r
127 this.maxwidth = this.$width[0];\r
128 if (this.$width[1] > this.$width[0]) {\r
129 this.maxwidth = this.$width[1];\r
130 }if (this.$width[2] > this.$width[1]) {\r
131 this.maxwidth = this.$width[2];\r
132 }});\r
133 Clazz.defineMethod (c$, "findScale", \r
134 function () {\r
135 var dim;\r
136 var width;\r
137 var height;\r
138 if (this.getSize ().width != 0) {\r
139 width = this.getSize ().width;\r
140 height = this.getSize ().height;\r
141 } else {\r
142 width = this.prefsize.width;\r
143 height = this.prefsize.height;\r
144 }if (width < height) {\r
145 dim = width;\r
146 } else {\r
147 dim = height;\r
148 }return dim * this.scalefactor / (2 * this.maxwidth);\r
149 });\r
150 Clazz.defineMethod (c$, "findCentre", \r
151 function () {\r
152 this.findWidth ();\r
153 this.centre[0] = (this.max[0] + this.min[0]) / 2;\r
154 this.centre[1] = (this.max[1] + this.min[1]) / 2;\r
155 this.centre[2] = (this.max[2] + this.min[2]) / 2;\r
156 });\r
157 Clazz.overrideMethod (c$, "getPreferredSize", \r
158 function () {\r
159 if (this.prefsize != null) {\r
160 return this.prefsize;\r
161 } else {\r
162 return  new java.awt.Dimension (400, 400);\r
163 }});\r
164 Clazz.overrideMethod (c$, "getMinimumSize", \r
165 function () {\r
166 return this.getPreferredSize ();\r
167 });\r
168 Clazz.overrideMethod (c$, "update", \r
169 function (g) {\r
170 this.paint (g);\r
171 }, "java.awt.Graphics");\r
172 Clazz.defineMethod (c$, "PaintComponent", \r
173 function (g) {\r
174 if (this.points == null) {\r
175 g.setFont ( new java.awt.Font ("Verdana", 0, 18));\r
176 g.drawString (jalview.util.MessageManager.getString ("label.calculating_pca") + "....", 20, Clazz.doubleToInt (this.getSize ().height / 2));\r
177 } else {\r
178 if ((this.img == null) || (this.prefsize.width != this.getSize ().width) || (this.prefsize.height != this.getSize ().height)) {\r
179 this.prefsize.width = this.getSize ().width;\r
180 this.prefsize.height = this.getSize ().height;\r
181 this.scale = this.findScale ();\r
182 this.img = this.createImage (this.getSize ().width, this.getSize ().height);\r
183 this.ig = this.img.getGraphics ();\r
184 }this.drawBackground (this.ig, java.awt.Color.black);\r
185 this.drawScene (this.ig);\r
186 if (this.$drawAxes == true) {\r
187 this.drawAxes (this.ig);\r
188 }if (this.tooltip != null) {\r
189 this.ig.setColor (java.awt.Color.red);\r
190 this.ig.drawString (this.tooltip, this.toolx, this.tooly);\r
191 }g.drawImage (this.img, 0, 0, this);\r
192 }}, "java.awt.Graphics");\r
193 Clazz.defineMethod (c$, "drawAxes", \r
194 function (g) {\r
195 g.setColor (java.awt.Color.yellow);\r
196 for (var i = 0; i < 3; i++) {\r
197 g.drawLine (Clazz.doubleToInt (this.getSize ().width / 2), Clazz.doubleToInt (this.getSize ().height / 2), Clazz.floatToInt (this.axes[i][0] * this.scale * this.max[0] + Clazz.doubleToInt (this.getSize ().width / 2)), Clazz.floatToInt (this.axes[i][1] * this.scale * this.max[1] + Clazz.doubleToInt (this.getSize ().height / 2)));\r
198 }\r
199 }, "java.awt.Graphics");\r
200 Clazz.defineMethod (c$, "drawBackground", \r
201 function (g, col) {\r
202 g.setColor (col);\r
203 g.fillRect (0, 0, this.prefsize.width, this.prefsize.height);\r
204 }, "java.awt.Graphics,java.awt.Color");\r
205 Clazz.defineMethod (c$, "drawScene", \r
206 function (g) {\r
207 var halfwidth = Clazz.doubleToInt (this.getSize ().width / 2);\r
208 var halfheight = Clazz.doubleToInt (this.getSize ().height / 2);\r
209 for (var i = 0; i < this.npoint; i++) {\r
210 var sp = this.points.elementAt (i);\r
211 var x = Clazz.floatToInt ((sp.coord[0] - this.centre[0]) * this.scale) + halfwidth;\r
212 var y = Clazz.floatToInt ((sp.coord[1] - this.centre[1]) * this.scale) + halfheight;\r
213 var z = sp.coord[1] - this.centre[2];\r
214 if (this.av.getSequenceColour (sp.sequence) === java.awt.Color.black) {\r
215 g.setColor (java.awt.Color.white);\r
216 } else {\r
217 g.setColor (this.av.getSequenceColour (sp.sequence));\r
218 }if (this.av.getSelectionGroup () != null) {\r
219 if (this.av.getSelectionGroup ().getSequences (null).contains ((this.points.elementAt (i)).sequence)) {\r
220 g.setColor (java.awt.Color.gray);\r
221 }}if (z < 0) {\r
222 g.setColor (g.getColor ().darker ());\r
223 }g.fillRect (x - 3, y - 3, 6, 6);\r
224 if (this.$showLabels) {\r
225 g.setColor (java.awt.Color.red);\r
226 g.drawString ((this.points.elementAt (i)).sequence.getName (), x - 3, y - 4);\r
227 }}\r
228 }, "java.awt.Graphics");\r
229 Clazz.defineMethod (c$, "minimumsize", \r
230 function () {\r
231 return this.prefsize;\r
232 });\r
233 Clazz.defineMethod (c$, "preferredsize", \r
234 function () {\r
235 return this.prefsize;\r
236 });\r
237 Clazz.overrideMethod (c$, "keyTyped", \r
238 function (evt) {\r
239 }, "java.awt.event.KeyEvent");\r
240 Clazz.overrideMethod (c$, "keyReleased", \r
241 function (evt) {\r
242 }, "java.awt.event.KeyEvent");\r
243 Clazz.overrideMethod (c$, "keyPressed", \r
244 function (evt) {\r
245 if (evt.getKeyCode () == 38) {\r
246 this.scalefactor = (this.scalefactor * 1.1);\r
247 this.scale = this.findScale ();\r
248 } else if (evt.getKeyCode () == 40) {\r
249 this.scalefactor = (this.scalefactor * 0.9);\r
250 this.scale = this.findScale ();\r
251 } else if (evt.getKeyChar () == 's') {\r
252 System.err.println ("DEBUG: Rectangle selection");\r
253 if (this.rectx2 != -1 && this.recty2 != -1) {\r
254 this.rectSelect (this.rectx1, this.recty1, this.rectx2, this.recty2);\r
255 }}this.repaint ();\r
256 }, "java.awt.event.KeyEvent");\r
257 Clazz.defineMethod (c$, "printPoints", \r
258 function () {\r
259 for (var i = 0; i < this.npoint; i++) {\r
260 var sp = this.points.elementAt (i);\r
261 jalview.util.Format.printLong (System.out, "%5d ", i);\r
262 for (var j = 0; j < 3; j++) {\r
263 jalview.util.Format.printDouble (System.out, "%13.3f  ", sp.coord[j]);\r
264 }\r
265 System.out.println ();\r
266 }\r
267 });\r
268 Clazz.overrideMethod (c$, "mouseClicked", \r
269 function (evt) {\r
270 }, "java.awt.event.MouseEvent");\r
271 Clazz.overrideMethod (c$, "mouseEntered", \r
272 function (evt) {\r
273 }, "java.awt.event.MouseEvent");\r
274 Clazz.overrideMethod (c$, "mouseExited", \r
275 function (evt) {\r
276 }, "java.awt.event.MouseEvent");\r
277 Clazz.overrideMethod (c$, "mouseReleased", \r
278 function (evt) {\r
279 }, "java.awt.event.MouseEvent");\r
280 Clazz.overrideMethod (c$, "mousePressed", \r
281 function (evt) {\r
282 var x = evt.getX ();\r
283 var y = evt.getY ();\r
284 this.mx = x;\r
285 this.my = y;\r
286 this.omx = this.mx;\r
287 this.omy = this.my;\r
288 this.startx = x;\r
289 this.starty = y;\r
290 this.rectx1 = x;\r
291 this.recty1 = y;\r
292 this.rectx2 = -1;\r
293 this.recty2 = -1;\r
294 var found = this.findPoint (x, y);\r
295 if (found != null) {\r
296 if (this.av.getSelectionGroup () != null) {\r
297 this.av.getSelectionGroup ().addOrRemove (found, true);\r
298 this.av.getSelectionGroup ().setEndRes (this.av.getAlignment ().getWidth () - 1);\r
299 } else {\r
300 this.av.setSelectionGroup ( new jalview.datamodel.SequenceGroup ());\r
301 this.av.getSelectionGroup ().addOrRemove (found, true);\r
302 this.av.getSelectionGroup ().setEndRes (this.av.getAlignment ().getWidth () - 1);\r
303 }jalview.appletgui.PaintRefresher.Refresh (this, this.av.getSequenceSetId ());\r
304 this.av.sendSelection ();\r
305 }this.repaint ();\r
306 }, "java.awt.event.MouseEvent");\r
307 Clazz.overrideMethod (c$, "mouseMoved", \r
308 function (evt) {\r
309 var found = this.findPoint (evt.getX (), evt.getY ());\r
310 if (found == null) {\r
311 this.tooltip = null;\r
312 } else {\r
313 this.tooltip = found.getName ();\r
314 this.toolx = evt.getX ();\r
315 this.tooly = evt.getY ();\r
316 }this.repaint ();\r
317 }, "java.awt.event.MouseEvent");\r
318 Clazz.overrideMethod (c$, "mouseDragged", \r
319 function (evt) {\r
320 this.mx = evt.getX ();\r
321 this.my = evt.getY ();\r
322 this.rotmat.setIdentity ();\r
323 this.rotmat.rotate (this.my - this.omy, 'x');\r
324 this.rotmat.rotate (this.mx - this.omx, 'y');\r
325 for (var i = 0; i < this.npoint; i++) {\r
326 var sp = this.points.elementAt (i);\r
327 sp.coord[0] -= this.centre[0];\r
328 sp.coord[1] -= this.centre[1];\r
329 sp.coord[2] -= this.centre[2];\r
330 sp.coord = this.rotmat.vectorMultiply (sp.coord);\r
331 sp.coord[0] += this.centre[0];\r
332 sp.coord[1] += this.centre[1];\r
333 sp.coord[2] += this.centre[2];\r
334 }\r
335 for (var i = 0; i < 3; i++) {\r
336 this.axes[i] = this.rotmat.vectorMultiply (this.axes[i]);\r
337 }\r
338 this.omx = this.mx;\r
339 this.omy = this.my;\r
340 this.paint (this.getGraphics ());\r
341 }, "java.awt.event.MouseEvent");\r
342 Clazz.defineMethod (c$, "rectSelect", \r
343 function (x1, y1, x2, y2) {\r
344 for (var i = 0; i < this.npoint; i++) {\r
345 var sp = this.points.elementAt (i);\r
346 var tmp1 = Clazz.doubleToInt ((sp.coord[0] - this.centre[0]) * this.scale + this.getSize ().width / 2.0);\r
347 var tmp2 = Clazz.doubleToInt ((sp.coord[1] - this.centre[1]) * this.scale + this.getSize ().height / 2.0);\r
348 if (tmp1 > x1 && tmp1 < x2 && tmp2 > y1 && tmp2 < y2) {\r
349 if (this.av != null) {\r
350 if (!this.av.getSelectionGroup ().getSequences (null).contains (sp.sequence)) {\r
351 this.av.getSelectionGroup ().addSequence (sp.sequence, true);\r
352 }}}}\r
353 }, "~N,~N,~N,~N");\r
354 Clazz.defineMethod (c$, "findPoint", \r
355 function (x, y) {\r
356 var halfwidth = Clazz.doubleToInt (this.getSize ().width / 2);\r
357 var halfheight = Clazz.doubleToInt (this.getSize ().height / 2);\r
358 var found = -1;\r
359 for (var i = 0; i < this.npoint; i++) {\r
360 var sp = this.points.elementAt (i);\r
361 var px = Clazz.floatToInt ((sp.coord[0] - this.centre[0]) * this.scale) + halfwidth;\r
362 var py = Clazz.floatToInt ((sp.coord[1] - this.centre[1]) * this.scale) + halfheight;\r
363 if (Math.abs (px - x) < 3 && Math.abs (py - y) < 3) {\r
364 found = i;\r
365 }}\r
366 if (found != -1) {\r
367 return (this.points.elementAt (found)).sequence;\r
368 } else {\r
369 return null;\r
370 }}, "~N,~N");\r
371 });\r