JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / jalview / appletgui / AnnotationPanel.js
1 Clazz.declarePackage ("jalview.appletgui");\r
2 Clazz.load (["awt2swing.Panel", "jalview.renderer.AwtRenderPanelI", "java.awt.event.ActionListener", "$.AdjustmentListener", "$.MouseListener", "$.MouseMotionListener", "java.awt.Color"], "jalview.appletgui.AnnotationPanel", ["awt2swing.MenuItem", "$.PopupMenu", "jalview.appletgui.EditNameDialog", "$.UserDefinedColours", "jalview.datamodel.Annotation", "jalview.renderer.AnnotationRenderer", "jalview.util.MessageManager", "$.Platform", "java.awt.Dimension", "java.lang.StringBuffer"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.av = null;\r
5 this.ap = null;\r
6 this.activeRow = -1;\r
7 this.HELIX = "Helix";\r
8 this.SHEET = "Sheet";\r
9 this.STEM = "RNA Helix";\r
10 this.LABEL = "Label";\r
11 this.REMOVE = "Remove Annotation";\r
12 this.COLOUR = "Colour";\r
13 this.HELIX_COLOUR = null;\r
14 this.SHEET_COLOUR = null;\r
15 this.image = null;\r
16 this.gg = null;\r
17 this.fm = null;\r
18 this.imgWidth = 0;\r
19 this.$fastPaint = false;\r
20 this.graphStretch = -1;\r
21 this.graphStretchY = -1;\r
22 this.mouseDragging = false;\r
23 this.MAC = false;\r
24 this.renderer = null;\r
25 this.needValidating = false;\r
26 this.scrollOffset = 0;\r
27 this.$bounds = null;\r
28 Clazz.instantialize (this, arguments);\r
29 }, jalview.appletgui, "AnnotationPanel", awt2swing.Panel, [jalview.renderer.AwtRenderPanelI, java.awt.event.AdjustmentListener, java.awt.event.ActionListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener]);\r
30 Clazz.prepareFields (c$, function () {\r
31 this.HELIX_COLOUR = java.awt.Color.red.darker ();\r
32 this.SHEET_COLOUR = java.awt.Color.green.darker ().darker ();\r
33 this.$bounds =  Clazz.newIntArray (2, 0);\r
34 });\r
35 Clazz.makeConstructor (c$, \r
36 function (ap) {\r
37 Clazz.superConstructor (this, jalview.appletgui.AnnotationPanel, []);\r
38 this.MAC = jalview.util.Platform.isAMac ();\r
39 this.ap = ap;\r
40 this.av = ap.av;\r
41 this.setLayout (null);\r
42 var height = this.adjustPanelHeight ();\r
43 ap.apvscroll.setValues (0, this.getSize ().height, 0, height);\r
44 this.addMouseMotionListener (this);\r
45 this.addMouseListener (this);\r
46 this.renderer =  new jalview.renderer.AnnotationRenderer ();\r
47 }, "jalview.appletgui.AlignmentPanel");\r
48 Clazz.makeConstructor (c$, \r
49 function (av) {\r
50 Clazz.superConstructor (this, jalview.appletgui.AnnotationPanel, []);\r
51 this.av = av;\r
52 this.renderer =  new jalview.renderer.AnnotationRenderer ();\r
53 }, "jalview.appletgui.AlignViewport");\r
54 Clazz.overrideMethod (c$, "adjustmentValueChanged", \r
55 function (evt) {\r
56 }, "java.awt.event.AdjustmentEvent");\r
57 Clazz.overrideMethod (c$, "actionPerformed", \r
58 function (evt) {\r
59 var aa = this.av.getAlignment ().getAlignmentAnnotation ();\r
60 if (aa == null) {\r
61 return;\r
62 }var anot = aa[this.activeRow].annotations;\r
63 if (anot.length < this.av.getColumnSelection ().getMax ()) {\r
64 var temp =  new Array (this.av.getColumnSelection ().getMax () + 2);\r
65 System.arraycopy (anot, 0, temp, 0, anot.length);\r
66 anot = temp;\r
67 aa[this.activeRow].annotations = anot;\r
68 }var label = "";\r
69 if (this.av.getColumnSelection () != null && this.av.getColumnSelection ().size () > 0 && anot[this.av.getColumnSelection ().getMin ()] != null) {\r
70 label = anot[this.av.getColumnSelection ().getMin ()].displayCharacter;\r
71 }if (evt.getActionCommand ().equals ("Remove Annotation")) {\r
72 for (var i = 0; i < this.av.getColumnSelection ().size (); i++) {\r
73 anot[this.av.getColumnSelection ().columnAt (i)] = null;\r
74 }\r
75 } else if (evt.getActionCommand ().equals ("Label")) {\r
76 label = this.enterLabel (label, "Enter Label");\r
77 if (label == null) {\r
78 return;\r
79 }if ((label.length > 0) && !aa[this.activeRow].hasText) {\r
80 aa[this.activeRow].hasText = true;\r
81 }for (var i = 0; i < this.av.getColumnSelection ().size (); i++) {\r
82 var index = this.av.getColumnSelection ().columnAt (i);\r
83 if (!this.av.getColumnSelection ().isVisible (index)) {\r
84 continue;\r
85 }if (anot[index] == null) {\r
86 anot[index] =  new jalview.datamodel.Annotation (label, "", ' ', 0);\r
87 }anot[index].displayCharacter = label;\r
88 }\r
89 } else if (evt.getActionCommand ().equals ("Colour")) {\r
90 var udc =  new jalview.appletgui.UserDefinedColours (this, java.awt.Color.black, this.ap.alignFrame);\r
91 var col = udc.getColor ();\r
92 for (var i = 0; i < this.av.getColumnSelection ().size (); i++) {\r
93 var index = this.av.getColumnSelection ().columnAt (i);\r
94 if (!this.av.getColumnSelection ().isVisible (index)) {\r
95 continue;\r
96 }if (anot[index] == null) {\r
97 anot[index] =  new jalview.datamodel.Annotation ("", "", ' ', 0);\r
98 }anot[index].colour = col;\r
99 }\r
100 } else {\r
101 var type = String.fromCharCode (0);\r
102 var symbol = "\u03B1";\r
103 if (evt.getActionCommand ().equals ("Helix")) {\r
104 type = 'H';\r
105 } else if (evt.getActionCommand ().equals ("Sheet")) {\r
106 type = 'E';\r
107 symbol = "\u03B2";\r
108 } else if (evt.getActionCommand ().equals ("RNA Helix")) {\r
109 type = 'S';\r
110 symbol = "\u03C3";\r
111 }if (!aa[this.activeRow].hasIcons) {\r
112 aa[this.activeRow].hasIcons = true;\r
113 }label = this.enterLabel (symbol, "Enter Label");\r
114 if (label == null) {\r
115 return;\r
116 }if ((label.length > 0) && !aa[this.activeRow].hasText) {\r
117 aa[this.activeRow].hasText = true;\r
118 if (evt.getActionCommand ().equals ("RNA Helix")) {\r
119 aa[this.activeRow].showAllColLabels = true;\r
120 }}for (var i = 0; i < this.av.getColumnSelection ().size (); i++) {\r
121 var index = this.av.getColumnSelection ().columnAt (i);\r
122 if (!this.av.getColumnSelection ().isVisible (index)) {\r
123 continue;\r
124 }if (anot[index] == null) {\r
125 anot[index] =  new jalview.datamodel.Annotation (label, "", type, 0);\r
126 }anot[index].secondaryStructure = type != 'S' ? type : label.length == 0 ? ' ' : label.charAt (0);\r
127 anot[index].displayCharacter = label;\r
128 }\r
129 }this.av.getAlignment ().validateAnnotation (aa[this.activeRow]);\r
130 this.ap.alignmentChanged ();\r
131 this.adjustPanelHeight ();\r
132 this.repaint ();\r
133 return;\r
134 }, "java.awt.event.ActionEvent");\r
135 Clazz.defineMethod (c$, "enterLabel", \r
136 function (text, label) {\r
137 var dialog =  new jalview.appletgui.EditNameDialog (text, null, label, null, this.ap.alignFrame, "Enter Label", 400, 200, true);\r
138 if (dialog.accept) {\r
139 return dialog.getName ();\r
140 } else {\r
141 return null;\r
142 }}, "~S,~S");\r
143 Clazz.overrideMethod (c$, "mousePressed", \r
144 function (evt) {\r
145 var aa = this.av.getAlignment ().getAlignmentAnnotation ();\r
146 if (aa == null) {\r
147 return;\r
148 }var height = -this.scrollOffset;\r
149 this.activeRow = -1;\r
150 for (var i = 0; i < aa.length; i++) {\r
151 if (aa[i].visible) {\r
152 height += aa[i].height;\r
153 }if (evt.getY () < height) {\r
154 if (aa[i].editable) {\r
155 this.activeRow = i;\r
156 } else if (aa[i].graph > 0) {\r
157 this.graphStretch = i;\r
158 this.graphStretchY = evt.getY ();\r
159 }break;\r
160 }}\r
161 if ((evt.getModifiers () & 4) == 4 && this.activeRow != -1) {\r
162 if (this.av.getColumnSelection () == null) {\r
163 return;\r
164 }var pop =  new awt2swing.PopupMenu (jalview.util.MessageManager.getString ("label.structure_type"));\r
165 var item;\r
166 if (this.av.getAlignment ().isNucleotide () == true) {\r
167 item =  new awt2swing.MenuItem ("RNA Helix");\r
168 item.addActionListener (this);\r
169 pop.add (item);\r
170 } else {\r
171 item =  new awt2swing.MenuItem ("Helix");\r
172 item.addActionListener (this);\r
173 pop.add (item);\r
174 item =  new awt2swing.MenuItem ("Sheet");\r
175 item.addActionListener (this);\r
176 pop.add (item);\r
177 }item =  new awt2swing.MenuItem ("Label");\r
178 item.addActionListener (this);\r
179 pop.add (item);\r
180 item =  new awt2swing.MenuItem ("Colour");\r
181 item.addActionListener (this);\r
182 pop.add (item);\r
183 item =  new awt2swing.MenuItem ("Remove Annotation");\r
184 item.addActionListener (this);\r
185 pop.add (item);\r
186 this.ap.alignFrame.add (pop);\r
187 pop.show (this, evt.getX (), evt.getY ());\r
188 return;\r
189 }this.ap.scalePanel.mousePressed (evt);\r
190 }, "java.awt.event.MouseEvent");\r
191 Clazz.overrideMethod (c$, "mouseReleased", \r
192 function (evt) {\r
193 this.graphStretch = -1;\r
194 this.graphStretchY = -1;\r
195 this.mouseDragging = false;\r
196 if (this.needValidating) {\r
197 this.ap.validate ();\r
198 this.needValidating = false;\r
199 }this.ap.scalePanel.mouseReleased (evt);\r
200 }, "java.awt.event.MouseEvent");\r
201 Clazz.overrideMethod (c$, "mouseClicked", \r
202 function (evt) {\r
203 }, "java.awt.event.MouseEvent");\r
204 Clazz.overrideMethod (c$, "mouseDragged", \r
205 function (evt) {\r
206 if (this.graphStretch > -1) {\r
207 this.av.getAlignment ().getAlignmentAnnotation ()[this.graphStretch].graphHeight += this.graphStretchY - evt.getY ();\r
208 if (this.av.getAlignment ().getAlignmentAnnotation ()[this.graphStretch].graphHeight < 0) {\r
209 this.av.getAlignment ().getAlignmentAnnotation ()[this.graphStretch].graphHeight = 0;\r
210 }this.graphStretchY = evt.getY ();\r
211 this.av.calcPanelHeight ();\r
212 this.needValidating = true;\r
213 this.ap.paintAlignment (true);\r
214 } else {\r
215 this.ap.scalePanel.mouseDragged (evt);\r
216 }}, "java.awt.event.MouseEvent");\r
217 Clazz.overrideMethod (c$, "mouseMoved", \r
218 function (evt) {\r
219 var aa = this.av.getAlignment ().getAlignmentAnnotation ();\r
220 if (aa == null) {\r
221 return;\r
222 }var row = -1;\r
223 var height = -this.scrollOffset;\r
224 for (var i = 0; i < aa.length; i++) {\r
225 if (aa[i].visible) {\r
226 height += aa[i].height;\r
227 }if (evt.getY () < height) {\r
228 row = i;\r
229 break;\r
230 }}\r
231 var res = Clazz.doubleToInt (evt.getX () / this.av.getCharWidth ()) + this.av.getStartRes ();\r
232 if (this.av.hasHiddenColumns ()) {\r
233 res = this.av.getColumnSelection ().adjustForHiddenColumns (res);\r
234 }if (row > -1 && res < aa[row].annotations.length && aa[row].annotations[res] != null) {\r
235 var text =  new StringBuffer ("Sequence position " + (res + 1));\r
236 if (aa[row].annotations[res].description != null) {\r
237 text.append ("  " + aa[row].annotations[res].description);\r
238 }this.ap.alignFrame.setStatus (text.toString ());\r
239 }}, "java.awt.event.MouseEvent");\r
240 Clazz.overrideMethod (c$, "mouseEntered", \r
241 function (evt) {\r
242 this.ap.scalePanel.mouseEntered (evt);\r
243 }, "java.awt.event.MouseEvent");\r
244 Clazz.overrideMethod (c$, "mouseExited", \r
245 function (evt) {\r
246 this.ap.scalePanel.mouseExited (evt);\r
247 }, "java.awt.event.MouseEvent");\r
248 Clazz.defineMethod (c$, "adjustPanelHeight", \r
249 function () {\r
250 return this.adjustPanelHeight (true);\r
251 });\r
252 Clazz.defineMethod (c$, "adjustPanelHeight", \r
253 function (repaint) {\r
254 var height = this.av.calcPanelHeight ();\r
255 this.setSize ( new java.awt.Dimension (this.getSize ().width, height));\r
256 if (repaint) {\r
257 this.repaint ();\r
258 }return height;\r
259 }, "~B");\r
260 Clazz.defineMethod (c$, "addEditableColumn", \r
261 function (i) {\r
262 if (this.activeRow == -1) {\r
263 var aa = this.av.getAlignment ().getAlignmentAnnotation ();\r
264 if (aa == null) {\r
265 return;\r
266 }for (var j = 0; j < aa.length; j++) {\r
267 if (aa[j].editable) {\r
268 this.activeRow = j;\r
269 break;\r
270 }}\r
271 }}, "~N");\r
272 Clazz.overrideMethod (c$, "paintComponent", \r
273 function (g) {\r
274 if (this.av.getWrapAlignment () || !this.av.isShowAnnotation ()) return;\r
275 var d = this.getSize ();\r
276 this.imgWidth = d.width;\r
277 if (this.imgWidth < 1 || d.height < 1) {\r
278 return;\r
279 }if (this.image == null || this.imgWidth != this.image.getWidth (this) || d.height != this.image.getHeight (this)) {\r
280 this.image = this.createImage (this.imgWidth, d.height);\r
281 this.gg = this.image.getGraphics ();\r
282 this.gg.setFont (this.av.getFont ());\r
283 this.fm = this.gg.getFontMetrics ();\r
284 this.$fastPaint = false;\r
285 }if (this.$fastPaint) {\r
286 g.drawImage (this.image, 0, 0, this);\r
287 this.$fastPaint = false;\r
288 return;\r
289 }this.gg.setColor (java.awt.Color.white);\r
290 this.gg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);\r
291 this.drawComponent (this.gg, this.av.startRes, this.av.endRes + 1);\r
292 g.drawImage (this.image, 0, 0, this);\r
293 }, "java.awt.Graphics");\r
294 Clazz.defineMethod (c$, "fastPaint", \r
295 function (horizontal) {\r
296 if (horizontal == 0 || this.av.getAlignment ().getAlignmentAnnotation () == null || this.av.getAlignment ().getAlignmentAnnotation ().length < 1) {\r
297 this.repaint ();\r
298 return;\r
299 }this.gg.copyArea (0, 0, this.imgWidth, this.getSize ().height, -horizontal * this.av.getCharWidth (), 0);\r
300 var sr = this.av.startRes;\r
301 var er = this.av.endRes + 1;\r
302 var transX = 0;\r
303 if (horizontal > 0) {\r
304 transX = (er - sr - horizontal) * this.av.getCharWidth ();\r
305 sr = er - horizontal;\r
306 } else if (horizontal < 0) {\r
307 er = sr - horizontal;\r
308 }this.gg.translate (transX, 0);\r
309 this.drawComponent (this.gg, sr, er);\r
310 this.gg.translate (-transX, 0);\r
311 this.$fastPaint = true;\r
312 this.repaint ();\r
313 }, "~N");\r
314 Clazz.defineMethod (c$, "drawComponent", \r
315 function (g, startRes, endRes) {\r
316 var ofont = this.av.getFont ();\r
317 g.setFont (ofont);\r
318 g.setColor (java.awt.Color.white);\r
319 g.fillRect (0, 0, (endRes - startRes) * this.av.getCharWidth (), this.getSize ().height);\r
320 if (this.fm == null) {\r
321 this.fm = g.getFontMetrics ();\r
322 }if ((this.av.getAlignment ().getAlignmentAnnotation () == null) || (this.av.getAlignment ().getAlignmentAnnotation ().length < 1)) {\r
323 g.setColor (java.awt.Color.white);\r
324 g.fillRect (0, 0, this.getSize ().width, this.getSize ().height);\r
325 g.setColor (java.awt.Color.black);\r
326 if (this.av.validCharWidth) {\r
327 g.drawString (jalview.util.MessageManager.getString ("label.alignment_has_no_annotations"), 20, 15);\r
328 }return;\r
329 }g.translate (0, -this.scrollOffset);\r
330 this.renderer.drawComponent (this, this.av, g, this.activeRow, startRes, endRes);\r
331 g.translate (0, +this.scrollOffset);\r
332 }, "java.awt.Graphics,~N,~N");\r
333 Clazz.defineMethod (c$, "setScrollOffset", \r
334 function (value, repaint) {\r
335 this.scrollOffset = value;\r
336 if (repaint) {\r
337 this.repaint ();\r
338 }}, "~N,~B");\r
339 Clazz.defineMethod (c$, "getFontMetrics", \r
340 function () {\r
341 return this.fm;\r
342 });\r
343 Clazz.overrideMethod (c$, "getFadedImage", \r
344 function () {\r
345 return this.image;\r
346 });\r
347 Clazz.overrideMethod (c$, "getFadedImageWidth", \r
348 function () {\r
349 return this.imgWidth;\r
350 });\r
351 Clazz.overrideMethod (c$, "getVisibleVRange", \r
352 function () {\r
353 if (this.ap != null && this.ap.alabels != null) {\r
354 var sOffset = -this.ap.alabels.scrollOffset;\r
355 var visHeight = sOffset + this.ap.annotationPanelHolder.getHeight ();\r
356 this.$bounds[0] = sOffset;\r
357 this.$bounds[1] = visHeight;\r
358 return this.$bounds;\r
359 } else {\r
360 return null;\r
361 }});\r
362 Clazz.defineStatics (c$,\r
363 "GRAPH_HEIGHT", 40);\r
364 });\r