|
Waldeinsamkeit
Senior Member
Posts: 745
Last Seen: 12-21-2012
|
Issue with Swing
So I am new to GUIs and Swing. I am having trouble getting this to work correctly. On my machine it will run correctly. I click the processCollection button and it goes. On other machines I click the button, it stays depressed for some time, then it goes back to it's normal state without running. After that when I click the button it just stays depressed while the mouse is over it, and goes to it's normal state when I move the mouse away. Any Ideas?
I can post additional code if needed, and excuse the messy code, this was just supposed to be a quick job to get a tool out ASAP.
Code:
package tf_db;
//Java imports
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.Vector;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
//Bio java imports
import org.biojava.bio.BioException;
/**
*
* @author Joe
*
*/
//program main
public class WorkflowGUI {
//member variables
private JButton loadGenomeButton = new JButton("Load Genome");
private JButton loadCollectionButton = new JButton("Load Collection");
private JButton processCollectionButton = new JButton("Process Collection");
private JButton setOutputDirButton = new JButton("Select Output Directory");
private JTextField genomeFilePath = new JTextField();
private JTextField sitesFilePath = new JTextField();
private JTextField outputPath = new JTextField();
private JTextField paperPMID = new JTextField();
private JTextField TFName = new JTextField();
private JTextField TFProtAcc = new JTextField();
private JTextField expMethod_1 = new JTextField();
private JTextField expMethod_2 = new JTextField();
private JTextField expMethod_3 = new JTextField();
private JTextField motifType = new JTextField();
private JTextField TFType = new JTextField();
private JTextArea paperPMIDBox = new JTextArea();
private JTextArea TFNameBox = new JTextArea();
private JTextArea TFProtAccBox = new JTextArea();
private JTextArea expMethod_1Box = new JTextArea();
private JTextArea expMethod_2Box = new JTextArea();
private JTextArea expMethod_3Box = new JTextArea();
private JTextArea motifTypeBox = new JTextArea();
private JTextArea TFTypeBox = new JTextArea();
private static Vector<String> args = new Vector<String>();
private static JFrame frame = new JFrame();
private JPanel filePanel = new JPanel();
private JPanel settingsPanel_1 = new JPanel();
//private JPanel settingsPanel_2 = new JPanel();
private JPanel runButtonPanel = new JPanel();
private boolean ready = false;
private static Vector<BindingSite> theSites; //the binding sites to search for
private static File CSVOutput; //file for outputting the CSV data
private static File textOutput; //text version of output
private static Vector<CDS> CDSResults; //holds the cds
private static Genome theGenome; //genome object
//GUI constructor
public WorkflowGUI(){
//add action listeners for buttons
//load genome button
this.loadGenomeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
// filter for FASTA and GenBank files only
JFileChooser fileChooser = new JFileChooser();
FileFilter filefilter = new ExtensionFileFilter("FASTA AND GENBANK ONLY", new String[]{"gb","gbk","fas","fasta","fma"});
fileChooser.setFileFilter(filefilter);
fileChooser.setCurrentDirectory(new java.io.File("."));
// set text field to chosen file
int returnVal = fileChooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
genomeFilePath.setText(file.getPath());
}
}
});
//load sites button
this.loadCollectionButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//filter for .FASTA files onle
JFileChooser fileChooser = new JFileChooser();
FileFilter fileFilter = new ExtensionFileFilter("FASTA ONLY", new String[]{"fas", "fasta"});
fileChooser.setFileFilter(fileFilter);
fileChooser.setCurrentDirectory(new java.io.File("."));
int ret = fileChooser.showOpenDialog(null);
if(ret==JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
sitesFilePath.setText(file.getPath());
}
}
});
//process collection button
this.processCollectionButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
/* Creates parameter container
* THIS ORDER IS IMPORTANT:
*
* GeneBank File
* Collection File
* OutputDirectory
* PMID
* TF Name
* TF Prot Accession
* TF Prot Type
* Motif Type
* Experimental Method 1
* Experimental Method 2
* Experimental Method 3
*
*/
//add strings to container
args.add(genomeFilePath.getText());
args.add(sitesFilePath.getText());
args.add(outputPath.getText());
args.add(paperPMID.getText());
args.add(TFName.getText());
args.add(TFProtAcc.getText());
args.add(expMethod_1.getText());
args.add(expMethod_2.getText());
args.add(expMethod_3.getText());
args.add(motifType.getText());
args.add(TFType.getText());
//process everything
//parse collection
try {
parseFASTACollection(args.get(1).toString());
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//parse genome (and process)
try {
theGenome = new Genome(args.get(0), theSites);
} catch (NoSuchElementException | BioException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//print
try {
generateCDSOutput();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//set ready to true
ready = true;
}
});
//set the output directory
this.setOutputDirButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setCurrentDirectory(new java.io.File("."));
int ret = fileChooser.showOpenDialog(null);
if(ret==JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
outputPath.setText(file.getPath());
}
}
});
//set the paper PMID
this.paperPMID.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//String text = paperPMIDBox.getText();
paperPMID.selectAll();
paperPMIDBox.setCaretPosition(paperPMIDBox.getDocument().getLength());
}
});
//set the TF Name
this.TFName.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//String text = TFNameBox.getText();
TFName.selectAll();
TFNameBox.setCaretPosition(TFNameBox.getDocument().getLength());
}
});
//set the TF PROT ACC
this.TFProtAcc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//String text = TFProtAccBox.getText();
TFProtAcc.selectAll();
TFProtAccBox.setCaretPosition(TFProtAccBox.getDocument().getLength());
}
});
//set the 1st experimental method
this.expMethod_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//String text = expMethod_1Box.getText();
expMethod_1.selectAll();
expMethod_1Box.setCaretPosition(expMethod_1Box.getDocument().getLength());
}
});
//set the second experimental method
this.expMethod_2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//String text = expMethod_2Box.getText();
expMethod_2.selectAll();
expMethod_2Box.setCaretPosition(expMethod_2Box.getDocument().getLength());
}
});
//set the third experimental method
this.expMethod_3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//String text = expMethod_3Box.getText();
expMethod_3.selectAll();
expMethod_3Box.setCaretPosition(expMethod_3Box.getDocument().getLength());
}
});
//set the Motif type
this.motifType.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//String text = motifTypeBox.getText();
motifType.selectAll();
motifTypeBox.setCaretPosition(motifTypeBox.getDocument().getLength());
}
});
//set the TF type
this.TFType.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//String text = TFTypeBox.getText();
TFType.selectAll();
TFTypeBox.setCaretPosition(TFTypeBox.getDocument().getLength());
}
});
//begin to configure GUI panel
// frame settings
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(3,2));
frame.setPreferredSize(new Dimension(600,500));
frame.setTitle("Transcription Factor Binding Site Database Tool");
//panel settings
filePanel = new JPanel(new GridLayout(3,3));
settingsPanel_1.setLayout(new GridLayout(0,4));
//settingsPanel_2 = new JPanel(new GridLayout(2,2));
runButtonPanel = new JPanel(new GridLayout(0,1));
//label buttons/ect
genomeFilePath.setBorder(BorderFactory.createTitledBorder("Genome GeneBank File"));
sitesFilePath.setBorder(BorderFactory.createTitledBorder("Binding Site Collection File"));
outputPath.setBorder(BorderFactory.createTitledBorder("Output Directory"));
paperPMID.setBorder(BorderFactory.createTitledBorder("Paper Reference PMID"));
TFName.setBorder(BorderFactory.createTitledBorder("Transcription Factor Name"));
TFProtAcc.setBorder(BorderFactory.createTitledBorder("Transcrption Factor Protein Accession"));
expMethod_1.setBorder(BorderFactory.createTitledBorder("First Experimental Mehtod"));
expMethod_2.setBorder(BorderFactory.createTitledBorder("Second Experimental Method"));
expMethod_3.setBorder(BorderFactory.createTitledBorder("Third Experimntal Method"));
motifType.setBorder(BorderFactory.createTitledBorder("Motif Type"));
TFType.setBorder(BorderFactory.createTitledBorder("Transciption Factor Type"));
genomeFilePath.setText("Genome File");
sitesFilePath.setText("Collection File");
outputPath.setText("Output Directory");
paperPMID.setText("Reference Paper PMID");
TFName.setText("Transcription Factor Name");
TFProtAcc.setText("Transcription Factor Protein Accession");
expMethod_1.setText("First Experimental Method");
expMethod_2.setText("Second Experimental Method");
expMethod_3.setText("Third Experiemental Method");
motifType.setText("Motif Type");
TFType.setText("Transcription Factor Type");
//add buttons and such
filePanel.add(genomeFilePath);
filePanel.add(loadGenomeButton);
filePanel.add(sitesFilePath);
filePanel.add(loadCollectionButton);
filePanel.add(outputPath);
filePanel.add(setOutputDirButton);
settingsPanel_1.add(TFName);
settingsPanel_1.add(TFNameBox);
settingsPanel_1.add(TFProtAcc);
settingsPanel_1.add(TFProtAccBox);
settingsPanel_1.add(TFType);
settingsPanel_1.add(TFTypeBox);
settingsPanel_1.add(motifType);
settingsPanel_1.add(motifTypeBox);
settingsPanel_1.add(paperPMID);
settingsPanel_1.add(paperPMIDBox);
settingsPanel_1.add(expMethod_1);
settingsPanel_1.add(expMethod_1Box);
settingsPanel_1.add(expMethod_2);
settingsPanel_1.add(expMethod_2Box);
settingsPanel_1.add(expMethod_3);
settingsPanel_1.add(expMethod_3Box);
runButtonPanel.add(processCollectionButton);
frame.add(filePanel);
frame.add(settingsPanel_1);
//frame.add(settingsPanel_2);
frame.add(runButtonPanel);
frame.pack();
frame.setVisible(true);
//end constructor
}
/**method to get args Vector<String>
* @param none
* @return Vector containing the arg values
* @author Joe Cornish
*/
public Vector<String> getArgs(){
return(args);
}
/**method to generate a user error message in a new frame
* @param Strig message: the error message to display to the user
* @return nne
* @author Joe Cornish
*/
public static void displayErrorMessage(String message){
JFrame error = new JFrame();
JTextArea errorMessage = new JTextArea();
error.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
errorMessage.setText(message);
error.add(errorMessage);
error.setBounds(200, 200, 300, 75);
error.pack();
error.setVisible(true);
}
/**Method to send a close command to the windo
* @param none
* @return none
* @author Joe Cornish
*/
public void closeGUIWindow(){
frame.dispose();
}
//get ready state
public boolean isReady(){return(ready);}
public static void generateCDSOutput() throws IOException{
CDSResults = new Vector<CDS>(theGenome.getAnnotatedCDS());
CSVOutput = new File(new String(args.get(2)) + "/DB_ENTRY_" + new String(args.get(4)) + "_PMID_" + new String(args.get(3)) + ".csv");
PrintWriter outStream = new PrintWriter(CSVOutput);
outStream.print("TF NAME, TF PROTEIN TYPE, TF PROTEIN ACCESSION, SPECIES, NCBI TAXON, REFSEQ ACCESSION, " +
"LOCUS TAG, PROTEIN ACCESSION, GENE NAME, GENE SYNONYM, GENE PRODUCT, PRODUCT FUNCTION, NOTES, START, END, " +
"LENGTH, STRAND, BINDING SITE, REV COMP, SITE STRAND, SITE POS, MOTIF TYPE, METHOD 1, METHOD 2, METHOD 3, REF PMID\n");
for(int i=0;i<CDSResults.size();++i){
/*
* genomeFileDir = new String(args.get(0));
collectionFileDir = new String(args.get(1));
outFileDir = new String(args.get(2));
PMID = new String(args.get(3));
TFName = new String(args.get(4));
TFProtAcc = new String(args.get(5));
expMethod_1 = new String(args.get(6));
expMethod_2 = new String(args.get(7));
expMethod_3 = new String(args.get(8));
motifType = new String(args.get(9));
TFProtType = new String(args.get(10));
*/
System.out.println(CDSResults.get(i).getCVSFormatCDS());
outStream.print(args.get(4).toString());
outStream.print(',');
outStream.print(args.get(10).toString());
outStream.print(',');
outStream.print(args.get(5).toString());
outStream.print(',');
outStream.print(theGenome.getGenomeCSV());
outStream.print(',');
outStream.print(CDSResults.get(i).getCVSFormatCDS());
outStream.print(',');
outStream.print(args.get(9).toString());
outStream.print(',');
outStream.print(args.get(6).toString());
outStream.print(',');
outStream.print(args.get(7).toString());
outStream.print(',');
outStream.print(args.get(8).toString());
outStream.print(',');
outStream.print(args.get(3).toString());
outStream.print(',');
outStream.print('\n');
}
//outStream.flush();
outStream.close();
//stick raw output into .txt file
//for now just user entered data
//later add CDS ****
textOutput = new File(args.get(2).toString() + "/DB_RAW_TEXT_" + args.get(4).toString() + "_PMID_" + args.get(3).toString() + ".txt" );
outStream = new PrintWriter(textOutput);
outStream.print("GENOME FILE USED: " + args.get(0).toString() + '\n');
outStream.print("COLLECTION FILE USED: " + args.get(1).toString() + '\n');
outStream.print("OUTPUT DIRECTORY USED: " + args.get(2).toString() + '\n');
outStream.print("DB ENTRY FILE SAVED TO: " + CSVOutput.getAbsolutePath() + '\n');
outStream.print("TRANSCRIPTION FACTOR NAME ENTERED: " + args.get(4).toString() + '\n');
outStream.print("TRANSCRIPTION FACTOR TYPE ENTERED: " + args.get(10).toString() + '\n');
outStream.print("TRANSCRIPTION FACTOR PROTIEN ACCESSION: " + '\n');
outStream.print("MOTIF TYPE ENTERED: " + args.get(9).toString() + '\n');
outStream.print("FIRST EXPERIMENTAL METHOD ENTERED: " + args.get(6).toString() + '\n');
outStream.print("SECOND EXPERIMENTAL METHOD ENTERED: " + args.get(7).toString() + '\n');
outStream.print("THIRD EXPERIMENTAL METHOD ENTERED: " + args.get(8).toString() + '\n');
outStream.print("PUBMED ID (PMID) ENTERED: " + args.get(3).toString() + '\n');
outStream.flush();
outStream.close();
}
/**Method to parse a fasta formatted collection of sites into a vector of binding sites
* @param string fileName: name and directory of the collection file name
* @return none
* @author Joe Cornish
* @throws FileNotFoundException
*/
public static void parseFASTACollection(String fileName) throws FileNotFoundException{
File file = new File(fileName);
CharSequence FASTAcarrot = ">";
theSites = new Vector<BindingSite>();
String temp = new String();
Scanner scan = new Scanner(new FileReader(file));
while(scan.hasNext()){
temp = scan.nextLine();
if(temp.contains(FASTAcarrot)){
temp = scan.nextLine();
//need to check for invalid characters
if(!temp.matches("[^atcgATCG]")){
theSites.add(new BindingSite(temp));
}
else{throw new RuntimeException();}
}
}
scan.close();
}
public static void main(String[] args) throws NoSuchElementException, BioException, IOException {
WorkflowGUI theGUI = new WorkflowGUI();
do{
}while(!theGUI.isReady());
theGUI.closeGUIWindow();
}
/*************************************************************************************************************************/
/**
*
* @author www.java2s.com
* http://www.java2s.com/Code/JavaAPI/javax.swing/JFileChoosersetFileFilterFileFilterfilter.htm
*
*/
class ExtensionFileFilter extends FileFilter {
String description;
String extensions[];
public ExtensionFileFilter(String description, String extension) {
this(description, new String[] { extension });
}
public ExtensionFileFilter(String description, String extensions[]) {
if (description == null) {
this.description = extensions[0];
} else {
this.description = description;
}
this.extensions = (String[]) extensions.clone();
toLower(this.extensions);
}
private void toLower(String array[]) {
for (int i = 0, n = array.length; i < n; i++) {
array[i] = array[i].toLowerCase();
}
}
public String getDescription() {
return description;
}
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else {
String path = file.getAbsolutePath().toLowerCase();
for (int i = 0, n = extensions.length; i < n; i++) {
String extension = extensions[i];
if ((path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.')) {
return true;
}
}
}
return false;
}
}
/*************************************************************************************************************************/
//end class
}
|