Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 16:11:35 GMT
  2. Package jalview.gui

File ScalePanel.java

 

Coverage histogram

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

Code metrics

68
168
19
1
582
390
58
0.35
8.84
19
3.05

Classes

Class Line # Actions
ScalePanel 57 168 58
0.7333333573.3%
 

Contributing tests

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