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

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

Moved DeleteCollectionPrompt and ExportCollectionPrompt out of "collection" into "gui".

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