001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.structure.align.gui;
022
023import org.biojava.nbio.structure.align.FarmJob;
024import org.biojava.nbio.structure.align.events.AlignmentProgressListener;
025
026import javax.swing.*;
027import java.awt.*;
028import java.awt.event.ActionEvent;
029import java.awt.event.ActionListener;
030
031/** a GUI that allows to watch progress as multiple alignments are being processed.
032 *
033 * @author Andreas Prlic
034 *
035 */
036public class GUIAlignmentProgressListener extends JPanel implements AlignmentProgressListener, ActionListener {
037
038
039
040
041        /**
042         *
043         */
044        private static final long serialVersionUID = 1L;
045
046        int alignmentsProcessed;
047
048        JProgressBar progressBar;
049        private JTextArea taskOutput;
050        private JButton stopButton;
051
052        FarmJob farmJob;
053
054        public GUIAlignmentProgressListener(){
055
056                super(new BorderLayout());
057                stopButton = new JButton("Stop");
058                stopButton.setActionCommand("Stop");
059                stopButton.addActionListener(this);
060
061                progressBar = new JProgressBar(0, 100);
062                progressBar.setValue(0);
063                progressBar.setStringPainted(true);
064
065                taskOutput = new JTextArea(5, 20);
066                taskOutput.setMargin(new Insets(5,5,5,5));
067                taskOutput.setEditable(false);
068
069                JPanel panel = new JPanel();
070                panel.add(stopButton);
071                panel.add(progressBar);
072
073                add(panel, BorderLayout.PAGE_START);
074                add(new JScrollPane(taskOutput), BorderLayout.CENTER);
075                setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
076
077        }
078
079
080
081         public FarmJob getFarmJob() {
082                return farmJob;
083        }
084
085
086
087        public void setFarmJob(FarmJob parent) {
088                this.farmJob = parent;
089        }
090
091
092
093        /**
094         * Invoked when the user presses the stop button.
095         */
096        @Override
097        public void actionPerformed(ActionEvent evt) {
098
099                //System.out.println("stopping!");
100                logStatus("terminating");
101                logStatus(" Total alignments processed: " + alignmentsProcessed);
102                stopButton.setEnabled(false);
103                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
104                progressBar.setIndeterminate(true);
105                progressBar.setStringPainted(false);
106                System.out.println("terminating jobs");
107
108                farmJob.terminate();
109
110                setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
111                progressBar.setIndeterminate(false);
112
113        }
114
115
116        @Override
117        public void alignmentEnded() {
118
119                alignmentsProcessed++;
120
121                //System.out.println("aligned " + alignmentsProcessed );
122                int v = progressBar.getValue();
123
124                progressBar.setValue(v+1);
125                progressBar.setString(v+"");
126                synchronized(this){notifyAll();}
127
128        }
129
130        @Override
131        public void alignmentStarted(String name1, String name2) {
132                logStatus("#" + progressBar.getValue() + " starting alignment of " + name1 + " " + name2);
133        }
134
135        @Override
136        public void downloadingStructures(String name) {
137                logStatus("Downloading " + name );
138        }
139
140        @Override
141        public void logStatus(String message) {
142                taskOutput.append(message+"\n");
143        }
144
145        @Override
146        public void requestingAlignmentsFromServer(int nrAlignments) {
147                logStatus("Requesting " + nrAlignments + " alignments to be calculated");
148                progressBar.setMaximum(nrAlignments);
149                progressBar.setValue(0);
150                synchronized(this){notifyAll();}
151
152        }
153
154        @Override
155        public void sentResultsToServer(int nrAlignments, String serverMessage) {
156                logStatus("sent alignment results back to server. Server responded: >"+serverMessage+"<");
157                progressBar.setValue(0);
158                synchronized(this){notifyAll();}
159        }
160
161
162}