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

Last change on this file since 5248 was 5248, checked in by jmt12, 21 years ago

Removed bogus shell encoding code

  • Property svn:keywords set to Author Date Id Revision
File size: 18.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 JRadioButton hide_extracted_metadata_radio_button;
24 private JRadioButton view_extracted_metadata_radio_button;
25 private JTextArea description;
26 private JTextField address;
27 private JTextField host;
28 private JTextField title;
29 private RestrictedTextField file;
30 private String description_final;
31 private String email_final;
32 private String name_final;
33 private String title_final;
34 static private Dimension label_size = new Dimension(230, 25);
35 /** The size of this new collection dialog box. */
36 static private Dimension size = new Dimension(700, 375);
37 static private int FILENAME_SIZE = 8;
38
39 /** Constructor.
40 * @see org.greenstone.gatherer.util.Utility
41 */
42 public NewCollectionDetailsPrompt() {
43 super(Gatherer.g_man, true);
44 this.cancelled = true;
45 this.self = this;
46 // Setup
47 setJMenuBar(new SimpleMenuBar("2.1"));
48 setSize(size);
49 setTitle(get("Title"));
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)) {
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.setBackground(Gatherer.config.getColor("coloring.editable_background", false));
117 description.setForeground(Gatherer.config.getColor("coloring.editable_foreground", false));
118 description.setRows(5);
119
120 JPanel bottom_pane = new JPanel();
121 // Base Collection
122 JPanel base_collection_pane = new JPanel();
123 JLabel base_collection_label = new JLabel(get("Base_Collection"));
124 base_collection = new JComboBox(base_collection_model);
125 JButton base_collection_browse = new JButton(get("General.Browse"));
126
127 JPanel extracted_metadata_pane = new JPanel();
128 ButtonGroup extracted_metadata_button_group = new ButtonGroup();
129 view_extracted_metadata_radio_button = new JRadioButton(get("View_Extracted_Metadata"));
130 view_extracted_metadata_radio_button.setOpaque(false);
131 extracted_metadata_button_group.add(view_extracted_metadata_radio_button);
132 hide_extracted_metadata_radio_button = new JRadioButton(get("Hide_Extracted_Metadata"));
133 hide_extracted_metadata_radio_button.setOpaque(false);
134 extracted_metadata_button_group.add(hide_extracted_metadata_radio_button);
135 view_extracted_metadata_radio_button.setSelected(true);
136
137 JPanel button_pane = new JPanel();
138 button_pane.setOpaque(false);
139 create_button = new JButton(get("General.OK"));
140 create_button.setMnemonic(KeyEvent.VK_O);
141 JButton cancel_button = new JButton(get("General.Cancel"));
142 cancel_button.setMnemonic(KeyEvent.VK_C);
143 ColorListener email_color_listener = new ColorListener(address, host);
144 // Connection
145 base_collection_browse.addActionListener(new BrowseListener());
146 cancel_button.addActionListener(new CancelListener());
147 create_button.addActionListener(new CreateListener());
148 address.addKeyListener(email_color_listener);
149 description.addKeyListener(new ColorListener(description));
150 description.addKeyListener(new DescriptionListener());
151 file.addKeyListener(new ColorListener(file));
152 host.addKeyListener(email_color_listener);
153 title.addKeyListener(new ColorListener(title));
154 title.getDocument().addDocumentListener(new TitleListener());
155 // Layout
156 title_label.setPreferredSize(label_size);
157
158 title_pane.setLayout(new BorderLayout());
159 title_pane.add(title_label, BorderLayout.WEST);
160 title_pane.add(title, BorderLayout.CENTER);
161
162 file_pane.setLayout(new BorderLayout());
163 file_pane.add(file, BorderLayout.WEST);
164 file_pane.add(file_label, BorderLayout.CENTER);
165
166 name_label.setPreferredSize(label_size);
167
168 name_pane.setLayout(new BorderLayout());
169 name_pane.add(name_label, BorderLayout.WEST);
170 name_pane.add(file_pane, BorderLayout.CENTER);
171
172 email_label.setPreferredSize(label_size);
173
174 address_pane.setLayout(new BorderLayout());
175 address_pane.add(address, BorderLayout.CENTER);
176 address_pane.add(at_label, BorderLayout.EAST);
177
178 host_pane.setLayout(new GridLayout(1,2));
179 host_pane.add(address_pane);
180 host_pane.add(host);
181
182 email_pane.setLayout(new BorderLayout());
183 email_pane.add(email_label, BorderLayout.WEST);
184 email_pane.add(host_pane, BorderLayout.CENTER);
185
186 upper_pane.setLayout(new GridLayout(4,1));
187 upper_pane.add(instructions_label);
188 upper_pane.add(title_pane);
189 upper_pane.add(name_pane);
190 upper_pane.add(email_pane);
191
192 description_pane.setLayout(new BorderLayout());
193 description_pane.add(description_label, BorderLayout.NORTH);
194 description_pane.add(new JScrollPane(description), BorderLayout.CENTER);
195
196 extracted_metadata_pane.setLayout(new GridLayout(1,2,5,0));
197 extracted_metadata_pane.add(view_extracted_metadata_radio_button);
198 extracted_metadata_pane.add(hide_extracted_metadata_radio_button);
199
200 base_collection_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
201 base_collection_pane.setLayout(new BorderLayout());
202 base_collection_pane.add(base_collection_label, BorderLayout.NORTH);
203 base_collection_pane.add(base_collection, BorderLayout.CENTER);
204 //base_collection_pane.add(base_collection_browse, BorderLayout.EAST);
205
206 center_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
207 center_pane.setLayout(new BorderLayout());
208 center_pane.add(description_pane, BorderLayout.CENTER);
209
210 bottom_pane.setLayout(new BorderLayout());
211 bottom_pane.add(extracted_metadata_pane, BorderLayout.NORTH);
212 bottom_pane.add(base_collection_pane, BorderLayout.CENTER);
213 bottom_pane.add(button_pane, BorderLayout.SOUTH);
214
215 button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
216 button_pane.setLayout(new GridLayout(1,2));
217 button_pane.add(create_button);
218 button_pane.add(cancel_button);
219
220 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
221 content_pane.setLayout(new BorderLayout());
222 content_pane.add(upper_pane, BorderLayout.NORTH);
223 content_pane.add(center_pane, BorderLayout.CENTER);
224 content_pane.add(bottom_pane, BorderLayout.SOUTH);
225 // Final dialog setup & positioning.
226 Dimension screen_size = Gatherer.config.screen_size;
227 setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2);
228 setVisible(true);
229 }
230
231 public boolean isCancelled() {
232 return cancelled;
233 }
234
235 public File getBase() {
236 return base_final;
237 }
238
239 public String getDescription() {
240 return description_final;
241 }
242
243 public String getEmail() {
244 return email_final;
245 }
246
247 public String getName() {
248 return name_final;
249 }
250
251 public String getTitle() {
252 return title_final;
253 }
254
255 public boolean viewExtractedMetadata() {
256 return view_extracted_metadata_radio_button.isSelected();
257 }
258
259 /** Gets a phrase from the dictionary based on key.
260 * @param key A <strong>String</strong> which serves as the unique identifier of a phrase.
261 * @return The desired phrase as a <strong>String</strong> or at least a meaningful error message.
262 */
263 private String get(String key) {
264 if(key.indexOf(".") == -1) {
265 key = "NewCollectionPrompt." + key;
266 }
267 return Gatherer.dictionary.get(key);
268 }
269
270 private class BrowseListener
271 implements ActionListener {
272 public void actionPerformed(ActionEvent event) {
273 File file;
274 if(Gatherer.config.gsdl_path != null) {
275 file = new File(Utility.getCollectionDir(Gatherer.config.gsdl_path));
276 }
277 else {
278 file = new File(Utility.BASE_DIR);
279 }
280 // Show OpenCollectionPrompt.
281 OpenCollectionDialog chooser = new OpenCollectionDialog(file);
282 file = chooser.getSelectedFile();
283 if(file != null) {
284 file = file.getParentFile();
285 CollectionConfiguration collect_cfg = new CollectionConfiguration(new File(file, Utility.META_DIR));
286 Item item = new Item(file, collect_cfg.getName());
287 base_collection.addItem(item);
288 base_collection.setSelectedItem(item);
289 }
290 }
291 }
292
293 private class CancelListener
294 implements ActionListener {
295 public void actionPerformed(ActionEvent event) {
296 cancelled = true;
297 self.dispose();
298 }
299 }
300
301 private class ColorListener
302 extends KeyAdapter {
303 private JTextComponent component1;
304 private JTextComponent component2;
305 public ColorListener(JTextComponent component) {
306 this.component1 = component;
307 this.component2 = null;
308 }
309 public ColorListener(JTextComponent component1, JTextComponent component2) {
310 this.component1 = component1;
311 this.component2 = component2;
312 }
313 public void keyPressed(KeyEvent event) {
314 component1.setForeground(Gatherer.config.getColor("coloring.editable_foreground", false));
315 component1.setBackground(Gatherer.config.getColor("coloring.editable_background", false));
316 if(component2 != null) {
317 component2.setForeground(Gatherer.config.getColor("coloring.editable_foreground", false));
318 component2.setBackground(Gatherer.config.getColor("coloring.editable_background", false));
319 }
320 }
321 }
322
323 private class CreateListener
324 implements ActionListener {
325 public void actionPerformed(ActionEvent event) {
326 // Validate.
327 title_final = title.getText();
328 if(title_final.length() == 0) {
329 GPrompt err_msg = new GPrompt(self, get("Error"), get("Title_Error"), "error.gif", null);
330 err_msg.display();
331 err_msg.destroy();
332 err_msg = null;
333 title.setForeground(Gatherer.config.getColor("coloring.error_foreground", false));
334 title.setBackground(Gatherer.config.getColor("coloring.error_background", false));
335 return;
336 }
337 name_final = file.getText();
338 if(name_final.length() > 0) {
339 // Determine if this filename is already in use.
340 File collection_directory = new File(Utility.getCollectionDir(Gatherer.config.gsdl_path));
341 File children[] = collection_directory.listFiles();
342 for(int i = 0; children != null && i < children.length; i++) {
343 if(children[i].getName().equals(name_final)) {
344 GPrompt err_msg = new GPrompt(self, get("Error"), get("Name_Error"), "error.gif", null);
345 err_msg.display();
346 err_msg.destroy();
347 err_msg = null;
348 file.setForeground(Gatherer.config.getColor("coloring.error_foreground", false));
349 file.setBackground(Gatherer.config.getColor("coloring.error_background", false));
350 return;
351 }
352 }
353 }
354 else {
355 GPrompt err_msg = new GPrompt(self, get("Error"), get("Name_Error"), "error.gif", null);
356 err_msg.display();
357 err_msg.destroy();
358 err_msg = null;
359 file.setForeground(Gatherer.config.getColor("coloring.error_foreground", false));
360 file.setBackground(Gatherer.config.getColor("coloring.error_background", false));
361 return;
362 }
363 email_final = address.getText() + "@" + host.getText();
364 if(email_final.length() == 0 || email_final.startsWith("@") || email_final.endsWith("@")) {
365 GPrompt err_msg = new GPrompt(self, get("Error"), get("Email_Error"), "error.gif", null);
366 err_msg.display();
367 err_msg.destroy();
368 err_msg = null;
369 address.setForeground(Gatherer.config.getColor("coloring.error_foreground", false));
370 address.setBackground(Gatherer.config.getColor("coloring.error_background", false));
371 host.setForeground(Gatherer.config.getColor("coloring.error_foreground", false));
372 host.setBackground(Gatherer.config.getColor("coloring.error_background", false));
373 return;
374 }
375 description_final = description.getText();
376 if(description_final.length() == 0) {
377 GPrompt err_msg = new GPrompt(self, get("Error"), get("Description_Error"), "error.gif", null);
378 err_msg.display();
379 err_msg.destroy();
380 err_msg = null;
381 description.setForeground(Gatherer.config.getColor("coloring.error_foreground", false));
382 description.setBackground(Gatherer.config.getColor("coloring.error_background", false));
383 return;
384 }
385 // If we got this far there are no errors.
386 Item item_final = (Item) base_collection.getSelectedItem();
387 base_final = item_final.getFile();
388
389 cancelled = false;
390
391 self.dispose();
392 }
393 }
394
395 private class DescriptionListener
396 extends KeyAdapter {
397 public void keyPressed(KeyEvent event) {
398 if(event.getKeyCode() == KeyEvent.VK_TAB) {
399 event.consume();
400 base_collection.grabFocus();
401 }
402 }
403 }
404
405 private class Item
406 implements Comparable {
407 private File file;
408 private String name;
409 public Item(File file, String name) {
410 this.file = file;
411 this.name = name;
412 }
413 public int compareTo(Object other) {
414 return toString().toLowerCase().compareTo(other.toString().toLowerCase());
415 }
416 public boolean equals(Object other) {
417 return compareTo(other) == 0;
418 }
419 public File getFile() {
420 return file;
421 }
422 public String toString() {
423 return name;
424 }
425 }
426
427 private class RestrictedTextField
428 extends JTextField {
429 public RestrictedTextField(RestrictedTextDocument document, String value, int cols) {
430 super(document, "", cols);
431 }
432 protected Document createDefaultModel() {
433 return new RestrictedTextDocument(11);
434 }
435 }
436
437 private class RestrictedTextDocument
438 extends PlainDocument {
439 private char block[];
440 private int cols;
441 private int current;
442 public RestrictedTextDocument(int cols) {
443 super();
444 this.cols = cols;
445 this.current = 0;
446 }
447 public void blockChar(char c) {
448 if(block == null) {
449 block = new char[1];
450 block[0] = c;
451 }
452 else {
453 char temp[] = block;
454 block = new char[temp.length + 1];
455 System.arraycopy(temp, 0, block, 0, temp.length);
456 block[temp.length] = c;
457 temp = null;
458 }
459 }
460 public void insertString(int offs, String str, AttributeSet a)
461 throws BadLocationException {
462 // Remove any blocked characters.
463 StringBuffer temp = new StringBuffer(str);
464 for(int i = 0; block != null && i < block.length; i++) {
465 for(int j = temp.length() - 1; j >= 0; j--) {
466 if(temp.charAt(j) == block[i]) {
467 temp.deleteCharAt(j);
468 }
469 }
470 }
471 str = temp.toString();
472 if(cols == -1 || str.length() + current <= cols) {
473 super.insertString(offs, str, a);
474 current = current + str.length();
475 }
476 }
477 public void remove(int offs, int len)
478 throws BadLocationException {
479 super.remove(offs, len);
480 current = current - len;
481 }
482 }
483
484 private class TitleListener
485 implements DocumentListener {
486 /** Gives notification that an attribute or set of attributes changed. */
487 public void changedUpdate(DocumentEvent e) {
488 updateFilename();
489 }
490
491 /** Gives notification that there was an insert into the document. */
492 public void insertUpdate(DocumentEvent e) {
493 updateFilename();
494 }
495
496 /** Gives notification that a portion of the document has been removed. */
497 public void removeUpdate(DocumentEvent e) {
498 updateFilename();
499 }
500
501 private void updateFilename() {
502 String current_name = file.getText();
503 String current_title = title.getText();
504 StringBuffer temp = new StringBuffer("");
505 int i = 0;
506 while(i < current_title.length() && temp.length() < 8) {
507 if(current_title.charAt(i) != ' ') {
508 temp.append(Character.toLowerCase(current_title.charAt(i)));
509 }
510 i++;
511 }
512 String result = temp.toString();
513 if(current_name.startsWith(result) || result.startsWith(current_name)) {
514 file.setText(result);
515 }
516 }
517 }
518}
Note: See TracBrowser for help on using the repository browser.