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/GeneratorPane.java


1   
2   package com.eireneh.bible.book.swing;
3   
4   import java.io.*;
5   import java.util.*;
6   import java.awt.*;
7   import java.awt.event.*;
8   
9   import javax.swing.*;
10  import javax.swing.border.*;
11  
12  import com.eireneh.util.*;
13  import com.eireneh.swing.*;
14  import com.eireneh.bible.book.*;
15  
16  /**
17  * Bible Generator allows the creation of new Books - although it
18  * really only converts from one implementation of Book to another.
19  * This is needed because I drivers like JDBCBook and GBMLBook will not
20  * be very speed optimized.
21  * <p>Since this code has been edited by JBuilder I have changed it
22  * and expect problems if it is edited that way again. The code that JB
23  * created did not compile with JDK1.1 and Swing 1.1 because it uses a
24  * constructor special to AWT in JDK 1.2, So I have changed code that read
25  *   <code>new GridBagConstraints</code>
26  * to
27  *   <code>GuiUtil.getConstraints</code>
28  * to fix this.
29  *
30  * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
31  * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
32  * Distribution Licence:<br />
33  * Project B is free software; you can redistribute it
34  * and/or modify it under the terms of the GNU General Public License,
35  * version 2 as published by the Free Software Foundation.<br />
36  * This program is distributed in the hope that it will be useful,
37  * but WITHOUT ANY WARRANTY; without even the implied warranty of
38  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39  * General Public License for more details.<br />
40  * The License is available on the internet
41  * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
42  * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
43  * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
44  * The copyright to this program is held by it's authors.
45  * </font></td></tr></table>
46  * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
47  * @see docs.Licence
48  * @author Joe Walker
49  */
50  public class GeneratorPane extends EirPanel
51  {
52      /**
53      * Construct a Bible Generator tool, this simply calls jbInit
54      */
55      public GeneratorPane()
56      {
57          jbInit();
58      }
59  
60      /**
61      * Create the GUI components.
62      */
63      private void jbInit()
64      {
65          cbo_source.setModel(mdl_source);
66          lbl_source.setText("  Source Bible: ");
67          pnl_source.setLayout(new BorderLayout());
68          pnl_source.setBorder(BorderFactory.createTitledBorder("Source"));
69          pnl_source.add(lbl_source, BorderLayout.WEST);
70          pnl_source.add(cbo_source, BorderLayout.CENTER);
71  
72          lbl_name.setText("New Name:");
73          lbl_driver.setText("Driver Class:");
74  
75          cbo_driver.setModel(mdl_driver);
76          pnl_dest.setLayout(lay_dest);
77          pnl_dest.setBorder(BorderFactory.createTitledBorder("Destination"));
78          pnl_dest.add(lbl_name, GuiUtil.getConstraints(0, 1, 1, 1, 0.0, 0.0
79              ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
80          pnl_dest.add(lbl_driver, GuiUtil.getConstraints(0, 2, 1, 1, 0.0, 0.0
81              ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 0), 0, 0));
82          pnl_dest.add(txt_name, GuiUtil.getConstraints(1, 1, 1, 1, 1.0, 0.0
83              ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 5, 5), 0, 0));
84          pnl_dest.add(cbo_driver, GuiUtil.getConstraints(1, 2, 1, 1, 1.0, 0.0
85              ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 5, 5), 0, 0));
86  
87          bar_prog.setBorderPainted(true);
88          bar_prog.setMaximum(100);
89          bar_prog.setString("");
90          bar_prog.setStringPainted(true);
91          pnl_prog.setLayout(new BorderLayout());
92          pnl_prog.setBorder(BorderFactory.createTitledBorder("Progress"));
93          pnl_prog.add(bar_prog, BorderLayout.CENTER);
94  
95          box_main = Box.createVerticalBox();
96          box_main.add(pnl_source, null);
97          box_main.add(pnl_dest, null);
98  
99          btn_generate.addActionListener(new ActionListener() {
100             public void actionPerformed(ActionEvent ev) { generate(); }
101         });
102         btn_generate.setText("Generate");
103         btn_generate.setMnemonic('G');
104 
105         chk_verify.setText("Verify After Generation");
106         chk_verify.setMnemonic('V');
107         chk_verify.setSelected(false);
108         lay_buttons.setAlignment(FlowLayout.RIGHT);
109         pnl_buttons.setLayout(lay_buttons);
110         pnl_buttons.add(chk_verify, null);
111         pnl_buttons.add(btn_generate, null);
112 
113         this.setLayout(new BorderLayout());
114         this.add(box_main, BorderLayout.NORTH);
115         this.add(pnl_prog, BorderLayout.CENTER);
116         this.add(pnl_buttons, BorderLayout.SOUTH);
117     }
118 
119     /**
120      * Show this Panel in a new dialog
121      */
122     public void showInDialog(Component parent)
123     {
124         showInDialog(parent, "Generator", false);
125     }
126 
127     /**
128     * This allows up to easily display this component in a window and
129     * have the 2 work together on close actions and so on.
130     */
131     public void showInFrame(Frame parent)
132     {
133         final JDialog frame = new JDialog(parent, "Bible Generator");
134 
135         btn_close = new JButton("Close");
136         btn_close.setMnemonic('C');
137         btn_close.addActionListener(new ActionListener() {
138             public void actionPerformed(ActionEvent ev)
139             {
140                 frame.setVisible(false);
141                 frame.dispose();
142             }
143         });
144         pnl_buttons.add(btn_close, null);
145 
146         this.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
147 
148         frame.addWindowListener(new WindowAdapter() {
149             public void windowClosed(WindowEvent ev)
150             {
151                 if (work != null)
152                     work.interrupt();
153             }
154         });
155         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
156         frame.getContentPane().setLayout(new BorderLayout());
157         frame.getContentPane().add(this, BorderLayout.CENTER);
158 
159         frame.pack();
160         GuiUtil.centerWindow(frame);
161         frame.setVisible(true);
162     }
163 
164     /**
165     * Actually start generating the new Book
166     */
167     public void generate()
168     {
169         // New thread to do the real work
170         work = new Thread(new GeneratorRunnable());
171         work.start();
172         work.setPriority(Thread.MIN_PRIORITY);
173     }
174 
175     /** The list of available drivers */
176     private String[] drivers = null;
177 
178     /** Holder for the source and destination area */
179     private Box box_main;
180 
181     /** The Source area */
182     private JPanel pnl_source = new JPanel();
183 
184     /** The destination area */
185     private JPanel pnl_dest = new JPanel();
186 
187     /** The source book label */
188     private JLabel lbl_source = new JLabel();
189 
190     /** The source picker */
191     private JComboBox cbo_source = new JComboBox();
192 
193     /** The model for the sources */
194     private BiblesComboBoxModel mdl_source = new BiblesComboBoxModel();
195 
196     /** Layout for the destination panel */
197     private GridBagLayout lay_dest = new GridBagLayout();
198 
199     /** The new version name label */
200     private JLabel lbl_name = new JLabel();
201 
202     /** Label for the new driver class */
203     private JLabel lbl_driver = new JLabel();
204 
205     /** Input field for the new version */
206     private JTextField txt_name = new JTextField();
207 
208     /** Input field for the driver class */
209     private JComboBox cbo_driver = new JComboBox();
210 
211     /** The model for the drivers */
212     private DriversComboBoxModel mdl_driver = new DriversComboBoxModel();
213 
214     /** The progress area */
215     private JPanel pnl_prog = new JPanel();
216 
217     /** The progress bar */
218     private JProgressBar bar_prog = new JProgressBar();
219 
220     /** The button bar */
221     private JPanel pnl_buttons = new JPanel();
222 
223     /** Layout for the button bar */
224     private FlowLayout lay_buttons = new FlowLayout();
225 
226     /** The generate button */
227     private JButton btn_generate = new JButton();
228 
229     /** The close button, only used if we are in our own Frame */
230     private JButton btn_close = null;
231 
232     /** The verify checkbox */
233     private JCheckBox chk_verify = new JCheckBox();
234 
235     /** Work in progress */
236     private Thread work;
237 
238     /** The progress listener */
239     private CustomProgressListener cpl = new CustomProgressListener();
240 
241     /**
242     * A class to be run in a Thread to do the real work of generating the
243     * new Bible
244     */
245     class GeneratorRunnable implements Runnable
246     {
247         public void run()
248         {
249             // While we are working stop anyone editing the values
250             SwingUtilities.invokeLater(new Runnable() {
251                 public void run()
252                 {
253                     cbo_source.setEnabled(false);
254                     txt_name.setEnabled(false);
255                     cbo_driver.setEnabled(false);
256                     btn_generate.setEnabled(false);
257                     chk_verify.setEnabled(false);
258                     btn_close.setText("Cancel");
259                 }
260             });
261 
262             try
263             {
264                 // Get the values
265                 Bible source = mdl_source.getSelectedBible();
266                 String dest_name = txt_name.getText();
267                 BibleDriver dest_driver = mdl_driver.getSelectedDriver();
268 
269                 // The real work
270                 Bible dest_version = Bibles.createBible(dest_name, dest_driver);
271                 dest_version.addProgressListener(cpl);
272                 dest_version.generate(source);
273                 dest_version.removeProgressListener(cpl);
274 
275                 // Now it MAY make sense to do something like:
276                 //   versions.put(dest_name, dest_version);
277                 // However I think we should be cautious and force the system to re-create it
278 
279                 // Check
280                 if (chk_verify.isEnabled())
281                 {
282                     Verifier ver = new Verifier();
283                     ver.setBible1(source);
284                     ver.setBible2(dest_version);
285 
286                     CompareResultsPane results = new CompareResultsPane(ver);
287                     results.setCheckText("");
288                     results.setCheckPassages(Verifier.WHOLE);
289                     results.showInFrame(GuiUtil.getFrame(GeneratorPane.this));
290                     results.startStop();
291                 }
292             }
293             catch (final Exception ex)
294             {
295                 SwingUtilities.invokeLater(new Runnable() {
296                     public void run()
297                     {
298                         ExceptionPane.showExceptionDialog(GeneratorPane.this, ex);
299                     }
300                 });
301             }
302 
303             // Re-enable the values
304             SwingUtilities.invokeLater(new Runnable() {
305                 public void run()
306                 {
307                     cbo_source.setEnabled(true);
308                     txt_name.setEnabled(true);
309                     cbo_driver.setEnabled(true);
310                     btn_generate.setEnabled(true);
311                     chk_verify.setEnabled(true);
312                     btn_close.setText("Close");
313                 }
314             });
315         }
316     }
317 
318     /**
319     * Report progress changes to the screen
320     */
321     class CustomProgressListener implements ProgressListener
322     {
323         /**
324         * This method is called to indicate that some progress has been made.
325         * The amount of progress is indicated by ev.getPercent()
326         * @param ev Describes the progress
327         */
328         public void progressMade(final ProgressEvent ev)
329         {
330             SwingUtilities.invokeLater(new Runnable() {
331                 public void run()
332                 {
333                     int percent = ev.getPercent();
334                     bar_prog.setString(ev.getDescription()+" "+percent+"%");
335                     bar_prog.setValue(percent);
336                 }
337             });
338         }
339     }
340 }