/* GeneComponentTree.java * * created: 2006 * * This file is part of Artemis * * Copyright(C) 2006 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/genebuilder/GeneComponentTree.java,v 1.23 2009-06-12 13:50:35 tjc Exp $ */ package uk.ac.sanger.artemis.components.genebuilder; import uk.ac.sanger.artemis.io.ChadoCanonicalGene; import uk.ac.sanger.artemis.io.DatabaseInferredFeature; import uk.ac.sanger.artemis.io.Feature; import uk.ac.sanger.artemis.io.InvalidRelationException; import uk.ac.sanger.artemis.Selection; import uk.ac.sanger.artemis.FeatureVector; import javax.swing.*; import javax.swing.event.*; import javax.swing.tree.*; import java.util.List; import java.util.Enumeration; import java.util.Vector; /** * Tree to display a gene hierarchy. */ public class GeneComponentTree extends JTree { /** */ private static final long serialVersionUID = 1L; private ChadoCanonicalGene chado_gene; private GeneBuilderFrame gene_builder; private Selection selection; private GeneTreeSelectionListener selection_listener; public GeneComponentTree(final ChadoCanonicalGene chado_gene, final GeneBuilderFrame gene_builder, final Selection selection) { this.chado_gene = chado_gene; this.gene_builder = gene_builder; this.selection = selection; final Feature gene = (Feature)chado_gene.getGene(); final String gene_id; try { gene_id = (String)gene.getQualifierByName("ID").getValues().get(0); DefaultMutableTreeNode top = new DefaultMutableTreeNode(gene_id); createNodes(top, chado_gene); DefaultTreeModel model = new DefaultTreeModel(top); setModel(model); } catch(InvalidRelationException e) { e.printStackTrace(); } //Listen for when a file is selected selection_listener = new GeneTreeSelectionListener(); addTreeSelectionListener(selection_listener); setSelection(selection); } /** * Select the tree nodes based on the features selected. * @param selection */ protected void setSelection(Selection selection) { removeTreeSelectionListener(selection_listener); FeatureVector features = selection.getAllFeatures(); TreePath path[] = new TreePath[features.size()]; for(int i=0; i 0) deleteChildNode(id, child); if(id.equals((String)child.getUserObject())) { ((DefaultTreeModel)getModel()).removeNodeFromParent(child); return; } } } /** * Add a new node for the selected feature. It uses the Parent * qualifier information to work out where it should be added. * @param feature */ protected void addNode(final uk.ac.sanger.artemis.Feature feature) { try { final String parent; if(feature.getQualifierByName("Parent") != null) parent = (String)feature.getQualifierByName("Parent").getValues().get(0); else parent = (String)feature.getQualifierByName("Derives_from").getValues().get(0); String name = (String)feature.getQualifierByName("ID").getValues().get(0); if(getNodeFromName(name) != null) return; DefaultMutableTreeNode parentNode = getNodeFromName(parent); if(parentNode != null) { ((DefaultTreeModel)getModel()).insertNodeInto( new DefaultMutableTreeNode(name), parentNode, parentNode.getChildCount()); } } catch(InvalidRelationException e) { e.printStackTrace(); } } private DefaultMutableTreeNode searchChildren( final DefaultMutableTreeNode node, final String id) { Enumeration root_children = node.children(); while(root_children.hasMoreElements()) { DefaultMutableTreeNode child = (DefaultMutableTreeNode)root_children.nextElement(); if(id.equals((String)child.getUserObject())) return child; } return null; } class GeneTreeSelectionListener implements TreeSelectionListener { public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode)GeneComponentTree.this.getLastSelectedPathComponent(); if(node == null) return; Feature embl_feature = (Feature)chado_gene.getFeatureFromId((String)node.getUserObject()); final uk.ac.sanger.artemis.Feature feature; if(embl_feature.getUserData() == null) feature = new uk.ac.sanger.artemis.Feature(embl_feature); else feature = (uk.ac.sanger.artemis.Feature)embl_feature.getUserData(); boolean isSet = true; if(feature.isReadOnly()) isSet = false; gene_builder.setActiveFeature(feature, isSet); if(selection != null) selection.set(feature); } } }