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

Last change on this file since 21999 was 19272, checked in by kjdon, 15 years ago

made delete prompt handle collection groups

  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 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 this.setComponentOrientation(Dictionary.getOrientation());
95 confirmation = new JCheckBox(Dictionary.get("DeleteCollectionPrompt.Confirm_Delete"));
96 confirmation.setComponentOrientation(Dictionary.getOrientation());
97 details = new JTextArea(Dictionary.get("DeleteCollectionPrompt.No_Collection"));
98 details.setComponentOrientation(Dictionary.getOrientation());
99 details.setEditable(false);
100 details_label = new JLabel(Dictionary.get("DeleteCollectionPrompt.Collection_Details"));
101 details_label.setComponentOrientation(Dictionary.getOrientation());
102
103 list = new JList();
104 list.setComponentOrientation(Dictionary.getOrientation());
105 list_label = new JLabel(Dictionary.get("DeleteCollectionPrompt.Collection_List"));
106 list_label.setComponentOrientation(Dictionary.getOrientation());
107
108 list_model = new DefaultListModel();
109 ok_button = new GLIButton(Dictionary.get("DeleteCollectionPrompt.Delete"), Dictionary.get("DeleteCollectionPrompt.Delete_Tooltip"));
110
111 prompt = this;
112 setModal(true);
113 setSize(SIZE);
114 setTitle(Dictionary.get("DeleteCollectionPrompt.Title"));
115
116 setJMenuBar(new SimpleMenuBar("deletingcollections"));
117 close_button.addActionListener(new CloseButtonListener());
118 confirmation.addActionListener(new ConfirmationCheckBoxListener());
119 confirmation.setEnabled(false);
120 confirmation.setSelected(false);
121 list.addListSelectionListener(new CollectionListListener());
122 list.clearSelection();
123 list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
124 list.setModel(list_model);
125 ok_button.addActionListener(new OKButtonListener());
126 ok_button.setEnabled(false);
127 scanForCollections();
128
129 }
130
131 /** Destructor. */
132 public void destroy() {
133 list_model.clear();
134 list_model = null;
135 close_button = null;
136 confirmation = null;
137 details = null;
138 details_label = null;
139 list = null;
140 ok_button = null;
141 prompt = null;
142 }
143
144 /** This method causes the modal prompt to be displayed.
145 * returns true if it has deleted the collection that is currently open */
146 public boolean display() {
147 // Central pane
148 JScrollPane scrol_tmp;
149 JPanel list_pane = new JPanel(new BorderLayout());
150 list_pane.setComponentOrientation(Dictionary.getOrientation());
151 list_pane.add(list_label, BorderLayout.NORTH);
152 scrol_tmp = new JScrollPane(list);
153 scrol_tmp.setComponentOrientation(Dictionary.getOrientation());
154 list_pane.add(scrol_tmp, BorderLayout.CENTER);
155 list_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
156
157 JPanel details_pane = new JPanel(new BorderLayout());
158 details_pane.setComponentOrientation(Dictionary.getOrientation());
159 details_pane.add(details_label, BorderLayout.NORTH);
160 scrol_tmp = new JScrollPane(details);
161 scrol_tmp.setComponentOrientation(Dictionary.getOrientation());
162 details_pane.add(scrol_tmp, BorderLayout.CENTER);
163 details_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
164
165 JPanel central_pane = new JPanel(new GridLayout(2, 1));
166 central_pane.setComponentOrientation(Dictionary.getOrientation());
167 central_pane.add(list_pane);
168 central_pane.add(details_pane);
169 central_pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
170
171 // Lower pane
172 JPanel confirmation_pane = new JPanel(new BorderLayout());
173 confirmation_pane.setComponentOrientation(Dictionary.getOrientation());
174 confirmation_pane.add(confirmation, BorderLayout.CENTER);
175 confirmation_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
176
177 JPanel button_pane = new JPanel(new GridLayout(1, 2));
178 button_pane.setComponentOrientation(Dictionary.getOrientation());
179 button_pane.add(ok_button);
180 button_pane.add(close_button);
181 button_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
182
183 JPanel lower_pane = new JPanel(new BorderLayout());
184 lower_pane.setComponentOrientation(Dictionary.getOrientation());
185 lower_pane.add(confirmation_pane, BorderLayout.NORTH);
186 lower_pane.add(button_pane, BorderLayout.SOUTH);
187 lower_pane.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
188
189 // Final.
190 JPanel content_pane = (JPanel)this.getContentPane();
191 content_pane.setComponentOrientation(Dictionary.getOrientation());
192 content_pane.setLayout(new BorderLayout());
193 content_pane.add(central_pane, BorderLayout.CENTER);
194 content_pane.add(lower_pane, BorderLayout.SOUTH);
195
196 // Center and display.
197 Dimension screen_size = Configuration.screen_size;
198 this.setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
199 this.setVisible(true); // blocks until the dialog is killed
200 return current_coll_deleted;
201 }
202
203
204 /** Shows a delete complete prompt.
205 * @param success A <strong>boolean</strong> indicating if the collection was successfully deleted.
206 * @see org.greenstone.gatherer.collection.Collection
207 */
208 public void resultPrompt(boolean success) {
209 args = new String[1];
210 args[0] = collection.getName();
211 if (success) {
212 JOptionPane.showMessageDialog(prompt,Dictionary.get("DeleteCollectionPrompt.Successful_Delete", args),Dictionary.get("DeleteCollectionPrompt.Successful_Title"),JOptionPane.INFORMATION_MESSAGE);
213 }
214 else {
215 JOptionPane.showMessageDialog(prompt,Dictionary.get("DeleteCollectionPrompt.Failed_Delete", args),Dictionary.get("DeleteCollectionPrompt.Failed_Title"),JOptionPane.WARNING_MESSAGE);
216 }
217 }
218
219 /** Method to scan the collect directory retrieving and reloading each collection it finds, while building the list of known collections.
220 * @see org.greenstone.gatherer.Configuration
221 * @see org.greenstone.gatherer.Gatherer
222 * @see org.greenstone.gatherer.util.ArrayTools
223 * @see org.greenstone.gatherer.util.Utility
224 */
225 private void scanForCollections() {
226 // Start at the collect dir.
227 File collect_directory = new File(Gatherer.getCollectDirectoryPath());
228 if (collect_directory.exists()) {
229 loadCollectionConfigs(collect_directory);
230 }
231 }
232
233 private void loadCollectionConfigs(File directory) {
234 // For each child directory see if it contains an
235 // etc/collect.cfg file and if so try to load it..
236 File collections[] = directory.listFiles();
237 String file_name = (Gatherer.GS3)? Utility.CONFIG_GS3_FILE : Utility.CONFIG_FILE;
238 ArrayTools.sort(collections);
239 for(int i = 0; collections != null && i < collections.length; i++) {
240 if(collections[i].isDirectory() && !collections[i].getName().equals(StaticStrings.MODEL_COLLECTION_NAME)) {
241 File config_file = new File(collections[i], file_name);
242 if (config_file.exists()) {
243 BasicCollectionConfiguration config = new BasicCollectionConfiguration(config_file);
244 if (config.getCollectGroup().equals("true")) {
245 loadCollectionConfigs(collections[i]);
246 } else {
247 list_model.addElement(config);
248 config = null;
249 }
250 }
251 }
252 }
253 }
254
255
256 /** A button listener implementation, which listens for actions on the close button and disposes of the dialog when detected. */
257 private class CloseButtonListener
258 implements ActionListener {
259 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
260 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
261 */
262 public void actionPerformed(ActionEvent event) {
263 // Done
264 prompt.dispose();
265 }
266 }
267
268 /** This private class listens for selection events in from the list and then displays the appropriate details for that collection.
269 */
270 private class CollectionListListener
271 implements ListSelectionListener {
272 /** Any implementation of ListSelectionListener must include this method so we can be informed when the list selection changes.
273 * @param event a <strong>ListSelectionEvent</strong> containing all the relevant information garnered from the event itself
274 */
275 public void valueChanged(ListSelectionEvent event) {
276 ok_button.setEnabled(false);
277 confirmation.setSelected(false);
278 if(!list.isSelectionEmpty()) {
279 collection = (BasicCollectionConfiguration) list.getSelectedValue();
280
281 String currentColName = (Gatherer.c_man.getCollection() == null) ? "" : Gatherer.c_man.getCollection().getName();
282 if(!collection.getShortName().equals(currentColName)) {
283 // the selected collection can only be deleted if it is NOT the collection that is currently open
284 confirmation.setEnabled(true);
285 confirmation.setToolTipText(null); // no tooltip
286 ok_button.setToolTipText(null);
287
288 args = new String[3];
289 args[0] = collection.getCreator();
290 args[1] = collection.getMaintainer();
291 args[2] = collection.getDescription();
292 details.setText(Dictionary.get("DeleteCollectionPrompt.Details", args));
293 details.setCaretPosition(0);
294 } else { // trying to delete the collection that is open at present
295 // add a tooltip saying to the confirmation checkbox saying that the current collection can't be deleted
296 String tooltip = Dictionary.get("DeleteCollectionPrompt.Cannot_Delete_Open_Collection_Tooltip",
297 collection.getName());
298 confirmation.setToolTipText(tooltip);
299 ok_button.setToolTipText(tooltip);
300 // delete/ok button and confirmation checkbox should be disabled
301 ok_button.setEnabled(false);
302 confirmation.setEnabled(false);
303 confirmation.setSelected(false);
304 }
305 }
306 else {
307 confirmation.setEnabled(false);
308 details.setText(Dictionary.get("DeleteCollectionPrompt.No_Collection"));
309 }
310 }
311 }
312
313 /** A check box listener so we can tell if the user has confirmed the deletion */
314 private class ConfirmationCheckBoxListener
315 implements ActionListener {
316 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
317 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
318 */
319 public void actionPerformed(ActionEvent event) {
320 // OK button is only enabled if the confirmation check box is ticked
321 ok_button.setEnabled(confirmation.isSelected());
322 }
323 }
324
325 /** The OK button listener implementation. */
326 private class OKButtonListener
327 implements ActionListener {
328 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
329 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
330 * @see org.greenstone.gatherer.Configuration
331 * @see org.greenstone.gatherer.Gatherer
332 * @see org.greenstone.gatherer.util.Utility
333 */
334 public void actionPerformed(ActionEvent event)
335 {
336 // Delete the selected collection.
337 if (Gatherer.c_man.deleteCollection(collection.getShortName())) {
338 // Refresh the collections shown in the workspace tree
339 Gatherer.g_man.refreshWorkspaceTree(WorkspaceTree.LIBRARY_CONTENTS_CHANGED);
340
341 if (collection.getShortName().equals(CollectionManager.getLoadedCollectionName())) {
342 current_coll_deleted = true;
343 }
344 list_model.removeElement(collection);
345
346 resultPrompt(true);
347 details.setText(Dictionary.get("DeleteCollectionPrompt.No_Collection"));
348 confirmation.setEnabled(false);
349 confirmation.setSelected(false);
350 ok_button.setEnabled(false);
351 collection = null;
352 }
353 else {
354 resultPrompt(false);
355 }
356 }
357 }
358}
Note: See TracBrowser for help on using the repository browser.