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

Last change on this file since 23238 was 23238, checked in by ak19, 13 years ago

In the New Collection and Open Collection dialogs, the OK buttons now come first/leftmost and are followed by the Change_Dir buttons. Kathy explained that the default button (OK) being leftmost is more sensible.

  • Property svn:keywords set to Author Date Id Revision
File size: 15.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 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(open_button);
148 button_pane.add(chdir_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 // Final dialog setup & positioning.
156 getRootPane().setDefaultButton(open_button);
157 Dimension screen_size = Configuration.screen_size;
158 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
159 screen_size = null;
160 }
161
162 public void destroy() {
163 }
164
165 public int display() {
166 setVisible(true);
167 return result;
168 }
169
170 public String getFileName() {
171 return this.filename;
172 }
173
174 private class ChangeDirListener implements ActionListener {
175 public void actionPerformed(ActionEvent event) {
176 JFileChooser chooser = new JFileChooser(newCollectPath);
177 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
178 chooser.setDialogTitle(Dictionary.get("General.ChooseCollectDirectory"));
179 int returnVal = chooser.showOpenDialog(OpenCollectionDialog.this);
180 if(returnVal == JFileChooser.APPROVE_OPTION) {
181 String oldCollectPath = newCollectPath;
182 newCollectPath = chooser.getSelectedFile().getAbsolutePath() + File.separator;
183
184 if(!newCollectPath.equals(oldCollectPath)) {
185 // reload the list of available collections using this new path
186 CollectionListModel listModel = (CollectionListModel)collection_list.getModel();
187 listModel.reload(newCollectPath);
188 collection_list.repaint();
189 }
190 }
191 }
192 }
193
194 private class CancelListener
195 implements ActionListener {
196
197 public void actionPerformed(ActionEvent event) {
198 result = CANCEL_OPTION;
199 OpenCollectionDialog.this.dispose();
200 }
201 }
202
203 private class CollectionListSelectionListener
204 extends MouseAdapter
205 implements ListSelectionListener {
206
207 public void mouseClicked(MouseEvent event) {
208 ///ystem.err.println("Mouse clicked");
209 if(event.getClickCount() >= 2) {
210 Point location = event.getPoint();
211 int index = collection_list.locationToIndex(location);
212 collection_list.setSelectedIndex(index);
213 location = null;
214 open_button.doClick();
215 }
216 }
217
218 public void valueChanged(ListSelectionEvent event) {
219 if(collection_list.isSelectionEmpty()) {
220 card_layout.show(description_pane, BLANK);
221 open_button.setEnabled(false);
222 }
223 else {
224 BasicCollectionConfiguration collection_configuration = (BasicCollectionConfiguration) collection_list.getSelectedValue();
225 description_textarea.setText(collection_configuration.getDescription());
226 description_textarea.setCaretPosition(0);
227 card_layout.show(description_pane, DESCRIPTION);
228 open_button.setEnabled(true);
229 }
230 }
231 }
232
233 // use this if we ever go back to viewing all colls at once
234 /* private class GS3CollectionListModel
235 extends AbstractListModel {
236
237 private TreeSet data;
238
239 public GS3CollectionListModel() {
240 data = new TreeSet();
241 File sites_folder = new File(Utility.getSitesDir(Configuration.gsdl3_path));
242 File sites[] = sites_folder.listFiles();
243 for (int s=0; s<sites.length; s++) {
244 String site_name = sites[s].getName();
245 File collect_folder = new File(Utility.getCollectDir(Configuration.gsdl3_path, site_name) );
246 if (!collect_folder.exists()) {
247 continue;
248 }
249 File[] collection_folders = collect_folder.listFiles();
250 for(int i = 0; i < collection_folders.length; i++) {
251 File collection_folder = collection_folders[i];
252 String collection_foldername = collection_folder.getName();
253 if(!collection_folder.isFile() && !collection_foldername.equals(StaticStrings.MODEL_COLLECTION_NAME)) {
254 BasicCollectionConfiguration collection_configuration = new BasicCollectionConfiguration(new File(collection_folder, Utility.CONFIG_FILE), site_name);
255 if(!collection_configuration.getName().equals(StaticStrings.ERROR_STR)) {
256 data.add(collection_configuration);
257 }
258 }
259 collection_foldername = null;
260 collection_folder = null;
261 } // for each collection
262 collection_folders = null;
263 collect_folder = null;
264 } // for each site
265 sites_folder = null;
266 sites = null;
267 }
268
269 public Object getElementAt(int index) {
270 Iterator iterator = data.iterator();
271 for(int i = 0; iterator.hasNext(); i++) {
272 Object object = iterator.next();
273 if(i == index) {
274 iterator = null;
275 return object;
276 }
277 object = null;
278 }
279 iterator = null;
280 return null;
281 }
282
283 public int getSize() {
284 return data.size();
285 }
286 }
287 */
288 private class CollectionListModel
289 extends AbstractListModel {
290
291 private TreeSet data;
292 static final public int COLLECT = 3;
293
294 public CollectionListModel() {
295 data = new TreeSet();
296 reload(Gatherer.getCollectDirectoryPath());
297 }
298
299 // to be called when the collection directory has changed
300 public void reload(String collectDirectoryPath) {
301 data.clear();
302 File collect_directory = new File(collectDirectoryPath);
303 if (!collect_directory.exists()) {
304 collect_directory = null;
305 return;
306 }
307
308 load_collection_configs(data,collect_directory);
309
310 // list size/contents has changed, this listmodel must notify the
311 // encapsulating JList, else it won't be able to display longer lists
312 fireContentsChanged(this, 0, data.size());
313 }
314
315
316 protected void load_collection_configs(TreeSet data, File collect_directory) {
317
318 File[] collection_folders = collect_directory.listFiles();
319
320 for(int i = 0; i < collection_folders.length; i++) {
321 File collection_folder = collection_folders[i];
322 String collection_foldername = collection_folder.getName();
323
324 if(collection_folder.isDirectory() && !collection_foldername.equals(StaticStrings.MODEL_COLLECTION_NAME)) {
325 // this 'file_name' has already been prefixed by 'etc'
326 String file_name;
327 if (Gatherer.GS3){
328 convertToGS3Collection(collection_folder);
329 file_name = Utility.CONFIG_GS3_FILE;
330 }else{
331 file_name = Utility.CONFIG_FILE;
332 }
333
334 File config_file = new File(collection_folder, file_name);
335 if (config_file.exists()) {
336 BasicCollectionConfiguration collection_configuration = new BasicCollectionConfiguration(config_file);
337
338 // look to see if group set?
339 // => if it is, don't add, but recurse to look for child collections in collect-group
340
341 if (collection_configuration.getCollectGroup().equals("true")) {
342 load_collection_configs(data, collection_folder);
343 }
344 else {
345 // add this collection. We want to allow non gli collections, so add anything that has a config file.
346 data.add(collection_configuration);
347 }
348 }
349 config_file = null;
350 }
351 collection_foldername = null;
352 collection_folder = null;
353 }
354 collection_folders = null;
355 collect_directory = null;
356 }
357
358
359
360 public Object getElementAt(int index) {
361 Iterator iterator = data.iterator();
362 for(int i = 0; iterator.hasNext(); i++) {
363 Object object = iterator.next();
364 if(i == index) {
365 iterator = null;
366 return object;
367 }
368 object = null;
369 }
370 iterator = null;
371 return null;
372 }
373
374 public int getSize() {
375 return data.size();
376 }
377
378 public void convertToGS3Collection(File collection_folder) {
379
380 File collect_cfg_file = new File(collection_folder.getAbsolutePath() + File.separator + "etc" + File.separator + "collect.cfg");
381 File build_cfg_file = new File(collection_folder.getAbsolutePath() + File.separator+"index" + File.separator + "build.cfg");
382
383 if (collect_cfg_file.exists() && build_cfg_file.exists()){
384 // Generate the convert_coll_from_gs2.pl command
385 ArrayList command_parts_list = new ArrayList();
386 if (!Gatherer.isGsdlRemote) {
387 command_parts_list.add(Configuration.perl_path);
388 command_parts_list.add("-S");
389 }
390 command_parts_list.add(Configuration.getGS3ScriptPath() + "convert_coll_from_gs2.pl");
391 command_parts_list.add("-collectdir");
392 command_parts_list.add(collection_folder.getParent());
393 command_parts_list.add(collection_folder.getName());
394
395 // Run the convert_coll_from_gs2.pl command
396 String[] command_parts = (String[]) command_parts_list.toArray(new String[0]);
397 GShell process = new GShell(command_parts, GShell.CONVERT, COLLECT, null, null, GShell.GSHELL_CONVERT);
398 //process.addGShellListener(this);
399 process.run(); // Don't bother threading this... yet
400
401 collect_cfg_file.delete();
402 build_cfg_file.delete();
403 }
404 }
405 }
406
407 private class OpenListener
408 implements ActionListener {
409
410 public void actionPerformed(ActionEvent event) {
411 result = OK_OPTION;
412 Object selected_object = collection_list.getSelectedValue();
413 if (selected_object instanceof BasicCollectionConfiguration) {
414 BasicCollectionConfiguration collection_configuration = (BasicCollectionConfiguration)selected_object; //(BasicCollectionConfiguration) collection_list.getSelectedValue();
415 //****************
416 File collect_cfg_file = collection_configuration.getFile(); // returns the collect.cfg file
417 File etc_folder = collect_cfg_file.getParentFile();
418 File collection_folder = etc_folder.getParentFile();
419 filename = collection_folder.getAbsolutePath() + File.separator + "gli.col";
420 collection_folder = null;
421 etc_folder = null;
422 collect_cfg_file = null;
423 collection_configuration = null;
424 OpenCollectionDialog.this.dispose();
425 }
426
427 Gatherer.collectDirectoryHasChanged(Gatherer.getCollectDirectoryPath(),
428 newCollectPath, Gatherer.g_man.getContentPane());
429 // will tell the server that the collect directory has changed and that
430 // the workspace needs to be refreshed (Documents in Greenstone Collections)
431 }
432 }
433}
Note: See TracBrowser for help on using the repository browser.