Clover icon

Coverage Report

  1. Project Clover database Wed Jan 7 2026 02:49:01 GMT
  2. Package jalview.gui

File ScalePanel.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
20% of files have more coverage

Code metrics

72
175
19
1
606
412
60
0.34
9.21
19
3.16

Classes

Class Line # Actions
ScalePanel 65 175 60
0.721804572.2%
 

Contributing tests

This file is covered by 208 tests. .

Source view

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