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

Last change on this file since 4366 was 4366, checked in by kjdon, 21 years ago

re-tabbed the code for java

  • Property svn:keywords set to Author Date Id Revision
File size: 12.8 KB
Line 
1package org.greenstone.gatherer.collection;
2/**
3 *#########################################################################
4 *
5 * A component of the Gatherer application, part of the Greenstone digital
6 * library suite from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * <BR><BR>
10 *
11 * Author: John Thompson, Greenstone Digital Library, University of Waikato
12 *
13 * <BR><BR>
14 *
15 * Copyright (C) 1999 New Zealand Digital Library Project
16 *
17 * <BR><BR>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * <BR><BR>
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * <BR><BR>
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 *########################################################################
37 */
38import java.awt.*;
39import java.awt.event.*;
40import java.io.*;
41import javax.swing.*;
42import javax.swing.event.*;
43import org.greenstone.gatherer.Gatherer;
44import org.greenstone.gatherer.collection.Collection;
45import org.greenstone.gatherer.util.ArrayTools;
46import org.greenstone.gatherer.util.Utility;
47/** 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.
48 * @author John Thompson, Greenstone Digital Library, University of Waikato
49 * @version 2.3
50 */
51public class DeleteCollectionPrompt
52 extends JDialog {
53 /** The currently selected collection for deletion. */
54 private Collection collection = null;
55 /** The model behind the list. */
56 private DefaultListModel list_model = null;
57 /** A reference to ourself so any inner-classes get dispose of us. */
58 private DeleteCollectionPrompt prompt = null;
59 /** The close button, which exits the prompt without deleting anything. */
60 private JButton close_button = null;
61 /** The ok button which causes the selected collection to be deleted. */
62 private JButton ok_button = null;
63 /** The confirmation check box. */
64 private JCheckBox confirmation = null;
65 /** The label above details. */
66 private JLabel details_label = null;
67 /** The label above the list. */
68 private JLabel list_label = null;
69 /** The list of available collections. */
70 private JList list = null;
71 /** The text area used to display details about the collection selected. */
72 private JTextArea details = null;
73 /** A string array used to pass arguments to the phrase retrieval method. */
74 private String args[] = null;
75 /** The size of the delete prompt screen. */
76 public static final Dimension SIZE = new Dimension(500, 500);
77 /** Constructor.
78 * @see org.greenstone.gatherer.collection.DeleteCollectionPrompt.CloseButtonListener
79 * @see org.greenstone.gatherer.collection.DeleteCollectionPrompt.CollectionListListener
80 * @see org.greenstone.gatherer.collection.DeleteCollectionPrompt.ConfirmationCheckBoxListener
81 * @see org.greenstone.gatherer.collection.DeleteCollectionPrompt.OKButtonListener
82 */
83 public DeleteCollectionPrompt() {
84 super();
85 this.close_button = new JButton(get("General.Close", null));
86 this.confirmation = new JCheckBox(get("Confirm_Delete", null));
87 this.details = new JTextArea();
88 this.details_label = new JLabel(get("Collection_Details", null));
89 this.list = new JList();
90 this.list_label = new JLabel(get("Collection_List", null));
91 this.list_model = new DefaultListModel();
92 this.ok_button = new JButton(get("General.OK", null));
93 this.prompt = this;
94 this.setModal(true);
95 this.setSize(SIZE);
96 this.setTitle(get("Title", null));
97 close_button.addActionListener(new CloseButtonListener());
98 confirmation.addActionListener(new ConfirmationCheckBoxListener());
99 confirmation.setEnabled(false);
100 confirmation.setSelected(false);
101 details.setFont(Gatherer.config.getFont("general.tooltip_font", false));
102 details.setText(get("No_Collection", null));
103 list.addListSelectionListener(new CollectionListListener());
104 list.clearSelection();
105 list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
106 list.setModel(list_model);
107 ok_button.addActionListener(new OKButtonListener());
108 ok_button.setEnabled(false);
109 scanForCollections();
110 }
111 /** Destructor. */
112 public void destroy() {
113 close_button = null;
114 confirmation = null;
115 details = null;
116 details_label = null;
117 list_model.clear();
118 list_model = null;
119 list = null;
120 ok_button = null;
121 prompt = null;
122 }
123 /** This method causes the modal prompt to be displayed. */
124 public void display() {
125 // Central pane
126 JPanel list_pane = new JPanel(new BorderLayout());
127 list_pane.add(list_label, BorderLayout.NORTH);
128 list_pane.add(new JScrollPane(list), BorderLayout.CENTER);
129 list_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
130
131 JPanel details_pane = new JPanel(new BorderLayout());
132 details_pane.add(details_label, BorderLayout.NORTH);
133 details_pane.add(new JScrollPane(details), BorderLayout.CENTER);
134 details_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
135
136 JPanel central_pane = new JPanel(new GridLayout(2, 1));
137 central_pane.add(list_pane);
138 central_pane.add(details_pane);
139 central_pane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
140
141 // Lower pane
142 JPanel confirmation_pane = new JPanel(new BorderLayout());
143 confirmation_pane.add(confirmation, BorderLayout.CENTER);
144 confirmation_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
145
146 JPanel button_pane = new JPanel(new GridLayout(1, 2));
147 button_pane.add(ok_button);
148 button_pane.add(close_button);
149 button_pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
150
151 JPanel lower_pane = new JPanel(new BorderLayout());
152 lower_pane.add(confirmation_pane, BorderLayout.NORTH);
153 lower_pane.add(button_pane, BorderLayout.SOUTH);
154 lower_pane.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
155
156 // Final.
157 JPanel content_pane = (JPanel)this.getContentPane();
158 content_pane.setLayout(new BorderLayout());
159 content_pane.add(central_pane, BorderLayout.CENTER);
160 content_pane.add(lower_pane, BorderLayout.SOUTH);
161
162 // Center and display.
163 Dimension screen_size = Gatherer.config.screen_size;
164 this.setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
165 this.show();
166 }
167 /** Shows a delete complete prompt.
168 * @param success A <strong>boolean</strong> indicating if the collection was successfully deleted.
169 * @see org.greenstone.gatherer.collection.Collection
170 */
171 public void resultPrompt(boolean success) {
172 args = new String[1];
173 args[0] = collection.getName();
174 if(success) {
175 JOptionPane.showMessageDialog(prompt,get("Successful_Delete", args),get("Successful_Title", null),JOptionPane.INFORMATION_MESSAGE);
176 }
177 else {
178 JOptionPane.showMessageDialog(prompt,get("Failed_Delete", args),get("Failed_Title", null),JOptionPane.WARNING_MESSAGE);
179 }
180 }
181 /** Retrieves a phrase from the <strong>Dictionary</strong> in <strong>Gatherer</strong>.
182 * @param key A <strong>String</strong> used to match against a specific phrase in the dictionary.
183 * @see org.greenstone.gatherer.Dictionary
184 * @see org.greenstone.gatherer.Gatherer
185 */
186 private String get(String key, String args[]) {
187 if(key.indexOf(".") == -1) {
188 key = "DeleteCollectionPrompt." + key;
189 }
190 return Gatherer.dictionary.get(key, args);
191 }
192 /** Attempts to load the given collection. Currently uses simple
193 * serialization of the collection class.
194 * @param location The path to the serialized collection.
195 */
196 public Collection loadCollection(File file) {
197 Collection col = null;
198 try {
199 FileInputStream fis = new FileInputStream(file);
200 BufferedInputStream bin = new BufferedInputStream(fis);
201 ObjectInputStream input = new ObjectInputStream(bin);
202 col = (Collection) input.readObject();
203 input.close();
204 input = null;
205 bin.close();
206 bin = null;
207 fis.close();
208 fis = null;
209 }
210 catch (Exception error) {
211 error.printStackTrace();
212 }
213 return col;
214 }
215 /** Method to scan the collect directory retrieving and reloading each collection it finds, while building the list of known collections.
216 * @see org.greenstone.gatherer.Configuration
217 * @see org.greenstone.gatherer.Gatherer
218 * @see org.greenstone.gatherer.util.ArrayTools
219 * @see org.greenstone.gatherer.util.Utility
220 */
221 private void scanForCollections() {
222 // Start at the collect dir.
223 String collect_directory_name = Utility.getCollectionDir(Gatherer.config.gsdl_path);
224 File collect_directory = new File(collect_directory_name);
225 if(collect_directory.exists()) {
226 // Now for each child directory see if it contains a .col file and
227 // if so try to load it..
228 File collections[] = collect_directory.listFiles();
229 ArrayTools.sort(collections);
230 for(int i = 0; collections != null && i < collections.length; i++) {
231 File children[] = collections[i].listFiles();
232 for(int j = 0; children != null && j < children.length; j++) {
233 if(children[j].getAbsolutePath().endsWith(".col")) {
234 Collection col = loadCollection(children[j]);
235 // If we've actually loaded
236 if(col != null) {
237 list_model.addElement(col);
238 }
239 }
240 }
241 }
242 }
243 // Otherwise the collect directory doesn't actually exist, so there ain't much we can do.
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 prompt.dispose();
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 if(!list.isSelectionEmpty()) {
264 confirmation.setEnabled(true);
265 collection = (Collection) list.getSelectedValue();
266 args = new String[4];
267 args[0] = collection.getTitle();
268 args[1] = collection.getName();
269 args[2] = collection.getEmail();
270 args[3] = collection.getDescription();
271 details.setText(get("Details", args));
272 }
273 else {
274 confirmation.setEnabled(false);
275 details.setText(get("No_Collection", null));
276 }
277 }
278 }
279 /** A check box listener so we can tell if the users selected confirm. */
280 private class ConfirmationCheckBoxListener
281 implements ActionListener {
282 /** Any implementation of ActionListener must include this method so we can be informed when the button is actioned.
283 * @param event An <strong>ActionEvent</strong> containing all the relevant information garnered from the event itself.
284 */
285 public void actionPerformed(ActionEvent event) {
286 if(confirmation.isSelected()) {
287 ok_button.setEnabled(true);
288 }
289 else {
290 ok_button.setEnabled(false);
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 File delete_me = new File(Utility.getCollectionDir(Gatherer.config.gsdl_path) + collection.getName() + File.separator);
306 if(Utility.delete(delete_me)) {
307 resultPrompt(true);
308 list_model.removeElement(collection);
309 details.setText(get("No_Collection", null));
310 confirmation.setEnabled(false);
311 confirmation.setSelected(false);
312 ok_button.setEnabled(false);
313 collection = null;
314 }
315 else {
316 resultPrompt(false);
317 }
318 }
319 }
320}
321
322
Note: See TracBrowser for help on using the repository browser.