[9104] | 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 | */
|
---|
| 27 | package org.greenstone.gatherer.gui;
|
---|
| 28 |
|
---|
| 29 | import java.awt.*;
|
---|
| 30 | import java.awt.event.*;
|
---|
| 31 | import javax.swing.*;
|
---|
| 32 | import javax.swing.event.*;
|
---|
| 33 | import javax.swing.text.*;
|
---|
[9160] | 34 | import java.io.*;
|
---|
[9104] | 35 | import java.util.ArrayList;
|
---|
| 36 |
|
---|
[9153] | 37 | import org.w3c.dom.Document;
|
---|
| 38 |
|
---|
[9104] | 39 | import org.greenstone.gatherer.Dictionary;
|
---|
| 40 | import org.greenstone.gatherer.Configuration;
|
---|
| 41 | import org.greenstone.gatherer.Gatherer;
|
---|
[10370] | 42 | import org.greenstone.gatherer.LocalGreenstone;
|
---|
[9153] | 43 | import org.greenstone.gatherer.cdm.Argument;
|
---|
[12305] | 44 | import org.greenstone.gatherer.cdm.ArgumentControl;
|
---|
[9104] | 45 | import org.greenstone.gatherer.cdm.CollectionDesignManager;
|
---|
[9127] | 46 | import org.greenstone.gatherer.cdm.Plugin;
|
---|
[9153] | 47 | import org.greenstone.gatherer.collection.ScriptOptions;
|
---|
[9104] | 48 | import org.greenstone.gatherer.gui.tree.DragTree;
|
---|
| 49 | import org.greenstone.gatherer.metadata.MetadataXMLFileManager;
|
---|
| 50 | import org.greenstone.gatherer.metadata.MetadataElement;
|
---|
| 51 | import org.greenstone.gatherer.metadata.MetadataSet;
|
---|
| 52 | import org.greenstone.gatherer.metadata.MetadataSetManager;
|
---|
| 53 | import org.greenstone.gatherer.metadata.MetadataTools;
|
---|
[9153] | 54 | import org.greenstone.gatherer.shell.GShell;
|
---|
| 55 | import org.greenstone.gatherer.shell.GShellEvent;
|
---|
| 56 | import org.greenstone.gatherer.shell.GShellListener;
|
---|
[9104] | 57 | import org.greenstone.gatherer.util.Utility;
|
---|
[9153] | 58 | import org.greenstone.gatherer.util.XMLTools;
|
---|
[9104] | 59 |
|
---|
[9153] | 60 | public class ExplodeMetadataPrompt
|
---|
| 61 | extends ModalDialog
|
---|
| 62 | implements GShellListener
|
---|
[9104] | 63 | {
|
---|
| 64 | /** The size of this new collection dialog box. */
|
---|
[11432] | 65 | static private Dimension SIZE = new Dimension(675, 350);
|
---|
[9104] | 66 | private JDialog self;
|
---|
| 67 |
|
---|
[9153] | 68 | /** the file we wil be exploding */
|
---|
[9104] | 69 | private File metadata_file = null;
|
---|
[12292] | 70 | /** the list of potential plugins to be used */
|
---|
| 71 | private Argument plugin_arg = null;
|
---|
[9153] | 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;
|
---|
[9104] | 80 |
|
---|
[9153] | 81 |
|
---|
[9104] | 82 | public ExplodeMetadataPrompt(File source_file) {
|
---|
| 83 | super(Gatherer.g_man, true);
|
---|
| 84 | this.self = this;
|
---|
| 85 | this.metadata_file = source_file;
|
---|
[9153] | 86 |
|
---|
[9104] | 87 | // check that we actually have an explodable file
|
---|
[12292] | 88 | ArrayList exp_plugins = CollectionDesignManager.plugin_manager.getExploderPlugins(source_file);
|
---|
| 89 | if (exp_plugins.size() == 0) {
|
---|
[9104] | 90 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("ExplodeMetadataPrompt.NotExplodable"), Dictionary.get("ExplodeMetadataPrompt.Title"), JOptionPane.ERROR_MESSAGE);
|
---|
| 91 | return;
|
---|
| 92 | }
|
---|
[12292] | 93 | plugin_arg = createPluginArgument(exp_plugins);
|
---|
[9104] | 94 | setJMenuBar(new SimpleMenuBar("explodingmetadata"));
|
---|
| 95 | setSize(SIZE);
|
---|
[12119] | 96 | setTitle(Dictionary.get("ExplodeMetadataPrompt.Title"));
|
---|
| 97 |
|
---|
[9153] | 98 | // set up the script options
|
---|
| 99 | // we have empty initial values
|
---|
| 100 | String dom_string = "<Options/>";
|
---|
[9160] | 101 | Document doc = XMLTools.parseXML(new StringReader(dom_string));
|
---|
[9153] | 102 | options = new ScriptOptions(doc.getDocumentElement(), "explode_metadata_database.pl", false);
|
---|
| 103 |
|
---|
[9104] | 104 | // Creation
|
---|
| 105 | JPanel content_pane = (JPanel) getContentPane();
|
---|
| 106 | content_pane.setOpaque(true);
|
---|
| 107 |
|
---|
[9153] | 108 | options_pane = new JPanel();
|
---|
| 109 | addScriptOptions(options_pane);
|
---|
| 110 | JScrollPane middle_pane = new JScrollPane(options_pane);
|
---|
[9104] | 111 |
|
---|
[12119] | 112 | JTextArea instructions_area = new JTextArea(Dictionary.get("ExplodeMetadataPrompt.Instructions"));
|
---|
[9104] | 113 | instructions_area.setEditable(false);
|
---|
| 114 | instructions_area.setLineWrap(true);
|
---|
| 115 | instructions_area.setRows(5);
|
---|
| 116 | instructions_area.setWrapStyleWord(true);
|
---|
[12119] | 117 |
|
---|
[9104] | 118 | JPanel button_pane = new JPanel();
|
---|
[12119] | 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"));
|
---|
[9104] | 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 | {
|
---|
[9153] | 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();
|
---|
[9677] | 155 | if (message.startsWith("explode_metadata_database.pl>")) {
|
---|
[11443] | 156 | message = message.substring(29);
|
---|
[9153] | 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 |
|
---|
[12292] | 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 | }
|
---|
[9153] | 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 |
|
---|
[12292] | 200 | // first we add the plugin arg
|
---|
[12305] | 201 | ArgumentControl argument_control = new ArgumentControl(plugin_arg, true, null);
|
---|
[12292] | 202 | argument_control.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
|
---|
| 203 | options_pane.add((JComponent)argument_control);
|
---|
[9153] | 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) {
|
---|
[11344] | 208 | if (argument.getName().equals("metadata_set")) {
|
---|
| 209 | argument.setType(Argument.METADATA_SET_NAMESPACE);
|
---|
[13158] | 210 | argument_control = new ArgumentControl(argument, true, "Exploded Metadata Set (exp)");
|
---|
[11344] | 211 | }
|
---|
[13158] | 212 | else {
|
---|
| 213 | // by default, all args are disabled, and no value
|
---|
| 214 | argument_control = new ArgumentControl(argument, false, null);
|
---|
| 215 | }
|
---|
[9153] | 216 | // make sure they are coloured the way we want - this is not the standard arg control coloring
|
---|
| 217 | argument_control.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
|
---|
| 218 | options_pane.add((JComponent)argument_control);
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | private void updateScriptOptions() {
|
---|
| 224 | for(int i = 0; i < options_pane.getComponentCount(); i++) {
|
---|
| 225 | Component component = options_pane.getComponent(i);
|
---|
| 226 | if(component instanceof ArgumentControl) {
|
---|
| 227 | ArgumentControl ac = (ArgumentControl)component;
|
---|
[11060] | 228 | String name = ac.getArgumentName();
|
---|
[9153] | 229 | String value = ac.getValue();
|
---|
| 230 | boolean enabled = ac.isEnabled();
|
---|
| 231 | if (!enabled && value==null) {
|
---|
| 232 | // flag type, remove from options altogether
|
---|
| 233 | options.removeValue(name);
|
---|
| 234 | } else {
|
---|
| 235 | if (value.equals("")) {
|
---|
| 236 | // if the value is empty, we don't want it to be passed to the script
|
---|
| 237 | options.setValue(name, false, value);
|
---|
| 238 | } else {
|
---|
| 239 | options.setValue(name, enabled, value);
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[9755] | 247 | private int explodeMetadata()
|
---|
| 248 | {
|
---|
[9153] | 249 | if (Gatherer.isGsdlRemote) {
|
---|
[10239] | 250 | // !! TO DO
|
---|
[9153] | 251 | return -1;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[9755] | 254 | // Generate the explode_metadata_database.pl command
|
---|
| 255 | ArrayList command_parts_list = new ArrayList();
|
---|
| 256 | if (Utility.isWindows() && (!Gatherer.isGsdlRemote)) {
|
---|
| 257 | command_parts_list.add(Configuration.perl_path);
|
---|
| 258 | command_parts_list.add("-S");
|
---|
[9153] | 259 | }
|
---|
[10370] | 260 | command_parts_list.add(LocalGreenstone.getBinScriptDirectoryPath() + "explode_metadata_database.pl");
|
---|
[12292] | 261 |
|
---|
[9755] | 262 | // Add in all the options from the user
|
---|
| 263 | String[] explode_options = options.getValues();
|
---|
| 264 | for (int i = 0; i < explode_options.length; i++) {
|
---|
| 265 | command_parts_list.add(explode_options[i]);
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | // Add in the filename
|
---|
| 269 | command_parts_list.add(metadata_file.getPath());
|
---|
| 270 | // Run the explode_metadata_database.pl command
|
---|
| 271 | String[] command_parts = (String[]) command_parts_list.toArray(new String[0]);
|
---|
[9153] | 272 | this.error_message = new StringBuffer();
|
---|
[9755] | 273 | GShell process = new GShell(command_parts, GShell.EXPLODE, 3, this, null, GShell.GSHELL_EXPLODE);
|
---|
[9153] | 274 | //process.start();
|
---|
| 275 | process.run();
|
---|
[9104] | 276 |
|
---|
[9153] | 277 | if (successful) {
|
---|
| 278 | return 0;
|
---|
| 279 | }
|
---|
| 280 | return -1;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[9677] | 283 | private void resultPrompt(boolean success, String message)
|
---|
| 284 | {
|
---|
[9153] | 285 | if (success) {
|
---|
[9677] | 286 | // !!! TO DO: Get explode_metadata_database.pl strings translated and use SimpleResultDialog below
|
---|
| 287 | JOptionPane.showMessageDialog(null, Dictionary.get("ExplodeMetadataPrompt.Successful_Explode", metadata_file.getName()), Dictionary.get("ExplodeMetadataPrompt.Successful_Title"), JOptionPane.INFORMATION_MESSAGE);
|
---|
[9153] | 288 | }
|
---|
[9677] | 289 | else {
|
---|
| 290 | String title = Dictionary.get("ExplodeMetadataPrompt.Failed_Title");
|
---|
| 291 | String label = Dictionary.get("ExplodeMetadataPrompt.Failed_Explode", metadata_file.getName());
|
---|
| 292 | SimpleResultDialog result_dialog = new SimpleResultDialog(title, label, message);
|
---|
| 293 | result_dialog.setVisible(true); // Blocks
|
---|
| 294 | result_dialog.dispose();
|
---|
| 295 | result_dialog = null;
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
[9153] | 298 |
|
---|
[9104] | 299 | private class CancelListener
|
---|
| 300 | implements ActionListener {
|
---|
| 301 | public void actionPerformed(ActionEvent event) {
|
---|
| 302 | self.dispose();
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | private class ExplodeListener
|
---|
| 307 | implements ActionListener {
|
---|
| 308 |
|
---|
| 309 | public void actionPerformed(ActionEvent event) {
|
---|
[9153] | 310 | // update the options
|
---|
| 311 | updateScriptOptions();
|
---|
| 312 | int exit_value = explodeMetadata();
|
---|
[9104] | 313 | if (exit_value == 0) { // success
|
---|
[12735] | 314 | // Load the new metadata.xml files
|
---|
| 315 | MetadataXMLFileManager.loadMetadataXMLFiles(metadata_file.getParentFile());
|
---|
[9104] | 316 | Gatherer.g_man.refreshCollectionTree(DragTree.COLLECTION_CONTENTS_CHANGED);
|
---|
[9153] | 317 | resultPrompt(true, error_message.toString());
|
---|
| 318 | } else { // failure
|
---|
| 319 | resultPrompt(false, error_message.toString());
|
---|
[9104] | 320 | }
|
---|
[9153] | 321 | error_message = null;
|
---|
| 322 | self.dispose();
|
---|
[9104] | 323 | }
|
---|
| 324 | }
|
---|
| 325 | }
|
---|