/* * * created: 2007 * * This file is part of Artemis * * Copyright (C) 2007 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. * */ package uk.ac.sanger.artemis.chado; import org.gmod.schema.cv.Cv; import org.gmod.schema.cv.CvTerm; import uk.ac.sanger.artemis.components.genebuilder.JExtendedComboBox; import uk.ac.sanger.artemis.components.genebuilder.cv.CvTermsComparator; import uk.ac.sanger.artemis.util.DatabaseLocationParser; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import java.util.Collections; import java.util.List; import java.util.Vector; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.ListCellRenderer; import javax.swing.SwingConstants; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; /** * Chado data access example code. This searches for features by their * uniquename and returns their properties and attributes. * * @author tjc * */ public class ChadoCvTermView extends JFrame { /** */ private static final long serialVersionUID = 1L; /** database URL */ private String location; /** password fields */ private JPasswordField pfield; private GmodDAO dao; /** * Chado demo */ public ChadoCvTermView(GmodDAO dao) { super("Controlled Vocabulary"); super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); try { if(dao == null) { setLocation(); dao = getDAO(); } this.dao = dao; final List cvs = dao.getAllCvs(); Cv cvSequence = null; for(int i=0; iJMenuBar. * * @return a JMenuBar */ public JMenuBar getJMenuBar(final GmodDAO dao) { JMenuBar mbar = new JMenuBar(); JMenu file = new JMenu("File"); mbar.add(file); JMenuItem exit = new JMenuItem("Exit"); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); file.add(exit); return mbar; } /** * Get the data access object (DAO). * * @return data access object */ private GmodDAO getDAO() throws java.net.ConnectException, SQLException { if(System.getProperty("ibatis") == null) return new JdbcDAO(location, pfield); else return new IBatisDAO(pfield); } /** * Set the database location as: * jdbc:postgresql://localhost:13001/chadoCVS?user=es2 * * @return true if location chosen */ private boolean setLocation() { Container bacross = new Container(); bacross.setLayout(new GridLayout(6, 2, 5, 5)); JLabel lServer = new JLabel("Server : "); bacross.add(lServer); JTextField inServer = new JTextField("localhost"); bacross.add(inServer); JLabel lPort = new JLabel("Port : "); bacross.add(lPort); JTextField inPort = new JTextField("5432"); bacross.add(inPort); JLabel lDB = new JLabel("Database : "); bacross.add(lDB); JTextField inDB = new JTextField("chado"); bacross.add(inDB); JLabel lUser = new JLabel("User : "); bacross.add(lUser); JTextField inUser = new JTextField("afumigatus"); bacross.add(inUser); JLabel lpasswd = new JLabel("Password : "); bacross.add(lpasswd); pfield = new JPasswordField(16); bacross.add(pfield); DatabaseLocationParser dlp; // given -Dchado=localhost:port/dbname?username if(System.getProperty("chado") != null) { String db_url = System.getProperty("chado").trim(); dlp = new DatabaseLocationParser(db_url); inServer.setText(dlp.getHost()); inPort.setText("" + dlp.getPort()); inDB.setText(dlp.getDatabase()); inUser.setText(dlp.getUsername()); } else { // Still need to initialise the object dlp = new DatabaseLocationParser(); } int n = JOptionPane.showConfirmDialog(null, bacross, "Enter Database Address", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if(n == JOptionPane.CANCEL_OPTION) return false; dlp.setHost(inServer.getText()); dlp.setPort(inPort.getText()); dlp.setDatabase(inDB.getText()); dlp.setUsername(inUser.getText()); location = dlp.getCompleteURL(); System.setProperty("chado", location); return true; } class CvRenderer extends JLabel implements ListCellRenderer { /** */ private static final long serialVersionUID = 1L; public CvRenderer() { setOpaque(true); } public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { if(value instanceof Cv) { Cv cv = (Cv)value; setText(cv.getName()); } else if(value instanceof CvTerm) { CvTerm cvTerm = (CvTerm)value; setText(cvTerm.getName()); } else setText((String)value); setBackground(isSelected ? Color.red : Color.white); setForeground(isSelected ? Color.white : Color.black); return this; } } class TextCellRenderer extends JTextArea implements TableCellRenderer { /** */ private static final long serialVersionUID = 1L; public TextCellRenderer() { setLineWrap(true); setWrapStyleWord(true); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if(value == null) setText(""); else setText(value.toString()); setSize(table.getColumnModel().getColumn(column).getWidth(), getPreferredSize().height); if(table.getRowHeight(row) != getPreferredSize().height) table.setRowHeight(row, getPreferredSize().height); return this; } } public class LabelTableRender extends DefaultTableCellRenderer { /** */ private static final long serialVersionUID = 1L; public LabelTableRender() { super(); this.setVerticalAlignment(SwingConstants.TOP); } } public static void main(String args[]) { new ChadoCvTermView(null); } }