1 package net.sf.bibkeeper.groups;
2
3 import java.awt;
4 import java.awt.event;
5 import javax.swing;
6 import java.util.Vector;
7 import net.sf.bibkeeper.Util;
8 import net.sf.bibkeeper.GUIGlobals;
9
10 /**
11 * Dialog for creating or modifying groups. Operates directly on the
12 * Vector containing group information.
13 */
14 class GroupDialog extends JDialog {
15
16 JTextField
17 name = new JTextField(60),
18 regexp = new JTextField(60),
19 field = new JTextField(60);
20 JLabel
21 nl = new JLabel("Group name:"),
22 nr = new JLabel("Search term:"),
23 nf = new JLabel("Field to search:");
24 JButton
25 ok = new JButton("Ok"),
26 cancel = new JButton("Cancel");
27 JPanel
28 main = new JPanel(),
29 opt = new JPanel();
30 private boolean ok_pressed = false;
31 private Vector groups;
32 private int index;
33 private JFrame parent;
34
35 private String /*name, regexp, field,*/ oldName, oldRegexp, oldField;
36
37 GridBagLayout gbl = new GridBagLayout();
38 GridBagConstraints con = new GridBagConstraints();
39
40 public GroupDialog(JFrame parent_, Vector groups_,
41 int index_, String defaultField) {
42 super(parent_, "Edit group", true);
43 parent = parent_;
44 groups = groups_;
45 index = index_;
46 if (index >= 0) {
47 // Group entry already exists.
48 try {
49 oldField = (String)groups.elementAt(index);
50 field.setText(oldField);
51 oldName = (String)groups.elementAt(index+1);
52 name.setText(oldName);
53 oldRegexp = (String)groups.elementAt(index+2);
54 regexp.setText(oldRegexp);
55
56 // We disable these text fields, since changing field
57 // or regexp would leave the entries added to the
58 // group hanging.
59 field.setEnabled(false);
60 regexp.setEnabled(false);
61 } catch (ArrayIndexOutOfBoundsException ex) {
62 }
63 } else
64 field.setText(defaultField);
65
66 ActionListener okListener = new ActionListener() {
67 public void actionPerformed(ActionEvent e) {
68
69 // Check that there are no empty strings.
70 if ((field.getText().equals("")) ||
71 (name.getText().equals("")) ||
72 (regexp.getText().equals(""))) {
73 JOptionPane.showMessageDialog
74 (parent, "You must provide a name, a search "
75 +"string and a field name for this group.",
76 "Create group", JOptionPane.ERROR_MESSAGE);
77 return;
78 }
79
80 // Handling of : and ; must also be done.
81
82 ok_pressed = true;
83
84 if (index < 0) {
85 // New group.
86 index = GroupSelector.findPos(groups, name.getText());
87 groups.add(index, regexp.getText());
88 groups.add(index, name.getText());
89 groups.add(index, field.getText());
90 } else if (index < groups.size()) {
91 // Change group.
92 for (int i=0; i<GroupSelector.DIM; i++)
93 groups.removeElementAt(index);
94 index = GroupSelector.findPos(groups, name.getText());
95 groups.add(index, regexp.getText());
96 groups.add(index, name.getText());
97 groups.add(index, field.getText());
98 }
99
100 dispose();
101 }
102 };
103 ok.addActionListener(okListener);
104 name.addActionListener(okListener);
105 regexp.addActionListener(okListener);
106 field.addActionListener(okListener);
107
108 /*cancel.addActionListener(new ActionListener() {
109 public void actionPerformed(ActionEvent e) {
110 dispose();
111 }
112 });*/
113
114 AbstractAction cancelAction = new AbstractAction() {
115 public void actionPerformed(ActionEvent e) {
116 dispose();
117 }
118 };
119
120 cancel.addActionListener(cancelAction);
121
122 // Key bindings:
123 ActionMap am = main.getActionMap();
124 InputMap im = main.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
125 im.put(GUIGlobals.exitDialog, "close");
126 am.put("close", cancelAction);
127
128
129 // Layout starts here.
130 main.setLayout(gbl);
131 opt.setLayout(gbl);
132 main.setBorder(BorderFactory.createTitledBorder
133 (BorderFactory.createEtchedBorder(),
134 "Group properties"));
135
136 // Main panel:
137 con.weightx = 0;
138 con.gridwidth = 1;
139 con.insets = new Insets(3, 5, 3, 5);
140 con.anchor = GridBagConstraints.EAST;
141 con.fill = GridBagConstraints.NONE;
142 con.gridx = 0;
143 con.gridy = 0;
144 gbl.setConstraints(nl, con);
145 main.add(nl);
146 con.gridy = 1;
147 gbl.setConstraints(nr, con);
148 main.add(nr);
149 con.gridy = 2;
150 gbl.setConstraints(nf, con);
151 main.add(nf);
152
153 con.weightx = 1;
154 con.anchor = GridBagConstraints.WEST;
155 con.fill = GridBagConstraints.HORIZONTAL;
156 con.gridy = 0;
157 con.gridx = 1;
158 gbl.setConstraints(name, con);
159 main.add(name);
160 con.gridy = 1;
161 gbl.setConstraints(regexp, con);
162 main.add(regexp);
163 con.gridy = 2;
164 gbl.setConstraints(field, con);
165 main.add(field);
166
167 // Option buttons:
168 con.gridx = GridBagConstraints.RELATIVE;
169 con.gridy = GridBagConstraints.RELATIVE;
170 con.weightx = 1;
171 con.gridwidth = 1;
172 con.anchor = GridBagConstraints.EAST;
173 con.fill = GridBagConstraints.NONE;
174 gbl.setConstraints(ok, con);
175 opt.add(ok);
176 con.anchor = GridBagConstraints.WEST;
177 con.gridwidth = GridBagConstraints.REMAINDER;
178 gbl.setConstraints(cancel, con);
179 opt.add(cancel);
180
181 getContentPane().add(main, BorderLayout.CENTER);
182 getContentPane().add(opt, BorderLayout.SOUTH);
183
184 //pack();
185 setSize(400, 170);
186
187 Util.placeDialog(this, parent);
188 }
189
190 public boolean okPressed() {
191 return ok_pressed;
192 }
193
194 public int index() { return index; }
195 public String oldField() { return oldField; }
196 public String oldName() { return oldName; }
197 public String oldRegexp() { return oldRegexp; }
198 public String field() { return field.getText(); }
199 public String name() { return name.getText(); }
200 public String regexp() { return regexp.getText(); }
201
202 }