Amend features/annotations in applet
[jalview.git] / src / jalview / appletgui / UserDefinedColours.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 \r
20 package jalview.appletgui;\r
21 \r
22 import java.util.*;\r
23 \r
24 import java.awt.*;\r
25 import java.awt.event.*;\r
26 \r
27 import jalview.datamodel.*;\r
28 import jalview.schemes.*;\r
29 \r
30 public class UserDefinedColours\r
31     extends Panel implements ActionListener, AdjustmentListener\r
32 {\r
33 \r
34   AlignmentPanel ap;\r
35   SequenceGroup seqGroup;\r
36   Button selectedButton;\r
37   Vector oldColours = new Vector();\r
38   ColourSchemeI oldColourScheme;\r
39   Frame frame;\r
40   MCview.AppletPDBCanvas pdbcanvas;\r
41   AppletJmol jmol;\r
42 \r
43   Dialog dialog;\r
44   Object caller;\r
45   String originalLabel;\r
46   Color originalColour;\r
47 \r
48   int R = 0, G = 0, B = 0;\r
49 \r
50   public ColourSchemeI loadDefaultColours()\r
51   {\r
52     // NOT IMPLEMENTED YET IN APPLET VERSION\r
53     return null;\r
54   }\r
55 \r
56   public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)\r
57   {\r
58     this.ap = ap;\r
59     seqGroup = sg;\r
60 \r
61     if (seqGroup != null)\r
62     {\r
63       oldColourScheme = seqGroup.cs;\r
64     }\r
65     else\r
66     {\r
67       oldColourScheme = ap.av.getGlobalColourScheme();\r
68     }\r
69 \r
70     init();\r
71   }\r
72 \r
73   public UserDefinedColours(MCview.AppletPDBCanvas pdb)\r
74   {\r
75     this.pdbcanvas = pdb;\r
76     init();\r
77   }\r
78 \r
79   public UserDefinedColours(AppletJmol jmol)\r
80   {\r
81     this.jmol = jmol;\r
82     init();\r
83   }\r
84 \r
85   public UserDefinedColours(FeatureRenderer fr, Frame alignframe)\r
86   {\r
87     caller = fr;\r
88     originalColour = fr.colourPanel.getBackground();\r
89     originalLabel = "Feature Colour";\r
90     setTargetColour(fr.colourPanel.getBackground());\r
91 \r
92     setForDialog("Select Feature Colour", alignframe);\r
93   }\r
94 \r
95   public UserDefinedColours(Component caller,\r
96                             Color col1,\r
97                             Frame alignframe)\r
98   {\r
99     this.caller = caller;\r
100     originalColour = col1;\r
101     originalLabel = "Select Colour";\r
102     setTargetColour(col1);\r
103 \r
104     setForDialog("Select Colour", alignframe);\r
105   }\r
106 \r
107 \r
108   public UserDefinedColours(Object caller,\r
109                             String label,\r
110                             Color colour)\r
111   {\r
112     this.caller = caller;\r
113     originalColour = colour;\r
114     originalLabel = label;\r
115     init();\r
116     remove(buttonPanel);\r
117 \r
118     setTargetColour(colour);\r
119 \r
120     okcancelPanel.setBounds(new Rectangle(0, 113, 400, 35));\r
121     frame.setTitle("User Defined Colours - " + label);\r
122     frame.setSize(420, 200);\r
123   }\r
124 \r
125   void setForDialog(String title, Frame alignframe)\r
126   {\r
127     init();\r
128     frame.setVisible(false);\r
129     remove(buttonPanel);\r
130     dialog = new Dialog(alignframe, title, true);\r
131 \r
132     dialog.add(this);\r
133     this.setSize(400,123);\r
134     okcancelPanel.setBounds(new Rectangle(0, 123, 400, 35));\r
135     int height = 160 + alignframe.getInsets().top + getInsets().bottom;\r
136     int width = 400;\r
137 \r
138     dialog.setBounds(alignframe.getBounds().x\r
139               + (alignframe.getSize().width - width) / 2,\r
140               alignframe.getBounds().y\r
141               + (alignframe.getSize().height - height) / 2,\r
142               width, height);\r
143 \r
144     dialog.show();\r
145   }\r
146 \r
147   public void actionPerformed(ActionEvent evt)\r
148   {\r
149     if (evt.getSource() == okButton)\r
150     {\r
151       okButton_actionPerformed();\r
152     }\r
153     else if (evt.getSource() == applyButton)\r
154     {\r
155       applyButton_actionPerformed();\r
156     }\r
157     else if (evt.getSource() == cancelButton)\r
158     {\r
159       cancelButton_actionPerformed();\r
160     }\r
161     else if (evt.getSource() == rText)\r
162     {\r
163       rText_actionPerformed();\r
164     }\r
165     else if (evt.getSource() == gText)\r
166     {\r
167       gText_actionPerformed();\r
168     }\r
169     else if (evt.getSource() == bText)\r
170     {\r
171       bText_actionPerformed();\r
172     }\r
173   }\r
174 \r
175   public void adjustmentValueChanged(AdjustmentEvent evt)\r
176   {\r
177     if (evt.getSource() == rScroller)\r
178     {\r
179       rScroller_adjustmentValueChanged();\r
180     }\r
181     else if (evt.getSource() == gScroller)\r
182     {\r
183       gScroller_adjustmentValueChanged();\r
184     }\r
185     else if (evt.getSource() == bScroller)\r
186     {\r
187       bScroller_adjustmentValueChanged();\r
188     }\r
189   }\r
190 \r
191   void init()\r
192   {\r
193     try\r
194     {\r
195       jbInit();\r
196     }\r
197     catch (Exception e)\r
198     {\r
199       e.printStackTrace();\r
200     }\r
201     frame = new Frame();\r
202     frame.add(this);\r
203     jalview.bin.JalviewLite.addFrame(frame, "User defined colours", 420, 345);\r
204 \r
205     if (seqGroup != null)\r
206     {\r
207       frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");\r
208     }\r
209 \r
210     for (int i = 0; i < 20; i++)\r
211     {\r
212       makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +\r
213                  "", ResidueProperties.aa[i]);\r
214     }\r
215 \r
216     makeButton("B", "B");\r
217     makeButton("Z", "Z");\r
218     makeButton("X", "X");\r
219     makeButton("Gap", "'.','-',' '");\r
220 \r
221     validate();\r
222   }\r
223 \r
224   protected void rText_actionPerformed()\r
225   {\r
226     try\r
227     {\r
228       int i = Integer.parseInt(rText.getText());\r
229       rScroller.setValue(i);\r
230       rScroller_adjustmentValueChanged();\r
231     }\r
232     catch (NumberFormatException ex)\r
233     {}\r
234   }\r
235 \r
236   protected void gText_actionPerformed()\r
237   {\r
238     try\r
239     {\r
240       int i = Integer.parseInt(gText.getText());\r
241       gScroller.setValue(i);\r
242       gScroller_adjustmentValueChanged();\r
243     }\r
244     catch (NumberFormatException ex)\r
245     {}\r
246 \r
247   }\r
248 \r
249   protected void bText_actionPerformed()\r
250   {\r
251     try\r
252     {\r
253       int i = Integer.parseInt(bText.getText());\r
254       bScroller.setValue(i);\r
255       bScroller_adjustmentValueChanged();\r
256     }\r
257     catch (NumberFormatException ex)\r
258     {}\r
259 \r
260   }\r
261 \r
262   protected void rScroller_adjustmentValueChanged()\r
263   {\r
264     R = rScroller.getValue();\r
265     rText.setText(R + "");\r
266     colourChanged();\r
267   }\r
268 \r
269   protected void gScroller_adjustmentValueChanged()\r
270   {\r
271     G = gScroller.getValue();\r
272     gText.setText(G + "");\r
273     colourChanged();\r
274   }\r
275 \r
276   protected void bScroller_adjustmentValueChanged()\r
277   {\r
278     B = bScroller.getValue();\r
279     bText.setText(B + "");\r
280     colourChanged();\r
281   }\r
282 \r
283   public void colourChanged()\r
284   {\r
285     Color col = new Color(R, G, B);\r
286     target.setBackground(col);\r
287     target.repaint();\r
288 \r
289     if (selectedButton != null)\r
290     {\r
291       selectedButton.setBackground(col);\r
292       selectedButton.repaint();\r
293     }\r
294   }\r
295 \r
296   void setTargetColour(Color col)\r
297   {\r
298     R = col.getRed();\r
299     G = col.getGreen();\r
300     B = col.getBlue();\r
301     rScroller.setValue(R);\r
302     gScroller.setValue(G);\r
303     bScroller.setValue(B);\r
304     rText.setText(R + "");\r
305     gText.setText(G + "");\r
306     bText.setText(B + "");\r
307     colourChanged();\r
308   }\r
309 \r
310   public void colourButtonPressed(MouseEvent e)\r
311   {\r
312     selectedButton = (Button) e.getSource();\r
313     setTargetColour(selectedButton.getBackground());\r
314   }\r
315 \r
316   void makeButton(String label, String aa)\r
317   {\r
318     final Button button = new Button();\r
319     Color col = Color.white;\r
320 \r
321     try\r
322     {\r
323       col = oldColourScheme.findColour(aa.charAt(0), -1);\r
324     }\r
325     catch (Exception ex)\r
326     {}\r
327 \r
328     button.setBackground(col);\r
329     oldColours.addElement(col);\r
330     button.setLabel(label);\r
331     button.setForeground(col.darker().darker().darker());\r
332     button.setFont(new java.awt.Font("Verdana", 1, 10));\r
333     button.addMouseListener(new java.awt.event.MouseAdapter()\r
334     {\r
335       public void mousePressed(MouseEvent e)\r
336       {\r
337         colourButtonPressed(e);\r
338       }\r
339     });\r
340 \r
341     buttonPanel.add(button, null);\r
342   }\r
343 \r
344   protected void okButton_actionPerformed()\r
345   {\r
346     applyButton_actionPerformed();\r
347     if (dialog != null)\r
348       dialog.setVisible(false);\r
349 \r
350     frame.setVisible(false);\r
351   }\r
352 \r
353   public Color getColor()\r
354   {\r
355     return new Color(R, G, B);\r
356   }\r
357 \r
358   protected void applyButton_actionPerformed()\r
359   {\r
360     if (caller != null)\r
361     {\r
362       if (caller instanceof FeatureSettings)\r
363       {\r
364         ( (FeatureSettings) caller).setUserColour\r
365             (originalLabel, getColor());\r
366       }\r
367       else if (caller instanceof AnnotationColourChooser)\r
368       {\r
369         if (originalLabel.equals("Min Colour"))\r
370         {\r
371           ( (AnnotationColourChooser) caller).minColour_actionPerformed\r
372               (getColor());\r
373         }\r
374         else\r
375         {\r
376           ( (AnnotationColourChooser) caller).maxColour_actionPerformed\r
377               (getColor());\r
378         }\r
379       }\r
380       else if(caller instanceof FeatureRenderer)\r
381       {\r
382         ((FeatureRenderer)caller).colourPanel.setBackground(getColor());\r
383       }\r
384 \r
385       return;\r
386     }\r
387 \r
388     Color[] newColours = new Color[24];\r
389     for (int i = 0; i < 24; i++)\r
390     {\r
391       Button button = (Button) buttonPanel.getComponent(i);\r
392       newColours[i] = button.getBackground();\r
393     }\r
394 \r
395     UserColourScheme ucs = new UserColourScheme(newColours);\r
396     if (ap != null)\r
397     {\r
398       ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());\r
399     }\r
400 \r
401     if (ap != null)\r
402     {\r
403       if (seqGroup != null)\r
404       {\r
405         seqGroup.cs = ucs;\r
406       }\r
407       else\r
408       {\r
409         ap.av.setGlobalColourScheme(ucs);\r
410       }\r
411       ap.seqPanel.seqCanvas.img = null;\r
412       ap.paintAlignment(true);\r
413     }\r
414     else if(jmol!=null)\r
415     {\r
416       jmol.setJalviewColourScheme(ucs);\r
417     }\r
418     else if (pdbcanvas != null)\r
419     {\r
420       pdbcanvas.setColours(ucs);\r
421     }\r
422   }\r
423 \r
424   protected void cancelButton_actionPerformed()\r
425   {\r
426     if (caller != null)\r
427     {\r
428       if (caller instanceof FeatureSettings)\r
429       {\r
430         ( (FeatureSettings) caller).setUserColour\r
431             (originalLabel, originalColour);\r
432       }\r
433       else if (caller instanceof AnnotationColourChooser)\r
434       {\r
435         if (originalLabel.equals("Min Colour"))\r
436         {\r
437           ( (AnnotationColourChooser) caller).minColour_actionPerformed\r
438               (originalColour);\r
439         }\r
440         else\r
441         {\r
442           ( (AnnotationColourChooser) caller).maxColour_actionPerformed\r
443               (originalColour);\r
444         }\r
445       }\r
446       else if (caller instanceof FeatureRenderer)\r
447       {\r
448         ( (FeatureRenderer) caller).colourPanel.setBackground(originalColour);\r
449 \r
450       }\r
451 \r
452       if(dialog!=null)\r
453         dialog.setVisible(false);\r
454 \r
455       frame.setVisible(false);\r
456       return;\r
457     }\r
458 \r
459     Color[] newColours = new Color[24];\r
460     for (int i = 0; i < 24; i++)\r
461     {\r
462       newColours[i] = (Color) oldColours.elementAt(i);\r
463       buttonPanel.getComponent(i).setBackground(newColours[i]);\r
464     }\r
465 \r
466     UserColourScheme ucs = new UserColourScheme(newColours);\r
467 \r
468     if (ap != null)\r
469     {\r
470       if (seqGroup != null)\r
471       {\r
472         seqGroup.cs = ucs;\r
473       }\r
474       else\r
475       {\r
476         ap.av.setGlobalColourScheme(ucs);\r
477       }\r
478       ap.paintAlignment(true);\r
479     }\r
480     else if(jmol !=null)\r
481     {\r
482       jmol.setJalviewColourScheme(ucs);\r
483     }\r
484     else if (pdbcanvas != null)\r
485     {\r
486       pdbcanvas.pdb.setColours(ucs);\r
487     }\r
488 \r
489     frame.setVisible(false);\r
490   }\r
491 \r
492   protected Panel buttonPanel = new Panel();\r
493   protected GridLayout gridLayout = new GridLayout();\r
494   Panel okcancelPanel = new Panel();\r
495   protected Button okButton = new Button();\r
496   protected Button applyButton = new Button();\r
497   protected Button cancelButton = new Button();\r
498   protected Scrollbar rScroller = new Scrollbar();\r
499   Label label1 = new Label();\r
500   protected TextField rText = new TextField();\r
501   Label label4 = new Label();\r
502   protected Scrollbar gScroller = new Scrollbar();\r
503   protected TextField gText = new TextField();\r
504   Label label5 = new Label();\r
505   protected Scrollbar bScroller = new Scrollbar();\r
506   protected TextField bText = new TextField();\r
507   protected Panel target = new Panel();\r
508 \r
509   private void jbInit()\r
510       throws Exception\r
511   {\r
512     this.setLayout(null);\r
513     buttonPanel.setLayout(gridLayout);\r
514     gridLayout.setColumns(6);\r
515     gridLayout.setRows(4);\r
516     okButton.setFont(new java.awt.Font("Verdana", 0, 11));\r
517     okButton.setLabel("OK");\r
518     okButton.addActionListener(this);\r
519     applyButton.setFont(new java.awt.Font("Verdana", 0, 11));\r
520     applyButton.setLabel("Apply");\r
521     applyButton.addActionListener(this);\r
522     cancelButton.setFont(new java.awt.Font("Verdana", 0, 11));\r
523     cancelButton.setLabel("Cancel");\r
524     cancelButton.addActionListener(this);\r
525     this.setBackground(new Color(212, 208, 223));\r
526     okcancelPanel.setBounds(new Rectangle(0, 265, 400, 35));\r
527     buttonPanel.setBounds(new Rectangle(0, 123, 400, 142));\r
528     rScroller.setMaximum(256);\r
529     rScroller.setMinimum(0);\r
530     rScroller.setOrientation(0);\r
531     rScroller.setUnitIncrement(1);\r
532     rScroller.setVisibleAmount(1);\r
533     rScroller.setBounds(new Rectangle(36, 27, 119, 19));\r
534     rScroller.addAdjustmentListener(this);\r
535     label1.setAlignment(Label.RIGHT);\r
536     label1.setText("R");\r
537     label1.setBounds(new Rectangle(19, 30, 16, 15));\r
538     rText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));\r
539     rText.setText("0        ");\r
540     rText.setBounds(new Rectangle(156, 27, 53, 19));\r
541     rText.addActionListener(this);\r
542     label4.setAlignment(Label.RIGHT);\r
543     label4.setText("G");\r
544     label4.setBounds(new Rectangle(15, 56, 20, 15));\r
545     gScroller.setMaximum(256);\r
546     gScroller.setMinimum(0);\r
547     gScroller.setOrientation(0);\r
548     gScroller.setUnitIncrement(1);\r
549     gScroller.setVisibleAmount(1);\r
550     gScroller.setBounds(new Rectangle(35, 52, 120, 20));\r
551     gScroller.addAdjustmentListener(this);\r
552     gText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));\r
553     gText.setText("0        ");\r
554     gText.setBounds(new Rectangle(156, 52, 53, 20));\r
555     gText.addActionListener(this);\r
556     label5.setAlignment(Label.RIGHT);\r
557     label5.setText("B");\r
558     label5.setBounds(new Rectangle(14, 82, 20, 15));\r
559     bScroller.setMaximum(256);\r
560     bScroller.setMinimum(0);\r
561     bScroller.setOrientation(0);\r
562     bScroller.setUnitIncrement(1);\r
563     bScroller.setVisibleAmount(1);\r
564     bScroller.setBounds(new Rectangle(35, 78, 120, 20));\r
565     bScroller.addAdjustmentListener(this);\r
566     bText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));\r
567     bText.setText("0        ");\r
568     bText.setBounds(new Rectangle(157, 78, 52, 20));\r
569     bText.addActionListener(this);\r
570     target.setBackground(Color.black);\r
571     target.setBounds(new Rectangle(229, 26, 134, 79));\r
572     this.add(okcancelPanel, null);\r
573     okcancelPanel.add(okButton, null);\r
574     okcancelPanel.add(applyButton, null);\r
575     okcancelPanel.add(cancelButton, null);\r
576     this.add(buttonPanel, null);\r
577     this.add(target, null);\r
578     this.add(gScroller);\r
579     this.add(rScroller);\r
580     this.add(bScroller);\r
581     this.add(label5);\r
582     this.add(label4);\r
583     this.add(label1);\r
584     this.add(gText);\r
585     this.add(rText);\r
586     this.add(bText);\r
587   }\r
588 \r
589 }\r