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