JAL-147 don't draw ScalePanel when in wrapped mode
[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.HiddenColumns;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.renderer.ScaleRenderer;
28 import jalview.renderer.ScaleRenderer.ScaleMark;
29 import jalview.util.MessageManager;
30 import jalview.util.Platform;
31 import jalview.viewmodel.ViewportListenerI;
32 import jalview.viewmodel.ViewportRanges;
33
34 import java.awt.Color;
35 import java.awt.FontMetrics;
36 import java.awt.Graphics;
37 import java.awt.Graphics2D;
38 import java.awt.RenderingHints;
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41 import java.awt.event.MouseEvent;
42 import java.awt.event.MouseListener;
43 import java.awt.event.MouseMotionListener;
44 import java.beans.PropertyChangeEvent;
45 import java.util.List;
46
47 import javax.swing.JMenuItem;
48 import javax.swing.JPanel;
49 import javax.swing.JPopupMenu;
50 import javax.swing.SwingUtilities;
51 import javax.swing.ToolTipManager;
52
53 /**
54  * The panel containing the sequence ruler (when not in wrapped mode), and
55  * supports a range of mouse operations to select, hide or reveal columns.
56  */
57 public class ScalePanel extends JPanel implements MouseMotionListener,
58         MouseListener, ViewportListenerI
59 {
60   protected int offy = 4;
61
62   public int width;
63
64   protected AlignViewport av;
65
66   AlignmentPanel ap;
67
68   boolean stretchingGroup = false;
69
70   /*
71    * min, max hold the extent of a mouse drag action
72    */
73   int min;
74
75   int max;
76
77   boolean mouseDragging = false;
78
79   /*
80    * holds a hidden column range when the mouse is over an adjacent column
81    */
82   int[] reveal;
83
84   /**
85    * Constructor
86    * 
87    * @param av
88    * @param ap
89    */
90   public ScalePanel(AlignViewport av, AlignmentPanel ap)
91   {
92     this.av = av;
93     this.ap = ap;
94
95     addMouseListener(this);
96     addMouseMotionListener(this);
97
98     av.getRanges().addPropertyChangeListener(this);
99   }
100
101   /**
102    * DOCUMENT ME!
103    * 
104    * @param evt
105    *          DOCUMENT ME!
106    */
107   @Override
108   public void mousePressed(MouseEvent evt)
109   {
110     int x = (evt.getX() / av.getCharWidth()) + av.getRanges().getStartRes();
111     final int res;
112
113     if (av.hasHiddenColumns())
114     {
115       x = av.getAlignment().getHiddenColumns().adjustForHiddenColumns(x);
116     }
117
118     if (x >= av.getAlignment().getWidth())
119     {
120       res = av.getAlignment().getWidth() - 1;
121     }
122     else
123     {
124       res = x;
125     }
126
127     min = res;
128     max = res;
129
130     if (evt.isPopupTrigger()) // Mac: mousePressed
131     {
132       rightMouseButtonPressed(evt, res);
133     }
134     else if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
135     {
136       /*
137        * defer right-mouse click handling to mouse up on Windows
138        * (where isPopupTrigger() will answer true)
139        * but accept Cmd-click on Mac which passes isRightMouseButton
140        */
141       return;
142     }
143     else
144     {
145       leftMouseButtonPressed(evt, res);
146     }
147   }
148
149   /**
150    * Handles right mouse button press. If pressed in a selected column, opens
151    * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
152    * opens context menu for 'Reveal / Reveal All'. Else does nothing.
153    * 
154    * @param evt
155    * @param res
156    */
157   protected void rightMouseButtonPressed(MouseEvent evt, final int res)
158   {
159     JPopupMenu pop = new JPopupMenu();
160     if (reveal != null)
161     {
162       JMenuItem item = new JMenuItem(
163               MessageManager.getString("label.reveal"));
164       item.addActionListener(new ActionListener()
165       {
166         @Override
167         public void actionPerformed(ActionEvent e)
168         {
169           av.showColumn(reveal[0]);
170           reveal = null;
171           ap.paintAlignment(true);
172           if (ap.overviewPanel != null)
173           {
174             ap.overviewPanel.updateOverviewImage();
175           }
176           av.sendSelection();
177         }
178       });
179       pop.add(item);
180
181       if (av.getAlignment().getHiddenColumns().hasHiddenColumns())
182       {
183         item = new JMenuItem(MessageManager.getString("action.reveal_all"));
184         item.addActionListener(new ActionListener()
185         {
186           @Override
187           public void actionPerformed(ActionEvent e)
188           {
189             av.showAllHiddenColumns();
190             reveal = null;
191             ap.paintAlignment(true);
192             if (ap.overviewPanel != null)
193             {
194               ap.overviewPanel.updateOverviewImage();
195             }
196             av.sendSelection();
197           }
198         });
199         pop.add(item);
200       }
201       pop.show(this, evt.getX(), evt.getY());
202     }
203     else if (av.getColumnSelection().contains(res))
204     {
205       JMenuItem item = new JMenuItem(
206               MessageManager.getString("label.hide_columns"));
207       item.addActionListener(new ActionListener()
208       {
209         @Override
210         public void actionPerformed(ActionEvent e)
211         {
212           av.hideColumns(res, res);
213           if (av.getSelectionGroup() != null
214                   && av.getSelectionGroup().getSize() == av.getAlignment()
215                           .getHeight())
216           {
217             av.setSelectionGroup(null);
218           }
219
220           ap.paintAlignment(true);
221           if (ap.overviewPanel != null)
222           {
223             ap.overviewPanel.updateOverviewImage();
224           }
225           av.sendSelection();
226         }
227       });
228       pop.add(item);
229       pop.show(this, evt.getX(), evt.getY());
230     }
231   }
232
233   /**
234    * Handles left mouse button press
235    * 
236    * @param evt
237    * @param res
238    */
239   protected void leftMouseButtonPressed(MouseEvent evt, final int res)
240   {
241     /*
242      * Ctrl-click/Cmd-click adds to the selection
243      * Shift-click extends the selection
244      */
245     // TODO Problem: right-click on Windows not reported until mouseReleased?!?
246     if (!Platform.isControlDown(evt) && !evt.isShiftDown())
247     {
248       av.getColumnSelection().clear();
249     }
250
251     av.getColumnSelection().addElement(res);
252     SequenceGroup sg = new SequenceGroup();
253     // try to be as quick as possible
254     SequenceI[] iVec = av.getAlignment().getSequencesArray();
255     for (int i = 0; i < iVec.length; i++)
256     {
257       sg.addSequence(iVec[i], false);
258       iVec[i] = null;
259     }
260     iVec = null;
261     sg.setStartRes(res);
262     sg.setEndRes(res);
263
264     if (evt.isShiftDown())
265     {
266       int min = Math.min(av.getColumnSelection().getMin(), res);
267       int max = Math.max(av.getColumnSelection().getMax(), res);
268       for (int i = min; i < max; i++)
269       {
270         av.getColumnSelection().addElement(i);
271       }
272       sg.setStartRes(min);
273       sg.setEndRes(max);
274     }
275     av.setSelectionGroup(sg);
276     ap.paintAlignment(false);
277     av.sendSelection();
278   }
279
280   /**
281    * DOCUMENT ME!
282    * 
283    * @param evt
284    *          DOCUMENT ME!
285    */
286   @Override
287   public void mouseReleased(MouseEvent evt)
288   {
289     mouseDragging = false;
290
291     int res = (evt.getX() / av.getCharWidth())
292             + av.getRanges().getStartRes();
293
294     if (av.hasHiddenColumns())
295     {
296       res = av.getAlignment().getHiddenColumns()
297               .adjustForHiddenColumns(res);
298     }
299
300     if (res >= av.getAlignment().getWidth())
301     {
302       res = av.getAlignment().getWidth() - 1;
303     }
304
305     if (!stretchingGroup)
306     {
307       if (evt.isPopupTrigger()) // Windows: mouseReleased
308       {
309         rightMouseButtonPressed(evt, res);
310       }
311       else
312       {
313         ap.paintAlignment(false);
314       }
315       return;
316     }
317
318     SequenceGroup sg = av.getSelectionGroup();
319
320     if (sg != null)
321     {
322       if (res > sg.getStartRes())
323       {
324         sg.setEndRes(res);
325       }
326       else if (res < sg.getStartRes())
327       {
328         sg.setStartRes(res);
329       }
330     }
331     stretchingGroup = false;
332     ap.paintAlignment(false);
333     av.sendSelection();
334   }
335
336   /**
337    * Action on dragging the mouse in the scale panel is to expand or shrink the
338    * selection group range (including any hidden columns that it spans)
339    * 
340    * @param evt
341    */
342   @Override
343   public void mouseDragged(MouseEvent evt)
344   {
345     mouseDragging = true;
346     ColumnSelection cs = av.getColumnSelection();
347     HiddenColumns hidden = av.getAlignment().getHiddenColumns();
348
349     int res = (evt.getX() / av.getCharWidth())
350             + av.getRanges().getStartRes();
351     res = Math.max(0, res);
352     res = hidden.adjustForHiddenColumns(res);
353     res = Math.min(res, av.getAlignment().getWidth() - 1);
354     min = Math.min(res, min);
355     max = Math.max(res, max);
356
357     SequenceGroup sg = av.getSelectionGroup();
358     if (sg != null)
359     {
360       stretchingGroup = true;
361       cs.stretchGroup(res, sg, min, max);
362       ap.paintAlignment(false);
363     }
364   }
365
366   @Override
367   public void mouseEntered(MouseEvent evt)
368   {
369     if (mouseDragging)
370     {
371       ap.getSeqPanel().scrollCanvas(null);
372     }
373   }
374
375   @Override
376   public void mouseExited(MouseEvent evt)
377   {
378     if (mouseDragging)
379     {
380       ap.getSeqPanel().scrollCanvas(evt);
381     }
382   }
383
384   @Override
385   public void mouseClicked(MouseEvent evt)
386   {
387   }
388
389   /**
390    * Creates a tooltip when the mouse is over a hidden columns marker
391    */
392   @Override
393   public void mouseMoved(MouseEvent evt)
394   {
395     this.setToolTipText(null);
396     reveal = null;
397     if (!av.hasHiddenColumns())
398     {
399       return;
400     }
401
402     int res = (evt.getX() / av.getCharWidth())
403             + av.getRanges().getStartRes();
404
405     reveal = av.getAlignment().getHiddenColumns()
406             .getRegionWithEdgeAtRes(res);
407
408     res = av.getAlignment().getHiddenColumns().adjustForHiddenColumns(res);
409
410     ToolTipManager.sharedInstance().registerComponent(this);
411     this.setToolTipText(
412             MessageManager.getString("label.reveal_hidden_columns"));
413     repaint();
414   }
415
416   /**
417    * DOCUMENT ME!
418    * 
419    * @param g
420    *          DOCUMENT ME!
421    */
422   @Override
423   public void paintComponent(Graphics g)
424   {
425     /*
426      * shouldn't get called in wrapped mode as the scale above is
427      * drawn instead by SeqCanvas.drawNorthScale
428      */
429     if (!av.getWrapAlignment())
430     {
431       drawScale(g, av.getRanges().getStartRes(),
432               av.getRanges().getEndRes(), getWidth(), getHeight());
433     }
434   }
435
436   // scalewidth will normally be screenwidth,
437   public void drawScale(Graphics g, int startx, int endx, int width,
438           int height)
439   {
440     Graphics2D gg = (Graphics2D) g;
441     gg.setFont(av.getFont());
442
443     if (av.antiAlias)
444     {
445       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
446               RenderingHints.VALUE_ANTIALIAS_ON);
447     }
448
449     // Fill in the background
450     gg.setColor(Color.white);
451     gg.fillRect(0, 0, width, height);
452     gg.setColor(Color.black);
453
454     // Fill the selected columns
455     ColumnSelection cs = av.getColumnSelection();
456     HiddenColumns hidden = av.getAlignment().getHiddenColumns();
457     int avCharWidth = av.getCharWidth();
458     int avCharHeight = av.getCharHeight();
459
460     if (cs != null)
461     {
462       gg.setColor(new Color(220, 0, 0));
463
464       for (int sel : cs.getSelected())
465       {
466         // TODO: JAL-2001 - provide a fast method to list visible selected in a
467         // given range
468
469         if (av.hasHiddenColumns())
470         {
471           if (hidden.isVisible(sel))
472           {
473             sel = hidden.findColumnPosition(sel);
474           }
475           else
476           {
477             continue;
478           }
479         }
480
481         if ((sel >= startx) && (sel <= endx))
482         {
483           gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
484                   getHeight());
485         }
486       }
487     }
488
489     int widthx = 1 + endx - startx;
490
491     FontMetrics fm = gg.getFontMetrics(av.getFont());
492     int y = avCharHeight;
493     int yOf = fm.getDescent();
494     y -= yOf;
495     if (av.hasHiddenColumns())
496     {
497       // draw any hidden column markers
498       gg.setColor(Color.blue);
499       int res;
500
501       if (av.getShowHiddenMarkers())
502       {
503         List<Integer> positions = hidden.findHiddenRegionPositions();
504         for (int pos : positions)
505         {
506           res = pos - startx;
507
508           if (res < 0 || res > widthx)
509           {
510             continue;
511           }
512
513           gg.fillPolygon(new int[] {
514               -1 + res * avCharWidth - avCharHeight / 4,
515               -1 + res * avCharWidth + avCharHeight / 4,
516               -1 + res * avCharWidth }, new int[] { y, y, y + 2 * yOf }, 3);
517         }
518       }
519     }
520     // Draw the scale numbers
521     gg.setColor(Color.black);
522
523     int maxX = 0;
524     List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
525             endx);
526
527     for (ScaleMark mark : marks)
528     {
529       boolean major = mark.major;
530       int mpos = mark.column; // (i - startx - 1)
531       String mstring = mark.text;
532       if (mstring != null)
533       {
534         if (mpos * avCharWidth > maxX)
535         {
536           gg.drawString(mstring, mpos * avCharWidth, y);
537           maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
538         }
539       }
540       if (major)
541       {
542         gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
543                 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
544       }
545       else
546       {
547         gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
548                 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
549       }
550     }
551   }
552
553   @Override
554   public void propertyChange(PropertyChangeEvent evt)
555   {
556     // Respond to viewport change events (e.g. alignment panel was scrolled)
557     // Both scrolling and resizing change viewport ranges: scrolling changes
558     // both start and end points, but resize only changes end values.
559     // Here we only want to fastpaint on a scroll, with resize using a normal
560     // paint, so scroll events are identified as changes to the horizontal or
561     // vertical start value.
562     if (evt.getPropertyName().equals(ViewportRanges.STARTRES))
563     {
564       // scroll event, repaint panel
565       repaint();
566     }
567   }
568
569 }