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