package org.greenstone.gatherer.gui; import java.awt.*; import java.awt.event.*; import java.io.File; import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import org.greenstone.gatherer.Dictionary; import org.greenstone.gatherer.Gatherer; import org.greenstone.gatherer.collection.CollectionConfiguration; import org.greenstone.gatherer.util.Utility; import org.greenstone.gatherer.gui.SimpleMenuBar; import org.greenstone.gatherer.gui.ModalDialog; public class NewCollectionDetailsPrompt extends ModalDialog { private boolean cancelled; private File base_final; private JButton create_button; private JComboBox base_collection; private JDialog self; private JTextArea description; private JTextField address; private JTextField host; private JTextField title; private RestrictedTextField file; private String description_final; private String email_final; private String name_final; private String title_final; static private Dimension label_size = new Dimension(230, 25); /** The size of this new collection dialog box. */ static private Dimension size = new Dimension(700, 375); static private int FILENAME_SIZE = 8; /** Constructor. * @see org.greenstone.gatherer.util.Utility */ public NewCollectionDetailsPrompt() { super(Gatherer.g_man, true); this.cancelled = true; this.self = this; // Setup setJMenuBar(new SimpleMenuBar("creatingacollection")); setSize(size); Dictionary.setText(this, "NewCollectionPrompt.Title"); // Model building. Build a model of all of the collections in the gsdl collect directory with the appropriate directories and hardcode the big five. Vector base_collection_model = new Vector(); // Dummy item File gsdl_collection_directory = new File(Utility.getCollectionDir(Gatherer.config.gsdl_path)); File[] possible_collections = gsdl_collection_directory.listFiles(); for(int i = 0; possible_collections != null && i < possible_collections.length; i++) { // The simpliest case is if the directory etc/collect.cfg file and a metadata/ in it. Thus it can easily be built upon. File collect_cfg_file = new File(possible_collections[i], Utility.CONFIG_DIR); File metadata_directory = new File(possible_collections[i], Utility.META_DIR); if(collect_cfg_file.exists()) { CollectionConfiguration collect_cfg = new CollectionConfiguration(collect_cfg_file); String collection_name = collect_cfg.getName(); // Even if there is no metadata directory we add it if its one of the 'big five + 1' that we know how to handle. if(metadata_directory.exists() || collection_name.equals(Utility.COLLECTION_DLS) || collection_name.equals(Utility.COLLECTION_DEMO)) { // Add to model. Item item = new Item(possible_collections[i], collection_name); if(!base_collection_model.contains(item)) { base_collection_model.add(item); } } // Else not a collection we know how to retrieve the metadata set for. } // Else not a collection at all. Someones pulling a fast one. } // Sort the result. Collections.sort(base_collection_model); base_collection_model.add(0, new Item(null, Dictionary.get("NewCollectionPrompt.NewCollection"))); // Creation JPanel content_pane = (JPanel) getContentPane(); content_pane.setOpaque(true); JPanel upper_pane = new JPanel(); JLabel instructions_label = new JLabel(); Dictionary.setText(instructions_label, "NewCollectionPrompt.Instructions"); JPanel title_pane = new JPanel(); JLabel title_label = new JLabel(); Dictionary.setText(title_label, "CDM.General.Collection_Name"); title = new JTextField(); Dictionary.setTooltip(title, "CDM.General.Collection_Name_Tooltip"); JPanel name_pane = new JPanel(); JLabel name_label = new JLabel(); Dictionary.setText(name_label, "NewCollectionPrompt.Collection_Name"); JPanel file_pane = new JPanel(); file = new RestrictedTextField(new RestrictedTextDocument(FILENAME_SIZE), "", FILENAME_SIZE); Dictionary.setTooltip(file, "NewCollectionPrompt.Collection_Name_Tooltip"); JLabel file_label = new JLabel(".col"); JPanel email_pane = new JPanel(); JLabel email_label = new JLabel(); Dictionary.setText(email_label, "NewCollectionPrompt.Collection_Email"); JPanel host_pane = new JPanel(); JPanel address_pane = new JPanel(); address = new JTextField(); Dictionary.setTooltip(address, "CDM.General.Email.Creator_Tooltip"); JLabel at_label = new JLabel("@"); host = new JTextField(); Dictionary.setTooltip(host, "CDM.General.Email.Creator_Tooltip"); JPanel center_pane = new JPanel(); JPanel description_pane = new JPanel(); JLabel description_label = new JLabel(); Dictionary.setText(description_label, "NewCollectionPrompt.Collection_Description"); description = new JTextArea(); description.setBackground(Gatherer.config.getColor("coloring.editable_background", false)); description.setForeground(Gatherer.config.getColor("coloring.editable_foreground", false)); description.setRows(5); Dictionary.setTooltip(description, "CDM.General.Collection_Extra_Tooltip"); JPanel bottom_pane = new JPanel(); // Base Collection JPanel base_collection_pane = new JPanel(); JLabel base_collection_label = new JLabel(); Dictionary.setText(base_collection_label, "NewCollectionPrompt.Base_Collection"); base_collection = new JComboBox(base_collection_model); Dictionary.setTooltip(base_collection, "NewCollectionPrompt.Base_Collection_Tooltip"); JPanel button_pane = new JPanel(); create_button = new JButton(); create_button.setMnemonic(KeyEvent.VK_O); Dictionary.setBoth(create_button, "General.OK", "General.OK_Tooltip"); JButton cancel_button = new JButton(); cancel_button.setMnemonic(KeyEvent.VK_C); Dictionary.setBoth(cancel_button, "General.Cancel", "General.Cancel_Tooltip"); ColorListener email_color_listener = new ColorListener(address, host); // Connection cancel_button.addActionListener(new CancelListener()); create_button.addActionListener(new CreateListener()); address.addKeyListener(email_color_listener); description.addKeyListener(new ColorListener(description)); description.addKeyListener(new DescriptionListener()); file.addKeyListener(new ColorListener(file)); host.addKeyListener(email_color_listener); title.addKeyListener(new ColorListener(title)); title.getDocument().addDocumentListener(new TitleListener()); // Layout title_label.setPreferredSize(label_size); title_pane.setLayout(new BorderLayout()); title_pane.add(title_label, BorderLayout.WEST); title_pane.add(title, BorderLayout.CENTER); file_pane.setLayout(new BorderLayout()); file_pane.add(file, BorderLayout.WEST); file_pane.add(file_label, BorderLayout.CENTER); name_label.setPreferredSize(label_size); name_pane.setLayout(new BorderLayout()); name_pane.add(name_label, BorderLayout.WEST); name_pane.add(file_pane, BorderLayout.CENTER); email_label.setPreferredSize(label_size); address_pane.setLayout(new BorderLayout()); address_pane.add(address, BorderLayout.CENTER); address_pane.add(at_label, BorderLayout.EAST); host_pane.setLayout(new GridLayout(1,2)); host_pane.add(address_pane); host_pane.add(host); email_pane.setLayout(new BorderLayout()); email_pane.add(email_label, BorderLayout.WEST); email_pane.add(host_pane, BorderLayout.CENTER); upper_pane.setLayout(new GridLayout(4,1)); upper_pane.add(instructions_label); upper_pane.add(title_pane); upper_pane.add(name_pane); upper_pane.add(email_pane); description_pane.setLayout(new BorderLayout()); description_pane.add(description_label, BorderLayout.NORTH); description_pane.add(new JScrollPane(description), BorderLayout.CENTER); base_collection_label.setPreferredSize(label_size); base_collection_pane.setLayout(new BorderLayout()); base_collection_pane.add(base_collection_label, BorderLayout.WEST); base_collection_pane.add(base_collection, BorderLayout.CENTER); // base_collection_pane.add(base_collection_browse, BorderLayout.EAST); center_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0)); center_pane.setLayout(new BorderLayout()); center_pane.add(description_pane, BorderLayout.CENTER); bottom_pane.setLayout(new BorderLayout()); bottom_pane.add(base_collection_pane, BorderLayout.CENTER); bottom_pane.add(button_pane, BorderLayout.SOUTH); button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0)); button_pane.setLayout(new GridLayout(1,2)); button_pane.add(create_button); button_pane.add(cancel_button); content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); content_pane.setLayout(new BorderLayout()); content_pane.add(upper_pane, BorderLayout.NORTH); content_pane.add(center_pane, BorderLayout.CENTER); content_pane.add(bottom_pane, BorderLayout.SOUTH); // Final dialog setup & positioning. Dimension screen_size = Gatherer.config.screen_size; setLocation((screen_size.width - size.width) / 2, (screen_size.height - size.height) / 2); setVisible(true); } public boolean isCancelled() { return cancelled; } public File getBase() { return base_final; } public String getDescription() { return description_final; } public String getEmail() { return email_final; } public String getName() { return name_final; } public String getTitle() { return title_final; } private class BrowseListener implements ActionListener { public void actionPerformed(ActionEvent event) { File file; if(Gatherer.config.gsdl_path != null) { file = new File(Utility.getCollectionDir(Gatherer.config.gsdl_path)); } else { file = new File(Utility.BASE_DIR); } // Show OpenCollectionPrompt. OpenCollectionDialog chooser = new OpenCollectionDialog(file); file = chooser.getSelectedFile(); if(file != null) { file = file.getParentFile(); CollectionConfiguration collect_cfg = new CollectionConfiguration(new File(file, Utility.META_DIR)); Item item = new Item(file, collect_cfg.getName()); base_collection.addItem(item); base_collection.setSelectedItem(item); } } } private class CancelListener implements ActionListener { public void actionPerformed(ActionEvent event) { cancelled = true; self.dispose(); } } private class ColorListener extends KeyAdapter { private JTextComponent component1; private JTextComponent component2; public ColorListener(JTextComponent component) { this.component1 = component; this.component2 = null; } public ColorListener(JTextComponent component1, JTextComponent component2) { this.component1 = component1; this.component2 = component2; } public void keyPressed(KeyEvent event) { component1.setForeground(Gatherer.config.getColor("coloring.editable_foreground", false)); component1.setBackground(Gatherer.config.getColor("coloring.editable_background", false)); if(component2 != null) { component2.setForeground(Gatherer.config.getColor("coloring.editable_foreground", false)); component2.setBackground(Gatherer.config.getColor("coloring.editable_background", false)); } } } private class CreateListener implements ActionListener { public void actionPerformed(ActionEvent event) { // Validate. title_final = title.getText(); if(title_final.length() == 0) { JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Title_Error"), Dictionary.get("NewCollectionPrompt.Error"), JOptionPane.ERROR_MESSAGE); title.setForeground(Gatherer.config.getColor("coloring.error_foreground", false)); title.setBackground(Gatherer.config.getColor("coloring.error_background", false)); return; } name_final = file.getText(); if(name_final.length() > 0) { // Determine if this filename is already in use. File collection_directory = new File(Utility.getCollectionDir(Gatherer.config.gsdl_path)); File children[] = collection_directory.listFiles(); for(int i = 0; children != null && i < children.length; i++) { if(children[i].getName().equals(name_final)) { JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Name_Error"), Dictionary.get("NewCollectionPrompt.Error"), JOptionPane.ERROR_MESSAGE); file.setForeground(Gatherer.config.getColor("coloring.error_foreground", false)); file.setBackground(Gatherer.config.getColor("coloring.error_background", false)); return; } } } else { JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Name_Error"), Dictionary.get("NewCollectionPrompt.Error"), JOptionPane.ERROR_MESSAGE); file.setForeground(Gatherer.config.getColor("coloring.error_foreground", false)); file.setBackground(Gatherer.config.getColor("coloring.error_background", false)); return; } email_final = address.getText() + "@" + host.getText(); if(email_final.length() == 0 || email_final.startsWith("@") || email_final.endsWith("@")) { JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Email_Error"), Dictionary.get("NewCollectionPrompt.Error"), JOptionPane.ERROR_MESSAGE); address.setForeground(Gatherer.config.getColor("coloring.error_foreground", false)); address.setBackground(Gatherer.config.getColor("coloring.error_background", false)); host.setForeground(Gatherer.config.getColor("coloring.error_foreground", false)); host.setBackground(Gatherer.config.getColor("coloring.error_background", false)); return; } description_final = description.getText(); if(description_final.length() == 0) { JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Description_Error"), Dictionary.get("NewCollectionPrompt.Error"), JOptionPane.ERROR_MESSAGE); description.setForeground(Gatherer.config.getColor("coloring.error_foreground", false)); description.setBackground(Gatherer.config.getColor("coloring.error_background", false)); return; } // If we got this far there are no errors. Item item_final = (Item) base_collection.getSelectedItem(); base_final = item_final.getFile(); cancelled = false; self.dispose(); } } private class DescriptionListener extends KeyAdapter { public void keyPressed(KeyEvent event) { if(event.getKeyCode() == KeyEvent.VK_TAB) { event.consume(); base_collection.grabFocus(); } } } private class Item implements Comparable { private File file; private String name; public Item(File file, String name) { this.file = file; this.name = name; } public int compareTo(Object other) { return toString().toLowerCase().compareTo(other.toString().toLowerCase()); } public boolean equals(Object other) { return compareTo(other) == 0; } public File getFile() { return file; } public String toString() { return name; } } private class RestrictedTextField extends JTextField { public RestrictedTextField(RestrictedTextDocument document, String value, int cols) { super(document, "", cols); } protected Document createDefaultModel() { return new RestrictedTextDocument(11); } } private class RestrictedTextDocument extends PlainDocument { private char block[]; private int cols; private int current; public RestrictedTextDocument(int cols) { super(); this.cols = cols; this.current = 0; } public void blockChar(char c) { if(block == null) { block = new char[1]; block[0] = c; } else { char temp[] = block; block = new char[temp.length + 1]; System.arraycopy(temp, 0, block, 0, temp.length); block[temp.length] = c; temp = null; } } public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { // Remove any blocked characters. StringBuffer temp = new StringBuffer(str); for(int i = 0; block != null && i < block.length; i++) { for(int j = temp.length() - 1; j >= 0; j--) { if(temp.charAt(j) == block[i]) { temp.deleteCharAt(j); } } } str = temp.toString(); if(cols == -1 || str.length() + current <= cols) { super.insertString(offs, str, a); current = current + str.length(); } } public void remove(int offs, int len) throws BadLocationException { super.remove(offs, len); current = current - len; } } private class TitleListener implements DocumentListener { /** Gives notification that an attribute or set of attributes changed. */ public void changedUpdate(DocumentEvent e) { updateFilename(); } /** Gives notification that there was an insert into the document. */ public void insertUpdate(DocumentEvent e) { updateFilename(); } /** Gives notification that a portion of the document has been removed. */ public void removeUpdate(DocumentEvent e) { updateFilename(); } private void updateFilename() { String current_name = file.getText(); String current_title = title.getText(); StringBuffer temp = new StringBuffer(""); int i = 0; while(i < current_title.length() && temp.length() < 8) { if(current_title.charAt(i) != ' ') { temp.append(Character.toLowerCase(current_title.charAt(i))); } i++; } String result = temp.toString(); if(current_name.startsWith(result) || result.startsWith(current_name)) { file.setText(result); } } } }