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

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

Changed text handling to use Dictionary.get rather than Dictionary.setText or Dictionary.registerBoth etc. also removed mnemonics cos they suck for other languages

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