/**
*#########################################################################
*
* A component of the Gatherer application, part of the Greenstone digital
* library suite from the New Zealand Digital Library Project at the
* University of Waikato, New Zealand.
*
*
*
* Author: John Thompson, Greenstone Digital Library, University of Waikato
*
*
*
* Copyright (C) 1999 New Zealand Digital Library Project
*
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*########################################################################
*/
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.filechooser.*;
import javax.swing.table.*;
import org.greenstone.gatherer.Dictionary;
import org.greenstone.gatherer.Gatherer;
import org.greenstone.gatherer.file.FileAssociationManager;
import org.greenstone.gatherer.gui.GComboBox;
import org.greenstone.gatherer.gui.ModalDialog;
import org.greenstone.gatherer.gui.NonWhitespaceField;
import org.greenstone.gatherer.gui.OpenCollectionDialog;
import org.greenstone.gatherer.gui.SimpleMenuBar;
import org.greenstone.gatherer.util.TableUtils;
import org.greenstone.gatherer.util.Utility;
/** The file association allows the entry of new file associations and the modification of existing ones.
* @author John Thompson, Greenstone Digital Library, University of Waikato
* @version 2.3
*/
public class FileAssociationDialog
extends ModalDialog {
/** The default size of a label. */
static final private Dimension LABEL_SIZE = new Dimension(150, 25);
/** The default size for the dialog. */
static final private Dimension SIZE = new Dimension(600, 450);
private boolean ignore = false;
private FileAssociationDialog self;
private FileAssociationManager file_association_manager;
private JButton add_button;
private JButton browse_button;
private JButton close_button;
private JButton remove_button;
private JButton replace_button;
private JTable existing_associations_table;
private JTextField command_field;
private JTextField extension_field;
/** Create a new file association dialog.
* @param manager A reference to the FileAssociationManager so we can determine what extensions are already associated.
*/
public FileAssociationDialog(FileAssociationManager file_association_manager) {
super(Gatherer.g_man);
this.file_association_manager = file_association_manager;
this.self = this;
// Creation
setModal(true);
setSize(SIZE);
setJMenuBar(new SimpleMenuBar("fileassociations"));
JPanel content_pane = (JPanel) getContentPane();
content_pane.setBackground(Gatherer.config.getColor("coloring.collection_heading_background", false));
JTextArea instructions_area = new JTextArea();
instructions_area.setEditable(false);
instructions_area.setLineWrap(true);
instructions_area.setRows(5);
instructions_area.setWrapStyleWord(true);
Dictionary.setText(instructions_area, "FileAssociationDialog.Instructions");
JPanel control_pane = new JPanel();
JLabel existing_associations_label = new JLabel();
existing_associations_table = new JTable(file_association_manager);
existing_associations_table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
existing_associations_table.setColumnSelectionAllowed(false);
existing_associations_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JPanel lower_pane = new JPanel();
JPanel details_pane = new JPanel();
JPanel extension_pane = new JPanel();
JLabel extension_label = new JLabel();
extension_label.setPreferredSize(LABEL_SIZE);
extension_field = new NonWhitespaceField();
JLabel command_label = new JLabel();
JPanel command_pane = new JPanel();
command_field = new JTextField();
browse_button = new JButton();
JPanel button_pane = new JPanel();
add_button = new JButton();
add_button.setEnabled(false);
replace_button = new JButton();
replace_button.setEnabled(false);
remove_button = new JButton();
remove_button.setEnabled(false);
close_button = new JButton();
// Connection
add_button.addActionListener(new AddButtonListener());
browse_button.addActionListener(new BrowseButtonListener());
close_button.addActionListener(new CloseButtonListener());
remove_button.addActionListener(new RemoveButtonListener());
replace_button.addActionListener(new ReplaceButtonListener());
CommandOrExtensionFieldListener coefl = new CommandOrExtensionFieldListener();
command_field.getDocument().addDocumentListener(coefl);
extension_field.getDocument().addDocumentListener(coefl);
coefl = null;
existing_associations_table.getSelectionModel().addListSelectionListener(new ExistingAssociationsTableListener());
Dictionary.setBoth(add_button, "FileAssociationDialog.Add", "FileAssociationDialog.Add_Tooltip");
Dictionary.setBoth(browse_button, "FileAssociationDialog.Browse", "FileAssociationDialog.Browse_Tooltip");
Dictionary.setBoth(close_button, "FileAssociationDialog.Close", "FileAssociationDialog.Close_Tooltip");
Dictionary.setBoth(remove_button, "FileAssociationDialog.Remove", "FileAssociationDialog.Remove_Tooltip");
Dictionary.setBoth(replace_button, "FileAssociationDialog.Replace", "FileAssociationDialog.Replace_Tooltip");
Dictionary.setText(command_label, "FileAssociationDialog.Command");
Dictionary.setText(extension_label, "FileAssociationDialog.Extension");
Dictionary.setText(existing_associations_label, "FileAssociationDialog.Existing_Associations");
Dictionary.setText(this, "FileAssociationDialog.Title");
// Layout
extension_pane.setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
extension_pane.setLayout(new BorderLayout());
extension_pane.add(extension_label, BorderLayout.WEST);
extension_pane.add(extension_field, BorderLayout.CENTER);
command_pane.setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
command_pane.setLayout(new BorderLayout());
command_pane.add(command_field, BorderLayout.CENTER);
command_pane.add(browse_button, BorderLayout.EAST);
details_pane.setBorder(BorderFactory.createTitledBorder(Dictionary.get("FileAssociationDialog.Details")));
details_pane.setLayout(new GridLayout(3,1));
details_pane.add(extension_pane);
details_pane.add(command_label);
details_pane.add(command_pane);
lower_pane.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
button_pane.setLayout(new GridLayout(2,3));
button_pane.add(add_button);
button_pane.add(replace_button);
button_pane.add(remove_button);
button_pane.add(new JPanel());
button_pane.add(new JPanel());
button_pane.add(close_button);
lower_pane.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
lower_pane.setLayout(new BorderLayout());
lower_pane.add(details_pane, BorderLayout.CENTER);
lower_pane.add(button_pane, BorderLayout.SOUTH);
control_pane.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
control_pane.setLayout(new BorderLayout());
control_pane.add(existing_associations_label, BorderLayout.NORTH);
control_pane.add(new JScrollPane(existing_associations_table), BorderLayout.CENTER);
control_pane.add(lower_pane, BorderLayout.SOUTH);
content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
content_pane.setLayout(new BorderLayout());
content_pane.add(new JScrollPane(instructions_area), BorderLayout.NORTH);
content_pane.add(control_pane, BorderLayout.CENTER);
Rectangle screen = Gatherer.g_man.getBounds(null);
setLocation((int)(screen.getX() + (screen.getWidth() - SIZE.width) / 2), (int)(screen.getY() + (screen.getHeight() - SIZE.height) / 2));
screen = null;
}
public void destroy() {
// Disconnect
// Clean up
file_association_manager = null;
self = null;
}
/** Redisplay the dialog, ensuring that the details (or lack thereof) for a certain extension is apparent. In fact if an extension is provided force the user to add it or cancel.
* @param new_extension The extension code as a String.
*/
public String display(String new_extension) {
// Clear the current selection
existing_associations_table.clearSelection();
if(new_extension != null) {
extension_field.setText(new_extension);
}
setVisible(true);
if(new_extension != null) {
return file_association_manager.getCommandString(new_extension);
}
return null;
}
protected void processWindowEvent(WindowEvent event) {
if(event.getID() == WindowEvent.WINDOW_ACTIVATED) {
TableUtils.fixColumnToPreferredWidth(existing_associations_table, 0);
}
else {
super.processWindowEvent(event);
}
}
private class AddButtonListener
implements ActionListener {
public void actionPerformed(ActionEvent event) {
String extension_str = extension_field.getText();
file_association_manager.setCommand(extension_str, command_field.getText());
// Select the appropriate entry in the table
int index = -1;
for(int i = 0; index == -1 && i < file_association_manager.size(); i++) {
if(file_association_manager.getExtension(i).equals(extension_str)) {
index = i;
}
}
if(index > -1) {
existing_associations_table.setRowSelectionInterval(index, index);
}
// And update buttons
add_button.setEnabled(false);
remove_button.setEnabled(true);
}
}
/** Batch filter shows only files ending in bat. Based on ImageFilter.java which is a 1.4 example used by FileChooserDemo2.java. */
private class BatchFileFilter
extends FileFilter {
/** Accept all .exe files
* @param file a File
* @return true is this file should be shown, false otherwise
*/
public boolean accept(File file) {
return file.getName().toLowerCase().endsWith(".bat");
}
/** The description of this filter
* @return a String
*/
public String getDescription() {
return Dictionary.get("FileAssociationDialog.Batch_File");
}
}
/** Whenever the user clicks the browse button, we should open up a file browser to allow them to select an executable file from somewhere in the file system. */
private class BrowseButtonListener
implements ActionListener {
/** Open up a simple JFileChooser when the user clicks the button.
* @param event An ActionEvent containing information about the event.
*/
public void actionPerformed(ActionEvent event) {
JFileChooser chooser = new JFileChooser(new File(Utility.BASE_DIR));
OpenCollectionDialog.disableRename(chooser);
chooser.setDialogTitle(Dictionary.get("FileAssociationDialog.Browse_Title"));
chooser.setFileFilter(new BatchFileFilter());
chooser.setFileFilter(new CoreObjectModelFileFilter());
chooser.setFileFilter(new ExecutableFileFilter());
chooser.setAcceptAllFileFilterUsed(true);
if(chooser.showOpenDialog(Gatherer.g_man) == JFileChooser.APPROVE_OPTION) {
command_field.setText(chooser.getSelectedFile().getAbsolutePath());
}
}
}
private class CloseButtonListener
implements ActionListener {
public void actionPerformed(ActionEvent event) {
existing_associations_table.clearSelection();
add_button.setEnabled(false);
remove_button.setEnabled(false);
replace_button.setEnabled(false);
self.dispose();
}
}
/** Command filter shows only files ending in com. Based on ImageFilter.java which is a 1.4 example used by FileChooserDemo2.java. */
private class CoreObjectModelFileFilter
extends FileFilter {
/** Accept all .exe files
* @param file a File
* @return true is this file should be shown, false otherwise
*/
public boolean accept(File file) {
return file.getName().toLowerCase().endsWith(".com");
}
/** The description of this filter
* @return a String
*/
public String getDescription() {
return Dictionary.get("FileAssociationDialog.Command_File");
}
}
private class CommandOrExtensionFieldListener
implements DocumentListener {
/** Gives notification that an attribute or set of attributes changed. */
public void changedUpdate(DocumentEvent e) {
changed();
}
/** Gives notification that there was an insert into the document. */
public void insertUpdate(DocumentEvent e) {
changed();
}
/** Gives notification that a portion of the document has been removed. */
public void removeUpdate(DocumentEvent e) {
changed();
}
private void changed() {
ignore = true;
String extension_str = extension_field.getText();
String command_str = command_field.getText();
// Determine if there is currently an association selected
int selected_index = -1;
if((selected_index = existing_associations_table.getSelectionModel().getMinSelectionIndex()) != -1) {
String current_extension_str = file_association_manager.getExtension(selected_index);
String current_command_str = file_association_manager.getCommandString(current_extension_str);
// What buttons are enabled depends a lot on the file extension, taken to be the 'key'
if(extension_str.equals(current_extension_str)) {
// If the extensions are the same then remove is enabled
remove_button.setEnabled(true);
// But if the command is different, then enable replace
if(current_command_str.length() == 0) {
add_button.setEnabled(true);
replace_button.setEnabled(false);
}
else if(!command_str.equals(current_command_str)) {
add_button.setEnabled(false);
replace_button.setEnabled(true);
}
else {
add_button.setEnabled(false);
replace_button.setEnabled(false);
}
}
// We can add it, but we first clear the selection
else {
existing_associations_table.clearSelection();
add_button.setEnabled(true);
remove_button.setEnabled(false);
replace_button.setEnabled(false);
}
}
// If not, we must first test if the extension is in use
else {
int index = -1;
for(int i = 0; index == -1 && i < file_association_manager.size(); i++) {
String existing_extension_str = file_association_manager.getExtension(i);
if(existing_extension_str.equals(extension_str)) {
index = i;
}
}
if(index != -1) {
// Selection that index
existing_associations_table.setRowSelectionInterval(index, index);
// Set replace and remove enabled
add_button.setEnabled(false);
remove_button.setEnabled(true);
replace_button.setEnabled(true);
}
// Otherwise if there is some text in both extension and command, enable add only
else if(extension_str.length() > 0 && command_str.length() > 0) {
add_button.setEnabled(true);
remove_button.setEnabled(false);
replace_button.setEnabled(false);
}
// Otherwise disable everything
else {
add_button.setEnabled(false);
remove_button.setEnabled(false);
replace_button.setEnabled(false);
}
}
ignore = false;
}
}
/** Executable filter shows only files ending in exe. Based on ImageFilter.java which is a 1.4 example used by FileChooserDemo2.java. */
private class ExecutableFileFilter
extends FileFilter {
/** Accept all .exe files
* @param file a File
* @return true is this file should be shown, false otherwise
*/
public boolean accept(File file) {
return file.getName().toLowerCase().endsWith(".exe");
}
/** The description of this filter
* @return a String
*/
public String getDescription() {
return Dictionary.get("FileAssociationDialog.Executable_File");
}
}
private class ExistingAssociationsTableListener
implements ListSelectionListener {
public void valueChanged(ListSelectionEvent event) {
if(!event.getValueIsAdjusting() && !ignore) {
int selected_index = -1;
if((selected_index = existing_associations_table.getSelectionModel().getMinSelectionIndex()) != -1) {
// Propagate the details of the selection down into the fields
String extension_str = file_association_manager.getExtension(selected_index);
extension_field.setText(extension_str);
command_field.setText(file_association_manager.getCommandString(extension_str));
extension_str = null;
remove_button.setEnabled(true);
}
else {
// Clear the fields
extension_field.setText("");
command_field.setText("");
remove_button.setEnabled(false);
}
add_button.setEnabled(false);
replace_button.setEnabled(false);
}
}
}
private class RemoveButtonListener
implements ActionListener {
public void actionPerformed(ActionEvent event) {
file_association_manager.setCommand(extension_field.getText(), null);
// And update buttons
add_button.setEnabled(true);
remove_button.setEnabled(false);
}
}
private class ReplaceButtonListener
implements ActionListener {
public void actionPerformed(ActionEvent event) {
file_association_manager.setCommand(extension_field.getText(), command_field.getText());
// And update buttons
replace_button.setEnabled(false);
}
}
}