1 package net.sf.bibkeeper;
2
3 import javax.swing;
4 import java.awt.event;
5 import java.awt;
6 import java.util;
7
8 public class EntryTypeDialog extends JDialog implements ActionListener {
9
10 /*
11 * Dialog that prompts the user to choose a type for an entry.
12 * Returns null if cancelled.
13 */
14
15 BibtexEntryType type = null;
16 CancelAction cancelAction = new CancelAction();
17 private final int COLNUM = 3;
18
19 class TypeButton extends JButton implements Comparable {
20 BibtexEntryType type;
21 public TypeButton(String label, BibtexEntryType type_) {
22 super(label);
23 type = type_;
24 }
25 public int compareTo(Object o) {
26 if (! (o instanceof TypeButton))
27 throw new ClassCastException();
28 return type.getName().compareTo(((TypeButton)o).type.getName());
29 }
30 }
31
32 public EntryTypeDialog(BibtexBaseFrame baseFrame_) {
33 super(baseFrame_, true); // Set modal on.
34
35
36 setTitle("Select entry type");
37
38 addWindowListener(new WindowAdapter() {
39 public void windowClosing(WindowEvent e) {
40 cancelAction.actionPerformed(null);
41 }
42 });
43
44 getContentPane().setLayout(new BorderLayout());
45 JPanel pan = new JPanel();
46 getContentPane().add(pan, BorderLayout.CENTER);
47 JPanel lower = new JPanel();
48 JButton // ok = new JButton("Ok"),
49 cancel = new JButton("Cancel");
50 //ok.addActionListener(this);
51 cancel.addActionListener(this);
52
53 // Make ESC close dialog, equivalent to clicking Cancel.
54 cancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
55 .put(GUIGlobals.exitDialog, "close");
56 cancel.getActionMap().put("close", cancelAction);
57
58
59 //lower.add(ok);
60 lower.add(cancel);
61 getContentPane().add(lower, BorderLayout.SOUTH);
62 GridBagLayout gbl = new GridBagLayout();
63 pan.setLayout(gbl);
64 GridBagConstraints con = new GridBagConstraints();
65 con.anchor = GridBagConstraints.WEST;
66 con.fill = GridBagConstraints.HORIZONTAL;
67 con.insets = new Insets(4, 4, 4, 4);
68 int col = 0;
69 TreeSet buttons = new TreeSet();
70 Iterator iter = BibtexEntryType.ALL_TYPES.iterator();
71 for (;iter.hasNext();) {
72 BibtexEntryType type = (BibtexEntryType)(iter.next());
73 TypeButton b = new TypeButton(type.getName(), type);
74 buttons.add(b);
75 }
76 // We added the buttons to a TreeSet, sorting them according to their names.
77 // The sort is based on the compareTo() method in TypeButton.
78 iter = buttons.iterator();
79 for (;iter.hasNext();) {
80 TypeButton b = (TypeButton)(iter.next());
81 b.setAlignmentX(SwingConstants.LEFT);
82 b.addActionListener(this);
83 // Check if we should finish the row.
84 col++;
85 if (col == COLNUM) {
86 col = 0;
87 con.gridwidth = GridBagConstraints.REMAINDER;
88 } else
89 con.gridwidth = 1;
90 gbl.setConstraints(b, con);
91 pan.add(b);
92 }
93 pan.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
94 "Entry types"));
95 //pan.setBackground(Color.white);
96 //lower.setBackground(Color.white);
97 pack();
98 setResizable(false);
99 }
100
101 public void actionPerformed(ActionEvent e) {
102 if (e.getSource() instanceof TypeButton) {
103 type = ((TypeButton)e.getSource()).type;
104 }
105 dispose();
106 }
107
108 public BibtexEntryType getChoice() {
109 //return type;
110 return type;
111 }
112
113 class CancelAction extends AbstractAction {
114 public CancelAction() {
115 super("Cancel");
116 // new ImageIcon(GUIGlobals.imagepath+GUIGlobals.closeIconFile));
117 //putValue(SHORT_DESCRIPTION, "Cancel");
118 //putValue(MNEMONIC_KEY, GUIGlobals.closeKeyCode);
119 }
120 public void actionPerformed(ActionEvent e) {
121 dispose();
122 }
123 }
124
125
126 }