JAL-1989 more unit tests and encapsulation for ColumnSelection
[jalview.git] / src / jalview / gui / ScalePanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.datamodel.ColumnSelection;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.util.MessageManager;
27
28 import java.awt.Color;
29 import java.awt.FontMetrics;
30 import java.awt.Graphics;
31 import java.awt.Graphics2D;
32 import java.awt.RenderingHints;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.MouseEvent;
36 import java.awt.event.MouseListener;
37 import java.awt.event.MouseMotionListener;
38
39 import javax.swing.JMenuItem;
40 import javax.swing.JPanel;
41 import javax.swing.JPopupMenu;
42 import javax.swing.SwingUtilities;
43 import javax.swing.ToolTipManager;
44
45 /**
46  * DOCUMENT ME!
47  * 
48  * @author $author$
49  * @version $Revision$
50  */
51 public class ScalePanel extends JPanel implements MouseMotionListener,
52         MouseListener
53 {
54   protected int offy = 4;
55
56   /** DOCUMENT ME!! */
57   public int width;
58
59   protected AlignViewport av;
60
61   AlignmentPanel ap;
62
63   boolean stretchingGroup = false;
64
65   int min; // used by mouseDragged to see if user
66
67   int max; // used by mouseDragged to see if user
68
69   boolean mouseDragging = false;
70
71   // wants to delete columns
72   public ScalePanel(AlignViewport av, AlignmentPanel ap)
73   {
74     this.av = av;
75     this.ap = ap;
76
77     addMouseListener(this);
78     addMouseMotionListener(this);
79   }
80
81   /**
82    * DOCUMENT ME!
83    * 
84    * @param evt
85    *          DOCUMENT ME!
86    */
87   @Override
88   public void mousePressed(MouseEvent evt)
89   {
90     int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
91     final int res;
92
93     if (av.hasHiddenColumns())
94     {
95       x = av.getColumnSelection().adjustForHiddenColumns(x);
96     }
97
98     if (x >= av.getAlignment().getWidth())
99     {
100       res = av.getAlignment().getWidth() - 1;
101     }
102     else
103     {
104       res = x;
105     }
106
107     min = res;
108     max = res;
109
110     if (SwingUtilities.isRightMouseButton(evt))
111     {
112       JPopupMenu pop = new JPopupMenu();
113       if (reveal != null)
114       {
115         JMenuItem item = new JMenuItem(
116                 MessageManager.getString("label.reveal"));
117         item.addActionListener(new ActionListener()
118         {
119           @Override
120           public void actionPerformed(ActionEvent e)
121           {
122             av.showColumn(reveal[0]);
123             reveal = null;
124             ap.paintAlignment(true);
125             if (ap.overviewPanel != null)
126             {
127               ap.overviewPanel.updateOverviewImage();
128             }
129           }
130         });
131         pop.add(item);
132
133         if (av.getColumnSelection().hasHiddenColumns())
134         {
135           item = new JMenuItem(
136                   MessageManager.getString("action.reveal_all"));
137           item.addActionListener(new ActionListener()
138           {
139             @Override
140             public void actionPerformed(ActionEvent e)
141             {
142               av.showAllHiddenColumns();
143               reveal = null;
144               ap.paintAlignment(true);
145               if (ap.overviewPanel != null)
146               {
147                 ap.overviewPanel.updateOverviewImage();
148               }
149             }
150           });
151           pop.add(item);
152         }
153         pop.show(this, evt.getX(), evt.getY());
154       }
155       else if (av.getColumnSelection().contains(res))
156       {
157         JMenuItem item = new JMenuItem(
158                 MessageManager.getString("label.hide_columns"));
159         item.addActionListener(new ActionListener()
160         {
161           @Override
162           public void actionPerformed(ActionEvent e)
163           {
164             av.hideColumns(res, res);
165             if (av.getSelectionGroup() != null
166                     && av.getSelectionGroup().getSize() == av
167                             .getAlignment().getHeight())
168             {
169               av.setSelectionGroup(null);
170             }
171
172             ap.paintAlignment(true);
173             if (ap.overviewPanel != null)
174             {
175               ap.overviewPanel.updateOverviewImage();
176             }
177             av.sendSelection();
178           }
179         });
180         pop.add(item);
181         pop.show(this, evt.getX(), evt.getY());
182       }
183     }
184     else
185     // LEFT MOUSE TO SELECT
186     {
187       if (!evt.isControlDown() && !evt.isShiftDown())
188       {
189         av.getColumnSelection().clear();
190       }
191
192       av.getColumnSelection().addElement(res);
193       SequenceGroup sg = new SequenceGroup();
194       // try to be as quick as possible
195       SequenceI[] iVec = av.getAlignment().getSequencesArray();
196       for (int i = 0; i < iVec.length; i++)
197       {
198         sg.addSequence(iVec[i], false);
199         iVec[i] = null;
200       }
201       iVec = null;
202       sg.setStartRes(res);
203       sg.setEndRes(res);
204
205       if (evt.isShiftDown())
206       {
207         int min = Math.min(av.getColumnSelection().getMin(), res);
208         int max = Math.max(av.getColumnSelection().getMax(), res);
209         for (int i = min; i < max; i++)
210         {
211           av.getColumnSelection().addElement(i);
212         }
213         sg.setStartRes(min);
214         sg.setEndRes(max);
215       }
216       av.setSelectionGroup(sg);
217     }
218
219     ap.paintAlignment(false);
220     av.sendSelection();
221   }
222
223   /**
224    * DOCUMENT ME!
225    * 
226    * @param evt
227    *          DOCUMENT ME!
228    */
229   @Override
230   public void mouseReleased(MouseEvent evt)
231   {
232     mouseDragging = false;
233
234     int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
235
236     if (av.hasHiddenColumns())
237     {
238       res = av.getColumnSelection().adjustForHiddenColumns(res);
239     }
240
241     if (res >= av.getAlignment().getWidth())
242     {
243       res = av.getAlignment().getWidth() - 1;
244     }
245
246     if (!stretchingGroup)
247     {
248       ap.paintAlignment(false);
249
250       return;
251     }
252
253     SequenceGroup sg = av.getSelectionGroup();
254
255     if (sg != null)
256     {
257       if (res > sg.getStartRes())
258       {
259         sg.setEndRes(res);
260       }
261       else if (res < sg.getStartRes())
262       {
263         sg.setStartRes(res);
264       }
265     }
266     stretchingGroup = false;
267     ap.paintAlignment(false);
268     av.sendSelection();
269   }
270
271   /**
272    * DOCUMENT ME!
273    * 
274    * @param evt
275    *          DOCUMENT ME!
276    */
277   @Override
278   public void mouseDragged(MouseEvent evt)
279   {
280     mouseDragging = true;
281
282     int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
283     if (res < 0)
284     {
285       res = 0;
286     }
287
288     if (av.hasHiddenColumns())
289     {
290       res = av.getColumnSelection().adjustForHiddenColumns(res);
291     }
292
293     if (res >= av.getAlignment().getWidth())
294     {
295       res = av.getAlignment().getWidth() - 1;
296     }
297
298     if (res < min)
299     {
300       min = res;
301     }
302
303     if (res > max)
304     {
305       max = res;
306     }
307
308     SequenceGroup sg = av.getSelectionGroup();
309
310     if (sg != null)
311     {
312       stretchingGroup = true;
313
314       if (!av.getColumnSelection().contains(res))
315       {
316         av.getColumnSelection().addElement(res);
317       }
318
319       if (res > sg.getStartRes())
320       {
321         sg.setEndRes(res);
322       }
323       if (res < sg.getStartRes())
324       {
325         sg.setStartRes(res);
326       }
327
328       int col;
329       for (int i = min; i <= max; i++)
330       {
331         col = i; // av.getColumnSelection().adjustForHiddenColumns(i);
332
333         if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
334         {
335           av.getColumnSelection().removeElement(col);
336         }
337         else
338         {
339           av.getColumnSelection().addElement(col);
340         }
341       }
342
343       ap.paintAlignment(false);
344     }
345   }
346
347   @Override
348   public void mouseEntered(MouseEvent evt)
349   {
350     if (mouseDragging)
351     {
352       ap.getSeqPanel().scrollCanvas(null);
353     }
354   }
355
356   @Override
357   public void mouseExited(MouseEvent evt)
358   {
359     if (mouseDragging)
360     {
361       ap.getSeqPanel().scrollCanvas(evt);
362     }
363   }
364
365   @Override
366   public void mouseClicked(MouseEvent evt)
367   {
368   }
369
370   @Override
371   public void mouseMoved(MouseEvent evt)
372   {
373     if (!av.hasHiddenColumns())
374     {
375       return;
376     }
377
378     int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
379
380     res = av.getColumnSelection().adjustForHiddenColumns(res);
381
382     reveal = null;
383     if (av.getColumnSelection().getHiddenColumns() != null)
384     {
385       for (int[] region : av.getColumnSelection().getHiddenColumns())
386       {
387         if (res + 1 == region[0] || res - 1 == region[1])
388         {
389           reveal = region;
390           ToolTipManager.sharedInstance().registerComponent(this);
391           this.setToolTipText(MessageManager
392                   .getString("label.reveal_hidden_columns"));
393           break;
394         }
395         else
396         {
397           this.setToolTipText(null);
398         }
399       }
400     }
401     repaint();
402   }
403
404   int[] reveal;
405
406   /**
407    * DOCUMENT ME!
408    * 
409    * @param g
410    *          DOCUMENT ME!
411    */
412   @Override
413   public void paintComponent(Graphics g)
414   {
415     drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
416   }
417
418   // scalewidth will normally be screenwidth,
419   public void drawScale(Graphics g, int startx, int endx, int width,
420           int height)
421   {
422     Graphics2D gg = (Graphics2D) g;
423     gg.setFont(av.getFont());
424
425     if (av.antiAlias)
426     {
427       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
428               RenderingHints.VALUE_ANTIALIAS_ON);
429     }
430
431     // Fill in the background
432     gg.setColor(Color.white);
433     gg.fillRect(0, 0, width, height);
434     gg.setColor(Color.black);
435
436     // Fill the selected columns
437     ColumnSelection cs = av.getColumnSelection();
438     int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
439
440     int s;
441     if (cs != null)
442     {
443       gg.setColor(new Color(220, 0, 0));
444
445       for (int sel : cs.getSelected())
446       {
447         if (av.hasHiddenColumns())
448         {
449           if (cs.isVisible(sel))
450           {
451             sel = cs.findColumnPosition(sel);
452           }
453           else
454           {
455             continue;
456           }
457         }
458
459         if ((sel >= startx) && (sel <= endx))
460         {
461           gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
462                   getHeight());
463         }
464       }
465     }
466     // Draw the scale numbers
467     gg.setColor(Color.black);
468
469     int scalestartx = (startx / 10) * 10;
470
471     FontMetrics fm = gg.getFontMetrics(av.getFont());
472     int y = avCharHeight - fm.getDescent();
473
474     if ((scalestartx % 10) == 0)
475     {
476       scalestartx += 5;
477     }
478
479     String string;
480     int maxX = 0;
481
482     for (int i = scalestartx; i < endx; i += 5)
483     {
484       if ((i % 10) == 0)
485       {
486         string = String.valueOf(av.getColumnSelection()
487                 .adjustForHiddenColumns(i));
488         if ((i - startx - 1) * avCharWidth > maxX)
489         {
490           gg.drawString(string, (i - startx - 1) * avCharWidth, y);
491           maxX = (i - startx + 1) * avCharWidth + fm.stringWidth(string);
492         }
493
494         gg.drawLine(((i - startx - 1) * avCharWidth) + (avCharWidth / 2),
495                 y + 2,
496                 ((i - startx - 1) * avCharWidth) + (avCharWidth / 2), y
497                         + (fm.getDescent() * 2));
498       }
499       else
500       {
501         gg.drawLine(((i - startx - 1) * avCharWidth) + (avCharWidth / 2), y
502                 + fm.getDescent(), ((i - startx - 1) * avCharWidth)
503                 + (avCharWidth / 2), y + (fm.getDescent() * 2));
504       }
505     }
506
507     if (av.hasHiddenColumns())
508     {
509       gg.setColor(Color.blue);
510       int res;
511       if (av.getShowHiddenMarkers()
512               && av.getColumnSelection().getHiddenColumns() != null)
513       {
514         for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
515                 .size(); i++)
516         {
517
518           res = av.getColumnSelection().findHiddenRegionPosition(i)
519                   - startx;
520
521           if (res < 0 || res > endx - scalestartx)
522           {
523             continue;
524           }
525
526           gg.fillPolygon(new int[] { res * avCharWidth - avCharHeight / 4,
527               res * avCharWidth + avCharHeight / 4, res * avCharWidth },
528                   new int[] { y - avCharHeight / 2, y - avCharHeight / 2,
529                       y + 8 }, 3);
530
531         }
532       }
533
534       if (reveal != null && reveal[0] > startx && reveal[0] < endx)
535       {
536         gg.drawString(MessageManager.getString("label.reveal_columns"),
537                 reveal[0] * avCharWidth, 0);
538       }
539     }
540
541   }
542 }