JAL-2388 Removing AlignmentPanel dependency (mostly)
[jalview.git] / src / jalview / appletgui / OverviewPanel.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.appletgui;
22
23 import jalview.viewmodel.OverviewDimensions;
24
25 import java.awt.BorderLayout;
26 import java.awt.Dimension;
27 import java.awt.Panel;
28 import java.awt.event.ComponentAdapter;
29 import java.awt.event.ComponentEvent;
30 import java.awt.event.MouseEvent;
31 import java.awt.event.MouseListener;
32 import java.awt.event.MouseMotionListener;
33
34 public class OverviewPanel extends Panel implements Runnable,
35         MouseMotionListener, MouseListener
36 {
37   private OverviewDimensions od;
38
39   private OverviewCanvas oviewCanvas;
40
41   private AlignViewport av;
42
43   private AlignmentPanel ap;
44
45   private boolean updateRunning = false;
46
47   public OverviewPanel(AlignmentPanel alPanel)
48   {
49     this.av = alPanel.av;
50     this.ap = alPanel;
51     setLayout(null);
52
53     od = new OverviewDimensions(av.getRanges(),
54             (av.isShowAnnotation() && av.getSequenceConsensusHash() != null));
55
56     oviewCanvas = new OverviewCanvas(od, av);
57     setLayout(new BorderLayout());
58     add(oviewCanvas, BorderLayout.CENTER);
59
60     setSize(new Dimension(od.getWidth(), od.getHeight()));
61     addComponentListener(new ComponentAdapter()
62     {
63
64       @Override
65       public void componentResized(ComponentEvent evt)
66       {
67         if ((getWidth() != od.getWidth())
68                 || (getHeight() != (od.getHeight())))
69         {
70           updateOverviewImage();
71         }
72       }
73     });
74
75     addMouseMotionListener(this);
76
77     addMouseListener(this);
78
79     updateOverviewImage();
80
81   }
82
83   @Override
84   public void mouseEntered(MouseEvent evt)
85   {
86   }
87
88   @Override
89   public void mouseExited(MouseEvent evt)
90   {
91   }
92
93   @Override
94   public void mouseClicked(MouseEvent evt)
95   {
96   }
97
98   @Override
99   public void mouseMoved(MouseEvent evt)
100   {
101   }
102
103   @Override
104   public void mousePressed(MouseEvent evt)
105   {
106     mouseAction(evt);
107   }
108
109   @Override
110   public void mouseReleased(MouseEvent evt)
111   {
112     mouseAction(evt);
113   }
114
115   @Override
116   public void mouseDragged(MouseEvent evt)
117   {
118     mouseAction(evt);
119   }
120
121   private void mouseAction(MouseEvent evt)
122   {
123     od.updateViewportFromMouse(evt.getX(), evt.getY(), av.getAlignment()
124             .getHiddenSequences(), av.getColumnSelection(), av
125             .getRanges());
126     ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
127     ap.paintAlignment(false);
128   }
129
130   /**
131    * Updates the overview image when the related alignment panel is updated
132    */
133   public void updateOverviewImage()
134   {
135     if ((getSize().width > 0) && (getSize().height > 0))
136     {
137       od.setWidth(getSize().width);
138       od.setHeight(getSize().height);
139     }
140     setSize(new Dimension(od.getWidth(), od.getHeight()));
141
142     if (updateRunning)
143     {
144       oviewCanvas.restartDraw();
145       return;
146     }
147
148     updateRunning = true;
149     Thread thread = new Thread(this);
150     thread.start();
151     repaint();
152     updateRunning = false;
153   }
154
155   @Override
156   public void run()
157   {
158     oviewCanvas.draw(av.isShowSequenceFeatures(),
159             (av.isShowAnnotation() && av
160                     .getAlignmentConservationAnnotation() != null), ap);
161     setBoxPosition();
162   }
163
164   /**
165    * Update the overview panel box when the associated alignment panel is
166    * changed
167    * 
168    */
169   public void setBoxPosition()
170   {
171     od.setBoxPosition(av.getAlignment()
172             .getHiddenSequences(), av.getColumnSelection(), av.getRanges());
173     repaint();
174   }
175 }