source: trunk/gli/src/org/greenstone/gatherer/collection/DeleteCollectionPrompt.java@ 5796

Last change on this file since 5796 was 5724, checked in by jmt12, 21 years ago

Fixed problem where delete collection prompt only showed one collection per title (didn't take into account collection short name as well)

  • Property svn:keywords set to Author Date Id Revision
File size: 14.3 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.collection;
38
39import java.awt.*;
40import java.awt.event.*;
41import java.io.*;
42import javax.swing.*;
43import javax.swing.event.*;
44import org.greenstone.gatherer.Dictionary;
45import org.greenstone.gatherer.Gatherer;
46import org.greenstone.gatherer.collection.Collection;
47import org.greenstone.gatherer.gui.ModalDialog;
48import org.greenstone.gatherer.gui.SimpleMenuBar;
49import org.greenstone.gatherer.util.ArrayTools;
50import org.greenstone.gatherer.util.GSDLSiteConfig;
51import org.greenstone.gatherer.util.StaticStrings;
52import org.greenstone.gatherer.util.Utility;
53
54/** 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.
55 * @author John Thompson, Greenstone Digital Library, University of Waikato
56 * @version 2.3
57 */
58public class DeleteCollectionPrompt
59 extends ModalDialog {
60 /** The currently selected collection for deletion. */
61 private CollectionEntry collection = null;
62 /** The model behind the list. */
63 private DefaultListModel list_model = null;
64 /** A reference to ourself so any inner-classes can dispose of us. */
65 private DeleteCollectionPrompt prompt = null;
66 /** The close button, which exits the prompt without deleting anything. */
67 private JButton close_button = null;
68 /** The ok button which causes the selected collection to be deleted. */
69 private JButton ok_button = null;
70 /** The confirmation check box. */
71 private JCheckBox confirmation = null;
72 /** The label above details. */
73 private JLabel details_label = null;
74 /** The label above the list. */
75 private JLabel list_label = null;
76 /** The list of available collections. */
77 private JList list = null;
78 /** The text area used to display details about the collection selected. */
79 private JTextArea details = null;
80 /** A string array used to pass arguments to the phrase retrieval method. */
81 private String args[] = null;
82
83 private boolean current_coll_deleted = false;
84 /** The size of the delete prompt screen. */
85 public static final Dimension SIZE = new Dimension(500, 500);
86
87 /** Constructor.
88 * @see org.greenstone.gatherer.collection.DeleteCollectionPrompt.CloseButtonListener
89 * @see org.greenstone.gatherer.collection.DeleteCollectionPrompt.CollectionListListener
90 * @see org.greenstone.gatherer.collection.DeleteCollectionPrompt.ConfirmationCheckBoxListener
91 * @see org.greenstone.gatherer.collection.DeleteCollectionPrompt.OKButtonListener
92 */
93 public DeleteCollectionPrompt() {
94 super(Gatherer.g_man);
95 close_button = new JButton();
96 close_button.setMnemonic(KeyEvent.VK_C);
97 Dictionary.setBoth(close_button, "General.Close", "General.Close_Tooltip");
98 confirmation = new JCheckBox();
99 Dictionary.setText(confirmation, "DeleteCollectionPrompt.Confirm_Delete");
100 details = new JTextArea();
101 details.setEditable(false);
102 details.setFont(Gatherer.config.getFont("general.tooltip_font", false));
103 Dictionary.setText(details, "DeleteCollectionPrompt.No_Collection");
104 details_label = new JLabel();
105 Dictionary.setText(details_label, "DeleteCollectionPrompt.Collection_Details");
106 list = new JList();
107 list_label = new JLabel();
108 Dictionary.setText(list_label, "DeleteCollectionPrompt.Collection_List");
109 list_model = new DefaultListModel();
110 ok_button = new JButton();
111 ok_button.setMnemonic(KeyEvent.VK_D);
112 Dictionary.setBoth(ok_button, "DeleteCollectionPrompt.Delete", "DeleteCollectionPrompt.Delete_Tooltip");
113 prompt = this;
114 setModal(true);
115 setSize(SIZE);
116 Dictionary.setText(this, "DeleteCollectionPrompt.Title");
117
118 setJMenuBar(new SimpleMenuBar("0")); // need to find an appropriate help page to open at
119 close_button.addActionListener(new CloseButtonListener());
120 confirmation.addActionListener(new ConfirmationCheckBoxListener());
121 confirmation.setEnabled(false);
122 confirmation.setSelected(false);
123 list.addListSelectionListener(new CollectionListListener());
124 list.clearSelection();
125 list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
126 list.setModel(list_model);
127 ok_button.addActionListener(new OKButtonListener());
128 ok_button.setEnabled(false);
129 scanForCollections();
130 }
131
132 /** Destructor. */
133 public void destroy() {
134 list_model.clear();
135 list_model = null;
136 close_button = null;
137 confirmation = null;
138 details = null;
139 details_label = null;
140 list = null;
141 ok_button = null;
142 prompt = null;
143 }
144
145 /** This method causes the modal prompt to be displayed.
146 * returns true if it has deleted the collection that is currently open */
147 public boolean display() {
148 // Central pane
149 JPanel list_pane = new JPanel(new BorderLayout());
150 list_pane.add(list_label, BorderLayout.NORTH);
151 list_pane.add(new JScrollPane(list), BorderLayout.CENTER);
152 list_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
153
154 JPanel details_pane = new JPanel(new BorderLayout());
155 details_pane.add(details_label, BorderLayout.NORTH);
156 details_pane.add(new JScrollPane(details), BorderLayout.CENTER);
157 details_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
158
159 JPanel central_pane = new JPanel(new GridLayout(2, 1));
160 central_pane.add(list_pane);
161 central_pane.add(details_pane);
162 central_pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
163
164 // Lower pane
165 JPanel confirmation_pane = new JPanel(new BorderLayout());
166 confirmation_pane.add(confirmation, BorderLayout.CENTER);
167 confirmation_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
168
169 JPanel button_pane = new JPanel(new GridLayout(1, 2));
170 button_pane.add(ok_button);
171 button_pane.add(close_button);
172 button_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
173
174 JPanel lower_pane = new JPanel(new BorderLayout());
175 lower_pane.add(confirmation_pane, BorderLayout.NORTH);
176 lower_pane.add(button_pane, BorderLayout.SOUTH);
177 lower_pane.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
178
179 // Final.
180 JPanel content_pane = (JPanel)this.getContentPane();
181 content_pane.setLayout(new BorderLayout());
182 content_pane.add(central_pane, BorderLayout.CENTER);
183 content_pane.add(lower_pane, BorderLayout.SOUTH);
184
185 // Center and display.
186 Dimension screen_size = Gatherer.config.screen_size;
187 this.setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
188 this.setVisible(true); // blocks until the dialog is killed
189 return current_coll_deleted;
190
191 }
192
193 /** Shows a delete complete prompt.
194 * @param success A <strong>boolean</strong> indicating if the collection was successfully deleted.
195 * @see org.greenstone.gatherer.collection.Collection
196 */
197 public void resultPrompt(boolean success) {
198 args = new String[1];
199 args[0] = collection.getName();
200 if (success) {
201 JOptionPane.showMessageDialog(prompt,Dictionary.get("DeleteCollectionPrompt.Successful_Delete", args),Dictionary.get("DeleteCollectionPrompt.Successful_Title"),JOptionPane.INFORMATION_MESSAGE);
202 }
203 else {
204 JOptionPane.showMessageDialog(prompt,Dictionary.get("DeleteCollectionPrompt.Failed_Delete", args),Dictionary.get("DeleteCollectionPrompt.Failed_Title"),JOptionPane.WARNING_MESSAGE);
205 }
206 }
207
208 /** Method to scan the collect directory retrieving and reloading each collection it finds, while building the list of known collections.
209 * @see org.greenstone.gatherer.Configuration
210 * @see org.greenstone.gatherer.Gatherer
211 * @see org.greenstone.gatherer.util.ArrayTools
212 * @see org.greenstone.gatherer.util.Utility
213 */
214 private void scanForCollections() {
215 // Start at the collect dir.
216 String collect_directory_name = Utility.getCollectionDir(Gatherer.config.gsdl_path);
217 File collect_directory = new File(collect_directory_name);
218 if(collect_directory.exists()) {
219 // Now for each child directory see if it contains a .col file and
220 // if so try to load it..
221 File collections[] = collect_directory.listFiles();
222 ArrayTools.sort(collections);
223 for(int i = 0; collections != null && i < collections.length; i++) {
224 if(!collections[i].getName().equals(StaticStrings.MODEL_COLLECTION_NAME)) {
225 File config_file = Utility.findConfigFile(collections[i]);
226 if (config_file != null) {
227 CollectionConfiguration config = new CollectionConfiguration(config_file);
228 if (config != null) {
229 CollectionEntry col = new CollectionEntry(collections[i].getName(), config);
230 list_model.addElement(col);
231 }
232 }
233 }
234 }
235 }
236 // Otherwise the collect directory doesn't actually exist, so there ain't much we can do.
237 }
238
239 /** A button listener implementation, which listens for actions on the close button and disposes of the dialog when detected. */
240 private class CloseButtonListener
241 implements ActionListener {
242 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
243 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
244 */
245 public void actionPerformed(ActionEvent event) {
246 // Ask the collection manager to refresh the 'Greenstone Collections' folder
247 Gatherer.c_man.refreshGreenstoneCollections();
248 // Done
249 prompt.dispose();
250 }
251 }
252
253 /** This private class listens for selection events in from the list and then displays the appropriate details for that collection.
254 */
255 private class CollectionListListener
256 implements ListSelectionListener {
257 /** Any implementation of ListSelectionListener must include this method so we can be informed when the list selection changes.
258 * @param event a <strong>ListSelectionEvent</strong> containing all the relevant information garnered from the event itself
259 */
260 public void valueChanged(ListSelectionEvent event) {
261 ok_button.setEnabled(false);
262 confirmation.setSelected(false);
263 if(!list.isSelectionEmpty()) {
264 confirmation.setEnabled(true);
265 collection = (CollectionEntry) list.getSelectedValue();
266 args = new String[5];
267 args[0] = collection.getTitle();
268 args[1] = collection.getName();
269 args[2] = collection.getCreator();
270 args[3] = collection.getMaintainer();
271 args[4] = collection.getDescription();
272 Dictionary.setText(details, "DeleteCollectionPrompt.Details", args);
273 details.setCaretPosition(0);
274 }
275 else {
276 confirmation.setEnabled(false);
277 Dictionary.setText(details, "DeleteCollectionPrompt.No_Collection");
278 }
279 }
280 }
281
282 /** A check box listener so we can tell if the user has confirmed the deletion */
283 private class ConfirmationCheckBoxListener
284 implements ActionListener {
285 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
286 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
287 */
288 public void actionPerformed(ActionEvent event) {
289 // OK button is only enabled if the confirmation check box is ticked
290 ok_button.setEnabled(confirmation.isSelected());
291 }
292 }
293
294 /** The OK button listener implementation. */
295 private class OKButtonListener
296 implements ActionListener {
297 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
298 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
299 * @see org.greenstone.gatherer.Configuration
300 * @see org.greenstone.gatherer.Gatherer
301 * @see org.greenstone.gatherer.util.Utility
302 */
303 public void actionPerformed(ActionEvent event) {
304 // Delete the selected collection.
305
306 // first of all we must release it from the local library
307 if(Gatherer.config.exec_file != null) {
308 ///ystem.err.println("Local Library Found!");
309 Gatherer.g_man.preview_pane.configServer(GSDLSiteConfig.RELEASE_COMMAND + collection.getName());
310 }
311
312 File delete_me = new File(Utility.getCollectionDir(Gatherer.config.gsdl_path) + collection.getName() + File.separator);
313 if(Utility.delete(delete_me)) {
314 if (Gatherer.c_man.getCollection()!=null && collection.getName().equals(Gatherer.c_man.getCollection().getName())) {
315 current_coll_deleted = true;
316 }
317
318 resultPrompt(true);
319 list_model.removeElement(collection);
320 Dictionary.setText(details, "DeleteCollectionPrompt.No_Collection");
321 confirmation.setEnabled(false);
322 confirmation.setSelected(false);
323 ok_button.setEnabled(false);
324 collection = null;
325 }
326 else {
327 resultPrompt(false);
328 }
329 }
330 }
331
332 private class CollectionEntry {
333
334 private String name = null;
335 private CollectionConfiguration config = null;
336
337 public CollectionEntry(String name, CollectionConfiguration config) {
338 this.name = name;
339 this.config = config;
340 }
341
342 public String getName() {
343 return name;
344 }
345
346 public String getTitle() {
347 return config.getName();
348 }
349
350 public String getCreator() {
351 return config.getCreator();
352 }
353
354 public String getDescription() {
355 return config.getDescription();
356 }
357
358 public String getMaintainer() {
359 return config.getMaintainer();
360 }
361
362 public String toString() {
363 return name + " - \"" + config.getName() + "\"";
364 }
365 }
366}
Note: See TracBrowser for help on using the repository browser.