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

Last change on this file since 13584 was 13584, checked in by mdewsnip, 17 years ago

Moved the code to deal with the local library when deleting a collection out of DeleteCollectionPrompt and into CollectionManager.

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