001/*
002 *                  BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on Nov 29, 2005
021 *
022 */
023package org.biojava.nbio.structure.gui.util;
024
025//import java.awt.Cursor;
026
027import org.biojava.nbio.structure.Chain;
028import org.biojava.nbio.structure.gui.SequenceDisplay;
029import org.biojava.nbio.structure.gui.events.AlignmentPositionListener;
030
031import java.awt.event.MouseEvent;
032import java.awt.event.MouseListener;
033import java.awt.event.MouseMotionListener;
034import java.util.ArrayList;
035import java.util.List;
036//import org.slf4j.Logger;
037//import org.slf4j.LoggerFactory;
038
039
040/** a mouse listener for the AbstractChainRenderer class
041 * it listens to all mouse events and triggeres appropriate
042 * SequenceListener and FeatureListener events
043 *
044 * @author Andreas Prlic
045 *
046 */
047public class SequenceMouseListener implements
048MouseListener,
049MouseMotionListener
050{
051
052
053        boolean selectionLocked;
054        boolean dragging;
055
056        int selectionStart;
057        int selectionEnd;
058        int draggingStart;
059        int oldSelectionStart;
060
061        int chainLength;
062
063
064        CoordManager coordManager;
065
066        //private static final Logger logger = LoggerFactory.getLogger(SequenceMouseListener.class);
067
068        SequenceDisplay parent;
069
070        List<AlignmentPositionListener> alignmentPositionListeners;
071
072        public SequenceMouseListener(SequenceDisplay parent) {
073                super();
074
075                this.parent = parent;
076
077                selectionLocked = false;
078                dragging = false;
079
080                selectionStart = -1 ;
081                selectionEnd = -1;
082                oldSelectionStart = -1;
083                draggingStart = -1;
084                chainLength = 0;
085                //clearSequenceListeners();
086
087                coordManager = new CoordManager();
088
089                alignmentPositionListeners = new ArrayList<AlignmentPositionListener>();
090                //renderer.getLayeredPane().addMouseListener(popupFrame);
091
092        }
093
094        public void clearListeners(){
095
096                alignmentPositionListeners.clear();
097        }
098        public void addAlignmentPositionListener(AlignmentPositionListener li){
099                alignmentPositionListeners.add(li);
100        }
101
102        @Override
103        public void mousePressed(MouseEvent event) {
104
105                int pos  = getSeqPos(event);
106
107                draggingStart=pos;
108                selectionStart = pos ;
109                //selectionEnd   = pos ;
110                //triggerClearSelection();
111                // triggerSelectionLocked(false);
112                triggerMouseOverPosition(pos,event.getY());
113
114
115
116
117        }
118
119        // mouse motion part
120
121        /** get the sequence position of the current mouse event
122         *
123         */
124        private int getSeqPos(MouseEvent e) {
125
126                int x = e.getX();
127                //int y = e.getY();
128                //float scale = seqScale.getScale();
129                //int DEFAULT_X_START = SequenceScalePanel.DEFAULT_X_START;
130
131                float scale = parent.getScale();
132
133                coordManager.setScale(scale);
134                int seqpos = coordManager.getSeqPos(x-2);
135
136
137                return seqpos  ;
138        }
139
140
141
142        public void setChain(Chain c){
143
144                chainLength = c.getAtomLength();
145                coordManager.setLength(chainLength);
146        }
147
148        private void setSelectionStart(int start){
149                if ( start < 0 )
150                        start = 0;
151                if ( start > chainLength)
152                        start = chainLength;
153                selectionStart = start;
154        }
155
156
157        private void setSelectionEnd(int end){
158                //if ( end < 0 )
159                        //end = 0;
160                if ( end > chainLength)
161                        end = chainLength;
162                selectionEnd = end;
163        }
164
165
166        @Override
167        public void mouseDragged(MouseEvent e) {
168                dragging = true;
169
170                int pos = getSeqPos(e) ;
171
172
173                if (( pos < 0)|| ( pos> chainLength)){
174                        return;
175                }
176
177                if (pos == oldSelectionStart){
178                        return;
179                }
180                oldSelectionStart = pos;
181
182                if ( pos > draggingStart){
183                        selectionStart = draggingStart;
184                        selectionEnd = pos ;
185                } else {
186                        selectionStart = pos;
187                        selectionEnd = draggingStart;
188                }
189                //triggerNewSequenceRange(selectionStart,selectionEnd);
190
191        }
192
193
194
195
196        @Override
197        public void mouseMoved(MouseEvent e) {
198
199
200                if ( selectionLocked )
201                        return;
202
203
204
205                int pos = getSeqPos(e) ;
206                //System.out.println("mouse moved " + pos);
207
208
209
210                oldSelectionStart = pos;
211
212
213                this.setSelectionStart(pos);
214                this.setSelectionEnd(pos);
215
216                triggerMouseOverPosition(pos,e.getY());
217
218
219
220        }
221
222
223
224        @Override
225        public void mouseClicked(MouseEvent arg0) {
226
227
228        }
229
230        @Override
231        public void mouseEntered(MouseEvent arg0) {
232
233
234        }
235
236        @Override
237        public void mouseExited(MouseEvent arg0) {
238
239        }
240
241
242        @Override
243        public void mouseReleased(MouseEvent event) {
244                // logger.info("mouse released");
245
246
247
248                draggingStart = -1;
249
250
251                if ( dragging) {
252                        if  ( ! selectionLocked) {
253                                // triggerSelectionLocked(true);
254                        }
255                } else {}
256
257                dragging = false ;
258        }
259
260
261
262        protected void triggerMouseOverPosition(int pos, int mouseY){
263                if ( selectionLocked )
264                        return;
265                List<AlignedPosition> apos = parent.getAligMap();
266                if (pos > apos.size()-1)
267                        return;
268                //System.out.println(parent.getAligMap().get(pos));
269
270                for (AlignmentPositionListener li : alignmentPositionListeners) {
271                        li.mouseOverPosition(apos.get(pos));
272                }
273
274
275
276        }
277
278}
279
280
281
282