0f305398a9dacc070b7ad20c26afbb26a34990f3
[jalview.git] / src / jalview / appletgui / UserDefinedColours.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 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
104   public UserDefinedColours(Component caller, Color col1, Frame alignframe)
105   {
106     this.caller = caller;
107     originalColour = col1;
108     originalLabel = "Select Colour";
109     setForDialog("Select Colour", alignframe);
110     setTargetColour(col1);
111     dialog.setVisible(true);
112   }
113
114   public UserDefinedColours(Object caller, String label, Color colour)
115   {
116     this.caller = caller;
117     originalColour = colour;
118     originalLabel = label;
119     init();
120     remove(buttonPanel);
121
122     setTargetColour(colour);
123
124     okcancelPanel.setBounds(new Rectangle(0, 113, 400, 35));
125     frame.setTitle("User Defined Colours - " + label);
126     frame.setSize(420, 200);
127   }
128
129   void setForDialog(String title, Frame alignframe)
130   {
131     init();
132     frame.setVisible(false);
133     remove(buttonPanel);
134     dialog = new Dialog(alignframe, title, true);
135
136     dialog.add(this);
137     this.setSize(400, 123);
138     okcancelPanel.setBounds(new Rectangle(0, 123, 400, 35));
139     int height = 160 + alignframe.getInsets().top + getInsets().bottom;
140     int width = 400;
141
142     dialog.setBounds(alignframe.getBounds().x
143             + (alignframe.getSize().width - width) / 2, alignframe
144             .getBounds().y
145             + (alignframe.getSize().height - height) / 2, width, height);
146
147   }
148
149   public void actionPerformed(ActionEvent evt)
150   {
151     if (evt.getSource() == okButton)
152     {
153       okButton_actionPerformed();
154     }
155     else if (evt.getSource() == applyButton)
156     {
157       applyButton_actionPerformed();
158     }
159     else if (evt.getSource() == cancelButton)
160     {
161       cancelButton_actionPerformed();
162     }
163     else if (evt.getSource() == rText)
164     {
165       rText_actionPerformed();
166     }
167     else if (evt.getSource() == gText)
168     {
169       gText_actionPerformed();
170     }
171     else if (evt.getSource() == bText)
172     {
173       bText_actionPerformed();
174     }
175   }
176
177   public void adjustmentValueChanged(AdjustmentEvent evt)
178   {
179     if (evt.getSource() == rScroller)
180     {
181       rScroller_adjustmentValueChanged();
182     }
183     else if (evt.getSource() == gScroller)
184     {
185       gScroller_adjustmentValueChanged();
186     }
187     else if (evt.getSource() == bScroller)
188     {
189       bScroller_adjustmentValueChanged();
190     }
191   }
192
193   void init()
194   {
195     try
196     {
197       jbInit();
198     } catch (Exception e)
199     {
200       e.printStackTrace();
201     }
202     frame = new Frame();
203     frame.add(this);
204     jalview.bin.JalviewLite.addFrame(frame, "User defined colours", 420,
205             345);
206
207     if (seqGroup != null)
208     {
209       frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
210     }
211
212     for (int i = 0; i < 20; i++)
213     {
214       makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
215               + "", ResidueProperties.aa[i]);
216     }
217
218     makeButton("B", "B");
219     makeButton("Z", "Z");
220     makeButton("X", "X");
221     makeButton("Gap", "'.','-',' '");
222
223     validate();
224   }
225
226   protected void rText_actionPerformed()
227   {
228     try
229     {
230       int i = Integer.parseInt(rText.getText());
231       rScroller.setValue(i);
232       rScroller_adjustmentValueChanged();
233     } catch (NumberFormatException ex)
234     {
235     }
236   }
237
238   protected void gText_actionPerformed()
239   {
240     try
241     {
242       int i = Integer.parseInt(gText.getText());
243       gScroller.setValue(i);
244       gScroller_adjustmentValueChanged();
245     } catch (NumberFormatException ex)
246     {
247     }
248
249   }
250
251   protected void bText_actionPerformed()
252   {
253     try
254     {
255       int i = Integer.parseInt(bText.getText());
256       bScroller.setValue(i);
257       bScroller_adjustmentValueChanged();
258     } catch (NumberFormatException ex)
259     {
260     }
261
262   }
263
264   protected void rScroller_adjustmentValueChanged()
265   {
266     R = rScroller.getValue();
267     rText.setText(R + "");
268     colourChanged();
269   }
270
271   protected void gScroller_adjustmentValueChanged()
272   {
273     G = gScroller.getValue();
274     gText.setText(G + "");
275     colourChanged();
276   }
277
278   protected void bScroller_adjustmentValueChanged()
279   {
280     B = bScroller.getValue();
281     bText.setText(B + "");
282     colourChanged();
283   }
284
285   public void colourChanged()
286   {
287     Color col = new Color(R, G, B);
288     target.setBackground(col);
289     target.repaint();
290
291     if (selectedButton != null)
292     {
293       selectedButton.setBackground(col);
294       selectedButton.repaint();
295     }
296   }
297
298   void setTargetColour(Color col)
299   {
300     R = col.getRed();
301     G = col.getGreen();
302     B = col.getBlue();
303
304     rScroller.setValue(R);
305     gScroller.setValue(G);
306     bScroller.setValue(B);
307     rText.setText(R + "");
308     gText.setText(G + "");
309     bText.setText(B + "");
310     colourChanged();
311   }
312
313   public void colourButtonPressed(MouseEvent e)
314   {
315     selectedButton = (Button) e.getSource();
316     setTargetColour(selectedButton.getBackground());
317   }
318
319   void makeButton(String label, String aa)
320   {
321     final Button button = new Button();
322     Color col = Color.white;
323
324     try
325     {
326       col = oldColourScheme.findColour(aa.charAt(0), -1);
327     } catch (Exception ex)
328     {
329     }
330
331     button.setBackground(col);
332     oldColours.addElement(col);
333     button.setLabel(label);
334     button.setForeground(col.darker().darker().darker());
335     button.setFont(new java.awt.Font("Verdana", 1, 10));
336     button.addMouseListener(new java.awt.event.MouseAdapter()
337     {
338       public void mousePressed(MouseEvent e)
339       {
340         colourButtonPressed(e);
341       }
342     });
343
344     buttonPanel.add(button, null);
345   }
346
347   protected void okButton_actionPerformed()
348   {
349     applyButton_actionPerformed();
350     if (dialog != null)
351       dialog.setVisible(false);
352
353     frame.setVisible(false);
354   }
355
356   public Color getColor()
357   {
358     return new Color(R, G, B);
359   }
360
361   protected void applyButton_actionPerformed()
362   {
363     if (caller != null)
364     {
365       if (caller instanceof FeatureSettings)
366       {
367         ((FeatureSettings) caller).setUserColour(originalLabel, getColor());
368       }
369       else if (caller instanceof AnnotationColourChooser)
370       {
371         if (originalLabel.equals("Min Colour"))
372         {
373           ((AnnotationColourChooser) caller)
374                   .minColour_actionPerformed(getColor());
375         }
376         else
377         {
378           ((AnnotationColourChooser) caller)
379                   .maxColour_actionPerformed(getColor());
380         }
381       }
382       else if (caller instanceof FeatureRenderer)
383       {
384         ((FeatureRenderer) caller).colourPanel.setBackground(getColor());
385       }
386
387       return;
388     }
389
390     Color[] newColours = new Color[24];
391     for (int i = 0; i < 24; i++)
392     {
393       Button button = (Button) buttonPanel.getComponent(i);
394       newColours[i] = button.getBackground();
395     }
396
397     UserColourScheme ucs = new UserColourScheme(newColours);
398     if (ap != null)
399     {
400       ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
401     }
402
403     if (ap != null)
404     {
405       if (seqGroup != null)
406       {
407         seqGroup.cs = ucs;
408       }
409       else
410       {
411         ap.av.setGlobalColourScheme(ucs);
412       }
413       ap.seqPanel.seqCanvas.img = null;
414       ap.paintAlignment(true);
415     }
416     else if (jmol != null)
417     {
418       jmol.setJalviewColourScheme(ucs);
419     }
420     else if (pdbcanvas != null)
421     {
422       pdbcanvas.setColours(ucs);
423     }
424   }
425
426   protected void cancelButton_actionPerformed()
427   {
428     if (caller != null)
429     {
430       if (caller instanceof FeatureSettings)
431       {
432         ((FeatureSettings) caller).setUserColour(originalLabel,
433                 originalColour);
434       }
435       else if (caller instanceof AnnotationColourChooser)
436       {
437         if (originalLabel.equals("Min Colour"))
438         {
439           ((AnnotationColourChooser) caller)
440                   .minColour_actionPerformed(originalColour);
441         }
442         else
443         {
444           ((AnnotationColourChooser) caller)
445                   .maxColour_actionPerformed(originalColour);
446         }
447       }
448       else if (caller instanceof FeatureRenderer)
449       {
450         ((FeatureRenderer) caller).colourPanel
451                 .setBackground(originalColour);
452
453       }
454
455       if (dialog != null)
456         dialog.setVisible(false);
457
458       frame.setVisible(false);
459       return;
460     }
461
462     Color[] newColours = new Color[24];
463     for (int i = 0; i < 24; i++)
464     {
465       newColours[i] = (Color) oldColours.elementAt(i);
466       buttonPanel.getComponent(i).setBackground(newColours[i]);
467     }
468
469     UserColourScheme ucs = new UserColourScheme(newColours);
470
471     if (ap != null)
472     {
473       if (seqGroup != null)
474       {
475         seqGroup.cs = ucs;
476       }
477       else
478       {
479         ap.av.setGlobalColourScheme(ucs);
480       }
481       ap.paintAlignment(true);
482     }
483     else if (jmol != null)
484     {
485       jmol.setJalviewColourScheme(ucs);
486     }
487     else if (pdbcanvas != null)
488     {
489       pdbcanvas.pdb.setColours(ucs);
490     }
491
492     frame.setVisible(false);
493   }
494
495   protected Panel buttonPanel = new Panel();
496
497   protected GridLayout gridLayout = new GridLayout();
498
499   Panel okcancelPanel = new Panel();
500
501   protected Button okButton = new Button();
502
503   protected Button applyButton = new Button();
504
505   protected Button cancelButton = new Button();
506
507   protected Scrollbar rScroller = new Scrollbar();
508
509   Label label1 = new Label();
510
511   protected TextField rText = new TextField();
512
513   Label label4 = new Label();
514
515   protected Scrollbar gScroller = new Scrollbar();
516
517   protected TextField gText = new TextField();
518
519   Label label5 = new Label();
520
521   protected Scrollbar bScroller = new Scrollbar();
522
523   protected TextField bText = new TextField();
524
525   protected Panel target = new Panel();
526
527   private void jbInit() throws Exception
528   {
529     this.setLayout(null);
530     buttonPanel.setLayout(gridLayout);
531     gridLayout.setColumns(6);
532     gridLayout.setRows(4);
533     okButton.setFont(new java.awt.Font("Verdana", 0, 11));
534     okButton.setLabel("OK");
535     okButton.addActionListener(this);
536     applyButton.setFont(new java.awt.Font("Verdana", 0, 11));
537     applyButton.setLabel("Apply");
538     applyButton.addActionListener(this);
539     cancelButton.setFont(new java.awt.Font("Verdana", 0, 11));
540     cancelButton.setLabel("Cancel");
541     cancelButton.addActionListener(this);
542     this.setBackground(new Color(212, 208, 223));
543     okcancelPanel.setBounds(new Rectangle(0, 265, 400, 35));
544     buttonPanel.setBounds(new Rectangle(0, 123, 400, 142));
545     rScroller.setMaximum(256);
546     rScroller.setMinimum(0);
547     rScroller.setOrientation(0);
548     rScroller.setUnitIncrement(1);
549     rScroller.setVisibleAmount(1);
550     rScroller.setBounds(new Rectangle(36, 27, 119, 19));
551     rScroller.addAdjustmentListener(this);
552     label1.setAlignment(Label.RIGHT);
553     label1.setText("R");
554     label1.setBounds(new Rectangle(19, 30, 16, 15));
555     rText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));
556     rText.setText("0        ");
557     rText.setBounds(new Rectangle(156, 27, 53, 19));
558     rText.addActionListener(this);
559     label4.setAlignment(Label.RIGHT);
560     label4.setText("G");
561     label4.setBounds(new Rectangle(15, 56, 20, 15));
562     gScroller.setMaximum(256);
563     gScroller.setMinimum(0);
564     gScroller.setOrientation(0);
565     gScroller.setUnitIncrement(1);
566     gScroller.setVisibleAmount(1);
567     gScroller.setBounds(new Rectangle(35, 52, 120, 20));
568     gScroller.addAdjustmentListener(this);
569     gText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));
570     gText.setText("0        ");
571     gText.setBounds(new Rectangle(156, 52, 53, 20));
572     gText.addActionListener(this);
573     label5.setAlignment(Label.RIGHT);
574     label5.setText("B");
575     label5.setBounds(new Rectangle(14, 82, 20, 15));
576     bScroller.setMaximum(256);
577     bScroller.setMinimum(0);
578     bScroller.setOrientation(0);
579     bScroller.setUnitIncrement(1);
580     bScroller.setVisibleAmount(1);
581     bScroller.setBounds(new Rectangle(35, 78, 120, 20));
582     bScroller.addAdjustmentListener(this);
583     bText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));
584     bText.setText("0        ");
585     bText.setBounds(new Rectangle(157, 78, 52, 20));
586     bText.addActionListener(this);
587     target.setBackground(Color.black);
588     target.setBounds(new Rectangle(229, 26, 134, 79));
589     this.add(okcancelPanel, null);
590     okcancelPanel.add(okButton, null);
591     okcancelPanel.add(applyButton, null);
592     okcancelPanel.add(cancelButton, null);
593     this.add(buttonPanel, null);
594     this.add(target, null);
595     this.add(gScroller);
596     this.add(rScroller);
597     this.add(bScroller);
598     this.add(label5);
599     this.add(label4);
600     this.add(label1);
601     this.add(gText);
602     this.add(rText);
603     this.add(bText);
604   }
605
606 }