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

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

Apparently I added a space. Weird

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