source: gli/trunk/src/org/greenstone/gatherer/gui/DeleteCollectionPrompt.java@ 15156

Last change on this file since 15156 was 15156, checked in by ak19, 16 years ago

Doesn't allow users to delete the currently open collection. Modified valueChanged() event-handler to disable delete button for it and show a tooltip over the confirmation checkbox and delete button explaining why the chosen collection can't be deleted

  • Property svn:keywords set to Author Date Id Revision
File size: 13.6 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.gui;
38
39import java.awt.*;
40import java.awt.event.*;
41import java.io.*;
42import java.net.*;
43import javax.swing.*;
44import javax.swing.event.*;
45import org.greenstone.gatherer.Configuration;
46import org.greenstone.gatherer.DebugStream;
47import org.greenstone.gatherer.Dictionary;
48import org.greenstone.gatherer.Gatherer;
49import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
50import org.greenstone.gatherer.collection.CollectionManager;
51import org.greenstone.gatherer.file.WorkspaceTree; // !!! Don't like this here
52import org.greenstone.gatherer.util.ArrayTools;
53import org.greenstone.gatherer.util.StaticStrings;
54import org.greenstone.gatherer.util.Utility;
55
56/** 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.
57 * @author John Thompson, Greenstone Digital Library, University of Waikato
58 * @version 2.3
59 */
60public class DeleteCollectionPrompt
61 extends ModalDialog {
62 /** The currently selected collection for deletion. */
63 private BasicCollectionConfiguration collection = null;
64 /** The model behind the list. */
65 private DefaultListModel list_model = null;
66 /** A reference to ourself so any inner-classes can dispose of us. */
67 private DeleteCollectionPrompt prompt = null;
68 /** The close button, which exits the prompt without deleting anything. */
69 private JButton close_button = null;
70 /** The ok button which causes the selected collection to be deleted. */
71 private JButton ok_button = null;
72 /** The confirmation check box. */
73 private JCheckBox confirmation = null;
74 /** The label above details. */
75 private JLabel details_label = null;
76 /** The label above the list. */
77 private JLabel list_label = null;
78 /** The list of available collections. */
79 private JList list = null;
80 /** The text area used to display details about the collection selected. */
81 private JTextArea details = null;
82 /** A string array used to pass arguments to the phrase retrieval method. */
83 private String args[] = null;
84
85 private boolean current_coll_deleted = false;
86 /** The size of the delete prompt screen. */
87 public static final Dimension SIZE = new Dimension(500, 500);
88
89 /** Constructor.
90 */
91 public DeleteCollectionPrompt() {
92 super(Gatherer.g_man);
93 close_button = new GLIButton(Dictionary.get("General.Close"), Dictionary.get("General.Close_Tooltip"));
94
95 confirmation = new JCheckBox(Dictionary.get("DeleteCollectionPrompt.Confirm_Delete"));
96 details = new JTextArea(Dictionary.get("DeleteCollectionPrompt.No_Collection"));
97 details.setEditable(false);
98 details_label = new JLabel(Dictionary.get("DeleteCollectionPrompt.Collection_Details"));
99
100 list = new JList();
101 list_label = new JLabel(Dictionary.get("DeleteCollectionPrompt.Collection_List"));
102
103 list_model = new DefaultListModel();
104 ok_button = new GLIButton(Dictionary.get("DeleteCollectionPrompt.Delete"), Dictionary.get("DeleteCollectionPrompt.Delete_Tooltip"));
105
106 prompt = this;
107 setModal(true);
108 setSize(SIZE);
109 setTitle(Dictionary.get("DeleteCollectionPrompt.Title"));
110
111 setJMenuBar(new SimpleMenuBar("deletingcollections"));
112 close_button.addActionListener(new CloseButtonListener());
113 confirmation.addActionListener(new ConfirmationCheckBoxListener());
114 confirmation.setEnabled(false);
115 confirmation.setSelected(false);
116 list.addListSelectionListener(new CollectionListListener());
117 list.clearSelection();
118 list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
119 list.setModel(list_model);
120 ok_button.addActionListener(new OKButtonListener());
121 ok_button.setEnabled(false);
122 scanForCollections();
123 }
124
125 /** Destructor. */
126 public void destroy() {
127 list_model.clear();
128 list_model = null;
129 close_button = null;
130 confirmation = null;
131 details = null;
132 details_label = null;
133 list = null;
134 ok_button = null;
135 prompt = null;
136 }
137
138 /** This method causes the modal prompt to be displayed.
139 * returns true if it has deleted the collection that is currently open */
140 public boolean display() {
141 // Central pane
142 JPanel list_pane = new JPanel(new BorderLayout());
143 list_pane.add(list_label, BorderLayout.NORTH);
144 list_pane.add(new JScrollPane(list), BorderLayout.CENTER);
145 list_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
146
147 JPanel details_pane = new JPanel(new BorderLayout());
148 details_pane.add(details_label, BorderLayout.NORTH);
149 details_pane.add(new JScrollPane(details), BorderLayout.CENTER);
150 details_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
151
152 JPanel central_pane = new JPanel(new GridLayout(2, 1));
153 central_pane.add(list_pane);
154 central_pane.add(details_pane);
155 central_pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
156
157 // Lower pane
158 JPanel confirmation_pane = new JPanel(new BorderLayout());
159 confirmation_pane.add(confirmation, BorderLayout.CENTER);
160 confirmation_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
161
162 JPanel button_pane = new JPanel(new GridLayout(1, 2));
163 button_pane.add(ok_button);
164 button_pane.add(close_button);
165 button_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
166
167 JPanel lower_pane = new JPanel(new BorderLayout());
168 lower_pane.add(confirmation_pane, BorderLayout.NORTH);
169 lower_pane.add(button_pane, BorderLayout.SOUTH);
170 lower_pane.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
171
172 // Final.
173 JPanel content_pane = (JPanel)this.getContentPane();
174 content_pane.setLayout(new BorderLayout());
175 content_pane.add(central_pane, BorderLayout.CENTER);
176 content_pane.add(lower_pane, BorderLayout.SOUTH);
177
178 // Center and display.
179 Dimension screen_size = Configuration.screen_size;
180 this.setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
181 this.setVisible(true); // blocks until the dialog is killed
182 return current_coll_deleted;
183 }
184
185
186 /** Shows a delete complete prompt.
187 * @param success A <strong>boolean</strong> indicating if the collection was successfully deleted.
188 * @see org.greenstone.gatherer.collection.Collection
189 */
190 public void resultPrompt(boolean success) {
191 args = new String[1];
192 args[0] = collection.getName();
193 if (success) {
194 JOptionPane.showMessageDialog(prompt,Dictionary.get("DeleteCollectionPrompt.Successful_Delete", args),Dictionary.get("DeleteCollectionPrompt.Successful_Title"),JOptionPane.INFORMATION_MESSAGE);
195 }
196 else {
197 JOptionPane.showMessageDialog(prompt,Dictionary.get("DeleteCollectionPrompt.Failed_Delete", args),Dictionary.get("DeleteCollectionPrompt.Failed_Title"),JOptionPane.WARNING_MESSAGE);
198 }
199 }
200
201 /** Method to scan the collect directory retrieving and reloading each collection it finds, while building the list of known collections.
202 * @see org.greenstone.gatherer.Configuration
203 * @see org.greenstone.gatherer.Gatherer
204 * @see org.greenstone.gatherer.util.ArrayTools
205 * @see org.greenstone.gatherer.util.Utility
206 */
207 private void scanForCollections() {
208 // Start at the collect dir.
209 File collect_directory = new File(Gatherer.getCollectDirectoryPath());
210 if (collect_directory.exists()) {
211 // Now for each child directory see if it contains a .col file and
212 // if so try to load it..
213 File collections[] = collect_directory.listFiles();
214 String file_name = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
215 ArrayTools.sort(collections);
216 for(int i = 0; collections != null && i < collections.length; i++) {
217 if(collections[i].isDirectory() && !collections[i].getName().equals(StaticStrings.MODEL_COLLECTION_NAME)) {
218 File config_file = new File(collections[i], file_name);
219 if (config_file.exists()) {
220 BasicCollectionConfiguration config = new BasicCollectionConfiguration(config_file);
221 list_model.addElement(config);
222 config = null;
223 }
224 }
225 }
226 }
227 // Otherwise the collect directory doesn't actually exist, so there ain't much we can do.
228 }
229
230 /** A button listener implementation, which listens for actions on the close button and disposes of the dialog when detected. */
231 private class CloseButtonListener
232 implements ActionListener {
233 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
234 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
235 */
236 public void actionPerformed(ActionEvent event) {
237 // Done
238 prompt.dispose();
239 }
240 }
241
242 /** This private class listens for selection events in from the list and then displays the appropriate details for that collection.
243 */
244 private class CollectionListListener
245 implements ListSelectionListener {
246 /** Any implementation of ListSelectionListener must include this method so we can be informed when the list selection changes.
247 * @param event a <strong>ListSelectionEvent</strong> containing all the relevant information garnered from the event itself
248 */
249 public void valueChanged(ListSelectionEvent event) {
250 ok_button.setEnabled(false);
251 confirmation.setSelected(false);
252 if(!list.isSelectionEmpty()) {
253 collection = (BasicCollectionConfiguration) list.getSelectedValue();
254
255 String currentColName = (Gatherer.c_man.getCollection() == null) ? "" : Gatherer.c_man.getCollection().getName();
256 if(!collection.getShortName().equals(currentColName)) {
257 // the selected collection can only be deleted if it is NOT the collection that is currently open
258 confirmation.setEnabled(true);
259 confirmation.setToolTipText(null); // no tooltip
260 ok_button.setToolTipText(null);
261
262 args = new String[3];
263 args[0] = collection.getCreator();
264 args[1] = collection.getMaintainer();
265 args[2] = collection.getDescription();
266 details.setText(Dictionary.get("DeleteCollectionPrompt.Details", args));
267 details.setCaretPosition(0);
268 } else { // trying to delete the collection that is open at present
269 // add a tooltip saying to the confirmation checkbox saying that the current collection can't be deleted
270 String tooltip = Dictionary.get("DeleteCollectionPrompt.Cannot_Delete_Open_Collection_Tooltip",
271 collection.getName());
272 confirmation.setToolTipText(tooltip);
273 ok_button.setToolTipText(tooltip);
274 // delete/ok button and confirmation checkbox should be disabled
275 ok_button.setEnabled(false);
276 confirmation.setEnabled(false);
277 confirmation.setSelected(false);
278 }
279 }
280 else {
281 confirmation.setEnabled(false);
282 details.setText(Dictionary.get("DeleteCollectionPrompt.No_Collection"));
283 }
284 }
285 }
286
287 /** A check box listener so we can tell if the user has confirmed the deletion */
288 private class ConfirmationCheckBoxListener
289 implements ActionListener {
290 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
291 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
292 */
293 public void actionPerformed(ActionEvent event) {
294 // OK button is only enabled if the confirmation check box is ticked
295 ok_button.setEnabled(confirmation.isSelected());
296 }
297 }
298
299 /** The OK button listener implementation. */
300 private class OKButtonListener
301 implements ActionListener {
302 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
303 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
304 * @see org.greenstone.gatherer.Configuration
305 * @see org.greenstone.gatherer.Gatherer
306 * @see org.greenstone.gatherer.util.Utility
307 */
308 public void actionPerformed(ActionEvent event)
309 {
310 // Delete the selected collection.
311 if (Gatherer.c_man.deleteCollection(collection.getShortName())) {
312 // Refresh the collections shown in the workspace tree
313 Gatherer.g_man.refreshWorkspaceTree(WorkspaceTree.LIBRARY_CONTENTS_CHANGED);
314
315 if (collection.getShortName().equals(CollectionManager.getLoadedCollectionName())) {
316 current_coll_deleted = true;
317 }
318 list_model.removeElement(collection);
319
320 resultPrompt(true);
321 details.setText(Dictionary.get("DeleteCollectionPrompt.No_Collection"));
322 confirmation.setEnabled(false);
323 confirmation.setSelected(false);
324 ok_button.setEnabled(false);
325 collection = null;
326 }
327 else {
328 resultPrompt(false);
329 }
330 }
331 }
332}
Note: See TracBrowser for help on using the repository browser.