source: trunk/gli/src/org/greenstone/gatherer/gui/ExplodeMetadataDatabasePrompt.java@ 13748

Last change on this file since 13748 was 13748, checked in by mdewsnip, 17 years ago

No longer saves the import.pl and buildcol.pl arguments in the GLI config.xml file. This was dodgy with different languages, and the saved options were often out of date. Now the options are simply read when required (similar to the plugin and classifier options).

  • Property svn:keywords set to Author Date Id Revision
File size: 13.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: Katherine Don, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 2005 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 javax.swing.*;
32import javax.swing.event.*;
33import javax.swing.text.*;
34import java.io.*;
35import java.util.ArrayList;
36
37import org.w3c.dom.Document;
38
39import org.greenstone.gatherer.Dictionary;
40import org.greenstone.gatherer.Configuration;
41import org.greenstone.gatherer.Gatherer;
42import org.greenstone.gatherer.cdm.Argument;
43import org.greenstone.gatherer.cdm.ArgumentControl;
44import org.greenstone.gatherer.cdm.CollectionDesignManager;
45import org.greenstone.gatherer.cdm.Plugin;
46import org.greenstone.gatherer.collection.CollectionManager;
47import org.greenstone.gatherer.collection.ScriptOptions;
48import org.greenstone.gatherer.greenstone.LocalGreenstone;
49import org.greenstone.gatherer.gui.tree.DragTree;
50import org.greenstone.gatherer.metadata.MetadataXMLFileManager;
51import org.greenstone.gatherer.metadata.MetadataElement;
52import org.greenstone.gatherer.metadata.MetadataSet;
53import org.greenstone.gatherer.metadata.MetadataSetManager;
54import org.greenstone.gatherer.metadata.MetadataTools;
55import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
56import org.greenstone.gatherer.shell.GShell;
57import org.greenstone.gatherer.shell.GShellEvent;
58import org.greenstone.gatherer.shell.GShellListener;
59import org.greenstone.gatherer.util.Utility;
60import org.greenstone.gatherer.util.XMLTools;
61
62public class ExplodeMetadataDatabasePrompt
63 extends ModalDialog
64 implements GShellListener
65{
66 /** The size of this new collection dialog box. */
67 static private Dimension SIZE = new Dimension(675, 350);
68 private JDialog self;
69
70 /** the file we wil be exploding */
71 private File metadata_file = null;
72 /** the list of potential plugins to be used */
73 private Argument plugin_arg = null;
74 /** holds all the available options for the exploding script */
75 private ScriptOptions options = null;
76 /** the pane containing the options */
77 private JPanel options_pane = null;
78 /** the error message if any */
79 private StringBuffer error_message = null;
80 /** whether we were successful or not */
81 private boolean successful;
82
83
84 public ExplodeMetadataDatabasePrompt(File source_file) {
85 super(Gatherer.g_man, true);
86 this.self = this;
87 this.metadata_file = source_file;
88
89 // check that we actually have an explodable file
90 ArrayList exp_plugins = CollectionDesignManager.plugin_manager.getExploderPlugins(source_file);
91 if (exp_plugins.size() == 0) {
92 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("ExplodeMetadataPrompt.NotExplodable"), Dictionary.get("ExplodeMetadataPrompt.Title"), JOptionPane.ERROR_MESSAGE);
93 return;
94 }
95 plugin_arg = createPluginArgument(exp_plugins);
96 setJMenuBar(new SimpleMenuBar("explodingmetadata"));
97 setSize(SIZE);
98 setTitle(Dictionary.get("ExplodeMetadataPrompt.Title"));
99
100 // set up the script options
101 // we have empty initial values
102 String dom_string = "<Options/>";
103 Document doc = XMLTools.parseXML(new StringReader(dom_string));
104 options = new ScriptOptions(doc.getDocumentElement(), "explode_metadata_database.pl");
105
106 // Creation
107 JPanel content_pane = (JPanel) getContentPane();
108 content_pane.setOpaque(true);
109
110 options_pane = new JPanel();
111 addScriptOptions(options_pane);
112 JScrollPane middle_pane = new JScrollPane(options_pane);
113
114 JTextArea instructions_area = new JTextArea(Dictionary.get("ExplodeMetadataPrompt.Instructions"));
115 instructions_area.setEditable(false);
116 instructions_area.setLineWrap(true);
117 instructions_area.setRows(5);
118 instructions_area.setWrapStyleWord(true);
119
120 JPanel button_pane = new JPanel();
121 JButton explode_button = new GLIButton(Dictionary.get("ExplodeMetadataPrompt.Explode"), Dictionary.get("ExplodeMetadataPrompt.Explode_Tooltip"));
122 JButton cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel_Tooltip"));
123
124 // Connection
125 cancel_button.addActionListener(new CancelListener());
126 explode_button.addActionListener(new ExplodeListener());
127
128 // Layout
129
130 button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
131 button_pane.setLayout(new GridLayout(1,2));
132 button_pane.add(explode_button);
133 button_pane.add(cancel_button);
134
135 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
136 content_pane.setLayout(new BorderLayout());
137 content_pane.add(instructions_area, BorderLayout.NORTH);
138 content_pane.add(middle_pane, BorderLayout.CENTER);
139 content_pane.add(button_pane, BorderLayout.SOUTH);
140
141 // Final dialog setup & positioning.
142 Dimension screen_size = Configuration.screen_size;
143 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
144 setVisible(true);
145 }
146
147 public void destroy()
148 {
149
150 }
151
152 /** All implementation of GShellListener must include this method so the listener can be informed of messages from the GShell.
153 * @param event A <strong>GShellEvent</strong> that contains, amoung other things, the message.
154 */
155 public synchronized void message(GShellEvent event) {
156 String message = event.getMessage();
157 if (message.startsWith("explode_metadata_database.pl>")) {
158 message = message.substring(29);
159 error_message.append(message);
160 error_message.append("\n");
161 }
162 }
163
164 /** All implementation of GShellListener must include this method so the listener can be informed when a GShell begins its task. Implementation side-effect, not actually used.
165 * @param event A <strong>GShellEvent</strong> that contains details of the initial state of the <strong>GShell</strong> before task comencement.
166 */
167 public synchronized void processBegun(GShellEvent event) {
168 // We don't care.
169 }
170
171 /** All implementation of GShellListener must include this method so the listener can be informed when a GShell completes its task.
172 * @param event A <strong>GShellEvent</strong> that contains details of the final state of the <strong>GShell</strong> after task completion.
173 */
174 public synchronized void processComplete(GShellEvent event) {
175 successful = false;
176 if(event.getStatus() == GShell.OK) {
177 successful = true;
178 }
179 }
180
181 private Argument createPluginArgument(ArrayList plugin_list) {
182 Argument arg = new Argument();
183 arg.setName("plugin");
184 arg.setDescription(Dictionary.get("ExplodeMetadataPrompt.Plugin"));
185 arg.setRequired(true);
186 arg.setType(Argument.ENUM);
187 for (int i=0; i<plugin_list.size(); i++) {
188 Plugin p = (Plugin)plugin_list.get(i);
189 arg.addOption(p.getName(), p.getName());
190 }
191 return arg;
192 }
193 private void addScriptOptions(JPanel options_pane) {
194
195 options_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
196 options_pane.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
197 options_pane.setLayout(new BoxLayout(options_pane, BoxLayout.Y_AXIS));
198
199 int current_mode = Configuration.getMode();
200 int total_argument_count = options.getArgumentCount();
201
202 // first we add the plugin arg
203 ArgumentControl argument_control = new ArgumentControl(plugin_arg, true, null);
204 argument_control.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
205 options_pane.add((JComponent)argument_control);
206 for(int i = 0; i < total_argument_count; i++) {
207 // Retrieve the argument so we know how to format the control.
208 Argument argument = options.getArgument(i);
209 if(!argument.isHiddenGLI() && argument.getModeLevel() <= current_mode) {
210 if (argument.getName().equals("metadata_set")) {
211 argument.setType(Argument.METADATA_SET_NAMESPACE);
212 argument_control = new ArgumentControl(argument, true, "Exploded Metadata Set (exp)");
213 }
214 else {
215 // by default, all args are disabled, and no value
216 argument_control = new ArgumentControl(argument, false, null);
217 }
218 // make sure they are coloured the way we want - this is not the standard arg control coloring
219 argument_control.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
220 options_pane.add((JComponent)argument_control);
221 }
222 }
223 }
224
225 private void updateScriptOptions() {
226 for(int i = 0; i < options_pane.getComponentCount(); i++) {
227 Component component = options_pane.getComponent(i);
228 if(component instanceof ArgumentControl) {
229 ArgumentControl ac = (ArgumentControl)component;
230 String name = ac.getArgumentName();
231 String value = ac.getValue();
232 boolean enabled = ac.isEnabled();
233 if (!enabled && value==null) {
234 // flag type, remove from options altogether
235 options.removeValue(name);
236 } else {
237 if (value.equals("")) {
238 // if the value is empty, we don't want it to be passed to the script
239 options.setValue(name, false, value);
240 } else {
241 options.setValue(name, enabled, value);
242 }
243 }
244
245 }
246 }
247 }
248
249 private int explodeMetadata()
250 {
251 // Generate the explode_metadata_database.pl command
252 ArrayList command_parts_list = new ArrayList();
253 if (Utility.isWindows() && (!Gatherer.isGsdlRemote)) {
254 command_parts_list.add(Configuration.perl_path);
255 command_parts_list.add("-S");
256 }
257 command_parts_list.add(LocalGreenstone.getBinScriptDirectoryPath() + "explode_metadata_database.pl");
258
259 // Add in all the options from the user
260 String[] explode_options = options.getValues();
261 for (int i = 0; i < explode_options.length; i++) {
262 command_parts_list.add(explode_options[i]);
263 }
264
265 // Local case
266 if (!Gatherer.isGsdlRemote) {
267 // Add in the filename
268 command_parts_list.add(metadata_file.getPath());
269 }
270 // Remote case
271 else {
272 // Add in the filename, relative to the collection directory
273 String collection_name = CollectionManager.getLoadedCollectionName();
274 String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
275 String metadata_file_relative_path = RemoteGreenstoneServer.getPathRelativeToDirectory(metadata_file, collection_directory_path);
276 command_parts_list.add("-file");
277 command_parts_list.add(metadata_file_relative_path);
278
279 // When running remotely we also need the collection name as the last argument
280 command_parts_list.add(collection_name);
281 }
282
283 // Run the explode_metadata_database.pl command
284 String[] command_parts = (String[]) command_parts_list.toArray(new String[0]);
285 this.error_message = new StringBuffer();
286 GShell process = new GShell(command_parts, GShell.EXPLODE, 3, this, null, GShell.GSHELL_EXPLODE);
287 //process.start();
288 process.run();
289
290 if (successful) {
291 return 0;
292 }
293 return -1;
294 }
295
296 private void resultPrompt(boolean success, String message)
297 {
298 if (success) {
299 // !!! TO DO: Get explode_metadata_database.pl strings translated and use SimpleResultDialog below
300 JOptionPane.showMessageDialog(null, Dictionary.get("ExplodeMetadataPrompt.Successful_Explode", metadata_file.getName()), Dictionary.get("ExplodeMetadataPrompt.Successful_Title"), JOptionPane.INFORMATION_MESSAGE);
301 }
302 else {
303 String title = Dictionary.get("ExplodeMetadataPrompt.Failed_Title");
304 String label = Dictionary.get("ExplodeMetadataPrompt.Failed_Explode", metadata_file.getName());
305 SimpleResultDialog result_dialog = new SimpleResultDialog(title, label, message);
306 result_dialog.setVisible(true); // Blocks
307 result_dialog.dispose();
308 result_dialog = null;
309 }
310 }
311
312 private class CancelListener
313 implements ActionListener {
314 public void actionPerformed(ActionEvent event) {
315 self.dispose();
316 }
317 }
318
319 private class ExplodeListener
320 implements ActionListener {
321
322 public void actionPerformed(ActionEvent event) {
323 Gatherer.g_man.wait(true);
324 // update the options
325 updateScriptOptions();
326 self.dispose();
327 (new ExplodeMetadataDatabaseTask()).start();
328 }
329 }
330
331
332 private class ExplodeMetadataDatabaseTask
333 extends Thread
334 {
335 public ExplodeMetadataDatabaseTask()
336 {
337 }
338
339 public void run()
340 {
341 int exit_value = explodeMetadata();
342 if (exit_value == 0) { // success
343 // Clear out the old directory containing the metadata file and download the new directory
344 if (Gatherer.isGsdlRemote) {
345 Utility.delete(metadata_file.getParentFile());
346 RemoteGreenstoneServer.downloadCollectionFile(CollectionManager.getLoadedCollectionName(), metadata_file.getParentFile());
347 }
348
349 // Load the new metadata.xml files
350 MetadataXMLFileManager.loadMetadataXMLFiles(metadata_file.getParentFile());
351 Gatherer.g_man.refreshCollectionTree(DragTree.COLLECTION_CONTENTS_CHANGED);
352 resultPrompt(true, error_message.toString());
353 } else { // failure
354 resultPrompt(false, error_message.toString());
355 }
356 error_message = null;
357 Gatherer.g_man.wait(false);
358 }
359 }
360}
Note: See TracBrowser for help on using the repository browser.