|
| 1 | +package amidst.gui.main.bookmarks; |
| 2 | + |
| 3 | +import java.awt.BorderLayout; |
| 4 | +import java.awt.Color; |
| 5 | +import java.awt.Dialog; |
| 6 | +import java.awt.Frame; |
| 7 | + |
| 8 | +import java.awt.event.ActionEvent; |
| 9 | + |
| 10 | +import javax.swing.AbstractAction; |
| 11 | +import javax.swing.JButton; |
| 12 | +import javax.swing.JDialog; |
| 13 | +import javax.swing.JPanel; |
| 14 | + |
| 15 | +import javax.swing.border.EmptyBorder; |
| 16 | + |
| 17 | +import javax.swing.event.DocumentEvent; |
| 18 | +import javax.swing.event.DocumentListener; |
| 19 | + |
| 20 | +import org.dishevelled.layout.ButtonPanel; |
| 21 | + |
| 22 | +class BookmarkChooserDialog extends JDialog { |
| 23 | + private boolean canceled = true; |
| 24 | + |
| 25 | + private final AbstractAction cancel = new AbstractAction("Cancel") { |
| 26 | + @Override |
| 27 | + public void actionPerformed(final ActionEvent event) |
| 28 | + { |
| 29 | + cancel(); |
| 30 | + } |
| 31 | + }; |
| 32 | + private final AbstractAction ok = new AbstractAction("OK") { |
| 33 | + @Override |
| 34 | + public void actionPerformed(final ActionEvent event) |
| 35 | + { |
| 36 | + ok(); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + private final JButton okButton; |
| 41 | + private final BookmarkChooser chooser; |
| 42 | + |
| 43 | + BookmarkChooserDialog(final Dialog owner, final String title, final boolean modal, final BookmarkChooser chooser) |
| 44 | + { |
| 45 | + super(owner, title, modal); |
| 46 | + |
| 47 | + ok.setEnabled(false); |
| 48 | + okButton = new JButton(ok); |
| 49 | + this.chooser = chooser; |
| 50 | + |
| 51 | + initialize(); |
| 52 | + } |
| 53 | + |
| 54 | + BookmarkChooserDialog(final Frame owner, final String title, final boolean modal, final BookmarkChooser chooser) |
| 55 | + { |
| 56 | + super(owner, title, modal); |
| 57 | + |
| 58 | + ok.setEnabled(false); |
| 59 | + okButton = new JButton(ok); |
| 60 | + this.chooser = chooser; |
| 61 | + |
| 62 | + initialize(); |
| 63 | + } |
| 64 | + |
| 65 | + private void initialize() |
| 66 | + { |
| 67 | + getRootPane().setDefaultButton(okButton); |
| 68 | + createListeners(); |
| 69 | + layoutComponents(); |
| 70 | + setSize(320, 200); |
| 71 | + } |
| 72 | + |
| 73 | + private void createListeners() |
| 74 | + { |
| 75 | + DocumentListener l = new DocumentListener() { |
| 76 | + @Override |
| 77 | + public void changedUpdate(final DocumentEvent e) { |
| 78 | + ok.setEnabled(chooser.ready()); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void insertUpdate(final DocumentEvent e) { |
| 83 | + ok.setEnabled(chooser.ready()); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void removeUpdate(final DocumentEvent e) { |
| 88 | + ok.setEnabled(chooser.ready()); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + chooser.x().getDocument().addDocumentListener(l); |
| 93 | + chooser.z().getDocument().addDocumentListener(l); |
| 94 | + chooser.label().getDocument().addDocumentListener(l); |
| 95 | + } |
| 96 | + |
| 97 | + private void layoutComponents() |
| 98 | + { |
| 99 | + ButtonPanel buttonPanel = new ButtonPanel(); |
| 100 | + buttonPanel.setBorder(new EmptyBorder(0, 12, 12, 12)); |
| 101 | + buttonPanel.add(cancel); |
| 102 | + buttonPanel.add(okButton); |
| 103 | + |
| 104 | + JPanel mainPanel = new JPanel(); |
| 105 | + mainPanel.setLayout(new BorderLayout()); |
| 106 | + mainPanel.add("Center", chooser); |
| 107 | + mainPanel.add("South", buttonPanel); |
| 108 | + |
| 109 | + setContentPane(mainPanel); |
| 110 | + } |
| 111 | + |
| 112 | + private void cancel() |
| 113 | + { |
| 114 | + canceled = true; |
| 115 | + hide(); |
| 116 | + } |
| 117 | + |
| 118 | + private void ok() |
| 119 | + { |
| 120 | + canceled = false; |
| 121 | + hide(); |
| 122 | + } |
| 123 | + |
| 124 | + boolean wasCanceled() |
| 125 | + { |
| 126 | + return canceled; |
| 127 | + } |
| 128 | + |
| 129 | + Bookmark getBookmark() |
| 130 | + { |
| 131 | + return wasCanceled() ? null : chooser.getBookmark(); |
| 132 | + } |
| 133 | +} |
0 commit comments