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

Last change on this file since 22357 was 22357, checked in by kjdon, 14 years ago

.col file now called gli.col, not collname.col (to make it easy to renmae collections). Also, don't check for existence of metadata directory - we want to allow non-gli collections to be opened. so an etc/collect.cfg is enough

  • 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 * 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 this.setComponentOrientation(Dictionary.getOrientation());
70 setSize(SIZE);
71 setTitle(Dictionary.get("OpenCollectionDialog.Title"));
72
73 // Creation
74 JPanel content_pane = (JPanel) getContentPane();
75 content_pane.setComponentOrientation(Dictionary.getOrientation());
76
77 JPanel center_pane = new JPanel();
78 center_pane.setComponentOrientation(Dictionary.getOrientation());
79
80 JPanel collection_list_pane = new JPanel();
81 collection_list_pane.setComponentOrientation(Dictionary.getOrientation());
82 JLabel collection_list_label = new JLabel(Dictionary.get("OpenCollectionDialog.Available_Collections"));
83 collection_list_label.setComponentOrientation(Dictionary.getOrientation());
84 collection_list = new JList(new CollectionListModel());
85 collection_list.setComponentOrientation(Dictionary.getOrientation());
86
87 description_pane = new JPanel();
88 description_pane.setComponentOrientation(Dictionary.getOrientation());
89 card_layout = new CardLayout();
90
91 JPanel blank_pane = new JPanel();
92 blank_pane.setComponentOrientation(Dictionary.getOrientation());
93
94 JPanel description_textarea_pane = new JPanel();
95 description_textarea_pane.setComponentOrientation(Dictionary.getOrientation());
96 JLabel description_textarea_label = new JLabel(Dictionary.get("OpenCollectionDialog.Description"));
97 description_textarea_label.setComponentOrientation(Dictionary.getOrientation());
98 description_textarea = new JTextArea();
99 description_textarea.setComponentOrientation(Dictionary.getOrientation());
100
101 JPanel button_pane = new JPanel();
102 button_pane.setComponentOrientation(Dictionary.getOrientation());
103 open_button = new GLIButton(Dictionary.get("OpenCollectionDialog.Open"), Dictionary.get("OpenCollectionDialog.Open_Tooltip"));
104 open_button.setEnabled(false);
105
106 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Pure_Cancel_Tooltip"));
107
108 // Connection
109 cancel_button.addActionListener(new CancelListener());
110 open_button.addActionListener(new OpenListener());
111 CollectionListSelectionListener clsl = new CollectionListSelectionListener();
112 collection_list.addListSelectionListener(clsl);
113 collection_list.addMouseListener(clsl);
114 clsl = null;
115
116 // Layout
117 JScrollPane scrol_tmp;
118
119 collection_list_pane.setLayout(new BorderLayout());
120 collection_list_pane.add(collection_list_label, BorderLayout.NORTH);
121 scrol_tmp = new JScrollPane(collection_list);
122 scrol_tmp.setComponentOrientation(Dictionary.getOrientation());
123 collection_list_pane.add(scrol_tmp, BorderLayout.CENTER);
124
125 description_textarea_pane.setLayout(new BorderLayout());
126 description_textarea_pane.add(description_textarea_label, BorderLayout.NORTH);
127 scrol_tmp =new JScrollPane(description_textarea);
128 scrol_tmp.setComponentOrientation(Dictionary.getOrientation());
129 description_textarea_pane.add(scrol_tmp, BorderLayout.CENTER);
130
131 description_pane.setLayout(card_layout);
132 description_pane.add(description_textarea_pane, DESCRIPTION);
133 description_pane.add(blank_pane, BLANK);
134
135 center_pane.setLayout(new GridLayout(2,1,0,5));
136 center_pane.add(collection_list_pane);
137 center_pane.add(description_pane);
138
139 button_pane.setLayout(new GridLayout(1,4));
140 JPanel tmp =new JPanel();
141 tmp.setComponentOrientation(Dictionary.getOrientation());
142 button_pane.add(tmp);
143 button_pane.add(open_button);
144 button_pane.add(cancel_button);
145
146 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
147 content_pane.setLayout(new BorderLayout());
148 content_pane.add(center_pane, BorderLayout.CENTER);
149 content_pane.add(button_pane, BorderLayout.SOUTH);
150
151 Dimension screen_size = Configuration.screen_size;
152 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
153 screen_size = null;
154 }
155
156 public void destroy() {
157 }
158
159 public int display() {
160 setVisible(true);
161 return result;
162 }
163
164 public String getFileName() {
165 return this.filename;
166 }
167
168 private class CancelListener
169 implements ActionListener {
170
171 public void actionPerformed(ActionEvent event) {
172 result = CANCEL_OPTION;
173 OpenCollectionDialog.this.dispose();
174 }
175 }
176
177 private class CollectionListSelectionListener
178 extends MouseAdapter
179 implements ListSelectionListener {
180
181 public void mouseClicked(MouseEvent event) {
182 ///ystem.err.println("Mouse clicked");
183 if(event.getClickCount() >= 2) {
184 Point location = event.getPoint();
185 int index = collection_list.locationToIndex(location);
186 collection_list.setSelectedIndex(index);
187 location = null;
188 open_button.doClick();
189 }
190 }
191
192 public void valueChanged(ListSelectionEvent event) {
193 if(collection_list.isSelectionEmpty()) {
194 card_layout.show(description_pane, BLANK);
195 open_button.setEnabled(false);
196 }
197 else {
198 BasicCollectionConfiguration collection_configuration = (BasicCollectionConfiguration) collection_list.getSelectedValue();
199 description_textarea.setText(collection_configuration.getDescription());
200 description_textarea.setCaretPosition(0);
201 card_layout.show(description_pane, DESCRIPTION);
202 open_button.setEnabled(true);
203 }
204 }
205 }
206
207 // use this if we ever go back to viewing all colls at once
208 /* private class GS3CollectionListModel
209 extends AbstractListModel {
210
211 private TreeSet data;
212
213 public GS3CollectionListModel() {
214 data = new TreeSet();
215 File sites_folder = new File(Utility.getSitesDir(Configuration.gsdl3_path));
216 File sites[] = sites_folder.listFiles();
217 for (int s=0; s<sites.length; s++) {
218 String site_name = sites[s].getName();
219 File collect_folder = new File(Utility.getCollectDir(Configuration.gsdl3_path, site_name) );
220 if (!collect_folder.exists()) {
221 continue;
222 }
223 File[] collection_folders = collect_folder.listFiles();
224 for(int i = 0; i < collection_folders.length; i++) {
225 File collection_folder = collection_folders[i];
226 String collection_foldername = collection_folder.getName();
227 if(!collection_folder.isFile() && !collection_foldername.equals(StaticStrings.MODEL_COLLECTION_NAME)) {
228 BasicCollectionConfiguration collection_configuration = new BasicCollectionConfiguration(new File(collection_folder, Utility.CONFIG_FILE), site_name);
229 if(!collection_configuration.getName().equals(StaticStrings.ERROR_STR)) {
230 data.add(collection_configuration);
231 }
232 }
233 collection_foldername = null;
234 collection_folder = null;
235 } // for each collection
236 collection_folders = null;
237 collect_folder = null;
238 } // for each site
239 sites_folder = null;
240 sites = null;
241 }
242
243 public Object getElementAt(int index) {
244 Iterator iterator = data.iterator();
245 for(int i = 0; iterator.hasNext(); i++) {
246 Object object = iterator.next();
247 if(i == index) {
248 iterator = null;
249 return object;
250 }
251 object = null;
252 }
253 iterator = null;
254 return null;
255 }
256
257 public int getSize() {
258 return data.size();
259 }
260 }
261 */
262 private class CollectionListModel
263 extends AbstractListModel {
264
265 private TreeSet data;
266 static final public int COLLECT = 3;
267
268 public CollectionListModel() {
269 data = new TreeSet();
270 File collect_directory = new File(Gatherer.getCollectDirectoryPath());
271 if (!collect_directory.exists()) {
272 collect_directory = null;
273 return;
274 }
275
276 load_collection_configs(data,collect_directory);
277
278 }
279
280
281 protected void load_collection_configs(TreeSet data, File collect_directory) {
282
283 File[] collection_folders = collect_directory.listFiles();
284
285 for(int i = 0; i < collection_folders.length; i++) {
286 File collection_folder = collection_folders[i];
287 String collection_foldername = collection_folder.getName();
288
289 if(collection_folder.isDirectory() && !collection_foldername.equals(StaticStrings.MODEL_COLLECTION_NAME)) {
290 // this 'file_name' has already been prefixed by 'etc'
291 String file_name;
292 if (Gatherer.GS3){
293 convertToGS3Collection(collection_folder);
294 file_name = Utility.CONFIG_GS3_FILE;
295 }else{
296 file_name = Utility.CONFIG_FILE;
297 }
298
299 File config_file = new File(collection_folder, file_name);
300 if (config_file.exists()) {
301 BasicCollectionConfiguration collection_configuration = new BasicCollectionConfiguration(config_file);
302
303 // look to see if group set?
304 // => if it is, don't add, but recurse to look for child collections in collect-group
305
306 if (collection_configuration.getCollectGroup().equals("true")) {
307 load_collection_configs(data, collection_folder);
308 }
309 else {
310 // add this collection. We want to allow non gli collections, so add anything that has a config file.
311 data.add(collection_configuration);
312 }
313 }
314 config_file = null;
315 }
316 collection_foldername = null;
317 collection_folder = null;
318 }
319 collection_folders = null;
320 collect_directory = null;
321 }
322
323
324
325 public Object getElementAt(int index) {
326 Iterator iterator = data.iterator();
327 for(int i = 0; iterator.hasNext(); i++) {
328 Object object = iterator.next();
329 if(i == index) {
330 iterator = null;
331 return object;
332 }
333 object = null;
334 }
335 iterator = null;
336 return null;
337 }
338
339 public int getSize() {
340 return data.size();
341 }
342
343 public void convertToGS3Collection(File collection_folder) {
344
345 File collect_cfg_file = new File(collection_folder.getAbsolutePath() + File.separator + "etc" + File.separator + "collect.cfg");
346 File build_cfg_file = new File(collection_folder.getAbsolutePath() + File.separator+"index" + File.separator + "build.cfg");
347
348 if (collect_cfg_file.exists() && build_cfg_file.exists()){
349 // Generate the convert_coll_from_gs2.pl command
350 ArrayList command_parts_list = new ArrayList();
351 if (!Gatherer.isGsdlRemote) {
352 command_parts_list.add(Configuration.perl_path);
353 command_parts_list.add("-S");
354 }
355 command_parts_list.add(Configuration.getGS3ScriptPath() + "convert_coll_from_gs2.pl");
356 command_parts_list.add("-collectdir");
357 command_parts_list.add(collection_folder.getParent());
358 command_parts_list.add(collection_folder.getName());
359
360 // Run the convert_coll_from_gs2.pl command
361 String[] command_parts = (String[]) command_parts_list.toArray(new String[0]);
362 GShell process = new GShell(command_parts, GShell.CONVERT, COLLECT, null, null, GShell.GSHELL_CONVERT);
363 //process.addGShellListener(this);
364 process.run(); // Don't bother threading this... yet
365
366 collect_cfg_file.delete();
367 build_cfg_file.delete();
368 }
369 }
370 }
371
372 private class OpenListener
373 implements ActionListener {
374
375 public void actionPerformed(ActionEvent event) {
376 result = OK_OPTION;
377 Object selected_object = collection_list.getSelectedValue();
378 if (selected_object instanceof BasicCollectionConfiguration) {
379 BasicCollectionConfiguration collection_configuration = (BasicCollectionConfiguration)selected_object; //(BasicCollectionConfiguration) collection_list.getSelectedValue();
380 //****************
381 File collect_cfg_file = collection_configuration.getFile(); // returns the collect.cfg file
382 File etc_folder = collect_cfg_file.getParentFile();
383 File collection_folder = etc_folder.getParentFile();
384 filename = collection_folder.getAbsolutePath() + File.separator + "gli.col";
385 collection_folder = null;
386 etc_folder = null;
387 collect_cfg_file = null;
388 collection_configuration = null;
389 OpenCollectionDialog.this.dispose();
390 }
391 }
392 }
393}
Note: See TracBrowser for help on using the repository browser.