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.util.ArrayList;
|
---|
36 |
|
---|
37 | import org.greenstone.gatherer.Dictionary;
|
---|
38 | import org.greenstone.gatherer.Configuration;
|
---|
39 | import org.greenstone.gatherer.Gatherer;
|
---|
40 | import org.greenstone.gatherer.cdm.CollectionDesignManager;
|
---|
41 | import org.greenstone.gatherer.cdm.Plugin;
|
---|
42 | import org.greenstone.gatherer.gui.tree.DragTree;
|
---|
43 | import org.greenstone.gatherer.metadata.MetadataXMLFileManager;
|
---|
44 | import org.greenstone.gatherer.metadata.MetadataElement;
|
---|
45 | import org.greenstone.gatherer.metadata.MetadataSet;
|
---|
46 | import org.greenstone.gatherer.metadata.MetadataSetManager;
|
---|
47 | import org.greenstone.gatherer.metadata.MetadataTools;
|
---|
48 | import org.greenstone.gatherer.util.Utility;
|
---|
49 |
|
---|
50 | public class ExplodeMetadataPrompt
|
---|
51 |
|
---|
52 | extends ModalDialog
|
---|
53 | {
|
---|
54 | /** The size of this new collection dialog box. */
|
---|
55 | static private Dimension SIZE = new Dimension(600, 280);
|
---|
56 | static private Dimension COMBO_SIZE = new Dimension(100, 30);
|
---|
57 | private JDialog self;
|
---|
58 |
|
---|
59 | private File metadata_file = null;
|
---|
60 | private String plugin_name = null;
|
---|
61 | private String encoding = null;
|
---|
62 |
|
---|
63 | private GComboBox encoding_combo = null;
|
---|
64 | //private GComboBox metadata_set_combo = null;
|
---|
65 | private JTextField filename_field_box = null;
|
---|
66 |
|
---|
67 | // private JButton explode_button = null;
|
---|
68 | // private JButton cancel_button = null;
|
---|
69 | public ExplodeMetadataPrompt(File source_file) {
|
---|
70 | super(Gatherer.g_man, true);
|
---|
71 | this.self = this;
|
---|
72 | this.metadata_file = source_file;
|
---|
73 |
|
---|
74 | // check that we actually have an explodable file
|
---|
75 | Plugin plugin = CollectionDesignManager.plugin_manager.getExploderPlugin(source_file);
|
---|
76 | if (plugin==null) {
|
---|
77 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("ExplodeMetadataPrompt.NotExplodable"), Dictionary.get("ExplodeMetadataPrompt.Title"), JOptionPane.ERROR_MESSAGE);
|
---|
78 | return;
|
---|
79 | }
|
---|
80 | plugin_name = plugin.getName();
|
---|
81 | setJMenuBar(new SimpleMenuBar("explodingmetadata"));
|
---|
82 | setSize(SIZE);
|
---|
83 | Dictionary.setText(this, "ExplodeMetadataPrompt.Title");
|
---|
84 |
|
---|
85 | // Creation
|
---|
86 | JPanel content_pane = (JPanel) getContentPane();
|
---|
87 | content_pane.setOpaque(true);
|
---|
88 |
|
---|
89 | JPanel middle_pane = new JPanel();
|
---|
90 | middle_pane.setLayout(new BorderLayout());
|
---|
91 |
|
---|
92 | JPanel options_pane = new JPanel();
|
---|
93 | options_pane.setLayout(new BorderLayout());
|
---|
94 | options_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
95 |
|
---|
96 | JPanel labels_pane = new JPanel();
|
---|
97 | labels_pane.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
|
---|
98 | labels_pane.setLayout(new GridLayout(2,1, 5, 5));
|
---|
99 |
|
---|
100 | JPanel values_pane = new JPanel();
|
---|
101 | values_pane.setLayout(new GridLayout(2,1, 5, 5));
|
---|
102 |
|
---|
103 | JLabel encoding_label = new JLabel();
|
---|
104 | Dictionary.setText(encoding_label, "ExplodeMetadataPrompt.Encoding");
|
---|
105 |
|
---|
106 | ArrayList encodings = new ArrayList();
|
---|
107 | encodings.add("iso_8859_1");
|
---|
108 | encodings.add("utf-8");
|
---|
109 |
|
---|
110 | encoding_combo = new GComboBox(encodings);
|
---|
111 | Dictionary.setTooltip(encoding_combo, "ExplodeMetadataPrompt.Encoding_Tooltip");
|
---|
112 | encoding_combo.setPreferredSize(COMBO_SIZE);
|
---|
113 |
|
---|
114 | //JLabel metadata_set_label = new JLabel();
|
---|
115 | ///Dictionary.setText(metadata_set_label, "ExplodeMetadataPrompt.MetadataSet");
|
---|
116 | //ArrayList metadata_sets = MetadataSetManager.getMetadataSets();
|
---|
117 | // remove the extracted metadata set
|
---|
118 | // for (int i = metadata_sets.size() - 1; i >= 0; i--) {
|
---|
119 | // if (((MetadataSet) metadata_sets.get(i)).getNamespace().equals(MetadataSetManager.EXTRACTED_METADATA_NAMESPACE)) {
|
---|
120 | // metadata_sets.remove(i);
|
---|
121 | // }
|
---|
122 | // }
|
---|
123 | // // add in none
|
---|
124 | // metadata_sets.add(0, Dictionary.get("ExplodeMetadataPrompt.MetadataSet.None"));
|
---|
125 |
|
---|
126 | // metadata_set_combo = new GComboBox(metadata_sets);
|
---|
127 |
|
---|
128 | //Dictionary.setTooltip(metadata_set_combo, "ExplodeMetadataPrompt.MetadataSet_Tooltip");
|
---|
129 |
|
---|
130 | JLabel filename_field_label = new JLabel();
|
---|
131 | Dictionary.setText(filename_field_label, "ExplodeMetadataPrompt.FilenameField");
|
---|
132 | filename_field_box = new JTextField();
|
---|
133 | Dictionary.setTooltip(filename_field_box, "ExplodeMetadataPrompt.FilenameField_Tooltip");
|
---|
134 | JTextArea instructions_area = new JTextArea();
|
---|
135 | instructions_area.setEditable(false);
|
---|
136 | instructions_area.setLineWrap(true);
|
---|
137 | instructions_area.setRows(5);
|
---|
138 | instructions_area.setWrapStyleWord(true);
|
---|
139 | Dictionary.setText(instructions_area, "ExplodeMetadataPrompt.Instructions");
|
---|
140 |
|
---|
141 | JPanel button_pane = new JPanel();
|
---|
142 | JButton explode_button = new GLIButton();
|
---|
143 | Dictionary.setBoth(explode_button, "ExplodeMetadataPrompt.Explode", "ExplodeMetadataPrompt.Explode_Tooltip");
|
---|
144 | JButton cancel_button = new GLIButton();
|
---|
145 | cancel_button.setMnemonic(KeyEvent.VK_C);
|
---|
146 | Dictionary.setBoth(cancel_button, "General.Cancel", "General.Cancel_Tooltip");
|
---|
147 |
|
---|
148 | // Connection
|
---|
149 | cancel_button.addActionListener(new CancelListener());
|
---|
150 | explode_button.addActionListener(new ExplodeListener());
|
---|
151 |
|
---|
152 | // Layout
|
---|
153 |
|
---|
154 | labels_pane.add(encoding_label);
|
---|
155 | //labels_pane.add(metadata_set_label);
|
---|
156 | labels_pane.add(filename_field_label);
|
---|
157 |
|
---|
158 | values_pane.add(encoding_combo);
|
---|
159 | //values_pane.add(metadata_set_combo);
|
---|
160 | values_pane.add(filename_field_box);
|
---|
161 |
|
---|
162 | options_pane.add(labels_pane, BorderLayout.WEST);
|
---|
163 | options_pane.add(values_pane, BorderLayout.CENTER);
|
---|
164 |
|
---|
165 | middle_pane.add(options_pane, BorderLayout.NORTH);
|
---|
166 |
|
---|
167 | button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
|
---|
168 | button_pane.setLayout(new GridLayout(1,2));
|
---|
169 | button_pane.add(explode_button);
|
---|
170 | button_pane.add(cancel_button);
|
---|
171 |
|
---|
172 | content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
173 | content_pane.setLayout(new BorderLayout());
|
---|
174 | content_pane.add(instructions_area, BorderLayout.NORTH);
|
---|
175 | content_pane.add(middle_pane, BorderLayout.CENTER);
|
---|
176 | content_pane.add(button_pane, BorderLayout.SOUTH);
|
---|
177 |
|
---|
178 | // Final dialog setup & positioning.
|
---|
179 | Dimension screen_size = Configuration.screen_size;
|
---|
180 | setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
|
---|
181 | setVisible(true);
|
---|
182 | }
|
---|
183 |
|
---|
184 | public void destroy()
|
---|
185 | {
|
---|
186 |
|
---|
187 |
|
---|
188 |
|
---|
189 | }
|
---|
190 |
|
---|
191 | private class CancelListener
|
---|
192 | implements ActionListener {
|
---|
193 | public void actionPerformed(ActionEvent event) {
|
---|
194 | //cancelled = true;
|
---|
195 | self.dispose();
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | private class ExplodeListener
|
---|
200 | implements ActionListener {
|
---|
201 |
|
---|
202 | public void actionPerformed(ActionEvent event) {
|
---|
203 | String encoding = (String)encoding_combo.getSelectedItem();
|
---|
204 | //Object selected_item = metadata_set_combo.getSelectedItem();
|
---|
205 | MetadataSet meta_set = null;
|
---|
206 | // if (selected_item instanceof MetadataSet) {
|
---|
207 | // meta_set = (MetadataSet)selected_item;
|
---|
208 | // }
|
---|
209 |
|
---|
210 | System.err.println("exploding the metadata, file "+metadata_file.getPath()+", plugin "+plugin_name+", encoding = "+encoding);
|
---|
211 | if (meta_set != null) {
|
---|
212 | System.err.println(", metadata set="+meta_set.toString());
|
---|
213 | }
|
---|
214 | String filename_field = filename_field_box.getText();
|
---|
215 | System.err.println("filename = "+filename_field);
|
---|
216 | // do the business
|
---|
217 | int exit_value = explodeMetadata(metadata_file, plugin_name, encoding, meta_set, filename_field);
|
---|
218 | //cancelled = false;
|
---|
219 | if (exit_value == 0) { // success
|
---|
220 | String new_dir = metadata_file.getPath();
|
---|
221 | // remove the extension to get the directory name
|
---|
222 | new_dir = new_dir.substring(0, new_dir.lastIndexOf('.'));
|
---|
223 | MetadataXMLFileManager.loadMetadataXMLFiles(new File(new_dir));
|
---|
224 | Gatherer.g_man.refreshCollectionTree(DragTree.COLLECTION_CONTENTS_CHANGED);
|
---|
225 |
|
---|
226 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("ExplodeMetadataPrompt.Success"), Dictionary.get("ExplodeMetadataPrompt.Title"), JOptionPane.INFORMATION_MESSAGE);
|
---|
227 | } else {
|
---|
228 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("ExplodeMetadataPrompt.Error"), Dictionary.get("ExplodeMetadataPrompt.Title"), JOptionPane.ERROR_MESSAGE);
|
---|
229 | }
|
---|
230 | self.dispose();
|
---|
231 |
|
---|
232 | }
|
---|
233 |
|
---|
234 | public int explodeMetadata(File file, String plugin_name,
|
---|
235 | String encoding, MetadataSet meta_set,
|
---|
236 | String filename_field) {
|
---|
237 |
|
---|
238 | String meta_set_namespace = "";
|
---|
239 | if (meta_set != null) {
|
---|
240 | meta_set_namespace = meta_set.getNamespace();
|
---|
241 | }
|
---|
242 | try {
|
---|
243 | if (Gatherer.isGsdlRemote) {
|
---|
244 | // what do we do here??
|
---|
245 | return -1;
|
---|
246 | }
|
---|
247 | else {
|
---|
248 | String command[] = null;
|
---|
249 |
|
---|
250 |
|
---|
251 | if (Utility.isWindows()) {
|
---|
252 | command = new String[11];
|
---|
253 | if(Configuration.perl_path != null) {
|
---|
254 | command[0] = Configuration.perl_path;
|
---|
255 | }
|
---|
256 | else {
|
---|
257 | command[0] = "Perl.exe";
|
---|
258 | }
|
---|
259 | command[1] = Configuration.gsdl_path + "bin" + File.separator + "script" + File.separator + "explode_metadata_database.pl";
|
---|
260 | command[2] = "-plugin";
|
---|
261 | command[3] = plugin_name;
|
---|
262 | command[4] = "-encoding";
|
---|
263 | command[5] = encoding;
|
---|
264 | command[6] = "-metadata_set";
|
---|
265 | command[7] = meta_set_namespace;
|
---|
266 | command[8] = "-filename_field";
|
---|
267 | command[9] = filename_field;
|
---|
268 | command[10] = file.getPath();
|
---|
269 | }
|
---|
270 | else {
|
---|
271 | command = new String[10];
|
---|
272 |
|
---|
273 | command[0] = "explode_metadata_database.pl";
|
---|
274 | command[1] = "-plugin";
|
---|
275 | command[2] = plugin_name;
|
---|
276 | command[3] = "-encoding";
|
---|
277 | command[4] = encoding;
|
---|
278 | command[5] = "-metadata_set";
|
---|
279 | command[6] = meta_set_namespace;
|
---|
280 | command[7] = "-filename_field";
|
---|
281 | command[8] = filename_field;
|
---|
282 | command[9] = file.getPath();
|
---|
283 |
|
---|
284 | }
|
---|
285 | Runtime runtime = Runtime.getRuntime();
|
---|
286 | Process process = runtime.exec(command);
|
---|
287 | process.waitFor();
|
---|
288 | return process.exitValue();
|
---|
289 |
|
---|
290 | } // else local
|
---|
291 |
|
---|
292 | }
|
---|
293 | catch (Exception error) {
|
---|
294 | error.printStackTrace();
|
---|
295 | return -1;
|
---|
296 | }
|
---|
297 |
|
---|
298 | }
|
---|
299 | }
|
---|
300 | }
|
---|