dad1619b1ae2c9a5fd3fe7c31d5978e2011e3886
[debian/jabref.git] / src / java / net / sf / jabref / EntryTypeDialog.java
1 /*
2 Copyright (C) 2003 Morten O. Alver
3
4 All programs in this directory and
5 subdirectories are published under the GNU General Public License as
6 described below.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or (at
11 your option) any later version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA
22
23 Further information about the GNU GPL is available at:
24 http://www.gnu.org/copyleft/gpl.ja.html
25
26 */
27
28 package net.sf.jabref;
29
30 import javax.swing.*;
31 import java.awt.event.*;
32 import java.awt.*;
33 import java.util.*;
34
35 public class EntryTypeDialog extends JDialog implements ActionListener {
36
37     /*
38      * Dialog that prompts the user to choose a type for an entry.
39      * Returns null if cancelled.
40      */
41
42     BibtexEntryType type = null;
43     CancelAction cancelAction = new CancelAction();
44     private final int COLNUM = 3;
45
46     class TypeButton extends JButton implements Comparable {
47         BibtexEntryType type;
48         public TypeButton(String label, BibtexEntryType type_) {
49             super(label);
50             type = type_;
51         }
52         public int compareTo(Object o) {
53             if (! (o instanceof TypeButton))
54                 throw new ClassCastException();
55             return type.getName().compareTo(((TypeButton)o).type.getName());
56         }
57     }
58
59     public EntryTypeDialog(JabRefFrame baseFrame_) {
60         super(baseFrame_, true); // Set modal on.
61
62
63         setTitle(Globals.lang("Select entry type"));
64
65         addWindowListener(new WindowAdapter() {
66                 public void windowClosing(WindowEvent e) {
67                     cancelAction.actionPerformed(null);
68                 }
69             });
70
71         getContentPane().setLayout(new BorderLayout());
72         JPanel pan = new JPanel();
73         getContentPane().add(pan, BorderLayout.CENTER);
74         JPanel lower = new JPanel();
75         JButton // ok = new JButton("Ok"),
76             cancel = new JButton(Globals.lang("Cancel"));
77         //ok.addActionListener(this);
78         cancel.addActionListener(this);
79
80         //pan.setBackground(GUIGlobals.lightGray);
81         //lower.setBackground(GUIGlobals.lightGray);
82         // Make ESC close dialog, equivalent to clicking Cancel.
83         cancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
84             .put(baseFrame_.prefs.getKey("Close dialog"), "close");
85         cancel.getActionMap().put("close", cancelAction);
86
87         //lower.add(ok);
88         lower.add(cancel);
89         getContentPane().add(lower, BorderLayout.SOUTH);
90         GridBagLayout gbl = new GridBagLayout();
91         pan.setLayout(gbl);
92         GridBagConstraints con = new GridBagConstraints();
93         con.anchor = GridBagConstraints.WEST;
94         con.fill = GridBagConstraints.HORIZONTAL;
95         con.insets = new Insets(4, 4, 4, 4);
96         int col = 0;
97
98         Iterator iter = BibtexEntryType.ALL_TYPES.keySet().iterator();
99         while (iter.hasNext()) {
100             BibtexEntryType tp = BibtexEntryType.getType((String)iter.next());
101             //System.out.println(tp.getName()+"\n"+tp);
102             TypeButton b = new TypeButton(Util.nCase(tp.getName()), tp);
103             b.setAlignmentX(SwingConstants.LEFT);
104             b.addActionListener(this);
105             // Check if we should finish the row.
106             col++;
107             if (col == COLNUM) {
108                 col = 0;
109                 con.gridwidth = GridBagConstraints.REMAINDER;
110             } else
111                 con.gridwidth = 1;
112             gbl.setConstraints(b, con);
113             pan.add(b);
114         }
115         pan.setBorder(BorderFactory.createTitledBorder
116                       (BorderFactory.createEtchedBorder(),
117                        Globals.lang("Entry types")));
118         //pan.setBackground(Color.white);
119         //lower.setBackground(Color.white);
120         pack();
121         setResizable(false);
122     }
123
124     public void actionPerformed(ActionEvent e) {
125         if (e.getSource() instanceof TypeButton) {
126             type = ((TypeButton)e.getSource()).type;
127         }
128         dispose();
129     }
130
131     public BibtexEntryType getChoice() {
132         //return type;
133         return type;
134     }
135
136     class CancelAction extends AbstractAction {
137         public CancelAction() {
138             super("Cancel");
139             //  new ImageIcon(GUIGlobals.imagepath+GUIGlobals.closeIconFile));
140             //putValue(SHORT_DESCRIPTION, "Cancel");
141             //putValue(MNEMONIC_KEY, GUIGlobals.closeKeyCode);
142         }
143         public void actionPerformed(ActionEvent e) {
144             dispose();
145         }
146     }
147
148
149 }