071bad8641e7316dc68e0de8e30c7915fe608e50
[jalview.git] / src / jalview / appletgui / UserDefinedColours.java
1 package jalview.appletgui;\r
2 \r
3 import jalview.jbappletgui.GUserDefinedColours;\r
4 import jalview.datamodel.*;\r
5 import jalview.schemes.*;\r
6 import jalview.io.*;\r
7 import java.io.*;\r
8 import java.awt.*;\r
9 import java.awt.event.*;\r
10 import java.util.*;\r
11 \r
12 \r
13 public class UserDefinedColours extends GUserDefinedColours\r
14 {\r
15 \r
16   AlignmentPanel ap;\r
17   SequenceGroup seqGroup;\r
18   Button selectedButton;\r
19   Vector oldColours = new Vector();\r
20   ColourSchemeI oldColourScheme;\r
21   Frame frame;\r
22 \r
23   int R = 0, G = 0, B = 0;\r
24 \r
25   public ColourSchemeI loadDefaultColours()\r
26   {\r
27     // NOT IMPLEMENTED YET IN APPLET VERSION\r
28     return null;\r
29   }\r
30 \r
31   public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)\r
32   {\r
33     super();\r
34     frame = new Frame();\r
35     frame.add(this);\r
36     jalview.bin.JalviewLite.addFrame(frame, "User defined colours", 420,345 );\r
37 \r
38     if(sg!=null)\r
39         frame.setTitle( frame.getTitle()+ " ("+sg.getName()+")");\r
40 \r
41 \r
42     this.ap = ap;\r
43     seqGroup = sg;\r
44 \r
45 \r
46       if (seqGroup != null)\r
47         oldColourScheme = seqGroup.cs;\r
48       else\r
49         oldColourScheme = ap.av.getGlobalColourScheme();\r
50 \r
51       for (int i = 0; i < 20; i++)\r
52         makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +\r
53                    "", ResidueProperties.aa[i]);\r
54 \r
55       makeButton("B", "B");\r
56       makeButton("Z", "Z");\r
57       makeButton("X", "X");\r
58       makeButton("Gap", "'.','-',' '");\r
59 \r
60   }\r
61 \r
62   protected void rText_actionPerformed(ActionEvent e) {\r
63     try{\r
64       int i = Integer.parseInt(rText.getText());\r
65       rScroller.setValue(i);\r
66     }catch(NumberFormatException ex){}\r
67   }\r
68 \r
69   protected void gText_actionPerformed(ActionEvent e) {\r
70     try{\r
71       int i = Integer.parseInt(gText.getText());\r
72       gScroller.setValue(i);\r
73     }catch(NumberFormatException ex){}\r
74 \r
75   }\r
76 \r
77   protected void bText_actionPerformed(ActionEvent e) {\r
78     try{\r
79       int i = Integer.parseInt(bText.getText());\r
80       bScroller.setValue(i);\r
81     }catch(NumberFormatException ex){}\r
82 \r
83   }\r
84 \r
85 \r
86   protected void rScroller_adjustmentValueChanged(AdjustmentEvent e) {\r
87     R = rScroller.getValue();\r
88     rText.setText(R+"");\r
89     colourChanged();\r
90   }\r
91 \r
92   protected void gScroller_adjustmentValueChanged(AdjustmentEvent e) {\r
93     G = gScroller.getValue();\r
94     gText.setText(G+"");\r
95     colourChanged();\r
96   }\r
97 \r
98   protected void bScroller_adjustmentValueChanged(AdjustmentEvent e) {\r
99     B = bScroller.getValue();\r
100     bText.setText(B+"");\r
101     colourChanged();\r
102   }\r
103 \r
104  public void colourChanged()\r
105  {\r
106    Color col = new Color(R,G,B);\r
107    target.setBackground( col );\r
108 \r
109    if(selectedButton!=null)\r
110      selectedButton.setBackground( col );\r
111  }\r
112 \r
113   public void colourButtonPressed(MouseEvent e)\r
114   {\r
115      selectedButton = (Button)e.getSource();\r
116      Color col = selectedButton.getBackground();\r
117      R = col.getRed();\r
118      G = col.getGreen();\r
119      B = col.getBlue();\r
120      rScroller.setValue(R);\r
121      gScroller.setValue(G);\r
122      bScroller.setValue(B);\r
123      rText.setText(R+"");\r
124      gText.setText(G+"");\r
125      bText.setText(B+"");\r
126 \r
127      colourChanged();\r
128   }\r
129 \r
130   void makeButton(String label, String aa)\r
131   {\r
132     final Button button = new Button();\r
133     Color col = Color.white;\r
134 \r
135 \r
136     try{\r
137       col = oldColourScheme.findColour(aa, -1);\r
138     }catch(Exception ex){}\r
139 \r
140     button.setBackground(  col );\r
141     oldColours.addElement( col );\r
142     button.setLabel( label );\r
143     button.setForeground( col.darker().darker().darker() );\r
144     button.setFont(new java.awt.Font("Verdana", 1, 10));\r
145     button.addMouseListener(new java.awt.event.MouseAdapter()\r
146     {\r
147       public void mouseClicked(MouseEvent e)\r
148       {\r
149         colourButtonPressed(e);\r
150       }\r
151     });\r
152 \r
153     buttonPanel.add(button, null);\r
154   }\r
155 \r
156 \r
157   protected void okButton_actionPerformed(ActionEvent e)\r
158   {\r
159     applyButton_actionPerformed(null);\r
160    frame.setVisible(false);\r
161   }\r
162 \r
163   protected void applyButton_actionPerformed(ActionEvent e)\r
164   {\r
165 \r
166     Color [] newColours = new Color[24];\r
167     for(int i=0; i<24; i++)\r
168     {\r
169           Button button = (Button)buttonPanel.getComponent(i);\r
170           newColours[i] = button.getBackground();\r
171     }\r
172 \r
173     UserColourScheme ucs = new UserColourScheme( newColours );\r
174     ucs.setThreshold(0);\r
175 \r
176     if(seqGroup!=null)\r
177       seqGroup.cs = ucs;\r
178     else\r
179       ap.av.setGlobalColourScheme(ucs);\r
180 \r
181     ap.repaint();\r
182   }\r
183 \r
184 \r
185   public static UserColourScheme loadDefaultColours(String file)\r
186   {\r
187     UserColourScheme ucs = null;\r
188     try\r
189     {\r
190       BufferedReader in = new BufferedReader(new FileReader(file));\r
191       Color [] newColours = new Color[24];\r
192       for (int i = 0; i < 24; i++)\r
193       {\r
194         newColours[i] = new Color(Integer.parseInt(in.readLine()));\r
195       }\r
196 \r
197       ucs = new UserColourScheme(newColours);\r
198       ucs.setThreshold(0);\r
199 \r
200     }\r
201     catch (Exception ex)\r
202     {}\r
203 \r
204     return ucs;\r
205   }\r
206 \r
207 \r
208   protected void cancelButton_actionPerformed(ActionEvent e)\r
209   {\r
210 \r
211     Color [] newColours = new Color[24];\r
212     for(int i=0; i<24; i++)\r
213     {\r
214           newColours[i] = (Color)oldColours.elementAt(i);\r
215           buttonPanel.getComponent(i).setBackground(newColours[i]);\r
216     }\r
217 \r
218     UserColourScheme ucs = new UserColourScheme( newColours );\r
219 \r
220     if (seqGroup != null)\r
221       seqGroup.cs = ucs;\r
222     else\r
223       ap.av.setGlobalColourScheme(ucs);\r
224 \r
225     ap.repaint();\r
226 \r
227     frame.setVisible(false);\r
228   }\r
229 \r
230 \r
231 }\r