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

Last change on this file since 18370 was 18370, checked in by kjdon, 15 years ago

committed code submitted by Amin Hedjazi for making the GLI right to left. I worked on this code on the rtl-gli branch, then merged the branch back to the trunk at revision 18368. The branch code was slightly different in a couple of places where it shouldn't have been. So don't use the branch code next time. Start a new branch.

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