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

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

Minor changes.

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