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