Skip to content
Snippets Groups Projects
Commit 8fc6dbf8 authored by tjc's avatar tjc
Browse files

JTree implementation for database sequence selection

git-svn-id: svn+ssh://svn.internal.sanger.ac.uk/repos/svn/pathsoft/artemis/trunk@2726 ee4ac58c-ac51-4696-9907-e4b3aa274f04
parent 2521e214
No related branches found
No related tags found
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/ArtemisMain.java,v 1.12 2005-05-31 15:35:53 tjc Exp $
* $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/components/ArtemisMain.java,v 1.13 2005-06-01 11:06:43 tjc Exp $
*/
package uk.ac.sanger.artemis.components;
......@@ -38,20 +38,17 @@ import org.biojava.bio.seq.io.SequenceFormat;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
import java.io.*;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.ListSelectionModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
/**
* The main window for the Artemis sequence editor.
*
* @author Kim Rutherford <kmr@sanger.ac.uk>
* @version $Id: ArtemisMain.java,v 1.12 2005-05-31 15:35:53 tjc Exp $
* @version $Id: ArtemisMain.java,v 1.13 2005-06-01 11:06:43 tjc Exp $
**/
public class ArtemisMain extends Splash
......@@ -117,10 +114,9 @@ public class ArtemisMain extends Splash
if(!entry_source.setLocation())
return;
String id = displayDatabases(entry_source);
if(id != null)
getEntryEditFromDatabase(id, entry_source);
final DatabaseJFrame frame = new DatabaseJFrame(entry_source,
ArtemisMain.this);
frame.setVisible(true);
}
};
......@@ -213,7 +209,6 @@ public class ArtemisMain extends Splash
continue;
}
if(new_entry_name.startsWith("+") && last_entry_edit != null ||
seen_plus)
{
......@@ -512,7 +507,7 @@ public class ArtemisMain extends Splash
/**
* Make an EntryEdit component from the given Entry.
**/
private EntryEdit makeEntryEdit(final Entry entry)
protected EntryEdit makeEntryEdit(final Entry entry)
{
final Bases bases = entry.getBases();
final EntryGroup entry_group = new SimpleEntryGroup(bases);
......@@ -551,91 +546,6 @@ public class ArtemisMain extends Splash
}
/**
*
* Display a list of the available relational database entries.
*
*/
private String displayDatabases(DatabaseEntrySource entry_source)
{
Hashtable db = entry_source.getDatabaseEntries();
String db_array[] = new String[db.size()];
Enumeration enum_db = db.keys();
for(int i=0; enum_db.hasMoreElements(); i++)
db_array[i] = (String)enum_db.nextElement();
java.util.Arrays.sort(db_array);
JList list_db = new JList(db_array);
list_db.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scroll_list = new JScrollPane(list_db);
Object[] options = { "OPEN IN ARTEMIS >", "CANCEL"};
boolean selecting = true;
int select = JOptionPane.showOptionDialog(null, scroll_list,
"Database Selection",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
if(select == 1 || list_db.getSelectedValue() == null)
return null;
return (String)db.get((String)list_db.getSelectedValue());
}
/**
*
* Retrieve a database entry.
*
*/
private void getEntryEditFromDatabase(final String id,
final DatabaseEntrySource entry_source)
{
SwingWorker entryWorker = new SwingWorker()
{
public Object construct()
{
try
{
final InputStreamProgressListener progress_listener =
getInputStreamProgressListener();
// DatabaseEntrySource entry_source = new DatabaseEntrySource();
final Entry entry = entry_source.getEntry(id, progress_listener);
if(entry == null)
return null;
final EntryEdit new_entry_edit = makeEntryEdit(entry);
new_entry_edit.setVisible(true);
}
catch(OutOfRangeException e)
{
new MessageDialog(ArtemisMain.this, "read failed: one of the features in " +
" the entry has an out of range " +
"location: " + e.getMessage());
}
catch(NoSequenceException e)
{
new MessageDialog(ArtemisMain.this, "read failed: entry contains no sequence");
}
catch(IOException e)
{
new MessageDialog(ArtemisMain.this, "read failed due to IO error: " + e);
}
return null;
}
};
entryWorker.start();
}
/**
* Read an Entry from the given EntrySource and make a new EntryEdit
* component for the Entry.
......
......@@ -24,11 +24,14 @@
package uk.ac.sanger.artemis.components;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.Box;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.tree.TreePath;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Cursor;
import java.net.*;
import java.io.*;
import java.util.*;
......@@ -49,9 +52,10 @@ import uk.ac.sanger.artemis.io.EntryInformationException;
*
**/
public class DatabaseEntrySource implements EntrySource
public class DatabaseEntrySource implements EntrySource
{
private String location;
private Hashtable entries;
/**
* Create a new DatabaseEntrySource.
......@@ -137,12 +141,83 @@ public class DatabaseEntrySource implements EntrySource
}
protected Hashtable getDatabaseEntries()
/**
*
* Given the entry name get the seq feature_id
*
*/
protected String getEntryID(String name)
{
return (String)entries.get(name);
}
/**
*
* Create database organism JTree.
*
*/
protected JTree getDatabaseTree()
{
DatabaseDocument doc = new DatabaseDocument(location);
// DatabaseDocument doc = new DatabaseDocument("jdbc:postgresql://localhost:13001/chadoCVS?user=es2");
// DatabaseDocument doc = new DatabaseDocument("jdbc:postgresql://tim1:2999/chadoCVS?user=tim");
return doc.getDatabaseEntries();
entries = doc.getDatabaseEntries();
Vector organism = doc.getOrganism();
DefaultMutableTreeNode top =
new DefaultMutableTreeNode("PSU Organism List");
createNodes(top,organism,entries);
final JTree tree = new JTree(top);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
return tree;
}
/**
*
* Get DefaultMutableTreeNode of selected node
* @return node that is currently selected
*
*/
protected DefaultMutableTreeNode getSelectedNode(JTree tree)
{
TreePath path = tree.getLeadSelectionPath();
if(path == null)
return null;
return (DefaultMutableTreeNode)path.getLastPathComponent();
}
/**
*
* Create the nodes of the organism JTree
* @param top root node
* @param org organism collection
* @param organism sequences collection
*
*/
private void createNodes(DefaultMutableTreeNode top, Vector org,
Hashtable organism)
{
Enumeration enum_org = org.elements();
DefaultMutableTreeNode org_node;
DefaultMutableTreeNode seq_node;
while(enum_org.hasMoreElements())
{
String name = (String)enum_org.nextElement();
org_node = new DefaultMutableTreeNode(name);
top.add(org_node);
Enumeration enum_seq = organism.keys();
while(enum_seq.hasMoreElements())
{
String seq_name = (String)enum_seq.nextElement();
if(seq_name.startsWith(name))
{
seq_node = new DefaultMutableTreeNode(seq_name);
org_node.add(seq_node);
}
}
}
}
......
/* DatabaseJFrame.java
*
* created: June 2005
*
* 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/DatabaseJFrame.java,v 1.1 2005-06-01 11:06:43 tjc Exp $
*/
package uk.ac.sanger.artemis.components;
import uk.ac.sanger.artemis.Entry;
import uk.ac.sanger.artemis.sequence.*;
import uk.ac.sanger.artemis.util.InputStreamProgressListener;
import uk.ac.sanger.artemis.util.OutOfRangeException;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Cursor;
import java.io.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class DatabaseJFrame extends JFrame
{
public DatabaseJFrame(final DatabaseEntrySource entry_source,
final ArtemisMain art_main)
{
super("PSU Organism");
final JTree tree = entry_source.getDatabaseTree();
//Listen for when the selection changes.
MouseListener mouseListener = new MouseAdapter()
{
private Cursor cbusy = new Cursor(Cursor.WAIT_CURSOR);
private Cursor cdone = new Cursor(Cursor.DEFAULT_CURSOR);
public void mouseClicked(MouseEvent e)
{
if(e.getClickCount() == 2 &&
!e.isPopupTrigger())
{
tree.setCursor(cbusy);
DefaultMutableTreeNode node = entry_source.getSelectedNode(tree);
if(!node.isLeaf())
return;
String id = entry_source.getEntryID((String)node.getUserObject());
if(id != null)
getEntryEditFromDatabase(id, entry_source, art_main);
tree.setCursor(cdone);
}
}
};
tree.addMouseListener(mouseListener);
JScrollPane scroll = new JScrollPane(tree);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dim_frame = new Dimension(screen.width*2 / 10, screen.height*6 / 10);
scroll.setPreferredSize(dim_frame);
getContentPane().add(scroll);
pack();
Utilities.rightJustifyFrame(this);
}
/**
*
* Retrieve a database entry.
*
*/
private void getEntryEditFromDatabase(final String id,
final DatabaseEntrySource entry_source,
final ArtemisMain art_main)
{
SwingWorker entryWorker = new SwingWorker()
{
public Object construct()
{
try
{
final InputStreamProgressListener progress_listener =
art_main.getInputStreamProgressListener();
final Entry entry = entry_source.getEntry(id, progress_listener);
if(entry == null)
return null;
final EntryEdit new_entry_edit = art_main.makeEntryEdit(entry);
new_entry_edit.setVisible(true);
}
catch(OutOfRangeException e)
{
new MessageDialog(art_main, "read failed: one of the features in " +
" the entry has an out of range " +
"location: " + e.getMessage());
}
catch(NoSequenceException e)
{
new MessageDialog(art_main, "read failed: entry contains no sequence");
}
catch(IOException e)
{
new MessageDialog(art_main, "read failed due to IO error: " + e);
}
return null;
}
};
entryWorker.start();
}
}
......@@ -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/Utilities.java,v 1.1 2004-06-09 09:47:57 tjc Exp $
* $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/components/Utilities.java,v 1.2 2005-06-01 11:06:43 tjc Exp $
*/
package uk.ac.sanger.artemis.components;
......@@ -37,7 +37,7 @@ import javax.swing.*;
* Utilities methods used by the uk.ac.sanger.artemis.components package.
*
* @author Kim Rutherford <kmr@sanger.ac.uk>
* @version $Id: Utilities.java,v 1.1 2004-06-09 09:47:57 tjc Exp $
* @version $Id: Utilities.java,v 1.2 2005-06-01 11:06:43 tjc Exp $
**/
public class Utilities
......@@ -69,6 +69,21 @@ public class Utilities
frame.setLocation(new Point(x_position, y_position));
}
/**
* Move the given JFrame to the right of the screen.
**/
public static void rightJustifyFrame(final JFrame frame)
{
final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int y_position =(screen.height - frame.getSize().height) / 2;
if(y_position < 10)
y_position = 10;
frame.setLocation(new Point(0, y_position));
}
/**
* Find the parent Frame of the given component and re-centre it on the
* screen.
......
......@@ -27,6 +27,7 @@ package uk.ac.sanger.artemis.util;
import java.sql.*;
import java.io.*;
import java.util.Hashtable;
import java.util.Vector;
import javax.swing.JOptionPane;
/**
......@@ -36,11 +37,12 @@ import javax.swing.JOptionPane;
public class DatabaseDocument extends Document
{
private String name = null;
private String feature_id = "1";
private Hashtable cvterm;
private InputStreamProgressListener progress_listener;
private Hashtable db;
private Vector organism;
/**
*
......@@ -278,7 +280,9 @@ public class DatabaseDocument extends Document
**/
public Hashtable getDatabaseEntries()
{
Hashtable db = new Hashtable();
db = new Hashtable();
organism = new Vector();
try
{
Connection conn = getConnection();
......@@ -299,7 +303,12 @@ public class DatabaseDocument extends Document
rs = st.executeQuery(sql);
while(rs.next())
db.put(rs.getString("abbreviation")+" - "+rs.getString("name"), rs.getString("feature_id"));
{
String org = rs.getString("abbreviation");
db.put(org+" - "+rs.getString("name"), rs.getString("feature_id"));
if(!organism.contains(org))
organism.add(org);
}
}
catch(java.sql.SQLException sqlExp)
{
......@@ -319,6 +328,10 @@ public class DatabaseDocument extends Document
return db;
}
public Vector getOrganism()
{
return organism;
}
public Connection getConnection() throws java.sql.SQLException,
java.net.ConnectException
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment