~sword-devel/jsword/trunk

« back to all changes in this revision

Viewing changes to jsword/java/historic/org/crosswire/bible/view/swing/test/TesterPane.java

  • Committer: joe
  • Date: 2002-10-08 21:36:18 UTC
  • Revision ID: svn-v4:a88caf3b-7e0a-0410-8d0d-cecb45342206:trunk:80
big config and comment update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
package org.crosswire.bible.view.swing.test;
3
 
 
4
 
import java.awt.BorderLayout;
5
 
import java.awt.Component;
6
 
import java.awt.FlowLayout;
7
 
import java.awt.event.ActionEvent;
8
 
import java.awt.event.ActionListener;
9
 
import java.io.PrintWriter;
10
 
import java.util.Hashtable;
11
 
 
12
 
import javax.swing.BorderFactory;
13
 
import javax.swing.JButton;
14
 
import javax.swing.JList;
15
 
import javax.swing.JPanel;
16
 
import javax.swing.JScrollPane;
17
 
import javax.swing.JSplitPane;
18
 
import javax.swing.JTextArea;
19
 
 
20
 
import org.crosswire.bible.control.test.TestList;
21
 
import org.crosswire.swing.DocumentWriter;
22
 
import org.crosswire.swing.EirPanel;
23
 
import org.crosswire.util.StringUtil;
24
 
import org.crosswire.util.TestBase;
25
 
 
26
 
/**
27
 
* Tester creates a list of the tests that can be run on the system,
28
 
* allows the user to select which of the tests to run, and then
29
 
* kicks them off in a separate thread.
30
 
*
31
 
* <table border='1' cellPadding='3' cellSpacing='0' width="100%">
32
 
* <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
33
 
* Distribution Licence:<br />
34
 
* Project B is free software; you can redistribute it
35
 
* and/or modify it under the terms of the GNU General Public License,
36
 
* version 2 as published by the Free Software Foundation.<br />
37
 
* This program is distributed in the hope that it will be useful,
38
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
39
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
40
 
* General Public License for more details.<br />
41
 
* The License is available on the internet
42
 
* <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
43
 
* <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
44
 
* MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
45
 
* The copyright to this program is held by it's authors.
46
 
* </font></td></tr></table>
47
 
* @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
48
 
* @see <{docs.Licence}>
49
 
* @author Joe Walker
50
 
*/
51
 
public class TesterPane extends EirPanel
52
 
{
53
 
    /**
54
 
    * Construct a Test tool, this simply calls jbInit
55
 
    */
56
 
    public TesterPane()
57
 
    {
58
 
        jbInit();
59
 
    }
60
 
 
61
 
    /**
62
 
    * Create the GUI components. Possible optimization - start the split
63
 
    * pane with only the list showing, and then bring the text area into
64
 
    * view when the results start appearing.
65
 
    */
66
 
    private void jbInit()
67
 
    {
68
 
        lst_tests.setVisibleRowCount(5);
69
 
        scr_tests.getViewport().add(lst_tests, null);
70
 
        pnl_tests.setBorder(BorderFactory.createTitledBorder("Available Tests"));
71
 
        pnl_tests.setLayout(new BorderLayout());
72
 
        pnl_tests.add(scr_tests, BorderLayout.CENTER);
73
 
 
74
 
        txt_results.setRows(10);
75
 
        txt_results.setEditable(false);
76
 
        scr_results.getViewport().add(txt_results, null);
77
 
        pnl_results.setBorder(BorderFactory.createTitledBorder("Test Results"));
78
 
        pnl_results.setLayout(new BorderLayout());
79
 
        pnl_results.add(scr_results, BorderLayout.CENTER);
80
 
 
81
 
        spl_main.setContinuousLayout(true);
82
 
        spl_main.setBorder(BorderFactory.createEmptyBorder());
83
 
        spl_main.setOrientation(JSplitPane.VERTICAL_SPLIT);
84
 
        spl_main.add(pnl_tests, JSplitPane.TOP);
85
 
        spl_main.add(pnl_results, JSplitPane.BOTTOM);
86
 
 
87
 
        btn_test.addActionListener(new ActionListener() {
88
 
            public void actionPerformed(ActionEvent ev)
89
 
            {
90
 
                test();
91
 
            }
92
 
        });
93
 
        btn_test.setText("Run Tests");
94
 
        btn_test.setMnemonic('R');
95
 
        btn_clear.addActionListener(new ActionListener() {
96
 
            public void actionPerformed(ActionEvent ev)
97
 
            {
98
 
                clear();
99
 
            }
100
 
        });
101
 
        btn_clear.setText("Clear Results");
102
 
        btn_clear.setMnemonic('C');
103
 
        lay_buttons.setAlignment(FlowLayout.RIGHT);
104
 
        pnl_buttons.setLayout(lay_buttons);
105
 
        pnl_buttons.add(btn_test, null);
106
 
        pnl_buttons.add(btn_clear, null);
107
 
 
108
 
        this.setLayout(new BorderLayout());
109
 
        this.add(spl_main, BorderLayout.CENTER);
110
 
        this.add(pnl_buttons, BorderLayout.SOUTH);
111
 
    }
112
 
 
113
 
    /**
114
 
     * Show this Panel in a new dialog
115
 
     */
116
 
    public void showInDialog(Component parent)
117
 
    {
118
 
        showInDialog(parent, "Unit Tests", false);
119
 
    }
120
 
 
121
 
    /**
122
 
    * Run the tests selected in a separate thread, placing the results
123
 
    * in the results window.
124
 
    */
125
 
    public void test()
126
 
    {
127
 
        Object[] objs = lst_tests.getSelectedValues();
128
 
        String[] selected = StringUtil.getStringArray(objs);
129
 
        TestThread runnable = new TestThread(selected);
130
 
 
131
 
        Thread work = new Thread(runnable);
132
 
        work.setPriority(Thread.MIN_PRIORITY);
133
 
        work.start();
134
 
    }
135
 
 
136
 
    /**
137
 
    * Clear the test results window
138
 
    */
139
 
    public void clear()
140
 
    {
141
 
        txt_results.setText("");
142
 
    }
143
 
 
144
 
    /** The packages to be tested */
145
 
    private Hashtable testers = TestList.getTesters();
146
 
 
147
 
    /** The button bar at the bottom of the panel */
148
 
    private JPanel pnl_buttons = new JPanel();
149
 
 
150
 
    /** The layout for the button bar */
151
 
    private FlowLayout lay_buttons = new FlowLayout();
152
 
 
153
 
    /** The run tests button */
154
 
    private JButton btn_test = new JButton();
155
 
 
156
 
    /** The clear tests results button */
157
 
    private JButton btn_clear = new JButton();
158
 
 
159
 
    /** The central split pane */
160
 
    private JSplitPane spl_main = new JSplitPane();
161
 
 
162
 
    /** Panel holding the test scroller */
163
 
    private JPanel pnl_tests = new JPanel();
164
 
 
165
 
    /** Scroller for the test list */
166
 
    private JScrollPane scr_tests = new JScrollPane();
167
 
 
168
 
    /** The list of available test */
169
 
    private JList lst_tests = new JList(TestList.getNames());
170
 
 
171
 
    /** Panel holding the result scroller */
172
 
    private JPanel pnl_results = new JPanel();
173
 
 
174
 
    /** Scroller for the result text area */
175
 
    private JScrollPane scr_results = new JScrollPane();
176
 
 
177
 
    /** The text output from the tests */
178
 
    private JTextArea txt_results = new JTextArea();
179
 
 
180
 
    /**
181
 
    * A thread for running tests in
182
 
    */
183
 
    class TestThread implements Runnable
184
 
    {
185
 
        /**
186
 
        * Configure a TestThread with a set of tests to run
187
 
        */
188
 
        TestThread(String[] selected)
189
 
        {
190
 
            this.selected = selected;
191
 
        }
192
 
 
193
 
        /**
194
 
        * Run the configured tests
195
 
        */
196
 
        public void run()
197
 
        {
198
 
            btn_test.setEnabled(false);
199
 
            btn_clear.setEnabled(false);
200
 
 
201
 
            PrintWriter out = new PrintWriter(new DocumentWriter(txt_results.getDocument()));
202
 
 
203
 
            for (int i=0; i<selected.length; i++)
204
 
            {
205
 
                TestBase test = (TestBase) testers.get(selected[i]);
206
 
                test.test(out, false);
207
 
            }
208
 
 
209
 
            btn_clear.setEnabled(true);
210
 
            btn_test.setEnabled(true);
211
 
        }
212
 
 
213
 
        /** The tests to run */
214
 
        private String[] selected;
215
 
    }
216
 
}
217