/**
*#########################################################################
*
* 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.collection;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import org.greenstone.gatherer.Configuration;
import org.greenstone.gatherer.DebugStream;
import org.greenstone.gatherer.Dictionary;
import org.greenstone.gatherer.Gatherer;
import org.greenstone.gatherer.gui.GLIButton;
import org.greenstone.gatherer.gui.ModalDialog;
import org.greenstone.gatherer.gui.SimpleMenuBar;
import org.greenstone.gatherer.gui.tree.WorkspaceTree;
import org.greenstone.gatherer.util.ArrayTools;
import org.greenstone.gatherer.util.GSDLSiteConfig;
import org.greenstone.gatherer.util.StaticStrings;
import org.greenstone.gatherer.util.Utility;
/** This class provides the functionality to delete current collections from the GSDLHOME/collect/ directory. The user chooses the collection from a list, where each entry also displays details about itself, confirms the delete of a collection by checking a checkbox then presses the ok button to actually delete the collection.
* @author John Thompson, Greenstone Digital Library, University of Waikato
* @version 2.3
*/
public class DeleteCollectionPrompt
extends ModalDialog {
/** The currently selected collection for deletion. */
private BasicCollectionConfiguration collection = null;
/** The model behind the list. */
private DefaultListModel list_model = null;
/** A reference to ourself so any inner-classes can dispose of us. */
private DeleteCollectionPrompt prompt = null;
/** The close button, which exits the prompt without deleting anything. */
private JButton close_button = null;
/** The ok button which causes the selected collection to be deleted. */
private JButton ok_button = null;
/** The confirmation check box. */
private JCheckBox confirmation = null;
/** The label above details. */
private JLabel details_label = null;
/** The label above the list. */
private JLabel list_label = null;
/** The list of available collections. */
private JList list = null;
/** The text area used to display details about the collection selected. */
private JTextArea details = null;
/** A string array used to pass arguments to the phrase retrieval method. */
private String args[] = null;
private boolean current_coll_deleted = false;
/** The size of the delete prompt screen. */
public static final Dimension SIZE = new Dimension(500, 500);
/** Constructor.
* @see org.greenstone.gatherer.collection.DeleteCollectionPrompt.CloseButtonListener
* @see org.greenstone.gatherer.collection.DeleteCollectionPrompt.CollectionListListener
* @see org.greenstone.gatherer.collection.DeleteCollectionPrompt.ConfirmationCheckBoxListener
* @see org.greenstone.gatherer.collection.DeleteCollectionPrompt.OKButtonListener
*/
public DeleteCollectionPrompt() {
super(Gatherer.g_man);
close_button = new GLIButton();
close_button.setMnemonic(KeyEvent.VK_C);
Dictionary.setBoth(close_button, "General.Close", "General.Close_Tooltip");
confirmation = new JCheckBox();
Dictionary.setText(confirmation, "DeleteCollectionPrompt.Confirm_Delete");
details = new JTextArea();
details.setEditable(false);
details.setFont(Configuration.getFont("general.tooltip_font", false));
Dictionary.setText(details, "DeleteCollectionPrompt.No_Collection");
details_label = new JLabel();
Dictionary.setText(details_label, "DeleteCollectionPrompt.Collection_Details");
list = new JList();
list_label = new JLabel();
Dictionary.setText(list_label, "DeleteCollectionPrompt.Collection_List");
list_model = new DefaultListModel();
ok_button = new GLIButton();
ok_button.setMnemonic(KeyEvent.VK_D);
Dictionary.setBoth(ok_button, "DeleteCollectionPrompt.Delete", "DeleteCollectionPrompt.Delete_Tooltip");
prompt = this;
setModal(true);
setSize(SIZE);
Dictionary.setText(this, "DeleteCollectionPrompt.Title");
setJMenuBar(new SimpleMenuBar("0")); // need to find an appropriate help page to open at
close_button.addActionListener(new CloseButtonListener());
confirmation.addActionListener(new ConfirmationCheckBoxListener());
confirmation.setEnabled(false);
confirmation.setSelected(false);
list.addListSelectionListener(new CollectionListListener());
list.clearSelection();
list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setModel(list_model);
ok_button.addActionListener(new OKButtonListener());
ok_button.setEnabled(false);
scanForCollections();
}
/** Destructor. */
public void destroy() {
list_model.clear();
list_model = null;
close_button = null;
confirmation = null;
details = null;
details_label = null;
list = null;
ok_button = null;
prompt = null;
}
/** This method causes the modal prompt to be displayed.
* returns true if it has deleted the collection that is currently open */
public boolean display() {
// Central pane
JPanel list_pane = new JPanel(new BorderLayout());
list_pane.add(list_label, BorderLayout.NORTH);
list_pane.add(new JScrollPane(list), BorderLayout.CENTER);
list_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
JPanel details_pane = new JPanel(new BorderLayout());
details_pane.add(details_label, BorderLayout.NORTH);
details_pane.add(new JScrollPane(details), BorderLayout.CENTER);
details_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
JPanel central_pane = new JPanel(new GridLayout(2, 1));
central_pane.add(list_pane);
central_pane.add(details_pane);
central_pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// Lower pane
JPanel confirmation_pane = new JPanel(new BorderLayout());
confirmation_pane.add(confirmation, BorderLayout.CENTER);
confirmation_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
JPanel button_pane = new JPanel(new GridLayout(1, 2));
button_pane.add(ok_button);
button_pane.add(close_button);
button_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
JPanel lower_pane = new JPanel(new BorderLayout());
lower_pane.add(confirmation_pane, BorderLayout.NORTH);
lower_pane.add(button_pane, BorderLayout.SOUTH);
lower_pane.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
// Final.
JPanel content_pane = (JPanel)this.getContentPane();
content_pane.setLayout(new BorderLayout());
content_pane.add(central_pane, BorderLayout.CENTER);
content_pane.add(lower_pane, BorderLayout.SOUTH);
// Center and display.
Dimension screen_size = Configuration.screen_size;
this.setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
this.setVisible(true); // blocks until the dialog is killed
return current_coll_deleted;
}
/** Shows a delete complete prompt.
* @param success A boolean indicating if the collection was successfully deleted.
* @see org.greenstone.gatherer.collection.Collection
*/
public void resultPrompt(boolean success) {
args = new String[1];
args[0] = collection.getName();
if (success) {
JOptionPane.showMessageDialog(prompt,Dictionary.get("DeleteCollectionPrompt.Successful_Delete", args),Dictionary.get("DeleteCollectionPrompt.Successful_Title"),JOptionPane.INFORMATION_MESSAGE);
}
else {
JOptionPane.showMessageDialog(prompt,Dictionary.get("DeleteCollectionPrompt.Failed_Delete", args),Dictionary.get("DeleteCollectionPrompt.Failed_Title"),JOptionPane.WARNING_MESSAGE);
}
}
/** Method to scan the collect directory retrieving and reloading each collection it finds, while building the list of known collections.
* @see org.greenstone.gatherer.Configuration
* @see org.greenstone.gatherer.Gatherer
* @see org.greenstone.gatherer.util.ArrayTools
* @see org.greenstone.gatherer.util.Utility
*/
private void scanForCollections() {
// Start at the collect dir.
File collect_directory;
if (Gatherer.GS3) {
collect_directory = new File(Utility.getCollectDir(Configuration.gsdl3_path, Configuration.site_name));
} else {
collect_directory = new File(Utility.getCollectDir(Configuration.gsdl_path));
}
if(collect_directory.exists()) {
// Now for each child directory see if it contains a .col file and
// if so try to load it..
File collections[] = collect_directory.listFiles();
ArrayTools.sort(collections);
for(int i = 0; collections != null && i < collections.length; i++) {
if(collections[i].isDirectory() && !collections[i].getName().equals(StaticStrings.MODEL_COLLECTION_NAME)) {
File config_file = new File(collections[i], Utility.CONFIG_FILE);
if (config_file.exists()) {
BasicCollectionConfiguration config = new BasicCollectionConfiguration(config_file);
list_model.addElement(config);
config = null;
}
}
}
}
// Otherwise the collect directory doesn't actually exist, so there ain't much we can do.
}
/** A button listener implementation, which listens for actions on the close button and disposes of the dialog when detected. */
private class CloseButtonListener
implements ActionListener {
/** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
* @param event An ActionEvent containing all the relevant information garnered from the event itself.
*/
public void actionPerformed(ActionEvent event) {
// Done
prompt.dispose();
}
}
/** This private class listens for selection events in from the list and then displays the appropriate details for that collection.
*/
private class CollectionListListener
implements ListSelectionListener {
/** Any implementation of ListSelectionListener must include this method so we can be informed when the list selection changes.
* @param event a ListSelectionEvent containing all the relevant information garnered from the event itself
*/
public void valueChanged(ListSelectionEvent event) {
ok_button.setEnabled(false);
confirmation.setSelected(false);
if(!list.isSelectionEmpty()) {
confirmation.setEnabled(true);
collection = (BasicCollectionConfiguration) list.getSelectedValue();
args = new String[5];
args[0] = collection.getName();
args[1] = collection.getShortName();
args[2] = collection.getCreator();
args[3] = collection.getMaintainer();
args[4] = collection.getDescription();
Dictionary.setText(details, "DeleteCollectionPrompt.Details", args);
details.setCaretPosition(0);
}
else {
confirmation.setEnabled(false);
Dictionary.setText(details, "DeleteCollectionPrompt.No_Collection");
}
}
}
/** A check box listener so we can tell if the user has confirmed the deletion */
private class ConfirmationCheckBoxListener
implements ActionListener {
/** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
* @param event An ActionEvent containing all the relevant information garnered from the event itself.
*/
public void actionPerformed(ActionEvent event) {
// OK button is only enabled if the confirmation check box is ticked
ok_button.setEnabled(confirmation.isSelected());
}
}
/** The OK button listener implementation. */
private class OKButtonListener
implements ActionListener {
/** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
* @param event An ActionEvent containing all the relevant information garnered from the event itself.
* @see org.greenstone.gatherer.Configuration
* @see org.greenstone.gatherer.Gatherer
* @see org.greenstone.gatherer.util.Utility
*/
public void actionPerformed(ActionEvent event) {
// Delete the selected collection.
// first of all we must release it from the local library
if (Configuration.exec_file != null) {
///ystem.err.println("Local Library Found!");
Gatherer.self.configServer(GSDLSiteConfig.RELEASE_COMMAND + collection.getShortName());
// This is very important -- it ensures that the above command has finished
Gatherer.self.configServer("");
}
if (Gatherer.isGsdlRemote) {
String launch_cgi = Gatherer.cgiBase + "launch?cmd=deldir";
String col_name = collection.getShortName();
String dir = ".";
try {
String col_name_encoded = URLEncoder.encode(col_name,"UTF-8");
String dir_encoded = URLEncoder.encode(dir,"UTF-8");
launch_cgi += "&c=" + col_name_encoded;
launch_cgi += "&dir=" + dir_encoded;
URL launch_url = new URL(launch_cgi);
URLConnection launch_connection = launch_url.openConnection();
InputStream stdis = launch_connection.getInputStream();
InputStreamReader stdisr = new InputStreamReader(stdis, "UTF-8" );
BufferedReader stdbr = new BufferedReader(stdisr);
while(true) {
String line = stdbr.readLine();
// ignore output of running lauch command
if (line == null) { break; }
}
stdbr.close();
}
// Exception
catch (Exception exception) {
DebugStream.println("Exception in GShell.runRemove() - unexpected");
DebugStream.printStackTrace(exception);
}
}
File delete_me;
if (Gatherer.GS3) {
delete_me = new File(Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, collection.getShortName()));
} else {
delete_me = new File(Utility.getCollectionDir(Configuration.gsdl_path, collection.getShortName()));
}
if (Utility.delete(delete_me)) {
if (Gatherer.c_man.getCollection() != null && collection.getShortName().equals(Gatherer.c_man.getCollection().getName())) {
current_coll_deleted = true;
}
list_model.removeElement(collection);
// Refresh the collections shown in the workspace tree
Gatherer.g_man.refreshWorkspaceTree(WorkspaceTree.LIBRARY_CONTENTS_CHANGED);
resultPrompt(true);
Dictionary.setText(details, "DeleteCollectionPrompt.No_Collection");
confirmation.setEnabled(false);
confirmation.setSelected(false);
ok_button.setEnabled(false);
collection = null;
}
else {
resultPrompt(false);
}
}
}
}