JAL-2388 Minor changes to thread flags
[jalview.git] / src / jalview / gui / 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.gui;
22
23 import jalview.viewmodel.OverviewDimensions;
24
25 import java.awt.BorderLayout;
26 import java.awt.Dimension;
27 import java.awt.event.ComponentAdapter;
28 import java.awt.event.ComponentEvent;
29 import java.awt.event.MouseAdapter;
30 import java.awt.event.MouseEvent;
31 import java.awt.event.MouseMotionAdapter;
32
33 import javax.swing.JPanel;
34
35 /**
36  * Panel displaying an overview of the full alignment, with an interactive box
37  * representing the viewport onto the alignment.
38  * 
39  * @author $author$
40  * @version $Revision$
41  */
42 public class OverviewPanel extends JPanel implements Runnable
43 {
44   private OverviewDimensions od;
45
46   private OverviewCanvas oviewCanvas;
47
48   private AlignViewport av;
49
50   private AlignmentPanel ap;
51
52   /**
53    * Creates a new OverviewPanel object.
54    * 
55    * @param alPanel
56    *          The alignment panel which is shown in the overview panel
57    */
58   public OverviewPanel(AlignmentPanel alPanel)
59   {
60     this.av = alPanel.av;
61     this.ap = alPanel;
62
63     od = new OverviewDimensions(av.getRanges(), av.isShowAnnotation());
64
65     oviewCanvas = new OverviewCanvas(od, av, ap, this);
66     setLayout(new BorderLayout());
67     add(oviewCanvas, BorderLayout.CENTER);
68
69     addComponentListener(new ComponentAdapter()
70     {
71       @Override
72       public void componentResized(ComponentEvent evt)
73       {
74         if ((getWidth() != od.getWidth())
75                 || (getHeight() != (od.getHeight())))
76         {
77           updateOverviewImage();
78         }
79       }
80     });
81
82     addMouseMotionListener(new MouseMotionAdapter()
83     {
84       @Override
85       public void mouseDragged(MouseEvent evt)
86       {
87         if (!av.getWrapAlignment())
88         {
89           od.updateViewportFromMouse(evt.getX(), evt.getY(), av
90                   .getAlignment().getHiddenSequences(), av
91                   .getColumnSelection(), av.getRanges());
92           ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
93         }
94       }
95     });
96
97     addMouseListener(new MouseAdapter()
98     {
99       @Override
100       public void mousePressed(MouseEvent evt)
101       {
102         if (!av.getWrapAlignment())
103         {
104           od.updateViewportFromMouse(evt.getX(), evt.getY(), av
105                   .getAlignment().getHiddenSequences(), av
106                   .getColumnSelection(), av.getRanges());
107           ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
108         }
109       }
110     });
111
112     updateOverviewImage();
113   }
114
115   /**
116    * Updates the overview image when the related alignment panel is updated
117    */
118   public void updateOverviewImage()
119   {
120     if (oviewCanvas.updating)
121     {
122       oviewCanvas.updateAgain = true;
123       return;
124     }
125
126     oviewCanvas.updating = true;
127
128     if ((getWidth() > 0) && (getHeight() > 0))
129     {
130       od.setWidth(getWidth());
131       od.setHeight(getHeight());
132     }
133
134     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
135
136     Thread thread = new Thread(this);
137     thread.start();
138     repaint();
139   }
140
141   @Override
142   public void run()
143   {
144     oviewCanvas.draw(av.isShowSequenceFeatures(), av.isShowAnnotation());
145     setBoxPosition();
146   }
147
148   /**
149    * Update the overview panel box when the associated alignment panel is
150    * changed
151    * 
152    */
153   public void setBoxPosition()
154   {
155     od.setBoxPosition(av.getAlignment()
156             .getHiddenSequences(), av.getColumnSelection(), av.getRanges());
157     repaint();
158   }
159 }