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.*;
|
---|
34 | import java.io.File;
|
---|
35 | import java.io.StringBufferInputStream;
|
---|
36 | import java.util.ArrayList;
|
---|
37 |
|
---|
38 | import org.w3c.dom.Document;
|
---|
39 |
|
---|
40 | import org.greenstone.gatherer.Dictionary;
|
---|
41 | import org.greenstone.gatherer.Configuration;
|
---|
42 | import org.greenstone.gatherer.Gatherer;
|
---|
43 | import org.greenstone.gatherer.cdm.Argument;
|
---|
44 | import org.greenstone.gatherer.cdm.CollectionDesignManager;
|
---|
45 | import org.greenstone.gatherer.cdm.Plugin;
|
---|
46 | import org.greenstone.gatherer.collection.ScriptOptions;
|
---|
47 | import org.greenstone.gatherer.gui.tree.DragTree;
|
---|
48 | import org.greenstone.gatherer.metadata.MetadataXMLFileManager;
|
---|
49 | import org.greenstone.gatherer.metadata.MetadataElement;
|
---|
50 | import org.greenstone.gatherer.metadata.MetadataSet;
|
---|
51 | import org.greenstone.gatherer.metadata.MetadataSetManager;
|
---|
52 | import org.greenstone.gatherer.metadata.MetadataTools;
|
---|
53 | import org.greenstone.gatherer.shell.GShell;
|
---|
54 | import org.greenstone.gatherer.shell.GShellEvent;
|
---|
55 | import org.greenstone.gatherer.shell.GShellListener;
|
---|
56 | import org.greenstone.gatherer.util.ArrayTools;
|
---|
57 | import org.greenstone.gatherer.util.Utility;
|
---|
58 | import org.greenstone.gatherer.util.XMLTools;
|
---|
59 |
|
---|
60 | public 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(600, 350);
|
---|
66 | private JDialog self;
|
---|
67 |
|
---|
68 | /** the file we wil be exploding */
|
---|
69 | private File metadata_file = null;
|
---|
70 | /** the name of the plugin to be used */
|
---|
71 | private String plugin_name = 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 | Plugin plugin = CollectionDesignManager.plugin_manager.getExploderPlugin(source_file);
|
---|
89 | if (plugin==null) {
|
---|
90 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("ExplodeMetadataPrompt.NotExplodable"), Dictionary.get("ExplodeMetadataPrompt.Title"), JOptionPane.ERROR_MESSAGE);
|
---|
91 | return;
|
---|
92 | }
|
---|
93 | plugin_name = plugin.getName();
|
---|
94 | setJMenuBar(new SimpleMenuBar("explodingmetadata"));
|
---|
95 | setSize(SIZE);
|
---|
96 | Dictionary.setText(this, "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 StringBufferInputStream(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();
|
---|
113 | instructions_area.setEditable(false);
|
---|
114 | instructions_area.setLineWrap(true);
|
---|
115 | instructions_area.setRows(5);
|
---|
116 | instructions_area.setWrapStyleWord(true);
|
---|
117 | Dictionary.setText(instructions_area, "ExplodeMetadataPrompt.Instructions");
|
---|
118 |
|
---|
119 | JPanel button_pane = new JPanel();
|
---|
120 | JButton explode_button = new GLIButton();
|
---|
121 | Dictionary.setBoth(explode_button, "ExplodeMetadataPrompt.Explode", "ExplodeMetadataPrompt.Explode_Tooltip");
|
---|
122 | JButton cancel_button = new GLIButton();
|
---|
123 | cancel_button.setMnemonic(KeyEvent.VK_C);
|
---|
124 | Dictionary.setBoth(cancel_button, "General.Cancel", "General.Cancel_Tooltip");
|
---|
125 |
|
---|
126 | // Connection
|
---|
127 | cancel_button.addActionListener(new CancelListener());
|
---|
128 | explode_button.addActionListener(new ExplodeListener());
|
---|
129 |
|
---|
130 | // Layout
|
---|
131 |
|
---|
132 | button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
|
---|
133 | button_pane.setLayout(new GridLayout(1,2));
|
---|
134 | button_pane.add(explode_button);
|
---|
135 | button_pane.add(cancel_button);
|
---|
136 |
|
---|
137 | content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
138 | content_pane.setLayout(new BorderLayout());
|
---|
139 | content_pane.add(instructions_area, BorderLayout.NORTH);
|
---|
140 | content_pane.add(middle_pane, BorderLayout.CENTER);
|
---|
141 | content_pane.add(button_pane, BorderLayout.SOUTH);
|
---|
142 |
|
---|
143 | // Final dialog setup & positioning.
|
---|
144 | Dimension screen_size = Configuration.screen_size;
|
---|
145 | setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
|
---|
146 | setVisible(true);
|
---|
147 | }
|
---|
148 |
|
---|
149 | public void destroy()
|
---|
150 | {
|
---|
151 |
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** All implementation of GShellListener must include this method so the listener can be informed of messages from the GShell.
|
---|
155 | * @param event A <strong>GShellEvent</strong> that contains, amoung other things, the message.
|
---|
156 | */
|
---|
157 | public synchronized void message(GShellEvent event) {
|
---|
158 | String message = event.getMessage();
|
---|
159 | if (message.startsWith("other>")) {
|
---|
160 | message = message.substring(6);
|
---|
161 | error_message.append(message);
|
---|
162 | error_message.append("\n");
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | /** 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.
|
---|
167 | * @param event A <strong>GShellEvent</strong> that contains details of the initial state of the <strong>GShell</strong> before task comencement.
|
---|
168 | */
|
---|
169 | public synchronized void processBegun(GShellEvent event) {
|
---|
170 | // We don't care.
|
---|
171 | }
|
---|
172 |
|
---|
173 | /** All implementation of GShellListener must include this method so the listener can be informed when a GShell completes its task.
|
---|
174 | * @param event A <strong>GShellEvent</strong> that contains details of the final state of the <strong>GShell</strong> after task completion.
|
---|
175 | */
|
---|
176 | public synchronized void processComplete(GShellEvent event) {
|
---|
177 | successful = false;
|
---|
178 | if(event.getStatus() == GShell.OK) {
|
---|
179 | successful = true;
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | private void addScriptOptions(JPanel options_pane) {
|
---|
184 |
|
---|
185 | options_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
186 | options_pane.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
|
---|
187 | options_pane.setLayout(new BoxLayout(options_pane, BoxLayout.Y_AXIS));
|
---|
188 |
|
---|
189 | int current_mode = Configuration.getMode();
|
---|
190 | int total_argument_count = options.getArgumentCount();
|
---|
191 |
|
---|
192 | for(int i = 0; i < total_argument_count; i++) {
|
---|
193 | // Retrieve the argument so we know how to format the control.
|
---|
194 | Argument argument = options.getArgument(i);
|
---|
195 | if(!argument.isHiddenGLI() && argument.getModeLevel() <= current_mode) {
|
---|
196 | // by default, all args are disabled, and no value
|
---|
197 | ArgumentControl argument_control = new ArgumentControl(argument,false, null);
|
---|
198 | // make sure they are coloured the way we want - this is not the standard arg control coloring
|
---|
199 | argument_control.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
|
---|
200 | options_pane.add((JComponent)argument_control);
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | private void updateScriptOptions() {
|
---|
206 | for(int i = 0; i < options_pane.getComponentCount(); i++) {
|
---|
207 | Component component = options_pane.getComponent(i);
|
---|
208 | if(component instanceof ArgumentControl) {
|
---|
209 | ArgumentControl ac = (ArgumentControl)component;
|
---|
210 | String name = ac.getName();
|
---|
211 | String value = ac.getValue();
|
---|
212 | boolean enabled = ac.isEnabled();
|
---|
213 | if (!enabled && value==null) {
|
---|
214 | // flag type, remove from options altogether
|
---|
215 | options.removeValue(name);
|
---|
216 | } else {
|
---|
217 | if (value.equals("")) {
|
---|
218 | // if the value is empty, we don't want it to be passed to the script
|
---|
219 | options.setValue(name, false, value);
|
---|
220 | } else {
|
---|
221 | options.setValue(name, enabled, value);
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | private int explodeMetadata() {
|
---|
230 |
|
---|
231 | String [] args;
|
---|
232 | if (Gatherer.isGsdlRemote) {
|
---|
233 | // what here??
|
---|
234 | return -1;
|
---|
235 | }
|
---|
236 | if(Utility.isWindows()){
|
---|
237 | args = new String[5];
|
---|
238 | args[0] = Configuration.perl_path;
|
---|
239 | args[1] = "-S";
|
---|
240 | args[2] = Configuration.getScriptPath() + "explode_metadata_database.pl";
|
---|
241 | args[3] = "-plugin";
|
---|
242 | args[4] = plugin_name;
|
---|
243 | } else {
|
---|
244 | args = new String[3];
|
---|
245 | args[0] = Configuration.getScriptPath() + "explode_metadata_database.pl";
|
---|
246 | args[1] = "-plugin";
|
---|
247 | args[2] = plugin_name;
|
---|
248 |
|
---|
249 | }
|
---|
250 | // add in all the options from the user
|
---|
251 | args = ArrayTools.add(args, options.getValues());
|
---|
252 | // add in the filename
|
---|
253 | args = ArrayTools.add(args, metadata_file.getPath());
|
---|
254 |
|
---|
255 | this.error_message = new StringBuffer();
|
---|
256 | GShell process = new GShell(args, GShell.OTHER, 3, this, null, GShell.GSHELL_OTHER);
|
---|
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 | String title;
|
---|
269 | String label;
|
---|
270 | if (success) {
|
---|
271 | title = Dictionary.get("ExplodeMetadataPrompt.Successful_Title");
|
---|
272 | label = Dictionary.get("ExplodeMetadataPrompt.Successful_Explode", metadata_file.getName());
|
---|
273 | } else {
|
---|
274 | title = Dictionary.get("ExplodeMetadataPrompt.Failed_Title");
|
---|
275 | label = Dictionary.get("ExplodeMetadataPrompt.Failed_Explode", metadata_file.getName());
|
---|
276 | }
|
---|
277 | SimpleResultDialog result_dialog = new SimpleResultDialog(title, label, message);
|
---|
278 | result_dialog.setVisible(true); // Blocks
|
---|
279 | result_dialog.dispose();
|
---|
280 | result_dialog = null;
|
---|
281 |
|
---|
282 | }
|
---|
283 | private class CancelListener
|
---|
284 | implements ActionListener {
|
---|
285 | public void actionPerformed(ActionEvent event) {
|
---|
286 | self.dispose();
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | private class ExplodeListener
|
---|
291 | implements ActionListener {
|
---|
292 |
|
---|
293 | public void actionPerformed(ActionEvent event) {
|
---|
294 | // update the options
|
---|
295 | updateScriptOptions();
|
---|
296 | int exit_value = explodeMetadata();
|
---|
297 | if (exit_value == 0) { // success
|
---|
298 | String new_dir = metadata_file.getPath();
|
---|
299 | // remove the extension to get the directory name
|
---|
300 | new_dir = new_dir.substring(0, new_dir.lastIndexOf('.'));
|
---|
301 | MetadataXMLFileManager.loadMetadataXMLFiles(new File(new_dir));
|
---|
302 | Gatherer.g_man.refreshCollectionTree(DragTree.COLLECTION_CONTENTS_CHANGED);
|
---|
303 | resultPrompt(true, error_message.toString());
|
---|
304 | } else { // failure
|
---|
305 | resultPrompt(false, error_message.toString());
|
---|
306 | }
|
---|
307 | error_message = null;
|
---|
308 | self.dispose();
|
---|
309 | }
|
---|
310 | }
|
---|
311 | }
|
---|