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