JAL-2388 Working hidden regions hide/show in desktop
[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.OverviewDimensionsWithHidden;
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 OverviewDimensionsWithHidden 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 OverviewDimensionsWithHidden(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.getAlignment().getHiddenColumns());
125     ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
126     ap.paintAlignment(false);
127   }
128
129   /**
130    * Updates the overview image when the related alignment panel is updated
131    */
132   public void updateOverviewImage()
133   {
134     if ((getSize().width > 0) && (getSize().height > 0))
135     {
136       od.setWidth(getSize().width);
137       od.setHeight(getSize().height);
138     }
139     setSize(new Dimension(od.getWidth(), od.getHeight()));
140
141     synchronized (this)
142     {
143       if (updateRunning)
144       {
145         oviewCanvas.restartDraw();
146         return;
147       }
148
149       updateRunning = true;
150     }
151     Thread thread = new Thread(this);
152     thread.start();
153     repaint();
154     updateRunning = false;
155   }
156
157   @Override
158   public void run()
159   {
160     oviewCanvas.draw(av.isShowSequenceFeatures(),
161             (av.isShowAnnotation() && av
162                     .getAlignmentConservationAnnotation() != null), ap);
163     setBoxPosition();
164   }
165
166   /**
167    * Update the overview panel box when the associated alignment panel is
168    * changed
169    * 
170    */
171   public void setBoxPosition()
172   {
173     od.setBoxPosition(av.getAlignment()
174 .getHiddenSequences(), av
175             .getAlignment().getHiddenColumns());
176     repaint();
177   }
178 }