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


1   
2   package com.eireneh.bible.book.swing;
3   
4   import java.awt.*;
5   import java.awt.event.*;
6   import javax.swing.*;
7   import javax.swing.event.*;
8   
9   import com.eireneh.bible.book.*;
10  import com.eireneh.util.*;
11  
12  /**
13  * BibleChooser is like JFileChooser except that it allows the user to
14  * select one of the available Bibles.
15  *
16  * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
17  * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
18  * Distribution Licence:<br />
19  * Project B is free software; you can redistribute it
20  * and/or modify it under the terms of the GNU General Public License,
21  * version 2 as published by the Free Software Foundation.<br />
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25  * General Public License for more details.<br />
26  * The License is available on the internet
27  * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
28  * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29  * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
30  * The copyright to this program is held by it's authors.
31  * </font></td></tr></table>
32  * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
33  * @see docs.Licence
34  * @author Joe Walker
35  * @version D0.I0.T0
36  */
37  public class BibleChooser extends JPanel
38  {
39      /**
40      * Basic constructor
41      */
42      public BibleChooser()
43      {
44          jbInit();
45      }
46  
47      /**
48      * Initializa all the GUI components
49      */
50      private void jbInit()
51      {
52          pnl_bibles.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
53          pnl_bibles.setLayout(new BorderLayout());
54          pnl_bibles.add(scr_bibles, BorderLayout.CENTER);
55          scr_bibles.setViewportView(lst_bibles);
56          lst_bibles.setModel(bmod);
57          lst_bibles.setCellRenderer(new BibleListCellRenderer());
58          lst_bibles.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
59          lst_bibles.addListSelectionListener(new ListSelectionListener() {
60              public void valueChanged(ListSelectionEvent ev) { selection(); }
61          });
62  
63          btn_ok.setText("OK");
64          btn_ok.setMnemonic('o');
65          btn_ok.addActionListener(new ActionListener() {
66              public void actionPerformed(ActionEvent ev) { ok(); }
67          });
68          btn_ok.setEnabled(selected != null);
69          btn_ok.setDefaultCapable(true);
70  
71          btn_cancel.setText("Cancel");
72          btn_cancel.setMnemonic('C');
73          btn_cancel.addActionListener(new ActionListener() {
74              public void actionPerformed(ActionEvent ev) { cancel(); }
75          });
76  
77          btn_help.setText("Help");
78          btn_help.setMnemonic('H');
79          btn_help.addActionListener(new ActionListener() {
80              public void actionPerformed(ActionEvent ev) { help(); }
81          });
82          btn_help.setEnabled(false);
83  
84          pnl_buttons.setLayout(new FlowLayout());
85          pnl_buttons.add(btn_ok);
86          pnl_buttons.add(btn_cancel);
87          pnl_buttons.add(btn_help);
88  
89          this.setLayout(new BorderLayout());
90          this.add(pnl_bibles, BorderLayout.CENTER);
91          this.add(pnl_buttons, BorderLayout.SOUTH);
92      }
93  
94      /**
95      * Display the BibleChooser in a modal dialog
96      */
97      public int showDialog(Component parent)
98      {
99          Frame frame = (parent instanceof Frame)
100                       ? (Frame) parent
101                       : (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);
102 
103         dialog = new JDialog(frame, title, true);
104 
105         dialog.getContentPane().setLayout(new BorderLayout());
106         dialog.getContentPane().add(this, BorderLayout.CENTER);
107         dialog.pack();
108         dialog.setLocationRelativeTo(parent);
109         dialog.addWindowListener(new WindowAdapter() {
110             public void windowClosed(WindowEvent ev) { cancel(); }
111         });
112 
113         dialog.setVisible(true);
114 
115         return reply;
116     }
117 
118     /**
119     * Sets the string that goes in the FileChooser window's title bar.
120     * @see #getDialogTitle
121     */
122     public void setDialogTitle(String title)
123     {
124         this.title = title;
125     }
126 
127     /**
128     * Gets the string that goes in the FileChooser's titlebar.
129     * @see #setDialogTitle
130     */
131     public String getDialogTitle()
132     {
133         return title;
134     }
135 
136     /**
137     * Returns the selected Bible.
138     * @return the selected Bible
139     */
140     public Bible getSelectedBible()
141     {
142         try
143         {
144             String name = bmod.getBibleName(lst_bibles.getSelectedValue());
145             return Bibles.getBible(name);
146         }
147         catch (BookException ex)
148         {
149             throw new LogicError(ex);
150         }
151     }
152 
153     /**
154     * When the list selection changes
155     */
156     public void selection()
157     {
158         selected = (String) lst_bibles.getSelectedValue();
159         btn_ok.setEnabled(selected != null);
160     }
161 
162     /**
163     * OK is selected
164     */
165     public void ok()
166     {
167         reply = APPROVE_OPTION;
168         dialog.setVisible(false);
169     }
170 
171     /**
172     * Cancel is selected
173     */
174     public void cancel()
175     {
176         reply = CANCEL_OPTION;
177         dialog.setVisible(false);
178     }
179 
180     /**
181     * Not implemented
182     */
183     public void help()
184     {
185     }
186 
187     /** Return value if cancel is chosen */
188     public static final int CANCEL_OPTION = 1;
189 
190     /** Return value if approve (yes, ok) is chosen */
191     public static final int APPROVE_OPTION = 0;
192 
193     /** Return value if an error occured */
194     public static final int ERROR_OPTION = -1;
195 
196     /** The name of the selected Bible */
197     private String selected = null;
198 
199     /** The way the dialog was closed */
200     private int reply = CANCEL_OPTION;
201 
202     /** The title of the dialog */
203     private String title = "Select a Bible";
204 
205     /** The Bible list model */
206     private BiblesListModel bmod = new BiblesListModel();;
207 
208     /* GUI Componenets */
209     private JDialog dialog;
210     private JPanel pnl_bibles = new JPanel();
211     private JScrollPane scr_bibles = new JScrollPane();
212     private JList lst_bibles = new JList();
213 
214     private JPanel pnl_buttons = new JPanel();
215     private JButton btn_ok = new JButton();
216     private JButton btn_cancel = new JButton();
217     private JButton btn_help = new JButton();
218 }