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

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

Made major rearrangements to the way path variables are calculated. This is to facilitate being able to specify a "-collectdir" argument to the GLI so one GLI installation can be used in a networked, multi-user situation. Hope I haven't broken anything!

  • Property svn:keywords set to Author Date Id Revision
File size: 14.4 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 = new File(Gatherer.getCollectDirectoryPath());
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].isDirectory() && !collections[i].getName().equals(StaticStrings.MODEL_COLLECTION_NAME)) {
225 File config_file = new File(collections[i], Utility.CONFIG_FILE);
226 if (config_file.exists()) {
227 BasicCollectionConfiguration config = new BasicCollectionConfiguration(config_file);
228 list_model.addElement(config);
229 config = null;
230 }
231 }
232 }
233 }
234 // Otherwise the collect directory doesn't actually exist, so there ain't much we can do.
235 }
236
237 /** A button listener implementation, which listens for actions on the close button and disposes of the dialog when detected. */
238 private class CloseButtonListener
239 implements ActionListener {
240 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
241 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
242 */
243 public void actionPerformed(ActionEvent event) {
244 // Done
245 prompt.dispose();
246 }
247 }
248
249 /** This private class listens for selection events in from the list and then displays the appropriate details for that collection.
250 */
251 private class CollectionListListener
252 implements ListSelectionListener {
253 /** Any implementation of ListSelectionListener must include this method so we can be informed when the list selection changes.
254 * @param event a <strong>ListSelectionEvent</strong> containing all the relevant information garnered from the event itself
255 */
256 public void valueChanged(ListSelectionEvent event) {
257 ok_button.setEnabled(false);
258 confirmation.setSelected(false);
259 if(!list.isSelectionEmpty()) {
260 confirmation.setEnabled(true);
261 collection = (BasicCollectionConfiguration) list.getSelectedValue();
262 args = new String[3];
263 args[0] = collection.getCreator();
264 args[1] = collection.getMaintainer();
265 args[2] = collection.getDescription();
266 Dictionary.setText(details, "DeleteCollectionPrompt.Details", args);
267 details.setCaretPosition(0);
268 }
269 else {
270 confirmation.setEnabled(false);
271 Dictionary.setText(details, "DeleteCollectionPrompt.No_Collection");
272 }
273 }
274 }
275
276 /** A check box listener so we can tell if the user has confirmed the deletion */
277 private class ConfirmationCheckBoxListener
278 implements ActionListener {
279 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
280 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
281 */
282 public void actionPerformed(ActionEvent event) {
283 // OK button is only enabled if the confirmation check box is ticked
284 ok_button.setEnabled(confirmation.isSelected());
285 }
286 }
287
288 /** The OK button listener implementation. */
289 private class OKButtonListener
290 implements ActionListener {
291 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
292 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
293 * @see org.greenstone.gatherer.Configuration
294 * @see org.greenstone.gatherer.Gatherer
295 * @see org.greenstone.gatherer.util.Utility
296 */
297 public void actionPerformed(ActionEvent event) {
298 // Delete the selected collection.
299
300 // First we must release it from the local library
301 if (LocalLibraryServer.isRunning() == true) {
302 LocalLibraryServer.releaseCollection(collection.getShortName());
303 }
304
305 if (Gatherer.isGsdlRemote) {
306
307 String launch_cgi = Gatherer.cgiBase + "launch?cmd=deldir";
308
309 String col_name = collection.getShortName();
310 String dir = ".";
311
312 try {
313
314 String col_name_encoded = URLEncoder.encode(col_name,"UTF-8");
315 String dir_encoded = URLEncoder.encode(dir,"UTF-8");
316
317 launch_cgi += "&c=" + col_name_encoded;
318 launch_cgi += "&dir=" + dir_encoded;
319
320 URL launch_url = new URL(launch_cgi);
321 URLConnection launch_connection = launch_url.openConnection();
322 InputStream stdis = launch_connection.getInputStream();
323 InputStreamReader stdisr = new InputStreamReader(stdis, "UTF-8" );
324
325 BufferedReader stdbr = new BufferedReader(stdisr);
326
327 while(true) {
328 String line = stdbr.readLine();
329 // ignore output of running lauch command
330
331 if (line == null) { break; }
332 }
333
334 stdbr.close();
335 }
336 // Exception
337 catch (Exception exception) {
338 DebugStream.println("Exception in GShell.runRemove() - unexpected");
339 DebugStream.printStackTrace(exception);
340 }
341 }
342
343 if (Gatherer.c_man.deleteCollection(collection.getShortName())) {
344 // Refresh the collections shown in the workspace tree
345 Gatherer.g_man.refreshWorkspaceTree(WorkspaceTree.LIBRARY_CONTENTS_CHANGED);
346
347 if (Gatherer.c_man.getCollection() != null && collection.getShortName().equals(Gatherer.c_man.getCollection().getName())) {
348 current_coll_deleted = true;
349 }
350 list_model.removeElement(collection);
351
352 resultPrompt(true);
353 Dictionary.setText(details, "DeleteCollectionPrompt.No_Collection");
354 confirmation.setEnabled(false);
355 confirmation.setSelected(false);
356 ok_button.setEnabled(false);
357 collection = null;
358 }
359 else {
360 resultPrompt(false);
361 }
362 }
363 }
364}
Note: See TracBrowser for help on using the repository browser.