b9c0950a8bc43cb55edd26a678761db074131d85
[jalview.git] / src / jalview / appletgui / UserDefinedColours.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 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 \r
42   Component caller;\r
43   String originalLabel;\r
44   Color originalColour;\r
45 \r
46   int R = 0, G = 0, B = 0;\r
47 \r
48   public ColourSchemeI loadDefaultColours()\r
49   {\r
50     // NOT IMPLEMENTED YET IN APPLET VERSION\r
51     return null;\r
52   }\r
53 \r
54   public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)\r
55   {\r
56     this.ap = ap;\r
57     seqGroup = sg;\r
58 \r
59     if (seqGroup != null)\r
60     {\r
61       oldColourScheme = seqGroup.cs;\r
62     }\r
63     else\r
64     {\r
65       oldColourScheme = ap.av.getGlobalColourScheme();\r
66     }\r
67 \r
68     init();\r
69   }\r
70 \r
71   public UserDefinedColours(MCview.AppletPDBCanvas pdb)\r
72   {\r
73     this.pdbcanvas = pdb;\r
74     init();\r
75   }\r
76 \r
77   public UserDefinedColours(Component caller,\r
78                             String label,\r
79                             Color colour)\r
80   {\r
81     this.caller = caller;\r
82     originalColour = colour;\r
83     originalLabel = label;\r
84     init();\r
85     remove(buttonPanel);\r
86 \r
87     setTargetColour(colour);\r
88 \r
89     okcancelPanel.setBounds(new Rectangle(0, 113, 400, 35));\r
90     frame.setTitle("User Defined Colours - "+label);\r
91     frame.setSize(420, 200);\r
92   }\r
93 \r
94   public void actionPerformed(ActionEvent evt)\r
95   {\r
96     if(evt.getSource()==okButton)\r
97      okButton_actionPerformed();\r
98    else if(evt.getSource()==applyButton)\r
99       applyButton_actionPerformed();\r
100    else if(evt.getSource()==cancelButton)\r
101      cancelButton_actionPerformed();\r
102    else if(evt.getSource()==rText)\r
103      rText_actionPerformed();\r
104    else if (evt.getSource() == gText)\r
105      gText_actionPerformed();\r
106    else if (evt.getSource() == bText)\r
107      bText_actionPerformed();\r
108 }\r
109 \r
110   public void adjustmentValueChanged(AdjustmentEvent evt)\r
111   {\r
112     if(evt.getSource()==rScroller)\r
113     rScroller_adjustmentValueChanged();\r
114   else if(evt.getSource()==gScroller)\r
115     gScroller_adjustmentValueChanged();\r
116   else if(evt.getSource()==bScroller)\r
117     bScroller_adjustmentValueChanged();\r
118 }\r
119 \r
120   void init()\r
121   {\r
122     try\r
123     {\r
124       jbInit();\r
125     }\r
126     catch (Exception e)\r
127     {\r
128       e.printStackTrace();\r
129     }\r
130     frame = new Frame();\r
131     frame.add(this);\r
132     jalview.bin.JalviewLite.addFrame(frame, "User defined colours", 420, 345);\r
133 \r
134     if (seqGroup != null)\r
135     {\r
136       frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");\r
137     }\r
138 \r
139     for (int i = 0; i < 20; i++)\r
140     {\r
141       makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +\r
142                  "", ResidueProperties.aa[i]);\r
143     }\r
144 \r
145     makeButton("B", "B");\r
146     makeButton("Z", "Z");\r
147     makeButton("X", "X");\r
148     makeButton("Gap", "'.','-',' '");\r
149 \r
150     validate();\r
151   }\r
152   protected void rText_actionPerformed()\r
153   {\r
154     try\r
155     {\r
156       int i = Integer.parseInt(rText.getText());\r
157       rScroller.setValue(i);\r
158       rScroller_adjustmentValueChanged();\r
159     }\r
160     catch (NumberFormatException ex)\r
161     {}\r
162   }\r
163 \r
164   protected void gText_actionPerformed()\r
165   {\r
166     try\r
167     {\r
168       int i = Integer.parseInt(gText.getText());\r
169       gScroller.setValue(i);\r
170       gScroller_adjustmentValueChanged();\r
171     }\r
172     catch (NumberFormatException ex)\r
173     {}\r
174 \r
175   }\r
176 \r
177   protected void bText_actionPerformed()\r
178   {\r
179     try\r
180     {\r
181       int i = Integer.parseInt(bText.getText());\r
182       bScroller.setValue(i);\r
183       bScroller_adjustmentValueChanged();\r
184     }\r
185     catch (NumberFormatException ex)\r
186     {}\r
187 \r
188   }\r
189 \r
190   protected void rScroller_adjustmentValueChanged()\r
191   {\r
192     R = rScroller.getValue();\r
193     rText.setText(R + "");\r
194     colourChanged();\r
195   }\r
196 \r
197   protected void gScroller_adjustmentValueChanged()\r
198   {\r
199     G = gScroller.getValue();\r
200     gText.setText(G + "");\r
201     colourChanged();\r
202   }\r
203 \r
204   protected void bScroller_adjustmentValueChanged()\r
205   {\r
206     B = bScroller.getValue();\r
207     bText.setText(B + "");\r
208     colourChanged();\r
209   }\r
210 \r
211   public void colourChanged()\r
212   {\r
213     Color col = new Color(R, G, B);\r
214     target.setBackground(col);\r
215     target.repaint();\r
216 \r
217     if (selectedButton != null)\r
218     {\r
219       selectedButton.setBackground(col);\r
220       selectedButton.repaint();\r
221     }\r
222   }\r
223 \r
224   void setTargetColour(Color col)\r
225   {\r
226     R = col.getRed();\r
227     G = col.getGreen();\r
228     B = col.getBlue();\r
229     rScroller.setValue(R);\r
230     gScroller.setValue(G);\r
231     bScroller.setValue(B);\r
232     rText.setText(R + "");\r
233     gText.setText(G + "");\r
234     bText.setText(B + "");\r
235     colourChanged();\r
236   }\r
237 \r
238   public void colourButtonPressed(MouseEvent e)\r
239   {\r
240     selectedButton = (Button) e.getSource();\r
241     setTargetColour(selectedButton.getBackground());\r
242   }\r
243 \r
244   void makeButton(String label, String aa)\r
245   {\r
246     final Button button = new Button();\r
247     Color col = Color.white;\r
248 \r
249     try\r
250     {\r
251       col = oldColourScheme.findColour(aa.charAt(0), -1);\r
252     }\r
253     catch (Exception ex)\r
254     {}\r
255 \r
256     button.setBackground(col);\r
257     oldColours.addElement(col);\r
258     button.setLabel(label);\r
259     button.setForeground(col.darker().darker().darker());\r
260     button.setFont(new java.awt.Font("Verdana", 1, 10));\r
261     button.addMouseListener(new java.awt.event.MouseAdapter()\r
262     {\r
263       public void mousePressed(MouseEvent e)\r
264       {\r
265         colourButtonPressed(e);\r
266       }\r
267     });\r
268 \r
269     buttonPanel.add(button, null);\r
270   }\r
271 \r
272   protected void okButton_actionPerformed()\r
273   {\r
274     applyButton_actionPerformed();\r
275     frame.setVisible(false);\r
276   }\r
277 \r
278   protected void applyButton_actionPerformed()\r
279   {\r
280     if (caller != null)\r
281     {\r
282       if(caller instanceof FeatureSettings)\r
283         ((FeatureSettings)caller).setUserColour\r
284             (originalLabel, new Color(R,G,B));\r
285       else if(caller instanceof AnnotationColourChooser)\r
286       {\r
287         if (originalLabel.equals("Min Colour"))\r
288           ( (AnnotationColourChooser) caller).minColour_actionPerformed\r
289               (new Color(R, G, B));\r
290         else\r
291           ( (AnnotationColourChooser) caller).maxColour_actionPerformed\r
292               (new Color(R, G, B));\r
293       }\r
294       return;\r
295     }\r
296 \r
297 \r
298     Color[] newColours = new Color[24];\r
299     for (int i = 0; i < 24; i++)\r
300     {\r
301       Button button = (Button) buttonPanel.getComponent(i);\r
302       newColours[i] = button.getBackground();\r
303     }\r
304 \r
305     UserColourScheme ucs = new UserColourScheme(newColours);\r
306     if(ap!=null)\r
307       ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());\r
308 \r
309     if(ap!=null)\r
310     {\r
311       if (seqGroup != null)\r
312       {\r
313         seqGroup.cs = ucs;\r
314       }\r
315       else\r
316       {\r
317         ap.av.setGlobalColourScheme(ucs);\r
318       }\r
319       ap.seqPanel.seqCanvas.img = null;\r
320       ap.repaint();\r
321     }\r
322     else if(pdbcanvas!=null)\r
323     {\r
324       pdbcanvas.pdb.setColours(ucs);\r
325       pdbcanvas.updateSeqColours();\r
326     }\r
327   }\r
328 \r
329 \r
330   protected void cancelButton_actionPerformed()\r
331   {\r
332     if (caller != null)\r
333     {\r
334       if(caller instanceof FeatureSettings)\r
335         ((FeatureSettings)caller).setUserColour\r
336             (originalLabel, originalColour);\r
337       else if (caller instanceof AnnotationColourChooser)\r
338       {\r
339         if (originalLabel.equals("Min Colour"))\r
340           ( (AnnotationColourChooser) caller).minColour_actionPerformed\r
341               (originalColour);\r
342         else\r
343           ( (AnnotationColourChooser) caller).maxColour_actionPerformed\r
344               (originalColour);\r
345       }\r
346       frame.setVisible(false);\r
347       return;\r
348     }\r
349 \r
350     Color[] newColours = new Color[24];\r
351     for (int i = 0; i < 24; i++)\r
352     {\r
353       newColours[i] = (Color) oldColours.elementAt(i);\r
354       buttonPanel.getComponent(i).setBackground(newColours[i]);\r
355     }\r
356 \r
357     UserColourScheme ucs = new UserColourScheme(newColours);\r
358 \r
359    if(ap!=null)\r
360     {\r
361       if (seqGroup != null)\r
362       {\r
363         seqGroup.cs = ucs;\r
364       }\r
365       else\r
366       {\r
367         ap.av.setGlobalColourScheme(ucs);\r
368       }\r
369       ap.repaint();\r
370     }\r
371     else if(pdbcanvas!=null)\r
372     {\r
373       pdbcanvas.pdb.setColours(ucs);\r
374     }\r
375 \r
376     frame.setVisible(false);\r
377   }\r
378 \r
379 \r
380   protected Panel buttonPanel = new Panel();\r
381   protected GridLayout gridLayout = new GridLayout();\r
382   Panel okcancelPanel = new Panel();\r
383   protected Button okButton = new Button();\r
384   protected Button applyButton = new Button();\r
385   protected Button cancelButton = new Button();\r
386   protected Scrollbar rScroller = new Scrollbar();\r
387   Label label1 = new Label();\r
388   protected TextField rText = new TextField();\r
389   Label label4 = new Label();\r
390   protected Scrollbar gScroller = new Scrollbar();\r
391   protected TextField gText = new TextField();\r
392   Label label5 = new Label();\r
393   protected Scrollbar bScroller = new Scrollbar();\r
394   protected TextField bText = new TextField();\r
395   protected Panel target = new Panel();\r
396 \r
397   private void jbInit() throws Exception {\r
398       this.setLayout(null);\r
399       buttonPanel.setLayout(gridLayout);\r
400       gridLayout.setColumns(6);\r
401       gridLayout.setRows(4);\r
402       okButton.setFont(new java.awt.Font("Verdana", 0, 11));\r
403       okButton.setLabel("OK");\r
404       okButton.addActionListener(this);\r
405       applyButton.setFont(new java.awt.Font("Verdana", 0, 11));\r
406       applyButton.setLabel("Apply");\r
407       applyButton.addActionListener(this);\r
408       cancelButton.setFont(new java.awt.Font("Verdana", 0, 11));\r
409       cancelButton.setLabel("Cancel");\r
410       cancelButton.addActionListener(this);\r
411       this.setBackground(new Color(212, 208, 223));\r
412       okcancelPanel.setBounds(new Rectangle(0, 265, 400, 35));\r
413       buttonPanel.setBounds(new Rectangle(0, 123, 400, 142));\r
414       rScroller.setMaximum(256);\r
415       rScroller.setMinimum(0);\r
416       rScroller.setOrientation(0);\r
417       rScroller.setUnitIncrement(1);\r
418   rScroller.setVisibleAmount(1);\r
419   rScroller.setBounds(new Rectangle(36, 27, 119, 19));\r
420   rScroller.addAdjustmentListener(this);\r
421   label1.setAlignment(Label.RIGHT);\r
422   label1.setText("R");\r
423   label1.setBounds(new Rectangle(19, 30, 16, 15));\r
424   rText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));\r
425   rText.setText("0        ");\r
426   rText.setBounds(new Rectangle(156, 27, 53, 19));\r
427   rText.addActionListener(this);\r
428   label4.setAlignment(Label.RIGHT);\r
429   label4.setText("G");\r
430   label4.setBounds(new Rectangle(15, 56, 20, 15));\r
431   gScroller.setMaximum(256);\r
432       gScroller.setMinimum(0);\r
433       gScroller.setOrientation(0);\r
434       gScroller.setUnitIncrement(1);\r
435   gScroller.setVisibleAmount(1);\r
436   gScroller.setBounds(new Rectangle(35, 52, 120, 20));\r
437   gScroller.addAdjustmentListener(this);\r
438   gText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));\r
439   gText.setText("0        ");\r
440   gText.setBounds(new Rectangle(156, 52, 53, 20));\r
441   gText.addActionListener(this);\r
442   label5.setAlignment(Label.RIGHT);\r
443   label5.setText("B");\r
444   label5.setBounds(new Rectangle(14, 82, 20, 15));\r
445   bScroller.setMaximum(256);\r
446       bScroller.setMinimum(0);\r
447       bScroller.setOrientation(0);\r
448       bScroller.setUnitIncrement(1);\r
449   bScroller.setVisibleAmount(1);\r
450   bScroller.setBounds(new Rectangle(35, 78, 120, 20));\r
451   bScroller.addAdjustmentListener(this);\r
452   bText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));\r
453   bText.setText("0        ");\r
454   bText.setBounds(new Rectangle(157, 78, 52, 20));\r
455   bText.addActionListener(this);\r
456   target.setBackground(Color.black);\r
457       target.setBounds(new Rectangle(229, 26, 134, 79));\r
458       this.add(okcancelPanel, null);\r
459       okcancelPanel.add(okButton, null);\r
460       okcancelPanel.add(applyButton, null);\r
461       okcancelPanel.add(cancelButton, null);\r
462       this.add(buttonPanel, null);\r
463   this.add(target, null);\r
464   this.add(gScroller);\r
465   this.add(rScroller);\r
466   this.add(bScroller);\r
467   this.add(label5);\r
468   this.add(label4);\r
469   this.add(label1);\r
470   this.add(gText);\r
471   this.add(rText);\r
472   this.add(bText);\r
473 }\r
474 \r
475 }\r