source: gli/trunk/src/org/greenstone/gatherer/gui/OpenCollectionDialog.java@ 16267

Last change on this file since 16267 was 16267, checked in by davidb, 16 years ago

Support for 'collectgroup' added, whereby Greenstone 2 collections can be grouped into different sets of collections. Thought I had all the angles covered but it turns out I've overlooked the case of 'base this on' collection feature of GLI, which currently doesn't know about the collectgroup feature. Committing what I have for now, as since there are no instructions on how to use this feature, no one is going to activating it

  • Property svn:keywords set to Author Date Id Revision
File size: 12.8 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 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.gui;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.io.*;
32import java.util.*;
33import javax.swing.*;
34import javax.swing.event.*;
35import org.greenstone.gatherer.Configuration;
36import org.greenstone.gatherer.Dictionary;
37import org.greenstone.gatherer.Gatherer;
38import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
39import org.greenstone.gatherer.util.StaticStrings;
40import org.greenstone.gatherer.util.Utility;
41import org.greenstone.gatherer.shell.GShell;
42import org.greenstone.gatherer.shell.GShellEvent;
43import org.greenstone.gatherer.shell.GShellListener;
44import org.greenstone.gatherer.shell.GShellProgressMonitor;
45
46/** A dialog which provides a straight-forward access to the currently installed collections. It also will contain the ability to continue through to the original OpenCollectionDialog if your source collection is located somewhere other than the gsdl collect folder. */
47public class OpenCollectionDialog
48 extends ModalDialog {
49
50 static final public int OK_OPTION = 0;
51 static final public int CANCEL_OPTION = 1;
52
53 static final private Dimension SIZE = new Dimension(640,480);
54 static final private String BLANK = "b";
55 static final private String DESCRIPTION = "d";
56
57 private CardLayout card_layout;
58 private int result;
59 private JButton cancel_button;
60 private JButton open_button;
61 private JList collection_list;
62 private JTextArea description_textarea;
63 private JPanel description_pane;
64 private String filename;
65
66 public OpenCollectionDialog() {
67 super(Gatherer.g_man, "", true);
68 setJMenuBar(new SimpleMenuBar("openingacollection"));
69 setSize(SIZE);
70 setTitle(Dictionary.get("OpenCollectionDialog.Title"));
71
72 // Creation
73 JPanel content_pane = (JPanel) getContentPane();
74
75 JPanel center_pane = new JPanel();
76
77 JPanel collection_list_pane = new JPanel();
78 JLabel collection_list_label = new JLabel(Dictionary.get("OpenCollectionDialog.Available_Collections"));
79 collection_list = new JList(new CollectionListModel());
80 description_pane = new JPanel();
81 card_layout = new CardLayout();
82
83 JPanel blank_pane = new JPanel();
84
85 JPanel description_textarea_pane = new JPanel();
86 JLabel description_textarea_label = new JLabel(Dictionary.get("OpenCollectionDialog.Description"));
87 description_textarea = new JTextArea();
88
89 JPanel button_pane = new JPanel();
90 open_button = new GLIButton(Dictionary.get("OpenCollectionDialog.Open"), Dictionary.get("OpenCollectionDialog.Open_Tooltip"));
91 open_button.setEnabled(false);
92
93 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Pure_Cancel_Tooltip"));
94
95 // Connection
96 cancel_button.addActionListener(new CancelListener());
97 open_button.addActionListener(new OpenListener());
98 CollectionListSelectionListener clsl = new CollectionListSelectionListener();
99 collection_list.addListSelectionListener(clsl);
100 collection_list.addMouseListener(clsl);
101 clsl = null;
102
103 // Layout
104 collection_list_pane.setLayout(new BorderLayout());
105 collection_list_pane.add(collection_list_label, BorderLayout.NORTH);
106 collection_list_pane.add(new JScrollPane(collection_list), BorderLayout.CENTER);
107
108 description_textarea_pane.setLayout(new BorderLayout());
109 description_textarea_pane.add(description_textarea_label, BorderLayout.NORTH);
110 description_textarea_pane.add(new JScrollPane(description_textarea), BorderLayout.CENTER);
111
112 description_pane.setLayout(card_layout);
113 description_pane.add(description_textarea_pane, DESCRIPTION);
114 description_pane.add(blank_pane, BLANK);
115
116 center_pane.setLayout(new GridLayout(2,1,0,5));
117 center_pane.add(collection_list_pane);
118 center_pane.add(description_pane);
119
120 button_pane.setLayout(new GridLayout(1,4));
121 button_pane.add(new JPanel());
122 button_pane.add(open_button);
123 button_pane.add(cancel_button);
124
125 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
126 content_pane.setLayout(new BorderLayout());
127 content_pane.add(center_pane, BorderLayout.CENTER);
128 content_pane.add(button_pane, BorderLayout.SOUTH);
129
130 Dimension screen_size = Configuration.screen_size;
131 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
132 screen_size = null;
133 }
134
135 public void destroy() {
136 }
137
138 public int display() {
139 setVisible(true);
140 return result;
141 }
142
143 public String getFileName() {
144 return this.filename;
145 }
146
147 private class CancelListener
148 implements ActionListener {
149
150 public void actionPerformed(ActionEvent event) {
151 result = CANCEL_OPTION;
152 OpenCollectionDialog.this.dispose();
153 }
154 }
155
156 private class CollectionListSelectionListener
157 extends MouseAdapter
158 implements ListSelectionListener {
159
160 public void mouseClicked(MouseEvent event) {
161 ///ystem.err.println("Mouse clicked");
162 if(event.getClickCount() >= 2) {
163 Point location = event.getPoint();
164 int index = collection_list.locationToIndex(location);
165 collection_list.setSelectedIndex(index);
166 location = null;
167 open_button.doClick();
168 }
169 }
170
171 public void valueChanged(ListSelectionEvent event) {
172 if(collection_list.isSelectionEmpty()) {
173 card_layout.show(description_pane, BLANK);
174 open_button.setEnabled(false);
175 }
176 else {
177 BasicCollectionConfiguration collection_configuration = (BasicCollectionConfiguration) collection_list.getSelectedValue();
178 description_textarea.setText(collection_configuration.getDescription());
179 description_textarea.setCaretPosition(0);
180 card_layout.show(description_pane, DESCRIPTION);
181 open_button.setEnabled(true);
182 }
183 }
184 }
185
186 // use this if we ever go back to viewing all colls at once
187 /* private class GS3CollectionListModel
188 extends AbstractListModel {
189
190 private TreeSet data;
191
192 public GS3CollectionListModel() {
193 data = new TreeSet();
194 File sites_folder = new File(Utility.getSitesDir(Configuration.gsdl3_path));
195 File sites[] = sites_folder.listFiles();
196 for (int s=0; s<sites.length; s++) {
197 String site_name = sites[s].getName();
198 System.err.println("found site "+site_name);
199 File collect_folder = new File(Utility.getCollectDir(Configuration.gsdl3_path, site_name) );
200 if (!collect_folder.exists()) {
201 continue;
202 }
203 File[] collection_folders = collect_folder.listFiles();
204 for(int i = 0; i < collection_folders.length; i++) {
205 File collection_folder = collection_folders[i];
206 String collection_foldername = collection_folder.getName();
207 if(!collection_folder.isFile() && !collection_foldername.equals(StaticStrings.MODEL_COLLECTION_NAME)) {
208 BasicCollectionConfiguration collection_configuration = new BasicCollectionConfiguration(new File(collection_folder, Utility.CONFIG_FILE), site_name);
209 if(!collection_configuration.getName().equals(StaticStrings.ERROR_STR)) {
210 data.add(collection_configuration);
211 }
212 }
213 collection_foldername = null;
214 collection_folder = null;
215 } // for each collection
216 collection_folders = null;
217 collect_folder = null;
218 } // for each site
219 sites_folder = null;
220 sites = null;
221 }
222
223 public Object getElementAt(int index) {
224 Iterator iterator = data.iterator();
225 for(int i = 0; iterator.hasNext(); i++) {
226 Object object = iterator.next();
227 if(i == index) {
228 iterator = null;
229 return object;
230 }
231 object = null;
232 }
233 iterator = null;
234 return null;
235 }
236
237 public int getSize() {
238 return data.size();
239 }
240 }
241 */
242 private class CollectionListModel
243 extends AbstractListModel {
244
245 private TreeSet data;
246 static final public int COLLECT = 3;
247
248 public CollectionListModel() {
249 data = new TreeSet();
250 File collect_directory = new File(Gatherer.getCollectDirectoryPath());
251 if (!collect_directory.exists()) {
252 collect_directory = null;
253 return;
254 }
255
256 load_collection_configs(data,collect_directory);
257
258 }
259
260
261 protected void load_collection_configs(TreeSet data, File collect_directory) {
262
263 File[] collection_folders = collect_directory.listFiles();
264
265 for(int i = 0; i < collection_folders.length; i++) {
266 File collection_folder = collection_folders[i];
267 String collection_foldername = collection_folder.getName();
268
269 if(collection_folder.isDirectory() && !collection_foldername.equals(StaticStrings.MODEL_COLLECTION_NAME)) {
270 // this 'file_name' has already been prefixed by 'etc'
271 String file_name;
272 if (Gatherer.GS3){
273 convertToGS3Collection(collection_folder);
274 file_name = Utility.CONFIG_GS3_FILE;
275 }else{
276 file_name = Utility.CONFIG_FILE;
277 }
278
279 File config_file = new File(collection_folder, file_name);
280 File collection_metadata_directory = new File(collection_folder, "metadata");
281 if (collection_metadata_directory.exists() && config_file.exists()) {
282 BasicCollectionConfiguration collection_configuration = new BasicCollectionConfiguration(config_file);
283
284 // look to see if group set?
285 // => if it is, don't add, but recurse to look for child collections in collect-group
286
287 if (collection_configuration.getCollectGroup().equals("true")) {
288 load_collection_configs(data, collection_folder);
289 }
290 else {
291 data.add(collection_configuration);
292 }
293 }
294 config_file = null;
295 }
296 collection_foldername = null;
297 collection_folder = null;
298 }
299 collection_folders = null;
300 collect_directory = null;
301 }
302
303
304
305 public Object getElementAt(int index) {
306 Iterator iterator = data.iterator();
307 for(int i = 0; iterator.hasNext(); i++) {
308 Object object = iterator.next();
309 if(i == index) {
310 iterator = null;
311 return object;
312 }
313 object = null;
314 }
315 iterator = null;
316 return null;
317 }
318
319 public int getSize() {
320 return data.size();
321 }
322
323 public void convertToGS3Collection(File collection_folder) {
324
325 File collect_cfg_file = new File(collection_folder.getAbsolutePath() + File.separator + "etc" + File.separator + "collect.cfg");
326 File build_cfg_file = new File(collection_folder.getAbsolutePath() + File.separator+"index" + File.separator + "build.cfg");
327
328 if (collect_cfg_file.exists() && build_cfg_file.exists()){
329 // Generate the convert_coll_from_gs2.pl command
330 ArrayList command_parts_list = new ArrayList();
331 if ((Utility.isWindows()) && (!Gatherer.isGsdlRemote)) {
332 command_parts_list.add(Configuration.perl_path);
333 command_parts_list.add("-S");
334 }
335 command_parts_list.add(Configuration.getGS3ScriptPath() + "convert_coll_from_gs2.pl");
336 command_parts_list.add("-collectdir");
337 command_parts_list.add(collection_folder.getParent());
338 command_parts_list.add(collection_folder.getName());
339
340 // Run the convert_coll_from_gs2.pl command
341 String[] command_parts = (String[]) command_parts_list.toArray(new String[0]);
342 GShell process = new GShell(command_parts, GShell.CONVERT, COLLECT, null, null, GShell.GSHELL_CONVERT);
343 //process.addGShellListener(this);
344 process.run(); // Don't bother threading this... yet
345
346 collect_cfg_file.delete();
347 build_cfg_file.delete();
348 }
349 }
350
351 }
352
353 private class OpenListener
354 implements ActionListener {
355
356 public void actionPerformed(ActionEvent event) {
357 result = OK_OPTION;
358 Object selected_object = collection_list.getSelectedValue();
359 if (selected_object instanceof BasicCollectionConfiguration) {
360 BasicCollectionConfiguration collection_configuration = (BasicCollectionConfiguration)selected_object; //(BasicCollectionConfiguration) collection_list.getSelectedValue();
361 //*************888
362 File collect_cfg_file = collection_configuration.getFile(); // returns the collect.cfg file
363 File etc_folder = collect_cfg_file.getParentFile();
364 File collection_folder = etc_folder.getParentFile();
365 filename = collection_folder.getAbsolutePath() + File.separator + collection_folder.getName() + ".col";
366 collection_folder = null;
367 etc_folder = null;
368 collect_cfg_file = null;
369 collection_configuration = null;
370 OpenCollectionDialog.this.dispose();
371 }
372 }
373 }
374}
Note: See TracBrowser for help on using the repository browser.