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