Clover icon

Coverage Report

  1. Project Clover database Wed Nov 6 2024 00:56:24 GMT
  2. Package jalview.gui

File IdwidthAdjuster.java

 

Coverage histogram

../../img/srcFileCovDistChart4.png
48% of files have more coverage

Code metrics

16
49
11
1
231
126
23
0.47
4.45
11
2.09

Classes

Class Line # Actions
IdwidthAdjuster 41 49 23
0.3552631435.5%
 

Contributing tests

This file is covered by 184 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.Cursor;
25    import java.awt.Graphics;
26    import java.awt.event.MouseEvent;
27    import java.awt.event.MouseListener;
28    import java.awt.event.MouseMotionListener;
29   
30    import javax.swing.JPanel;
31   
32    import jalview.api.AlignViewportI;
33    import jalview.bin.Cache;
34   
35    /**
36    * DOCUMENT ME!
37    *
38    * @author $author$
39    * @version $Revision$
40    */
 
41    public class IdwidthAdjuster extends JPanel
42    implements MouseListener, MouseMotionListener
43    {
44    public static final int MIN_ID_WIDTH = 20;
45   
46    int oldX = 0;
47   
48    AlignmentPanel ap;
49   
50    /**
51    * Creates a new IdwidthAdjuster object.
52    *
53    * @param ap
54    * DOCUMENT ME!
55    */
 
56  478 toggle public IdwidthAdjuster(AlignmentPanel ap)
57    {
58  478 this.ap = ap;
59  478 setBackground(Color.white);
60  478 addMouseListener(this);
61  478 addMouseMotionListener(this);
62    }
63   
64    /**
65    * Action on mouse pressed is to save the start position for any drag
66    *
67    * @param evt
68    */
 
69  0 toggle @Override
70    public void mousePressed(MouseEvent evt)
71    {
72  0 oldX = evt.getX();
73    }
74   
75    /**
76    * On release of mouse drag to resize the width, if there is a complementary
77    * alignment in a split frame, sets the complement to the same id width and
78    * repaints the split frame. Note this is done whether or not the protein
79    * characters are scaled to codon width.
80    *
81    * @param evt
82    */
 
83  0 toggle @Override
84    public void mouseReleased(MouseEvent evt)
85    {
86  0 repaint();
87   
88    /*
89    * If in a SplitFrame, set the other's id width to match
90    */
91  0 final AlignViewportI viewport = ap.getAlignViewport();
92  0 if (viewport.getCodingComplement() != null)
93    {
94  0 viewport.getCodingComplement().setIdWidth(viewport.getIdWidth());
95  0 SplitFrame sf = (SplitFrame) ap.alignFrame.getSplitViewContainer();
96  0 sf.repaint();
97    }
98    }
99   
100    /**
101    * When this region is entered, repaints to show a left-right move cursor
102    *
103    * @param evt
104    */
 
105  0 toggle @Override
106    public void mouseEntered(MouseEvent evt)
107    {
108  0 repaint();
109    }
110   
 
111  0 toggle @Override
112    public void mouseExited(MouseEvent evt)
113    {
114    }
115   
116    /**
117    * Adjusts the id panel width for a mouse drag left or right (subject to a
118    * minimum of 20 pixels) and repaints the alignment
119    *
120    * @param evt
121    */
 
122  0 toggle @Override
123    public void mouseDragged(MouseEvent evt)
124    {
125  0 int mouseX = evt.getX();
126  0 final AlignViewportI viewport = ap.getAlignViewport();
127  0 int curwidth = viewport.getIdWidth();
128  0 int dif = mouseX - oldX;
129   
130  0 final int newWidth = curwidth + dif;
131   
132    /*
133    * don't drag below minimum width
134    */
135  0 if (newWidth < MIN_ID_WIDTH || newWidth > this.ap.getWidth())
136    {
137  0 return;
138    }
139   
140    /*
141    * don't allow residue width to be < 1 in wrapped format
142    */
143  0 if (viewport.getWrapAlignment())
144    {
145  0 SeqCanvas sc = ap.getSeqPanel().seqCanvas;
146  0 if (sc != null && sc.getWrappedCanvasWidth(sc.getWidth() - dif) < 1)
147    {
148  0 return;
149    }
150    }
151   
152  0 oldX = evt.getX();
153   
154    /*
155    * don't drag right if mouse is to the left of the region
156    */
157  0 if (dif > 0 && mouseX < 0)
158    {
159  0 return;
160    }
161   
162    // TODO JAL-4260 - work out how to trigger recomputation of wrapped pages !
163  0 int curCol = viewport.getRanges().getStartRes()
164    + viewport.getRanges().getViewportWidth();
165   
166  0 viewport.setIdWidth(newWidth);
167   
168  0 ap.validateAnnotationDimensions(false);
169  0 if (viewport.getWrapAlignment())
170    {
171  0 viewport.getRanges().scrollToWrappedVisible(
172    curCol - viewport.getRanges().getViewportWidth());
173    }
174  0 ap.paintAlignment(true, false);
175   
176  0 ap.getIdPanel().getIdCanvas().setManuallyAdjusted(true);
177    }
178   
 
179  113 toggle public void setWidth(int newWidth)
180    {
181  113 if (newWidth < MIN_ID_WIDTH
182    || ap.getIdPanel().getIdCanvas().isManuallyAdjusted())
183    {
184  13 return;
185    }
186  100 final AlignViewportI viewport = ap.getAlignViewport();
187  100 viewport.setIdWidth(newWidth);
188  100 ap.paintAlignment(true, false);
189    }
190   
 
191  2170 toggle public boolean manuallyAdjusted()
192    {
193  2170 return ap.getIdPanel().getIdCanvas().isManuallyAdjusted();
194    }
195   
 
196  0 toggle @Override
197    public void mouseMoved(MouseEvent evt)
198    {
199    }
200   
 
201  0 toggle @Override
202    public void mouseClicked(MouseEvent evt)
203    {
204    }
205   
206    /**
207    * Paints this region, showing a left-right move cursor if currently 'active'
208    *
209    * @param g
210    */
 
211  2415 toggle @Override
212    public void paintComponent(Graphics g)
213    {
214  2415 int width = getWidth();
215  2415 int height = getHeight();
216  2415 g.setColor(Color.white);
217  2415 g.fillRect(0, 0, width, height);
218   
219  2415 if (!Cache.getDefault(AnnotationLabels.RESIZE_MARGINS_MARK_PREF, false))
220    // && !ap.getAlignViewport().getWrapAlignment()) // now allowing adjustment
221    // in wrap mode
222    {
223  2415 int spacer = Math.max(2, AnnotationLabels.HEIGHT_ADJUSTER_HEIGHT / 4);
224  2415 g.setColor(Color.LIGHT_GRAY);
225  2415 g.drawLine(width - 3 * spacer, 0, width - 3 * spacer, height / 2);
226  2415 g.drawLine(width - spacer, 0, width - spacer, height / 2);
227    }
228   
229  2415 setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
230    }
231    }