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

Last change on this file since 14753 was 14753, checked in by qq6, 16 years ago

Convert gs2 collects to gs3 collects

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