0ebdfa180685c5c5a0e37bcc443a13a8dd057614
[jalview.git] / src / jalview / gui / IdwidthAdjuster.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 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
34 /**
35  * DOCUMENT ME!
36  * 
37  * @author $author$
38  * @version $Revision$
39  */
40 public class IdwidthAdjuster extends JPanel
41         implements MouseListener, MouseMotionListener
42 {
43   public static final int MIN_ID_WIDTH = 20;
44
45   int oldX = 0;
46
47   AlignmentPanel ap;
48
49   /**
50    * Creates a new IdwidthAdjuster object.
51    * 
52    * @param ap
53    *          DOCUMENT ME!
54    */
55   public IdwidthAdjuster(AlignmentPanel ap)
56   {
57     this.ap = ap;
58     setBackground(Color.white);
59     addMouseListener(this);
60     addMouseMotionListener(this);
61   }
62
63   /**
64    * Action on mouse pressed is to save the start position for any drag
65    * 
66    * @param evt
67    */
68   @Override
69   public void mousePressed(MouseEvent evt)
70   {
71     oldX = evt.getX();
72   }
73
74   /**
75    * On release of mouse drag to resize the width, if there is a complementary
76    * alignment in a split frame, sets the complement to the same id width and
77    * repaints the split frame. Note this is done whether or not the protein
78    * characters are scaled to codon width.
79    * 
80    * @param evt
81    */
82   @Override
83   public void mouseReleased(MouseEvent evt)
84   {
85     repaint();
86
87     /*
88      * If in a SplitFrame, set the other's id width to match
89      */
90     final AlignViewportI viewport = ap.getAlignViewport();
91     if (viewport.getCodingComplement() != null)
92     {
93       viewport.getCodingComplement().setIdWidth(viewport.getIdWidth());
94       SplitFrame sf = (SplitFrame) ap.alignFrame.getSplitViewContainer();
95       sf.repaint();
96     }
97   }
98
99   /**
100    * When this region is entered, repaints to show a left-right move cursor
101    * 
102    * @param evt
103    */
104   @Override
105   public void mouseEntered(MouseEvent evt)
106   {
107     repaint();
108   }
109
110   @Override
111   public void mouseExited(MouseEvent evt)
112   {
113   }
114
115   /**
116    * Adjusts the id panel width for a mouse drag left or right (subject to a
117    * minimum of 20 pixels) and repaints the alignment
118    * 
119    * @param evt
120    */
121   @Override
122   public void mouseDragged(MouseEvent evt)
123   {
124     int mouseX = evt.getX();
125     final AlignViewportI viewport = ap.getAlignViewport();
126     int curwidth = viewport.getIdWidth();
127     int dif = mouseX - oldX;
128
129     final int newWidth = curwidth + dif;
130
131     /*
132      * don't drag below minimum width
133      */
134     if (newWidth < MIN_ID_WIDTH)
135     {
136       return;
137     }
138
139     oldX = evt.getX();
140
141     /*
142      * don't drag right if mouse is to the left of the region
143      */
144     if (dif > 0 && mouseX < 0)
145     {
146       return;
147     }
148     viewport.setIdWidth(newWidth);
149     ap.validateAnnotationDimensions(false);
150     ap.paintAlignment(true, false);
151
152     ap.getIdPanel().getIdCanvas().setManuallyAdjusted(true);
153   }
154
155   public void setWidth(int newWidth)
156   {
157     if (newWidth < MIN_ID_WIDTH)
158     {
159       return;
160     }
161     final AlignViewportI viewport = ap.getAlignViewport();
162     viewport.setIdWidth(newWidth);
163     ap.paintAlignment(true, false);
164   }
165
166   public boolean manuallyAdjusted()
167   {
168     return ap.getIdPanel().getIdCanvas().manuallyAdjusted();
169   }
170
171   @Override
172   public void mouseMoved(MouseEvent evt)
173   {
174   }
175
176   @Override
177   public void mouseClicked(MouseEvent evt)
178   {
179   }
180
181   /**
182    * Paints this region, showing a left-right move cursor if currently 'active'
183    * 
184    * @param g
185    */
186   @Override
187   public void paintComponent(Graphics g)
188   {
189     g.setColor(Color.white);
190     g.fillRect(0, 0, getWidth(), getHeight());
191     setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
192   }
193 }