Newer
Older
/* QualifierTextArea.java
*
* created: Tue Oct 23 2001
*
* This file is part of Artemis
*
* Copyright (C) 2000 Genome Research Limited
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* 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.3 2007-09-11 11:03:20 tjc Exp $
*/
package uk.ac.sanger.artemis.components;
import uk.ac.sanger.artemis.Options;
import uk.ac.sanger.artemis.editor.BrowserControl;
import uk.ac.sanger.artemis.editor.DataCollectionPane;
import uk.ac.sanger.artemis.io.QualifierParseException;
import uk.ac.sanger.artemis.io.QualifierVector;
import uk.ac.sanger.artemis.io.EntryInformation;
import uk.ac.sanger.artemis.io.EmblStreamFeature;
import uk.ac.sanger.artemis.util.StringVector;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.StringReader;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
* This component is a JTextPane that understands qualifiers.
* It provides hyperlinks to databases.
public class QualifierTextArea extends JTextPane
{
private static String[] DATABASES =
{ "SWALL", "EMBL", "UniProt", "PMID" };
public QualifierTextArea ()
{
super();
initStyles();
int nrows = (Options.getOptions ().getPropertyTruthValue ("alicat_mode") ||
18);
int ncolumns = 81;
setPreferredSize(new Dimension( ncolumns*getColumnWidth(), nrows*getRowHeight() ));
try // no such method in java1.3
{
setDragEnabled(true);
}
catch(java.lang.NoSuchMethodError err){}
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount() == 2)
handleMouseClick(e);
}
});
}
public void append(final String s)
{
StyledDocument doc = super.getStyledDocument();
try
{
doc.insertString(doc.getLength(), s, getLogicalStyle());
for(int i=0; i<DATABASES.length; i++)
setStyleForHyperLinks(s, DATABASES[i]);
}
catch(BadLocationException e)
{
e.printStackTrace();
}
}
private void initStyles()
{
// Makes text Blue
Style style = addStyle("Blue", null);
StyleConstants.setForeground(style, Color.blue);
// Inherits from "Blue"; makes text red and underlined
style = addStyle("Blue Underline", style);
StyleConstants.setUnderline(style, true);
}
public void setText(String text)
{
super.setText(text);
for(int i=0; i<DATABASES.length; i++)
setStyleForHyperLinks(text, DATABASES[i]);
}
/**
* Parse and return the qualifiers in this TextArea in a QualifierVector.
**/
public QualifierVector
getParsedQualifiers (final EntryInformation entry_information)
throws QualifierParseException
{
final String qualifier_string = getText ();
return getQualifiersFromString (qualifier_string,
entry_information);
}
/**
* Return a QualifierVector containing the qualifiers from a String.
* @param qual_string contains the qualifiers to parse
*/
private static QualifierVector
getQualifiersFromString (final String qual_string,
final EntryInformation entry_information)
throws QualifierParseException
{
final StringReader string_reader = new StringReader (qual_string);
final QualifierVector embl_qualifiers =
EmblStreamFeature.readQualifiers (string_reader,
entry_information);
}
catch (IOException exception)
{
throw (new QualifierParseException (exception.getMessage ()));
}
}
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
private int getColumnWidth()
{
FontMetrics metrics = getFontMetrics(getFont());
return metrics.charWidth('m');
}
private int getRowHeight()
{
FontMetrics metrics = getFontMetrics(getFont());
return metrics.getHeight();
}
/**
* This routine sets the hyerlink style for a given database
* @param s
* @param db
*/
private void setStyleForHyperLinks(final String s,
final String db)
{
int ind = 0;
while((ind = s.indexOf(db, ind)) > -1)
{
int ind2 = getEndOfLink(s,ind);
getStyledDocument().setCharacterAttributes(ind, ind2-ind,
getStyle("Blue Underline"), true);
ind = ind+1;
}
}
private int getEndOfLink(String s, int ind)
{
int ind2 = s.indexOf(" ",ind);
if(ind2 == -1)
ind2 = s.indexOf(";",ind);
int ind3 = s.indexOf(")",ind);
if(ind3>-1 && (ind3<ind2 || ind2 == -1))
ind2 = ind3;
ind3 = s.indexOf(";",ind);
if(ind3>-1 && (ind3<ind2 || ind2 == -1))
ind2 = ind3;
ind3 = s.indexOf("\"",ind);
if(ind3>-1 && (ind3<ind2 || ind2 == -1))
ind2 = ind3;
ind3 = s.indexOf("\n",ind);
if(ind3>-1 && (ind3<ind2 || ind2 == -1))
ind2 = ind3;
return ind2;
}
private int getStartOfLink(final String s)
{
int lastIndexLink = -1;
for(int i=0; i<DATABASES.length; i++)
{
int index = s.lastIndexOf(DATABASES[i]);
if(index > lastIndexLink)
lastIndexLink = index;
}
return lastIndexLink;
}
public static String getPubMedSite()
{
StringVector pubmed = Options.getOptions().getOptionValues("pubmed_url");
if(pubmed != null)
return (String)pubmed.elementAt(0);
return "http://www.ncbi.nlm.nih.gov/sites/entrez?Db=pubmed&Cmd=ShowDetailView&TermToSearch=";
}
private void handleMouseClick(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
{
String textAtPosition = getStyledDocument().getText(start, length);
int indEnd = getEndOfLink(textAtPosition, (pos-start));
textAtPosition = textAtPosition.substring(0, indEnd);
int intStart = getStartOfLink(textAtPosition);
if(intStart < 0)
return;
textAtPosition = textAtPosition.substring(intStart);
String cmd;
if(textAtPosition.indexOf("PMID")>-1)
{
String id[] = textAtPosition.split(":");
if(id.length < 2)
return;
cmd = getPubMedSite()+id[1];
}
else
{
cmd = DataCollectionPane.getSrsSite()+"/wgetz?-e+["+textAtPosition+"]";
// 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";
}
BrowserControl.displayURL(cmd);
}
catch(BadLocationException e1)
{
e1.printStackTrace();
}
}