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