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

Last change on this file since 29016 was 29016, checked in by ak19, 10 years ago

Cosmetic changes: 1. renamed inner StreamGobbler classes. 2. Reverting accidental commit of debugging statements in OpenCollectionDialog.java

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