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