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

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

Changed the font for collection details on the delete/export dialog to be the standard font, because it looks nicer and has better Unicode support.

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