Jskad/source/org/thdl/savant/TwoWayTextPlayer.java
dchandler 403f21c8db Added Javadoc files overview.html and several package.html files.
Added a "Quit" option to Savant's File menu.  Factored out the Close
option in doing so.

Exceptions in many action listeners are now handled by
org.thdl.util.ThdlActionListener or org.thdl.util.ThdlAbstractAction.

Many exceptions that we used to just log now optionally cause aborts.
This option is on by default for developers using 'ant savant-run'-style
targets, but it is off for users.

An erroneous CLASSPATH now causes a useful error message in almost
all situations.

Fixed some typos and bad links in Javadoc comments.

Added a simple assertion facility, but the overhead is suffered even in
release builds.

Factored out the code that sets up log files like savant.log and jskad.log.
2002-10-06 18:23:27 +00:00

127 lines
3.8 KiB
Java

/*
The contents of this file are subject to the THDL Open Community License
Version 1.0 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License on the THDL web site
(http://www.thdl.org/).
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific terms governing rights and limitations under the
License.
The Initial Developer of this software is the Tibetan and Himalayan Digital
Library (THDL). Portions created by the THDL are Copyright 2001 THDL.
All Rights Reserved.
Contributor(s): ______________________________________.
*/
package org.thdl.savant;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;
import java.util.SortedMap;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import javax.swing.text.JTextComponent;
import org.thdl.util.ThdlDebug;
public class TwoWayTextPlayer extends TextHighlightPlayer
{
protected TreeMap orderedOffsets = null;
protected SoundPanel sound = null;
protected long doubleClickDelay = 300L;
protected long lastClickTime;
protected Point lastClickPoint = null;
public TwoWayTextPlayer(SoundPanel sp, TranscriptView view, Color highlightColor)
{
super(view, highlightColor);
sound = sp;
orderedOffsets = new TreeMap();
for (Enumeration e = hashEnd.keys() ; e.hasMoreElements() ;)
{
String id = (String)e.nextElement();
orderedOffsets.put(hashEnd.get(id), id);
}
for (Enumeration e = hashStart.keys() ; e.hasMoreElements() ;)
{
String id = (String)e.nextElement();
orderedOffsets.put(hashStart.get(id), id);
}
view.getTextComponent().addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
//note - can't use e.getClickCount() here
//because every double click event is
//preceded by a single click event, so
//no easy way to distinguish the two
try
{
final long currentClickTime = e.getWhen();
final Point currentClickPoint = e.getPoint();
if (lastClickPoint != null && currentClickPoint.equals(lastClickPoint))
{ //this could be the second click of a double click
if (currentClickTime - lastClickTime < doubleClickDelay)
{ //must be a double-click: play only one line
lastClickPoint = null; //makes sure second click won't be interpreted as single click also
int pos = text.viewToModel(currentClickPoint)+1;
Integer i = new Integer(pos);
SortedMap map = orderedOffsets.headMap(i);
final String id = (String)map.get(map.lastKey());
highlighter.removeAllHighlights();
sound.cmd_playS(id);
return;
}
}
//otherwise, this click can only be single click or first click
lastClickTime = currentClickTime;
lastClickPoint = new Point(currentClickPoint);
final Timer clickTimer = new Timer(true);
clickTimer.schedule(new TimerTask()
{
public void run()
{
if (lastClickPoint != null)
{ //must be a single click: play line onwards
int pos = text.viewToModel(currentClickPoint)+1;
Integer i = new Integer(pos);
SortedMap map = orderedOffsets.headMap(i);
final String id = (String)map.get(map.lastKey());
highlighter.removeAllHighlights();
sound.cmd_playFrom(id);
}
clickTimer.cancel();
}
}, doubleClickDelay);
}
catch (NoSuchElementException nsee)
{
nsee.printStackTrace();
ThdlDebug.noteIffyCode();
}
}
});
}
public void reset()
{
sound.removeAnnotationPlayer(this);
highlighter.removeAllHighlights();
}
public JTextComponent getTextComponent()
{
return text;
}
}