Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/eireneh/bible/book/swing/CompareResultsPane.java


1   
2   package com.eireneh.bible.book.swing;
3   
4   import java.io.*;
5   import java.awt.*;
6   import java.awt.event.*;
7   
8   import javax.swing.*;
9   import javax.swing.border.*;
10  import javax.swing.text.*;
11  
12  import com.eireneh.util.*;
13  import com.eireneh.swing.*;
14  import com.eireneh.bible.book.*;
15  import com.eireneh.bible.passage.*;
16  
17  /**
18  * This displays the results of a comparision that occurs in a separate
19  * thread.
20  *
21  * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
22  * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
23  * Distribution Licence:<br />
24  * Project B is free software; you can redistribute it
25  * and/or modify it under the terms of the GNU General Public License,
26  * version 2 as published by the Free Software Foundation.<br />
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30  * General Public License for more details.<br />
31  * The License is available on the internet
32  * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
33  * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34  * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
35  * The copyright to this program is held by it's authors.
36  * </font></td></tr></table>
37  * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
38  * @see docs.Licence
39  * @author Joe Walker
40  */
41  public class CompareResultsPane extends JPanel implements Runnable
42  {
43      /**
44      * Basic Constructor
45      */
46      public CompareResultsPane(Verifier ver)
47      {
48          this.ver = ver;
49          jbInit();
50      }
51  
52      /**
53      * Create the GUI
54      */
55      private void jbInit()
56      {
57          setTitles();
58          box_bibles = Box.createVerticalBox();
59          box_bibles.add(lbl_bible1, null);
60          box_bibles.add(lbl_bible2, null);
61  
62          bar_progress.setString("");
63          bar_progress.setStringPainted(true);
64          txt_results.setRows(5);
65          txt_results.setColumns(40);
66          scr_results.getViewport().add(txt_results, null);
67          pnl_results.setLayout(new BorderLayout(5, 5));
68          pnl_results.setBorder(new TitledBorder("Results"));
69          pnl_results.add(scr_results, BorderLayout.CENTER);
70          pnl_results.add(bar_progress, BorderLayout.NORTH);
71  
72          btn_stop.setMnemonic('S');
73          btn_stop.setText("Start");
74          btn_stop.addActionListener(new ActionListener()
75          {
76              public void actionPerformed(ActionEvent ev) { startStop(); }
77          });
78          pnl_buttons.add(btn_stop, null);
79  
80          this.setLayout(new BorderLayout());
81          this.add(box_bibles, BorderLayout.NORTH);
82          this.add(pnl_results, BorderLayout.CENTER);
83          this.add(pnl_buttons, BorderLayout.SOUTH);
84      }
85  
86      /**
87      * This allows up to easily display this component in a window and
88      * have the 2 work together on close actions and so on.
89      */
90      public void showInFrame(Frame parent)
91      {
92          final JDialog frame = new JDialog(parent, "Verify Results");
93  
94          btn_close = new JButton("Close");
95          btn_close.setMnemonic('C');
96          btn_close.addActionListener(new ActionListener() {
97              public void actionPerformed(ActionEvent ev)
98              {
99                  if (work != null) startStop();
100                 frame.setVisible(false);
101                 frame.dispose();
102             }
103         });
104         pnl_buttons.add(btn_close, null);
105 
106         this.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
107 
108         frame.addWindowListener(new WindowAdapter() {
109             public void windowClosed(WindowEvent ev)
110             {
111                 if (work != null) startStop();
112             }
113         });
114         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
115         frame.getContentPane().setLayout(new BorderLayout());
116         frame.getContentPane().add(this, BorderLayout.CENTER);
117 
118         frame.pack();
119         GuiUtil.centerWindow(frame);
120         frame.setVisible(true);
121     }
122 
123     /**
124     * Start running the tests
125     */
126     public void startStop()
127     {
128         if (work == null)
129         {
130             // New thread to do the real work
131             work = new Thread(this);
132             work.start();
133             work.setPriority(Thread.MIN_PRIORITY);
134         }
135         else
136         {
137             alive = false;
138             ver.stopChecking();
139             work = null;
140         }
141     }
142 
143     /**
144     * The text that we will check, null for no check, we apply startsWith
145     * to the given word before we run the check.
146     */
147     public void setCheckText(String check_text)
148     {
149         this.check_text = check_text;
150         setTitles();
151     }
152 
153     /**
154     * The Passage that we will check, null for no check.
155     */
156     public void setCheckPassages(Passage check_ref)
157     {
158         this.check_ref = check_ref;
159         setTitles();
160     }
161 
162     /**
163     * Set the title of the pane to what we are doing
164     */
165     private void setTitles()
166     {
167         lbl_bible1.setText("<html><b>Bibles:</b> "+
168                            ver.getBible1().getName()+" / "+
169                            ver.getBible2().getName());
170 
171         String compare = "<html><b>Comparing:</b> ";
172         if (check_ref != null)
173             compare += "Passage="+check_ref+" ";
174 
175         if (check_text != null)
176             compare += "Word="+(check_text.equals("") ? "*" : check_text);
177 
178         lbl_bible2.setText(compare);
179     }
180 
181     /**
182     * A class to be run in a Thread to do the real work of comparing the
183     * selected Bibles
184     */
185     public void run()
186     {
187         // While we are working stop anyone editing the values
188         SwingUtilities.invokeLater(new Runnable() {
189             public void run()
190             {
191                 btn_stop.setText("Stop");
192             }
193         });
194 
195         Document doc = txt_results.getDocument();
196         dout.setDocument(doc);
197         PrintWriter out = new PrintWriter(dout);
198         alive = true;
199 
200         try
201         {
202             ver.addProgressListener(cpl);
203 
204             if (check_text != null && check_text.equals("") && alive)
205                 ver.checkPassage(check_text, out);
206 
207             if (check_ref != null && check_ref.isEmpty() && alive)
208                 ver.checkText(check_ref, out);
209         }
210         catch (final Exception ex)
211         {
212             SwingUtilities.invokeLater(new Runnable() {
213                 public void run()
214                 {
215                     ExceptionPane.showExceptionDialog(CompareResultsPane.this, ex);
216                 }
217             });
218         }
219         finally
220         {
221             ver.removeProgressListener(cpl);
222         }
223 
224         // Re-enable the values
225         SwingUtilities.invokeLater(new Runnable() {
226             public void run()
227             {
228                 btn_stop.setText("Start");
229             }
230         });
231     }
232 
233     /** Are we being told to die */
234     private boolean alive = true;
235 
236     /** The text to check */
237     private String check_text = null;
238 
239     /** The passage to check */
240     private Passage check_ref = null;
241 
242     /** The Bible verifier */
243     private Verifier ver;
244 
245     /** The DocumentWriter that the comparison can write to */
246     private DocumentWriter dout = new DocumentWriter();
247 
248     /** Work in progress */
249     private Thread work;
250 
251     /** The progress listener */
252     private CustomProgressListener cpl = new CustomProgressListener();
253 
254     /* GUI components */
255     private JPanel pnl_results = new JPanel();
256     private JScrollPane scr_results = new JScrollPane();
257     private JTextArea txt_results = new JTextArea();
258     private JProgressBar bar_progress = new JProgressBar();
259     private Box box_bibles;
260     private JLabel lbl_bible1 = new JLabel();
261     private JPanel pnl_buttons = new JPanel();
262     private JButton btn_stop = new JButton();
263     private JButton btn_close = null;
264     private JLabel lbl_bible2 = new JLabel();
265 
266     /**
267     * Report progress changes to the screen
268     */
269     class CustomProgressListener implements ProgressListener
270     {
271         /**
272         * This method is called to indicate that some progress has been made.
273         * The amount of progress is indicated by ev.getPercent()
274         * @param ev Describes the progress
275         */
276         public void progressMade(final ProgressEvent ev)
277         {
278             SwingUtilities.invokeLater(new Runnable() {
279                 public void run()
280                 {
281                     int percent = ev.getPercent();
282                     bar_progress.setString(ev.getDescription()+" "+percent+"%");
283                     bar_progress.setValue(percent);
284                 }
285             });
286         }
287     }
288 }