Skip to content
Snippets Groups Projects
Commit 956742e7 authored by tjc's avatar tjc
Browse files

set hand cursor over hyperlinks

git-svn-id: svn+ssh://svn.internal.sanger.ac.uk/repos/svn/pathsoft/artemis/trunk@6386 ee4ac58c-ac51-4696-9907-e4b3aa274f04
parent 920c3adc
Branches
Tags
No related merge requests found
......@@ -20,7 +20,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/components/QualifierTextArea.java,v 1.5 2007-09-13 10:15:28 tjc Exp $
* $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/components/QualifierTextArea.java,v 1.6 2007-09-14 10:13:49 tjc Exp $
*/
package uk.ac.sanger.artemis.components;
......@@ -35,11 +35,13 @@ import uk.ac.sanger.artemis.io.EmblStreamFeature;
import uk.ac.sanger.artemis.util.StringVector;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import java.io.StringReader;
......@@ -55,9 +57,13 @@ import javax.swing.text.StyledDocument;
* (rather than using HyperlinkListener) so it can remain editable.
**/
public class QualifierTextArea extends JTextPane
implements MouseMotionListener
{
private static String[] DATABASES =
{ "SWALL", "EMBL", "UniProt", "PMID", "PubMed", "InterPro" };
private static Cursor cbusy = new Cursor(Cursor.WAIT_CURSOR);
private static Cursor cdone = new Cursor(Cursor.DEFAULT_CURSOR);
private static Cursor chand = new Cursor(Cursor.HAND_CURSOR);
/**
* Create a new QualifierTextArea containing no text.
......@@ -85,10 +91,12 @@ public class QualifierTextArea extends JTextPane
{
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount() == 2)
handleMouseDoubleClick(e);
if(e.getClickCount() == 1)
handleMouseSingleClick(e);
}
});
super.addMouseMotionListener(this);
}
......@@ -258,66 +266,103 @@ public class QualifierTextArea extends JTextPane
* Process double click event.
* @param e
*/
private void handleMouseDoubleClick(final MouseEvent e)
{
Point pt = new Point(e.getX(), e.getY());
int pos = viewToModel(pt);
int start;
if(pos < 15)
start = 0;
else
start = pos - 15;
int length = 30;
if( (start+30) > getStyledDocument().getLength() )
length = getStyledDocument().getLength()-start;
try
private void handleMouseSingleClick(final MouseEvent e)
{
String textAtPosition = getStyledDocument().getText(start, length);
int indEnd = getEndOfLink(textAtPosition, (pos-start));
textAtPosition = textAtPosition.substring(0, indEnd);
int intStart = getStartOfLink(textAtPosition);
if(intStart < 0)
final String hyperlinkText = getHyperlinkTextAtMouseEvent(e);
if(hyperlinkText == null)
return;
textAtPosition = textAtPosition.substring(intStart);
String cmd;
if(textAtPosition.indexOf("PMID") > -1 ||
textAtPosition.indexOf("PubMed") > -1)
final String cmd;
if(hyperlinkText.indexOf("PMID") > -1 ||
hyperlinkText.indexOf("PubMed") > -1)
{
String id[] = textAtPosition.split(":");
String id[] = hyperlinkText.split(":");
if(id.length < 2)
return;
cmd = getPubMedSite()+id[1];
}
else if(textAtPosition.indexOf("InterPro") > -1)
else if(hyperlinkText.indexOf("InterPro") > -1)
{
String id[] = textAtPosition.split(":");
String id[] = hyperlinkText.split(":");
if(id.length < 2)
return;
cmd = getInterProSite()+id[1];
}
else
{
cmd = DataCollectionPane.getSrsSite()+"/wgetz?-e+["+textAtPosition+"]";
String cmd2 = DataCollectionPane.getSrsSite()+"/wgetz?-e+["+hyperlinkText+"]";
int ind = cmd2.indexOf("UniProt:");
// link to uniprot accession
if( (intStart = cmd.indexOf("UniProt:")) > -1)
cmd = cmd.substring(0,intStart+7)+"-acc:"+
cmd.substring(intStart+8);
if(cmd.indexOf("ebi.ac.uk") > -1)
cmd = cmd + "+-vn+2";
if(ind > -1)
cmd2 = cmd2.substring(0,ind+7)+"-acc:"+
cmd2.substring(ind+8);
if(cmd2.indexOf("ebi.ac.uk") > -1)
cmd2 = cmd2 + "+-vn+2";
cmd = cmd2;
}
SwingWorker browserLaunch = new SwingWorker()
{
public Object construct()
{
setCursor(cbusy);
BrowserControl.displayURL(cmd);
setCursor(cdone);
return null;
}
};
browserLaunch.start();
}
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e)
{
String hyperlinkText = getHyperlinkTextAtMouseEvent(e);
if(hyperlinkText == null)
setCursor(cdone);
else
setCursor(chand);
}
/**
* Get hyperlink text from a MouseEvent position. Return null if
* no hyperlink found.
* @param e
* @return
*/
private String getHyperlinkTextAtMouseEvent(final MouseEvent e)
{
final Point pt = new Point(e.getX(), e.getY());
final int pos = viewToModel(pt);
final int start;
if(pos < 15)
start = 0;
else
start = pos - 15;
int length = 30;
if( (start+30) > getStyledDocument().getLength() )
length = getStyledDocument().getLength()-start;
try
{
String textAtPosition = getStyledDocument().getText(start, length);
int indEnd = getEndOfLink(textAtPosition, (pos-start));
if(indEnd < 0)
return null;
textAtPosition = textAtPosition.substring(0, indEnd);
int indStart = getStartOfLink(textAtPosition);
if(indStart < 0)
return null;
return textAtPosition.substring(indStart);
}
catch(BadLocationException e1)
{
e1.printStackTrace();
//e1.printStackTrace();
}
return null;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment