merge from 2_4_Release branch
[jalview.git] / src / jalview / appletgui / FeatureSettings.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
28 public class FeatureSettings extends Panel implements ItemListener,
29         MouseListener, MouseMotionListener, ActionListener,
30         AdjustmentListener
31 {
32   FeatureRenderer fr;
33
34   AlignmentPanel ap;
35
36   AlignViewport av;
37
38   Frame frame;
39
40   Panel groupPanel;
41
42   Panel featurePanel = new Panel();
43
44   ScrollPane scrollPane;
45
46   boolean alignmentHasFeatures = false;
47
48   Image linkImage;
49
50   Scrollbar transparency;
51
52   public FeatureSettings(final AlignmentPanel ap)
53   {
54     this.ap = ap;
55     this.av = ap.av;
56     ap.av.featureSettings = this;
57     fr = ap.seqPanel.seqCanvas.getFeatureRenderer();
58
59     transparency = new Scrollbar(Scrollbar.HORIZONTAL,
60             100 - (int) (fr.transparency * 100), 1, 1, 100);
61
62     if (fr.transparencySetter != null)
63     {
64       transparency.addAdjustmentListener(this);
65     }
66     else
67     {
68       transparency.setEnabled(false);
69     }
70
71     java.net.URL url = getClass().getResource("/images/link.gif");
72     if (url != null)
73     {
74       linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
75     }
76
77     if (av.featuresDisplayed == null)
78     {
79       fr.findAllFeatures();
80     }
81
82     setTableData();
83
84     this.setLayout(new BorderLayout());
85     scrollPane = new ScrollPane();
86     scrollPane.add(featurePanel);
87     if (alignmentHasFeatures)
88     {
89       add(scrollPane, BorderLayout.CENTER);
90     }
91
92     Button invert = new Button("Invert Selection");
93     invert.addActionListener(this);
94
95     Panel lowerPanel = new Panel(new GridLayout(2, 1, 5, 10));
96     lowerPanel.add(invert);
97
98     Panel tPanel = new Panel(new BorderLayout());
99
100     if (fr.transparencySetter != null)
101     {
102       tPanel.add(transparency, BorderLayout.CENTER);
103       tPanel.add(new Label("Transparency"), BorderLayout.EAST);
104     }
105     else
106     {
107       tPanel.add(
108               new Label("Transparency not available in this web browser"),
109               BorderLayout.CENTER);
110     }
111
112     lowerPanel.add(tPanel, BorderLayout.SOUTH);
113
114     add(lowerPanel, BorderLayout.SOUTH);
115
116     if (groupPanel != null)
117     {
118       groupPanel.setLayout(new GridLayout(fr.featureGroups.size() / 4 + 1,
119               4));
120       groupPanel.validate();
121
122       add(groupPanel, BorderLayout.NORTH);
123     }
124     frame = new Frame();
125     frame.add(this);
126     final FeatureSettings me = this;
127     frame.addWindowListener(new WindowAdapter()
128     {
129       public void windowClosing(WindowEvent e)
130       {
131         if (me.av.featureSettings == me)
132         {
133           me.av.featureSettings = null;
134           me.ap = null;
135           me.av = null;
136         }
137       }
138     });
139     int height = featurePanel.getComponentCount() * 50 + 60;
140
141     height = Math.max(200, height);
142     height = Math.min(400, height);
143     int width = 300;
144     jalview.bin.JalviewLite.addFrame(frame, "Feature Settings", width,
145             height);
146   }
147
148   public void paint(Graphics g)
149   {
150     g.setColor(Color.black);
151     g.drawString("No Features added to this alignment!!", 10, 20);
152     g.drawString("(Features can be added from searches or", 10, 40);
153     g.drawString("from Jalview / GFF features files)", 10, 60);
154   }
155
156   public void setTableData()
157   {
158     alignmentHasFeatures = fr.buildGroupHash();
159     if (alignmentHasFeatures)
160     {
161       rebuildGroups();
162
163     }
164     resetTable(false);
165   }
166
167   /**
168    * rebuilds the group panel
169    */
170   public void rebuildGroups()
171   {
172     boolean rdrw = false;
173     if (groupPanel == null)
174     {
175       groupPanel = new Panel();
176     }
177     else
178     {
179       rdrw = true;
180       groupPanel.removeAll();
181     }
182
183     Enumeration gps = fr.featureGroups.keys();
184     while (gps.hasMoreElements())
185     {
186       String group = (String) gps.nextElement();
187       Boolean vis = (Boolean) fr.featureGroups.get(group);
188       Checkbox check = new MyCheckbox(group, vis.booleanValue(),
189               (fr.featureLinks != null && fr.featureLinks
190                       .containsKey(group)));
191       check.addMouseListener(this);
192       check.setFont(new Font("Serif", Font.BOLD, 12));
193       check.addItemListener(this);
194       groupPanel.add(check);
195     }
196     if (rdrw)
197     {
198       groupPanel.validate();
199     }
200   }
201
202   // This routine adds and removes checkboxes depending on
203   // Group selection states
204   void resetTable(boolean groupsChanged)
205   {
206     SequenceFeature[] tmpfeatures;
207     String group = null, type;
208     Vector visibleChecks = new Vector();
209
210     for (int i = 0; i < av.alignment.getHeight(); i++)
211     {
212       if (av.alignment.getSequenceAt(i).getSequenceFeatures() == null)
213       {
214         continue;
215       }
216
217       tmpfeatures = av.alignment.getSequenceAt(i).getSequenceFeatures();
218       int index = 0;
219       while (index < tmpfeatures.length)
220       {
221         group = tmpfeatures[index].featureGroup;
222
223         if (group == null || fr.featureGroups.get(group) == null
224                 || ((Boolean) fr.featureGroups.get(group)).booleanValue())
225         {
226           type = tmpfeatures[index].getType();
227           if (!visibleChecks.contains(type))
228           {
229             visibleChecks.addElement(type);
230           }
231         }
232         index++;
233       }
234     }
235
236     Component[] comps;
237     int cSize = featurePanel.getComponentCount();
238     Checkbox check;
239     // This will remove any checkboxes which shouldn't be
240     // visible
241     for (int i = 0; i < cSize; i++)
242     {
243       comps = featurePanel.getComponents();
244       check = (Checkbox) comps[i];
245       if (!visibleChecks.contains(check.getLabel()))
246       {
247         featurePanel.remove(i);
248         cSize--;
249         i--;
250       }
251     }
252
253     if (fr.renderOrder != null)
254     {
255       // First add the checks in the previous render order,
256       // in case the window has been closed and reopened
257       for (int ro = fr.renderOrder.length - 1; ro > -1; ro--)
258       {
259         String item = fr.renderOrder[ro];
260
261         if (!visibleChecks.contains(item))
262         {
263           continue;
264         }
265
266         visibleChecks.removeElement(item);
267
268         addCheck(false, item);
269       }
270     }
271
272     // now add checkboxes which should be visible,
273     // if they have not already been added
274     Enumeration en = visibleChecks.elements();
275
276     while (en.hasMoreElements())
277     {
278       addCheck(groupsChanged, en.nextElement().toString());
279     }
280
281     featurePanel.setLayout(new GridLayout(featurePanel.getComponentCount(),
282             1, 10, 5));
283     featurePanel.validate();
284
285     if (scrollPane != null)
286     {
287       scrollPane.validate();
288     }
289
290     itemStateChanged(null);
291   }
292
293   /**
294    * update the checklist of feature types with the given type
295    * 
296    * @param groupsChanged
297    *                true means if the type is not in the display list then it
298    *                will be added and displayed
299    * @param type
300    *                feature type to be checked for in the list.
301    */
302   void addCheck(boolean groupsChanged, String type)
303   {
304     boolean addCheck;
305     Component[] comps = featurePanel.getComponents();
306     Checkbox check;
307     addCheck = true;
308     for (int i = 0; i < featurePanel.getComponentCount(); i++)
309     {
310       check = (Checkbox) comps[i];
311       if (check.getLabel().equals(type))
312       {
313         addCheck = false;
314         break;
315       }
316     }
317
318     if (addCheck)
319     {
320       boolean selected = false;
321       if (groupsChanged || av.featuresDisplayed.containsKey(type))
322       {
323         selected = true;
324       }
325
326       check = new MyCheckbox(
327               type,
328               selected,
329               (fr.featureLinks != null && fr.featureLinks.containsKey(type)));
330
331       check.addMouseListener(this);
332       check.addMouseMotionListener(this);
333       check.setBackground(fr.getColour(type));
334       check.addItemListener(this);
335       if (groupsChanged)
336       {
337         // add at beginning of stack.
338         featurePanel.add(check, 0);
339       }
340       else
341       {
342         // add at end of stack.
343         featurePanel.add(check);
344       }
345     }
346   }
347
348   public void actionPerformed(ActionEvent evt)
349   {
350     for (int i = 0; i < featurePanel.getComponentCount(); i++)
351     {
352       Checkbox check = (Checkbox) featurePanel.getComponent(i);
353       check.setState(!check.getState());
354     }
355     selectionChanged();
356   }
357
358   public void itemStateChanged(ItemEvent evt)
359   {
360     if (evt != null)
361     {
362       // Is the source a top level featureGroup?
363       Checkbox source = (Checkbox) evt.getSource();
364       if (fr.featureGroups.containsKey(source.getLabel()))
365       {
366         fr.featureGroups.put(source.getLabel(), new Boolean(source
367                 .getState()));
368         ap.seqPanel.seqCanvas.repaint();
369         if (ap.overviewPanel != null)
370         {
371           ap.overviewPanel.updateOverviewImage();
372         }
373
374         resetTable(true);
375         return;
376       }
377     }
378     selectionChanged();
379   }
380
381   void selectionChanged()
382   {
383     Component[] comps = featurePanel.getComponents();
384     int cSize = comps.length;
385
386     Object[][] tmp = new Object[cSize][3];
387     int tmpSize = 0;
388     for (int i = 0; i < cSize; i++)
389     {
390       Checkbox check = (Checkbox) comps[i];
391       tmp[tmpSize][0] = check.getLabel();
392       tmp[tmpSize][1] = fr.getColour(check.getLabel());
393       tmp[tmpSize][2] = new Boolean(check.getState());
394       tmpSize++;
395     }
396
397     Object[][] data = new Object[tmpSize][3];
398     System.arraycopy(tmp, 0, data, 0, tmpSize);
399
400     fr.setFeaturePriority(data);
401
402     ap.paintAlignment(true);
403   }
404
405   MyCheckbox selectedCheck;
406
407   boolean dragging = false;
408
409   public void mousePressed(MouseEvent evt)
410   {
411
412     selectedCheck = (MyCheckbox) evt.getSource();
413
414     if (fr.featureLinks != null
415             && fr.featureLinks.containsKey(selectedCheck.getLabel()))
416     {
417       if (evt.getX() > selectedCheck.stringWidth + 20)
418       {
419         evt.consume();
420       }
421     }
422
423   }
424
425   public void mouseDragged(MouseEvent evt)
426   {
427     if (((Component) evt.getSource()).getParent() != featurePanel)
428     {
429       return;
430     }
431     dragging = true;
432   }
433
434   public void mouseReleased(MouseEvent evt)
435   {
436     if (((Component) evt.getSource()).getParent() != featurePanel)
437     {
438       return;
439     }
440
441     Component comp = null;
442     Checkbox target = null;
443
444     int height = evt.getY() + evt.getComponent().getLocation().y;
445
446     if (height > featurePanel.getSize().height)
447     {
448
449       comp = featurePanel
450               .getComponent(featurePanel.getComponentCount() - 1);
451     }
452     else if (height < 0)
453     {
454       comp = featurePanel.getComponent(0);
455     }
456     else
457     {
458       comp = featurePanel.getComponentAt(evt.getX(), evt.getY()
459               + evt.getComponent().getLocation().y);
460     }
461
462     if (comp != null && comp instanceof Checkbox)
463     {
464       target = (Checkbox) comp;
465     }
466
467     if (selectedCheck != null && target != null && selectedCheck != target)
468     {
469       int targetIndex = -1;
470       for (int i = 0; i < featurePanel.getComponentCount(); i++)
471       {
472         if (target == featurePanel.getComponent(i))
473         {
474           targetIndex = i;
475           break;
476         }
477       }
478
479       featurePanel.remove(selectedCheck);
480       featurePanel.add(selectedCheck, targetIndex);
481       featurePanel.validate();
482       itemStateChanged(null);
483     }
484   }
485
486   public void setUserColour(String feature, Color col)
487   {
488     fr.setColour(feature, col);
489     featurePanel.removeAll();
490     resetTable(false);
491     ap.paintAlignment(true);
492   }
493
494   public void mouseEntered(MouseEvent evt)
495   {
496   }
497
498   public void mouseExited(MouseEvent evt)
499   {
500   }
501
502   public void mouseClicked(MouseEvent evt)
503   {
504     MyCheckbox check = (MyCheckbox) evt.getSource();
505
506     if (fr.featureLinks != null
507             && fr.featureLinks.containsKey(check.getLabel()))
508     {
509       if (evt.getX() > check.stringWidth + 20)
510       {
511         evt.consume();
512         String link = fr.featureLinks.get(check.getLabel()).toString();
513         ap.alignFrame.showURL(link.substring(link.indexOf("|") + 1), link
514                 .substring(0, link.indexOf("|")));
515       }
516     }
517
518     if (check.getParent() != featurePanel)
519     {
520       return;
521     }
522
523     if (evt.getClickCount() > 1)
524     {
525       new UserDefinedColours(this, check.getLabel(), fr.getColour(check
526               .getLabel()));
527     }
528   }
529
530   public void mouseMoved(MouseEvent evt)
531   {
532   }
533
534   public void adjustmentValueChanged(AdjustmentEvent evt)
535   {
536     fr.transparency = ((float) (100 - transparency.getValue()) / 100f);
537     ap.seqPanel.seqCanvas.repaint();
538
539   }
540
541   class MyCheckbox extends Checkbox
542   {
543     public int stringWidth;
544
545     boolean hasLink;
546
547     public MyCheckbox(String label, boolean checked, boolean haslink)
548     {
549       super(label, checked);
550
551       FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
552       stringWidth = fm.stringWidth(label);
553       this.hasLink = haslink;
554     }
555
556     public void paint(Graphics g)
557     {
558       if (hasLink)
559       {
560         g.drawImage(linkImage, stringWidth + 25,
561                 (getSize().height - linkImage.getHeight(this)) / 2, this);
562       }
563     }
564   }
565 }