source: trunk/gli/src/org/greenstone/gatherer/gui/NewCollectionDetailsPrompt.java@ 4428

Last change on this file since 4428 was 4428, checked in by kjdon, 21 years ago

the modal dialog now is one of our special ModalDialogs which only block the parent, enabling the use of help files while the dialog is open. A SimpleMenuBar with help on it has been added to the dialog.

  • Property svn:keywords set to Author Date Id Revision
File size: 16.9 KB
Line 
1package org.greenstone.gatherer.gui;
2
3import java.awt.*;
4import java.awt.event.*;
5import java.io.File;
6import java.util.*;
7import javax.swing.*;
8import javax.swing.event.*;
9import javax.swing.text.*;
10import org.greenstone.gatherer.Gatherer;
11import org.greenstone.gatherer.collection.CollectionConfiguration;
12import org.greenstone.gatherer.util.Utility;
13import org.greenstone.gatherer.gui.SimpleMenuBar;
14import org.greenstone.gatherer.gui.ModalDialog;
15
16public class NewCollectionDetailsPrompt
17 extends ModalDialog {
18 private boolean cancelled;
19 private File base_final;
20 private JButton create_button;
21 private JComboBox base_collection;
22 private JDialog self;
23 private JTextArea description;
24 private JTextField address;
25 private JTextField host;
26 private JTextField title;
27 private RestrictedTextField file;
28 private String description_final;
29 private String email_final;
30 private String name_final;
31 private String title_final;
32 static private Dimension label_size = new Dimension(230, 25);
33 /** The size of this new collection dialog box. */
34 static private Dimension size = new Dimension(700, 375);
35 static private int FILENAME_SIZE = 8;
36
37 /** Constructor.
38 * @see org.greenstone.gatherer.util.Utility
39 */
40 public NewCollectionDetailsPrompt() {
41 super(Gatherer.g_man);
42 this.cancelled = true;
43 this.self = this;
44 // Setup
45 setJMenuBar(new SimpleMenuBar("2.1"));
46 setModal(true);
47 setSize(size);
48 setTitle(get("Title"));
49
50 // Model building. Build a model of all of the collections in the gsdl collect directory with the appropriate directories and hardcode the big five.
51 Vector base_collection_model = new Vector();
52 // Dummy item
53 File gsdl_collection_directory = new File(Utility.getCollectionDir(Gatherer.config.gsdl_path));
54 File[] possible_collections = gsdl_collection_directory.listFiles();
55 for(int i = 0; possible_collections != null && i < possible_collections.length; i++) {
56 // The simpliest case is if the directory etc/collect.cfg file and a metadata/ in it. Thus it can easily be built upon.
57 File collect_cfg_file = new File(possible_collections[i], Utility.CONFIG_DIR);
58 File metadata_directory = new File(possible_collections[i], Utility.META_DIR);
59 if(collect_cfg_file.exists()) {
60 CollectionConfiguration collect_cfg = new CollectionConfiguration(collect_cfg_file);
61 String collection_name = collect_cfg.getName();
62 // Even if there is no metadata directory we add it if its one of the 'big five + 1' that we know how to handle.
63 if(metadata_directory.exists() || collection_name.equals(Utility.COLLECTION_DLS) || collection_name.equals(Utility.COLLECTION_DEMO)) { /** @todo - the other big five */
64 // Add to model.
65 Item item = new Item(possible_collections[i], collection_name);
66 if(!base_collection_model.contains(item)) {
67 base_collection_model.add(item);
68 }
69 }
70 // Else not a collection we know how to retrieve the metadata set for.
71 }
72 // Else not a collection at all. Someones pulling a fast one.
73 }
74 // Sort the result.
75 Collections.sort(base_collection_model);
76 base_collection_model.add(0, new Item(null, get("NewCollection")));
77 // Creation.
78 JPanel content_pane = (JPanel) getContentPane();
79 content_pane.setOpaque(true);
80 JPanel upper_pane = new JPanel();
81 upper_pane.setOpaque(false);
82 JLabel instructions_label = new JLabel(get("Instructions"));
83 instructions_label.setOpaque(false);
84 JPanel title_pane = new JPanel();
85 title_pane.setOpaque(false);
86 JLabel title_label = new JLabel(get("Collection_Title"));
87 title_label.setOpaque(false);
88 title = new JTextField();
89 JPanel name_pane = new JPanel();
90 name_pane.setOpaque(false);
91 JLabel name_label = new JLabel(get("Collection_Name"));
92 name_label.setOpaque(false);
93 JPanel file_pane = new JPanel();
94 file_pane.setOpaque(false);
95 file = new RestrictedTextField(new RestrictedTextDocument(FILENAME_SIZE), "", FILENAME_SIZE);
96 JLabel file_label = new JLabel(".col");
97 file_label.setOpaque(false);
98 JPanel email_pane = new JPanel();
99 email_pane.setOpaque(false);
100 JLabel email_label = new JLabel(get("Collection_Email"));
101 email_label.setOpaque(false);
102 JPanel host_pane = new JPanel();
103 host_pane.setOpaque(false);
104 JPanel address_pane = new JPanel();
105 address_pane.setOpaque(false);
106 address = new JTextField();
107 JLabel at_label = new JLabel("@");
108 host = new JTextField();
109 JPanel center_pane = new JPanel();
110 center_pane.setOpaque(false);
111 JPanel description_pane = new JPanel();
112 description_pane.setOpaque(false);
113 JLabel description_label = new JLabel(get("Collection_Description"));
114 description_label.setOpaque(false);
115 description = new JTextArea();
116 description.setRows(5);
117
118 JPanel bottom_pane = new JPanel();
119 // Base Collection
120 JPanel base_collection_pane = new JPanel();
121 JLabel base_collection_label = new JLabel(get("Base_Collection"));
122 base_collection = new JComboBox(base_collection_model);
123 JButton base_collection_browse = new JButton(get("General.Browse"));
124
125 JPanel button_pane = new JPanel();
126 button_pane.setOpaque(false);
127 create_button = new JButton(get("General.OK"));
128 create_button.setMnemonic(KeyEvent.VK_O);
129 JButton cancel_button = new JButton(get("General.Cancel"));
130 cancel_button.setMnemonic(KeyEvent.VK_C);
131 ColorListener email_color_listener = new ColorListener(address, host);
132 // Connection
133 base_collection_browse.addActionListener(new BrowseListener());
134 cancel_button.addActionListener(new CancelListener());
135 create_button.addActionListener(new CreateListener());
136 address.addKeyListener(email_color_listener);
137 description.addKeyListener(new ColorListener(description));
138 description.addKeyListener(new DescriptionListener());
139 file.addKeyListener(new ColorListener(file));
140 host.addKeyListener(email_color_listener);
141 title.addKeyListener(new ColorListener(title));
142 title.getDocument().addDocumentListener(new TitleListener());
143 // Layout
144 title_label.setPreferredSize(label_size);
145
146 title_pane.setLayout(new BorderLayout());
147 title_pane.add(title_label, BorderLayout.WEST);
148 title_pane.add(title, BorderLayout.CENTER);
149
150 file_pane.setLayout(new BorderLayout());
151 file_pane.add(file, BorderLayout.WEST);
152 file_pane.add(file_label, BorderLayout.CENTER);
153
154 name_label.setPreferredSize(label_size);
155
156 name_pane.setLayout(new BorderLayout());
157 name_pane.add(name_label, BorderLayout.WEST);
158 name_pane.add(file_pane, BorderLayout.CENTER);
159
160 email_label.setPreferredSize(label_size);
161
162 address_pane.setLayout(new BorderLayout());
163 address_pane.add(address, BorderLayout.CENTER);
164 address_pane.add(at_label, BorderLayout.EAST);
165
166 host_pane.setLayout(new GridLayout(1,2));
167 host_pane.add(address_pane);
168 host_pane.add(host);
169
170 email_pane.setLayout(new BorderLayout());
171 email_pane.add(email_label, BorderLayout.WEST);
172 email_pane.add(host_pane, BorderLayout.CENTER);
173
174 upper_pane.setLayout(new GridLayout(4,1));
175 upper_pane.add(instructions_label);
176 upper_pane.add(title_pane);
177 upper_pane.add(name_pane);
178 upper_pane.add(email_pane);
179
180 description_pane.setLayout(new BorderLayout());
181 description_pane.add(description_label, BorderLayout.NORTH);
182 description_pane.add(new JScrollPane(description), BorderLayout.CENTER);
183
184 base_collection_pane.setLayout(new BorderLayout());
185 base_collection_pane.add(base_collection_label, BorderLayout.NORTH);
186 base_collection_pane.add(base_collection, BorderLayout.CENTER);
187 //base_collection_pane.add(base_collection_browse, BorderLayout.EAST);
188
189 center_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
190 center_pane.setLayout(new BorderLayout());
191 center_pane.add(description_pane, BorderLayout.CENTER);
192
193 bottom_pane.setLayout(new BorderLayout());
194 bottom_pane.add(base_collection_pane, BorderLayout.CENTER);
195 bottom_pane.add(button_pane, BorderLayout.SOUTH);
196
197 button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
198 button_pane.setLayout(new GridLayout(1,2));
199 button_pane.add(create_button);
200 button_pane.add(cancel_button);
201
202 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
203 content_pane.setLayout(new BorderLayout());
204 content_pane.add(upper_pane, BorderLayout.NORTH);
205 content_pane.add(center_pane, BorderLayout.CENTER);
206 content_pane.add(bottom_pane, BorderLayout.SOUTH);
207 // Final dialog setup & positioning.
208 Dimension screen_size = Gatherer.config.screen_size;
209 setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2);
210 setVisible(true);
211 }
212
213 public boolean isCancelled() {
214 return cancelled;
215 }
216
217 public File getBase() {
218 return base_final;
219 }
220
221 public String getDescription() {
222 return description_final;
223 }
224
225 public String getEmail() {
226 return email_final;
227 }
228
229 public String getName() {
230 return name_final;
231 }
232
233 public String getTitle() {
234 return title_final;
235 }
236
237 /** Gets a phrase from the dictionary based on key.
238 * @param key A <strong>String</strong> which serves as the unique identifier of a phrase.
239 * @return The desired phrase as a <strong>String</strong> or at least a meaningful error message.
240 */
241 private String get(String key) {
242 if(key.indexOf(".") == -1) {
243 key = "NewCollectionPrompt." + key;
244 }
245 return Gatherer.dictionary.get(key);
246 }
247
248 private class BrowseListener
249 implements ActionListener {
250 public void actionPerformed(ActionEvent event) {
251 File file;
252 if(Gatherer.config.gsdl_path != null) {
253 file = new File(Utility.getCollectionDir(Gatherer.config.gsdl_path));
254 }
255 else {
256 file = new File(Utility.BASE_DIR);
257 }
258 // Show OpenCollectionPrompt.
259 OpenCollectionDialog chooser = new OpenCollectionDialog(file);
260 file = chooser.getSelectedFile();
261 if(file != null) {
262 file = file.getParentFile();
263 CollectionConfiguration collect_cfg = new CollectionConfiguration(new File(file, Utility.META_DIR));
264 Item item = new Item(file, collect_cfg.getName());
265 base_collection.addItem(item);
266 base_collection.setSelectedItem(item);
267 }
268 }
269 }
270
271 private class CancelListener
272 implements ActionListener {
273 public void actionPerformed(ActionEvent event) {
274 cancelled = true;
275 self.dispose();
276 }
277 }
278
279 private class ColorListener
280 extends KeyAdapter {
281 private JTextComponent component1;
282 private JTextComponent component2;
283 public ColorListener(JTextComponent component) {
284 this.component1 = component;
285 this.component2 = null;
286 }
287 public ColorListener(JTextComponent component1, JTextComponent component2) {
288 this.component1 = component1;
289 this.component2 = component2;
290 }
291 public void keyPressed(KeyEvent event) {
292 component1.setForeground(Gatherer.config.getColor("coloring.collection_tree_foreground", false));
293 component1.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
294 if(component2 != null) {
295 component2.setForeground(Gatherer.config.getColor("coloring.collection_tree_foreground", false));
296 component2.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
297 }
298 }
299 }
300
301 private class CreateListener
302 implements ActionListener {
303 public void actionPerformed(ActionEvent event) {
304 // Validate.
305 title_final = title.getText();
306 if(title_final.length() == 0) {
307 GPrompt err_msg = new GPrompt(self, get("Error"), get("Title_Error"), "error.gif", null);
308 err_msg.display();
309 err_msg.destroy();
310 err_msg = null;
311 title.setForeground(Gatherer.config.getColor("coloring.error_foreground", false));
312 title.setBackground(Gatherer.config.getColor("coloring.error_background", false));
313 return;
314 }
315 name_final = file.getText();
316 if(name_final.length() > 0) {
317 // Determine if this filename is already in use.
318 File collection_directory = new File(Utility.getCollectionDir(Gatherer.config.gsdl_path));
319 File children[] = collection_directory.listFiles();
320 for(int i = 0; children != null && i < children.length; i++) {
321 if(children[i].getName().equals(name_final)) {
322 GPrompt err_msg = new GPrompt(self, get("Error"), get("Name_Error"), "error.gif", null);
323 err_msg.display();
324 err_msg.destroy();
325 err_msg = null;
326 file.setForeground(Gatherer.config.getColor("coloring.error_foreground", false));
327 file.setBackground(Gatherer.config.getColor("coloring.error_background", false));
328 return;
329 }
330 }
331 }
332 else {
333 GPrompt err_msg = new GPrompt(self, get("Error"), get("Name_Error"), "error.gif", null);
334 err_msg.display();
335 err_msg.destroy();
336 err_msg = null;
337 file.setForeground(Gatherer.config.getColor("coloring.error_foreground", false));
338 file.setBackground(Gatherer.config.getColor("coloring.error_background", false));
339 return;
340 }
341 email_final = address.getText() + "@" + host.getText();
342 if(email_final.length() == 0 || email_final.startsWith("@") || email_final.endsWith("@")) {
343 GPrompt err_msg = new GPrompt(self, get("Error"), get("Email_Error"), "error.gif", null);
344 err_msg.display();
345 err_msg.destroy();
346 err_msg = null;
347 address.setForeground(Gatherer.config.getColor("coloring.error_foreground", false));
348 address.setBackground(Gatherer.config.getColor("coloring.error_background", false));
349 host.setForeground(Gatherer.config.getColor("coloring.error_foreground", false));
350 host.setBackground(Gatherer.config.getColor("coloring.error_background", false));
351 return;
352 }
353 description_final = description.getText();
354 if(description_final.length() == 0) {
355 GPrompt err_msg = new GPrompt(self, get("Error"), get("Description_Error"), "error.gif", null);
356 err_msg.display();
357 err_msg.destroy();
358 err_msg = null;
359 description.setForeground(Gatherer.config.getColor("coloring.error_foreground", false));
360 description.setBackground(Gatherer.config.getColor("coloring.error_background", false));
361 return;
362 }
363 description_final = Utility.encodeGreenstone(description_final);
364 // If we got this far there are no errors.
365 Item item_final = (Item) base_collection.getSelectedItem();
366 base_final = item_final.getFile();
367
368 cancelled = false;
369
370 self.dispose();
371 }
372 }
373
374 private class DescriptionListener
375 extends KeyAdapter {
376 public void keyPressed(KeyEvent event) {
377 if(event.getKeyCode() == KeyEvent.VK_TAB) {
378 event.consume();
379 base_collection.grabFocus();
380 }
381 }
382 }
383
384 private class Item
385 implements Comparable {
386 private File file;
387 private String name;
388 public Item(File file, String name) {
389 this.file = file;
390 this.name = name;
391 }
392 public int compareTo(Object other) {
393 return toString().toLowerCase().compareTo(other.toString().toLowerCase());
394 }
395 public boolean equals(Object other) {
396 return compareTo(other) == 0;
397 }
398 public File getFile() {
399 return file;
400 }
401 public String toString() {
402 return name;
403 }
404 }
405
406 private class RestrictedTextField
407 extends JTextField {
408 public RestrictedTextField(RestrictedTextDocument document, String value, int cols) {
409 super(document, "", cols);
410 }
411 protected Document createDefaultModel() {
412 return new RestrictedTextDocument(11);
413 }
414 }
415
416 private class RestrictedTextDocument
417 extends PlainDocument {
418 private char block[];
419 private int cols;
420 private int current;
421 public RestrictedTextDocument(int cols) {
422 super();
423 this.cols = cols;
424 this.current = 0;
425 }
426 public void blockChar(char c) {
427 if(block == null) {
428 block = new char[1];
429 block[0] = c;
430 }
431 else {
432 char temp[] = block;
433 block = new char[temp.length + 1];
434 System.arraycopy(temp, 0, block, 0, temp.length);
435 block[temp.length] = c;
436 temp = null;
437 }
438 }
439 public void insertString(int offs, String str, AttributeSet a)
440 throws BadLocationException {
441 // Remove any blocked characters.
442 StringBuffer temp = new StringBuffer(str);
443 for(int i = 0; block != null && i < block.length; i++) {
444 for(int j = temp.length() - 1; j >= 0; j--) {
445 if(temp.charAt(j) == block[i]) {
446 temp.deleteCharAt(j);
447 }
448 }
449 }
450 str = temp.toString();
451 if(cols == -1 || str.length() + current <= cols) {
452 super.insertString(offs, str, a);
453 current = current + str.length();
454 }
455 }
456 public void remove(int offs, int len)
457 throws BadLocationException {
458 super.remove(offs, len);
459 current = current - len;
460 }
461 }
462
463 private class TitleListener
464 implements DocumentListener {
465 /** Gives notification that an attribute or set of attributes changed. */
466 public void changedUpdate(DocumentEvent e) {
467 updateFilename();
468 }
469
470 /** Gives notification that there was an insert into the document. */
471 public void insertUpdate(DocumentEvent e) {
472 updateFilename();
473 }
474
475 /** Gives notification that a portion of the document has been removed. */
476 public void removeUpdate(DocumentEvent e) {
477 updateFilename();
478 }
479
480 private void updateFilename() {
481 String current_name = file.getText();
482 String current_title = title.getText();
483 StringBuffer temp = new StringBuffer("");
484 int i = 0;
485 while(i < current_title.length() && temp.length() < 8) {
486 if(current_title.charAt(i) != ' ') {
487 temp.append(Character.toLowerCase(current_title.charAt(i)));
488 }
489 i++;
490 }
491 String result = temp.toString();
492 if(current_name.startsWith(result) || result.startsWith(current_name)) {
493 file.setText(result);
494 }
495 }
496 }
497}
Note: See TracBrowser for help on using the repository browser.