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

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

Fixed hardcoded change for dls - John

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