1
22 package org.crosswire.bibledesktop.book.install;
23
24 import java.awt.BorderLayout;
25 import java.awt.Component;
26 import java.awt.Dimension;
27 import java.awt.FlowLayout;
28 import java.awt.Frame;
29 import java.awt.GridBagConstraints;
30 import java.awt.GridBagLayout;
31 import java.awt.Insets;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.KeyEvent;
35 import java.io.IOException;
36 import java.io.ObjectInputStream;
37
38 import javax.swing.BorderFactory;
39 import javax.swing.JButton;
40 import javax.swing.JComboBox;
41 import javax.swing.JComponent;
42 import javax.swing.JDialog;
43 import javax.swing.JLabel;
44 import javax.swing.JList;
45 import javax.swing.JOptionPane;
46 import javax.swing.JPanel;
47 import javax.swing.JScrollPane;
48 import javax.swing.JSeparator;
49 import javax.swing.JSplitPane;
50 import javax.swing.JTextField;
51 import javax.swing.KeyStroke;
52 import javax.swing.ListSelectionModel;
53 import javax.swing.WindowConstants;
54 import javax.swing.event.DocumentEvent;
55 import javax.swing.event.DocumentListener;
56 import javax.swing.event.ListSelectionEvent;
57 import javax.swing.event.ListSelectionListener;
58
59 import org.crosswire.bibledesktop.BDMsg;
60 import org.crosswire.common.swing.ActionFactory;
61 import org.crosswire.common.swing.CWAction;
62 import org.crosswire.common.swing.CWLabel;
63 import org.crosswire.common.swing.CWOptionPane;
64 import org.crosswire.common.swing.CWScrollPane;
65 import org.crosswire.common.swing.FixedSplitPane;
66 import org.crosswire.common.swing.GuiUtil;
67 import org.crosswire.jsword.book.install.InstallManager;
68 import org.crosswire.jsword.book.install.Installer;
69 import org.crosswire.jsword.book.install.InstallerFactory;
70
71
79 public class EditSitePane extends JPanel {
80
83 public EditSitePane(InstallManager imanager) {
84 this.imanager = imanager;
85 userInitiated = true;
86
87 init();
88 setState(EditState.DISPLAY, null);
89 select();
90 }
91
92
95 private void init() {
96 actions = new ActionFactory(this);
97
98 lstSite = new JList(new InstallManagerComboBoxModel(imanager));
99 JScrollPane scrSite = new CWScrollPane(lstSite);
100
101 lstSite.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
102 lstSite.addListSelectionListener(new ListSelectionListener() {
103 public void valueChanged(ListSelectionEvent ev) {
104 if (ev.getValueIsAdjusting()) {
105 return;
106 }
107
108 select();
109 }
110 });
111
112 CWAction action = actions.addAction("Add", BDMsg.gettext("Add"));
114 action.setTooltip(BDMsg.gettext("Add a new installation site."));
116 JButton btnAdd = new JButton(action);
117
118 action = actions.addAction("Edit", BDMsg.gettext("Edit"));
120 action.setTooltip(BDMsg.gettext("Edit the current installation site."));
122 JButton btnEdit = new JButton(action);
123
124 action = actions.addAction("Delete", BDMsg.gettext("Delete"));
126 action.setTooltip(BDMsg.gettext("Delete Site?"));
128 JButton btnDelete = new JButton(action);
129
130 JPanel pnlBtn1 = new JPanel();
131 pnlBtn1.add(btnAdd, null);
132 pnlBtn1.add(btnEdit, null);
133 pnlBtn1.add(btnDelete, null);
134
135 JPanel pnlSite = new JPanel();
136 pnlSite.setLayout(new BorderLayout());
137 pnlSite.add(scrSite, BorderLayout.CENTER);
138 pnlSite.add(pnlBtn1, BorderLayout.SOUTH);
139
140 txtName = new JTextField();
141 txtName.setColumns(10);
142 txtName.getDocument().addDocumentListener(new DocumentListener() {
143 public void changedUpdate(DocumentEvent ev) {
144 siteUpdate();
145 }
146
147 public void insertUpdate(DocumentEvent ev) {
148 siteUpdate();
149 }
150
151 public void removeUpdate(DocumentEvent ev) {
152 siteUpdate();
153 }
154 });
155
156 JLabel lblName = CWLabel.createJLabel(BDMsg.gettext("Site Name:"));
158 lblName.setLabelFor(txtName);
159
160 cboType = new JComboBox(new InstallerFactoryComboBoxModel(imanager));
161 cboType.setEditable(false);
162 cboType.setSelectedIndex(0);
163 cboType.addActionListener(new ActionListener() {
164 public void actionPerformed(ActionEvent ev) {
165 newType();
166 }
167 });
168
169 JLabel lblType = CWLabel.createJLabel(BDMsg.gettext("Site Type"));
173 lblType.setLabelFor(cboType);
174
175 lblMesg = new JLabel();
176 lblMesg.setText(" ");
177
178 action = actions.addAction("Reset", BDMsg.gettext("Reset"));
181 action.setTooltip(BDMsg.gettext("Reset the details."));
184 JButton btnReset = new JButton(action);
185
186 action = actions.addAction("Save", BDMsg.gettext("Save"));
188 action.setTooltip(BDMsg.gettext("Save the current changes."));
190 JButton btnSave = new JButton(action);
191
192 JPanel pnlBtn2 = new JPanel();
193 pnlBtn2.add(btnSave, null);
194 pnlBtn2.add(btnReset, null);
195
196 siteEditorPane = new JPanel();
197 siteEditorPane.setLayout(new GridBagLayout());
198 JPanel pnlMain = new JPanel();
199 pnlMain.setPreferredSize(new Dimension(300, 300));
200 pnlMain.setLayout(new GridBagLayout());
201 pnlMain.add(lblMesg, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0));
202 pnlMain.add(lblName, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(2, 10, 2, 2), 0, 0));
203 pnlMain.add(txtName, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 10), 0, 0));
204 if (imanager.getInstallerFactoryNames().size() > 1) {
206 pnlMain.add(lblType, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(2, 10, 2, 2), 0, 0));
207 pnlMain.add(cboType, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 10), 0, 0));
208 }
209 pnlMain.add(new JSeparator(), new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0));
210 pnlMain.add(siteEditorPane, new GridBagConstraints(0, 4, 2, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
211 pnlMain.add(pnlBtn2, new GridBagConstraints(0, 5, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
212
213 JSplitPane sptMain = new FixedSplitPane();
214 sptMain.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
215 sptMain.setResizeWeight(0.0D);
217 sptMain.setLeftComponent(pnlSite);
218 sptMain.setRightComponent(pnlMain);
219
220 this.setLayout(new BorderLayout());
221 this.add(sptMain, BorderLayout.CENTER);
222
223 btnClose = new JButton(actions.addAction("Close", BDMsg.gettext("Close")));
225
226 pnlAction = new JPanel();
227 pnlAction.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
228 pnlAction.setLayout(new FlowLayout(FlowLayout.TRAILING));
229 pnlAction.add(btnClose, null);
230 GuiUtil.applyDefaultOrientation(this);
231 }
232
233
239 public void showInDialog(Component parent) {
240 Frame root = JOptionPane.getFrameForComponent(parent);
241 dlgMain = new JDialog(root);
242
243 ActionListener closer = new ActionListener() {
244 public void actionPerformed(ActionEvent ev) {
245 doClose();
246 }
247 };
248
249 KeyStroke esc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
250
251 dlgMain.getContentPane().setLayout(new BorderLayout());
252 dlgMain.getContentPane().add(new JPanel(), BorderLayout.NORTH);
253 dlgMain.getContentPane().add(pnlAction, BorderLayout.SOUTH);
254 dlgMain.getContentPane().add(this, BorderLayout.CENTER);
255 dlgMain.getContentPane().add(new JPanel(), BorderLayout.LINE_END);
256 dlgMain.getContentPane().add(new JPanel(), BorderLayout.LINE_START);
257 dlgMain.getRootPane().setDefaultButton(btnClose);
258 dlgMain.getRootPane().registerKeyboardAction(closer, esc, JComponent.WHEN_IN_FOCUSED_WINDOW);
259 dlgMain.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
260 dlgMain.setTitle(BDMsg.gettext("Edit Update Sites"));
262 dlgMain.setResizable(true);
263 dlgMain.setModal(true);
264
265 GuiUtil.setSize(dlgMain, new Dimension(750, 400));
266 GuiUtil.centerOnScreen(dlgMain);
267 GuiUtil.applyDefaultOrientation(dlgMain);
268 dlgMain.setVisible(true);
269 dlgMain.toFront();
270 }
271
272
275 public void doClose() {
276 imanager.save();
277 dlgMain.dispose();
278 }
279
280
283 public final void siteUpdate() {
284 if (txtName.isEditable()) {
285 String name = txtName.getText().trim();
286
287 if (name.length() == 0) {
288 setState(EditState.EDIT_ERROR, BDMsg.gettext("Missing site name"));
290 return;
291 }
292
293 if (imanager.getInstaller(name) != null) {
294 setState(EditState.EDIT_ERROR, BDMsg.gettext("Duplicate site name"));
296 return;
297 }
298
299 setState(EditState.EDIT_OK, "");
300 }
301 }
302
303
306 final void newType() {
307 if (userInitiated) {
308 String type = (String) cboType.getSelectedItem();
309 InstallerFactory ifactory = imanager.getInstallerFactory(type);
310 Installer installer = ifactory.createInstaller();
311
312 setInstaller(installer);
313 }
314 }
315
316
319 protected final void select() {
320 String name = (String) lstSite.getSelectedValue();
321 if (name == null) {
322 actions.findAction("Edit").setEnabled(false);
323 clear();
324 } else {
325 actions.findAction("Edit").setEnabled(true);
326
327 Installer installer = imanager.getInstaller(name);
328 display(name, installer);
329 }
330
331 if (siteEditor != null) {
335 siteEditor.setEditable(false);
336 }
337 }
338
339
342 public void doAdd() {
343 newType();
344
345 editName = null;
346 editInstaller = null;
347
348 setState(EditState.EDIT_OK, null);
351 siteUpdate();
352
353 GuiUtil.refresh(this);
354 }
355
356
359 public void doEdit() {
360 String name = (String) lstSite.getSelectedValue();
361 if (name == null) {
362 String title = BDMsg.gettext("No Site");
364 String msg = BDMsg.gettext("No selected site to edit");
366 CWOptionPane.showMessageDialog(this, msg, title, JOptionPane.INFORMATION_MESSAGE);
367 return;
368 }
369
370 editName = name;
371 editInstaller = imanager.getInstaller(name);
372
373 imanager.removeInstaller(name);
374
375 setState(EditState.EDIT_OK, null);
376 siteUpdate();
377
378 txtName.grabFocus();
379 }
380
381
384 public void doDelete() {
385 String name = (String) lstSite.getSelectedValue();
386 if (name == null) {
387 return;
388 }
389 String title = BDMsg.gettext("Delete Site?");
391 String msg = BDMsg.gettext("Are you sure you want to delete {0}?", name);
393 if (CWOptionPane.showConfirmDialog(this, msg, title, JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
394 imanager.removeInstaller(name);
395 }
396
397 clear();
398 setState(EditState.DISPLAY, null);
399 }
400
401
404 public void doReset() {
405 if (editName != null) {
406 imanager.addInstaller(editName, editInstaller);
407 }
408
409 clear();
410 editName = null;
411 editInstaller = null;
412
413 setState(EditState.DISPLAY, "");
414 select();
415 }
416
417
420 public void doSave() {
421 String name = txtName.getText();
422 siteEditor.save();
423 Installer installer = siteEditor.getInstaller();
424 imanager.addInstaller(name, installer);
425
426 clear();
427 editName = null;
428 editInstaller = null;
429
430 setState(EditState.DISPLAY, "");
431 select();
432 }
433
434
437 private void setState(EditState stateEditError, String message) {
438 switch (stateEditError) {
439 case DISPLAY:
440 actions.findAction("Add").setEnabled(true);
441 actions.findAction("Delete").setEnabled(true);
442 actions.findAction("Edit").setEnabled(true);
443 lstSite.setEnabled(true);
444
445 actions.findAction("Reset").setEnabled(false);
446 actions.findAction("Save").setEnabled(false);
447
448 actions.findAction("Close").setEnabled(true);
449
450 txtName.setEditable(false);
451 cboType.setEnabled(false);
452
453 if (siteEditor != null) {
454 siteEditor.setEditable(false);
455 }
456
457 break;
458
459 case EDIT_OK:
460 case EDIT_ERROR:
461 actions.findAction("Add").setEnabled(false);
462 actions.findAction("Delete").setEnabled(false);
463 actions.findAction("Edit").setEnabled(false);
464 lstSite.setEnabled(false);
465
466 actions.findAction("Reset").setEnabled(true);
467 actions.findAction("Save").setEnabled(stateEditError == EditState.EDIT_OK);
468
469 actions.findAction("Close").setEnabled(false);
470
471 txtName.setEditable(true);
472 cboType.setEnabled(true);
473
474 if (siteEditor != null) {
475 siteEditor.setEditable(true);
476 }
477
478 break;
479
480 default:
481 assert false : stateEditError;
482 }
483
484 if (message == null || message.trim().length() == 0) {
485 lblMesg.setText(" ");
486 } else {
487 lblMesg.setText(message);
488 }
489 }
490
491
494 private void display(String name, Installer installer) {
495 txtName.setText(name);
496
497 String type = imanager.getFactoryNameForInstaller(installer);
498 userInitiated = false;
499 cboType.setSelectedItem(type);
500 userInitiated = true;
501
502 setInstaller(installer);
503 }
504
505
508 private void clear() {
509 txtName.setText("");
510 setInstaller(null);
511 }
512
513
520 private void setInstaller(Installer installer) {
521 siteEditorPane.removeAll();
522 siteEditor = null;
523 if (installer != null) {
524 siteEditor = SiteEditorFactory.createSiteEditor(installer);
525 siteEditorPane.add((Component) siteEditor, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
526 new Insets(0, 0, 0, 0), 0, 0));
527 GuiUtil.applyDefaultOrientation(siteEditorPane);
528 }
529
530 GuiUtil.refresh(this);
531 }
532
533
540 private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
541 imanager = null;
543 editInstaller = null;
544 actions = new ActionFactory(this);
545 is.defaultReadObject();
546 }
547
548
549
552 private enum EditState {
553
556 DISPLAY,
557
558
561 EDIT_OK,
562
563
566 EDIT_ERROR,
567 }
568
569
572 private transient InstallManager imanager;
573
574
578 private String editName;
579
580
584 private transient Installer editInstaller;
585
586
590 private boolean userInitiated;
591
592
596 private transient ActionFactory actions;
597
598
601 private JList lstSite;
602
603
606 private JLabel lblMesg;
607 private JTextField txtName;
608 private JComboBox cboType;
609 private JPanel siteEditorPane;
610 private SiteEditor siteEditor;
611
612
617 protected JDialog dlgMain;
618 private JButton btnClose;
619 private JPanel pnlAction;
620
621
624 private static final long serialVersionUID = 3256446910616057650L;
625 }
626