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

Last change on this file since 22605 was 22605, checked in by ak19, 14 years ago

Ticket #152: Allowing different paths to collect dir so that GLI can work with collect dirs on pen drives. NONE OF THESE CHANGES ARE FOR Client-GLI AS YET. 1. Preferences (Connection tab), Open and New Collection dialogs allow one to change the current collect directory containing the collections to select from. 2. New Collection dialog allows one to base a new collection on an existing collection in a collect dir other than the current collect dir. 3. Collections in the Documents in Greenstone Collections Workspace Tree Node now have two additional rightclick options: to move and copy these collections to another location.

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