source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/gui/OpenCollectionDialog.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

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