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

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

Changed calls to the Dictionary.

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