package versusSNP.gui;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import versusSNP.Size;
import versusSNP.blast.BlastSet;
import versusSNP.genome.ORF;
import versusSNP.util.HTMLSequenceFormatter;
import versusSNP.util.PlatformUtils;
import versusSNP.util.swing.PercentLayout;
public class SeqPanel extends JPanel implements Observer, MouseListener, ActionListener {
private static final long serialVersionUID = -1947316732150656096L;
private JTextPane seqArea, noteArea, currentArea;
private JPopupMenu popupMenu;
private JMenuItem copyItem;
public SeqPanel() {
super();
setPreferredSize(Size.main_frame_south);
seqArea = new JTextPane();
noteArea = new JTextPane();
popupMenu = new JPopupMenu();
copyItem = new JMenuItem(UICaption.popup_menuitem_copy);
popupMenu.add(copyItem);
copyItem.addActionListener(this);
seqArea.addMouseListener(this);
noteArea.addMouseListener(this);
seqArea.setEditable(false);
noteArea.setEditable(false);
seqArea.setContentType("text/html");
noteArea.setContentType("text/plain");
Font font = PlatformUtils.getPlatformSpecificFont();
seqArea.setFont(font);
noteArea.setFont(font);
seqArea.setPreferredSize(Size.panel_seq_seq);
noteArea.setPreferredSize(Size.panel_seq_note);
JScrollPane scroll1 = new JScrollPane(seqArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JScrollPane scroll2 = new JScrollPane(noteArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
setLayout(new PercentLayout(PercentLayout.HORIZONTAL, 2));
add(scroll1, "60%");
add(scroll2, "40%");
}
@Override
public void update(Observable o, Object arg) {
// Document document = (Document)o;
}
public void setSequence(ORF orf) {
if (orf.getSequence() == null) {
seqArea.setText(UICaption.panel_seq_seq_no_sequence);
return;
}
try {
seqArea.setDocument(HTMLSequenceFormatter.format(orf.getSequence()));
} catch (IOException e) {
JOptionPane.showMessageDialog(null, UICaption.dialog_exception_internal+UICaption.about_bug_report+UICaption.about_email, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
} catch (BadLocationException e) {
JOptionPane.showMessageDialog(null, UICaption.dialog_exception_internal+UICaption.about_bug_report+UICaption.about_email, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
}
}
public void setOrfSummary(ORF orf) {
if (orf.getSequence() == null) {
noteArea.setText(orf.getBriefSummary());
return;
}
noteArea.setText(orf.getSummary());
}
public void setAlignmentSummary(BlastSet set) {
try {
seqArea.setDocument(HTMLSequenceFormatter.format(set.getAlignmentStore()));
} catch (IOException e) {
JOptionPane.showMessageDialog(null, UICaption.dialog_exception_internal+UICaption.about_bug_report+UICaption.about_email, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
} catch (BadLocationException e) {
JOptionPane.showMessageDialog(null, UICaption.dialog_exception_internal+UICaption.about_bug_report+UICaption.about_email, UICaption.dialog_caption_error, JOptionPane.ERROR_MESSAGE);
}
noteArea.setText(set.getSummary());
}
private static boolean canCopy(JTextPane pane) {
return pane.getSelectionStart() != pane.getSelectionEnd();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@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) {
if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {
Component component = e.getComponent();
JTextPane textPane = (JTextPane) component;
copyItem.setEnabled(canCopy(textPane));
copyItem.setText(UICaption.popup_menuitem_copy);
currentArea = textPane;
popupMenu.show(component, e.getX(), e.getY());
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == copyItem)
currentArea.copy();
}
public void clear() {
seqArea.setText("");
noteArea.setText("");
}
}