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

Last change on this file was 37236, checked in by kjdon, 15 months ago

first stab at doing some authentication in webswing gli. firstly, library_url must be passed in as an arg. we prompt for a username and password very early on. If no collection editing groups, or invalid user, then the gli won't start. open collection dialog only shows collections that the user has permission for

  • Property svn:keywords set to Author Date Id Revision
File size: 16.9 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
327 // this 'file_name' has already been prefixed by 'etc'
328 String file_name;
329 if (Gatherer.GS3){
330 convertToGS3Collection(collection_folder);
331 file_name = Utility.CONFIG_GS3_FILE;
332 }else{
333 file_name = Utility.CONFIG_FILE;
334 }
335
336 File config_file = new File(collection_folder, file_name);
337 if (config_file.exists()) {
338 BasicCollectionConfiguration collection_configuration = new BasicCollectionConfiguration(config_file);
339
340 // look to see if group set?
341 // => if it is, don't add, but recurse to look for child collections in collect-group
342
343 if (collection_configuration.getCollectGroup().equals("true")) {
344 load_collection_configs(data, collection_folder);
345 }
346 else {
347 // add this collection. We want to allow non gli collections, so add anything that has a config file.
348 // webswing - check whether we are allowed to edit this coll
349 if (Gatherer.isWebswing) {
350 if (Gatherer.webswingAuthenticator.canEditCollection(collection_foldername)) {
351 data.add(collection_configuration);
352 }
353 }
354 else {
355 data.add(collection_configuration);
356 }
357 }
358 }
359 config_file = null;
360 }
361 collection_foldername = null;
362 collection_folder = null;
363 }
364 collection_folders = null;
365 collect_directory = null;
366 }
367
368
369
370 public Object getElementAt(int index) {
371 Iterator iterator = data.iterator();
372 for(int i = 0; iterator.hasNext(); i++) {
373 Object object = iterator.next();
374 if(i == index) {
375 iterator = null;
376 return object;
377 }
378 object = null;
379 }
380 iterator = null;
381 return null;
382 }
383
384 public int getSize() {
385 return data.size();
386 }
387
388 public void convertToGS3Collection(File collection_folder) {
389
390 File collect_cfg_file = new File(collection_folder.getAbsolutePath() + File.separator + "etc" + File.separator + "collect.cfg");
391 File build_cfg_file = new File(collection_folder.getAbsolutePath() + File.separator+"index" + File.separator + "build.cfg");
392
393 if (collect_cfg_file.exists() && build_cfg_file.exists()){
394 // Generate the convert_coll_from_gs2.pl command
395 ArrayList command_parts_list = new ArrayList();
396 if (!Gatherer.isGsdlRemote) {
397 command_parts_list.add(Configuration.perl_path);
398 command_parts_list.add("-S");
399 }
400 command_parts_list.add(Configuration.getGS3ScriptPath() + "convert_coll_from_gs2.pl");
401 command_parts_list.add("-collectdir");
402 command_parts_list.add(collection_folder.getParent());
403 command_parts_list.add(collection_folder.getName());
404
405 // Run the convert_coll_from_gs2.pl command
406 String[] command_parts = (String[]) command_parts_list.toArray(new String[0]);
407 GShell process = new GShell(command_parts, GShell.CONVERT, COLLECT, null, null, GShell.GSHELL_CONVERT);
408 //process.addGShellListener(this);
409 process.run(); // Don't bother threading this... yet
410
411 // From now, won't dangerously delete GS2 collect and build config files anymore
412 //collect_cfg_file.delete();
413 //build_cfg_file.delete();
414
415 File collect_bak_file = new File(collection_folder.getAbsolutePath() + File.separator + "etc" + File.separator + "collect.cfg.bak");
416 File build_bak_file = new File(collection_folder.getAbsolutePath() + File.separator+"index" + File.separator + "build.cfg.bak");
417 if(!collect_cfg_file.renameTo(collect_bak_file)) {
418 System.err.println("Unable to move collect.cfg to " + collect_bak_file);
419 }
420 if(!build_cfg_file.renameTo(build_bak_file)) {
421 System.err.println("Unable to move build.cfg to " + build_bak_file);
422 }
423 }
424 }
425 }
426
427 private class OpenListener
428 implements ActionListener {
429
430 public void actionPerformed(ActionEvent event) {
431 result = OK_OPTION;
432 Object selected_object = collection_list.getSelectedValue();
433 if (selected_object instanceof BasicCollectionConfiguration) {
434 BasicCollectionConfiguration collection_configuration = (BasicCollectionConfiguration)selected_object; //(BasicCollectionConfiguration) collection_list.getSelectedValue();
435 //****************
436 File collect_cfg_file = collection_configuration.getFile(); // returns the collect.cfg file
437 result = FormatConversionDialog.checkForGS2FormatStatements(collect_cfg_file);
438 if(result == OK_OPTION) { // either there were no gs2 format stmts or user chose to proceed
439 File etc_folder = collect_cfg_file.getParentFile();
440 File collection_folder = etc_folder.getParentFile();
441 filename = collection_folder.getAbsolutePath() + File.separator + "gli.col";
442 collection_folder = null;
443 etc_folder = null;
444 collect_cfg_file = null;
445 collection_configuration = null;
446 }
447 OpenCollectionDialog.this.dispose();
448 }
449
450 Gatherer.collectDirectoryHasChanged(Gatherer.getCollectDirectoryPath(),
451 newCollectPath, Gatherer.g_man.getContentPane());
452 // will tell the server that the collect directory has changed and that
453 // the workspace needs to be refreshed (Documents in Greenstone Collections)
454
455 }
456 }
457}
Note: See TracBrowser for help on using the repository browser.