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

Last change on this file since 5312 was 5312, checked in by mdewsnip, 21 years ago

Removed "hide/view extracted metadata" controls.

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