source: trunk/gli/src/org/greenstone/gatherer/gui/ExplodeMetadataPrompt.java@ 12305

Last change on this file since 12305 was 12305, checked in by kjdon, 18 years ago

removed gui.ArgumentControl class and ArgumentConfiguration inner class Argument control, and added a new class cdm/ArgumentControl.

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