Source code: com/eireneh/bible/book/swing/MaintenancePane.java
1
2 package com.eireneh.bible.book.swing;
3
4 import java.net.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.*;
8 import javax.swing.event.*;
9
10 import com.eireneh.util.*;
11 import com.eireneh.config.*;
12 import com.eireneh.config.swing.*;
13 import com.eireneh.swing.*;
14 import com.eireneh.bible.book.*;
15
16 /**
17 * Allows various maintenance procedures to be done on Bibles like
18 * deletion, renaming, and viewing the notes that are associated with the
19 * generation of a version.
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 MaintenancePane extends EirPanel
42 {
43 /**
44 * Create a new BibleMaintenance Panel
45 */
46 public MaintenancePane()
47 {
48 jbInit();
49 updateButtons();
50 }
51
52 /**
53 * Ensure that the buttons are enabled correctly
54 */
55 private void updateButtons()
56 {
57 boolean selected = (lst_versions.getSelectedIndex() != -1);
58 btn_remove.setEnabled(selected);
59 btn_props.setEnabled(selected);
60 btn_rename.setEnabled(selected);
61 }
62
63 /**
64 * Show this Panel in a new dialog
65 */
66 public void showInDialog(Component parent)
67 {
68 showInDialog(parent, "Version Maintenance", false);
69 }
70
71 /**
72 * Create the GUI
73 */
74 private void jbInit()
75 {
76 scr_versions.getViewport().add(lst_versions, null);
77 lst_versions.setModel(mdl_versions);
78 lst_versions.setCellRenderer(new BibleListCellRenderer());
79 lst_versions.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
80 lst_versions.addListSelectionListener(new ListSelectionListener()
81 {
82 public void valueChanged(ListSelectionEvent ev) { updateButtons(); }
83 });
84 lst_versions.addMouseListener(new MouseAdapter()
85 {
86 public void mouseClicked(MouseEvent ev)
87 {
88 if (ev.getClickCount() == 2)
89 properties();
90 }
91 });
92
93 btn_add.setText("Add ...");
94 btn_add.setMnemonic('A');
95 btn_add.addActionListener(new ActionListener()
96 {
97 public void actionPerformed(ActionEvent ev) { add(); }
98 });
99
100 btn_remove.setText("Remove");
101 btn_remove.setMnemonic('R');
102 btn_remove.addActionListener(new ActionListener()
103 {
104 public void actionPerformed(ActionEvent ev) { delete(); }
105 });
106
107 btn_props.setText("Properties ...");
108 btn_props.setMnemonic('P');
109 btn_props.addActionListener(new ActionListener()
110 {
111 public void actionPerformed(ActionEvent ev) { properties(); }
112 });
113
114 btn_rename.setText("Rename");
115 btn_rename.setMnemonic('R');
116 btn_rename.addActionListener(new ActionListener()
117 {
118 public void actionPerformed(ActionEvent ev) { rename(); }
119 });
120
121 lay_buttons.setAlignment(FlowLayout.RIGHT);
122 pnl_buttons.setLayout(lay_buttons);
123 pnl_buttons.add(btn_add, null);
124 pnl_buttons.add(btn_remove, null);
125 pnl_buttons.add(btn_props, null);
126 pnl_buttons.add(btn_rename, null);
127
128 this.setLayout(new BorderLayout());
129 this.add(scr_versions, BorderLayout.CENTER);
130 this.add(pnl_buttons, BorderLayout.SOUTH);
131 }
132
133 /**
134 * Create a new Bible
135 */
136 public void add()
137 {
138 GeneratorPane vergen = new GeneratorPane();
139 vergen.showInFrame(GuiUtil.getFrame(this));
140 }
141
142 /**
143 * Rename the selected Bible
144 */
145 public void rename()
146 {
147 try
148 {
149 String old_name = getSelected();
150 if (old_name == null)
151 {
152 JOptionPane.showMessageDialog(this,
153 "Please select a Bible to rename.",
154 "Delete Bible",
155 JOptionPane.INFORMATION_MESSAGE);
156 return;
157 }
158
159 Object data = JOptionPane.showInputDialog(this,
160 "Enter new name for the Bible formerly known as "+old_name,
161 "Rename Bible",
162 JOptionPane.QUESTION_MESSAGE,
163 null,
164 null,
165 old_name);
166
167 if (data == null)
168 return;
169
170 String new_name = data.toString().trim();
171 Bibles.renameBible(old_name, new_name);
172 }
173 catch (Exception ex)
174 {
175 Reporter.informUser(this, ex);
176 }
177 }
178
179 /**
180 * Delete a selected Bible
181 */
182 public void delete()
183 {
184 try
185 {
186 String name = getSelected();
187 if (name == null)
188 {
189 JOptionPane.showMessageDialog(this,
190 "Please select a Bible to delete.",
191 "Delete Bible",
192 JOptionPane.INFORMATION_MESSAGE);
193 return;
194 }
195
196 if (JOptionPane.showConfirmDialog(this,
197 "Are you sure you want to delete "+name+"?\nDeleted Bibles can not be recovered",
198 "Delete Bible",
199 JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
200 {
201 Bibles.deleteBible(name);
202 }
203 }
204 catch (Exception ex)
205 {
206 Reporter.informUser(this, ex);
207 }
208 }
209
210 /**
211 * View the properties for a selected Bible
212 */
213 public void properties()
214 {
215 try
216 {
217 String name = getSelected();
218 if (name == null)
219 {
220 JOptionPane.showMessageDialog(this,
221 "Please select a Bible to view notes on.",
222 "Delete Bible",
223 JOptionPane.INFORMATION_MESSAGE);
224
225 return;
226 }
227
228 Bible version = Bibles.getBible(name);
229 Config config = version.getProperties();
230
231 if (config == null)
232 {
233 JOptionPane.showMessageDialog(this,
234 "There are no options to configure for the "+name+" Bible.",
235 "Bible Properties",
236 JOptionPane.INFORMATION_MESSAGE);
237 }
238 else
239 {
240 // The visuals for the display class
241 SwingConfig.setDisplayClass(TabbedConfigPane.class);
242
243 // Where do we save the choices?
244 URL prop_url = version.getPropertiesURL();
245
246 if (prop_url == null)
247 {
248 // Show the dialog, and don't bother to save.
249 SwingConfig.showDialog(config, this, new ActionListener() {
250 public void actionPerformed(ActionEvent ev) { }
251 });
252 }
253 else
254 {
255 // Show the dialog, with a place to save to.
256 SwingConfig.showDialog(config, this, prop_url);
257 }
258 }
259 }
260 catch (Exception ex)
261 {
262 Reporter.informUser(this, ex);
263 }
264 }
265
266 /**
267 * What is the selected Bible name?
268 * @return The version name or null if none is selected
269 */
270 private String getSelected()
271 {
272 Object selected = lst_versions.getSelectedValue();
273 if (selected == null)
274 return null;
275
276 return mdl_versions.getBibleName(selected);
277
278 /*
279 // To have versions displayed as "name (driver)"
280 String list = selected.toString();
281 int space = list.indexOf(" ");
282 return list.substring(0, space);
283 */
284 }
285
286 /** The version list scroller */
287 private JScrollPane scr_versions = new JScrollPane();
288
289 /** The version list */
290 private JList lst_versions = new JList();
291
292 /** The BiblesModel for the list */
293 private BiblesComboBoxModel mdl_versions = new BiblesComboBoxModel();
294
295 /** The button bar */
296 private JPanel pnl_buttons = new JPanel();
297
298 /** View Notes button */
299 private JButton btn_add = new JButton();
300
301 /** View Notes button */
302 private JButton btn_props = new JButton();
303
304 /** Delete Bible button */
305 private JButton btn_remove = new JButton();
306
307 /** Rename Bible button */
308 private JButton btn_rename = new JButton();
309
310 /** Layout for the button bar */
311 private FlowLayout lay_buttons = new FlowLayout();
312 }