package versusSNP.gui;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import versusSNP.Document;
import versusSNP.Size;
import versusSNP.VersusSNP;
import versusSNP.blast.BlastSet;
import versusSNP.genome.ORF;
import versusSNP.gui.widgets.ORFCellRenderer;
import versusSNP.gui.widgets.ORFTable;
public class ChoosePanel extends JPanel implements Observer, ActionListener, MouseListener {
private static final long serialVersionUID = -1802105300361725856L;
private ORFTable table;
private DefaultTableModel tableModel;
private VersusSNP controller;
private ORF currentORF; // Temporarily stores the ORF clicked
public ChoosePanel() {
super();
setLayout(new GridLayout(1,1));
setPreferredSize(Size.main_frame_west);
tableModel = new DefaultTableModel(
new Object[] { UICaption.panel_choose_table_name_column }, 0) {
private static final long serialVersionUID = 8713334423930665984L;
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
table = new ORFTable(tableModel);
DefaultTableCellRenderer headRenderer = new DefaultTableCellRenderer();
headRenderer.setPreferredSize(new Dimension(0,0));
table.getTableHeader().setDefaultRenderer(headRenderer);
table.setDefaultRenderer(ORF.class, new ORFCellRenderer());
table.addMouseListener(this);
add(new JScrollPane(table));
}
public ChoosePanel(VersusSNP versusSNP) {
this();
this.controller = versusSNP;
}
@Override
public void update(Observable o, Object arg) {
Document document = (Document)o;
tableModel.setRowCount(0); // clear previous table things
if (document.getQueryGenome() == null)
return;
for (Iterator<ORF> iter = document.getQueryGenome().getOrfList().iterator(); iter.hasNext();) {
ORF orf = iter.next();
tableModel.insertRow(tableModel.getRowCount(), new ORF[]{orf});
}
}
private void showPopup(MouseEvent e) {
// if (!e.isPopupTrigger()) return;
Point point = e.getPoint();
int row = table.rowAtPoint(point);
int col = table.columnAtPoint(point);
if (row == -1 || col == -1)
return;
Object obj = tableModel.getValueAt(row, col);
if (obj instanceof ORF) {
currentORF = (ORF) obj;
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem menuItem = new JMenuItem(UICaption.popup_menuitem_orf_sequence);
menuItem.addActionListener(this);
popupMenu.add(menuItem);
popupMenu.addSeparator();
for (Iterator<BlastSet> iter = currentORF.getBlastList().iterator(); iter.hasNext();) {
BlastSet set = iter.next();
JMenuItem orfItem = new JMenuItem(set.getSName());
orfItem.addActionListener(this);
popupMenu.add(orfItem);
}
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
@Override
public void mouseClicked(MouseEvent e) {
showPopup(e);
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// showPopup(e);
}
@Override
public void mouseReleased(MouseEvent e) {
// showPopup(e);
}
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj instanceof JMenuItem) {
if (((JMenuItem)obj).getActionCommand().equals(UICaption.popup_menuitem_orf_sequence)) {
controller.getSeqPanel().setSequence(currentORF);
controller.getSeqPanel().setOrfSummary(currentORF);
} else {
String subjectName = ((JMenuItem)obj).getActionCommand();
BlastSet set = currentORF.getBlastSet(subjectName);
// currentORF.statSNP();
if (set != null) {
controller.getSeqPanel().setAlignmentSummary(set);
controller.getTabbedPane().setSelectedComponent(controller.getTabbedPane().getSnpPanel());
controller.getTabbedPane().getSnpPanel().accept(set.getSNPList());
}
}
}
}
}