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