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

Last change on this file since 9017 was 9017, checked in by mdewsnip, 19 years ago

Moved some GUI stuff out of CollectionManager and into GUIManager.

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