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: John Thompson, Greenstone Project, NZDL, University of Waikato
|
---|
9 | *
|
---|
10 | * Copyright (C) 2003 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 java.io.File;
|
---|
32 | import java.util.*;
|
---|
33 | import javax.swing.*;
|
---|
34 | import javax.swing.event.*;
|
---|
35 | import javax.swing.text.*;
|
---|
36 | import org.greenstone.gatherer.Dictionary;
|
---|
37 | import org.greenstone.gatherer.Gatherer;
|
---|
38 | import org.greenstone.gatherer.collection.BasicCollectionConfiguration;
|
---|
39 | import org.greenstone.gatherer.gui.GLIButton;
|
---|
40 | import org.greenstone.gatherer.gui.SimpleMenuBar;
|
---|
41 | import org.greenstone.gatherer.gui.ModalDialog;
|
---|
42 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
43 | import org.greenstone.gatherer.util.Utility;
|
---|
44 |
|
---|
45 | public class NewCollectionDetailsPrompt
|
---|
46 | extends ModalDialog {
|
---|
47 |
|
---|
48 | static public boolean titleClashes(String title, File current_config_file) {
|
---|
49 | // An empty collection title never clashes with anything. It may look ugly in the final collection but there is nothing wrong with having no title for a particular language.
|
---|
50 | if(title == null || title.length() == 0) {
|
---|
51 | return false;
|
---|
52 | }
|
---|
53 | File collection_directory;
|
---|
54 | if (Gatherer.GS3) {
|
---|
55 | collection_directory = new File(Utility.getCollectDir(Gatherer.config.gsdl3_path, Gatherer.config.site_name));
|
---|
56 | } else {
|
---|
57 | collection_directory = new File(Utility.getCollectDir(Gatherer.config.gsdl_path));
|
---|
58 | }
|
---|
59 | File children[] = collection_directory.listFiles();
|
---|
60 | for(int i = 0; children != null && i < children.length; i++) {
|
---|
61 | if(children[i].isDirectory()) {
|
---|
62 | File config_file = new File(children[i], Utility.CONFIG_FILE);
|
---|
63 | if(current_config_file == null || !config_file.equals(current_config_file)) {
|
---|
64 | BasicCollectionConfiguration other_collection = new BasicCollectionConfiguration(config_file);
|
---|
65 | if(other_collection.getName().equalsIgnoreCase(title)) {
|
---|
66 | return true;
|
---|
67 | }
|
---|
68 | other_collection = null;
|
---|
69 | }
|
---|
70 | config_file = null;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 |
|
---|
76 | private boolean cancelled;
|
---|
77 | private File base_final;
|
---|
78 | private JButton create_button;
|
---|
79 | private JComboBox base_collection;
|
---|
80 | private JDialog self;
|
---|
81 | private JTextArea description;
|
---|
82 | private JTextField title;
|
---|
83 | private String description_final;
|
---|
84 | private String title_final;
|
---|
85 | static private Dimension COMPONENT_SIZE = new Dimension(230, 25);
|
---|
86 | /** The size of this new collection dialog box. */
|
---|
87 | static private Dimension SIZE = new Dimension(600, 280);
|
---|
88 | static private int FILENAME_SIZE = 8;
|
---|
89 |
|
---|
90 | /** Constructor.
|
---|
91 | * @see org.greenstone.gatherer.util.Utility
|
---|
92 | */
|
---|
93 | public NewCollectionDetailsPrompt() {
|
---|
94 | super(Gatherer.g_man, true);
|
---|
95 | this.cancelled = true;
|
---|
96 | this.self = this;
|
---|
97 | // Setup
|
---|
98 | setJMenuBar(new SimpleMenuBar("creatingacollection"));
|
---|
99 | setSize(SIZE);
|
---|
100 | Dictionary.setText(this, "NewCollectionPrompt.Title");
|
---|
101 |
|
---|
102 | // Model building. Build a model of all of the collections in the gsdl collect directory with the appropriate directories.
|
---|
103 | Vector base_collection_model = new Vector();
|
---|
104 | // need to modify this to base a coll on any collection from any site
|
---|
105 | File gsdl_collection_directory;
|
---|
106 | if (Gatherer.GS3) {
|
---|
107 | gsdl_collection_directory = new File(Utility.getCollectDir(Gatherer.config.gsdl3_path, Gatherer.config.site_name));
|
---|
108 | } else {
|
---|
109 | gsdl_collection_directory = new File(Utility.getCollectDir(Gatherer.config.gsdl_path));
|
---|
110 | }
|
---|
111 | File[] possible_collections = gsdl_collection_directory.listFiles();
|
---|
112 | for (int i = 0; possible_collections != null && i < possible_collections.length; i++) {
|
---|
113 | // If the directory has a etc/collect.cfg file then it looks like a collection
|
---|
114 | File collect_cfg_file = new File(possible_collections[i], Utility.CONFIG_FILE);
|
---|
115 | if (collect_cfg_file.exists()) {
|
---|
116 | // If the directory has a metadata/ then the collection can be used as a base
|
---|
117 | File metadata_directory = new File(possible_collections[i], Utility.META_DIR);
|
---|
118 | if (metadata_directory.exists()) {
|
---|
119 | // Add to model
|
---|
120 | BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(collect_cfg_file);
|
---|
121 | Item item = new Item(possible_collections[i], collect_cfg);
|
---|
122 | if (!base_collection_model.contains(item)) {
|
---|
123 | base_collection_model.add(item);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | // Sort the result.
|
---|
130 | Collections.sort(base_collection_model);
|
---|
131 | base_collection_model.add(0, new Item(null, Dictionary.get("NewCollectionPrompt.NewCollection")));
|
---|
132 |
|
---|
133 | // Creation
|
---|
134 | JPanel content_pane = (JPanel) getContentPane();
|
---|
135 | content_pane.setOpaque(true);
|
---|
136 | JPanel upper_pane = new JPanel();
|
---|
137 | JLabel instructions_label = new JLabel();
|
---|
138 | Dictionary.setText(instructions_label, "NewCollectionPrompt.Instructions");
|
---|
139 | JPanel title_pane = new JPanel();
|
---|
140 | JLabel title_label = new JLabel();
|
---|
141 | Dictionary.setText(title_label, "CDM.General.Collection_Name");
|
---|
142 | title = new JTextField();
|
---|
143 | title.setPreferredSize(COMPONENT_SIZE);
|
---|
144 | Dictionary.setTooltip(title, "CDM.General.Collection_Name_Tooltip");
|
---|
145 | JPanel name_pane = new JPanel();
|
---|
146 | JLabel name_label = new JLabel();
|
---|
147 | Dictionary.setText(name_label, "NewCollectionPrompt.Collection_Name");
|
---|
148 |
|
---|
149 | JPanel center_pane = new JPanel();
|
---|
150 | JPanel description_pane = new JPanel();
|
---|
151 | JLabel description_label = new JLabel();
|
---|
152 | Dictionary.setText(description_label, "NewCollectionPrompt.Collection_Description");
|
---|
153 | description = new JTextArea();
|
---|
154 | description.setBackground(Gatherer.config.getColor("coloring.editable_background", false));
|
---|
155 | description.setForeground(Gatherer.config.getColor("coloring.editable_foreground", false));
|
---|
156 | description.setRows(5);
|
---|
157 | Dictionary.setTooltip(description, "CDM.General.Collection_Extra_Tooltip");
|
---|
158 |
|
---|
159 | JPanel bottom_pane = new JPanel();
|
---|
160 | // Base Collection
|
---|
161 | JPanel base_collection_pane = new JPanel();
|
---|
162 | JLabel base_collection_label = new JLabel();
|
---|
163 | Dictionary.setText(base_collection_label, "NewCollectionPrompt.Base_Collection");
|
---|
164 | base_collection = new JComboBox(base_collection_model);
|
---|
165 | Dictionary.setTooltip(base_collection, "NewCollectionPrompt.Base_Collection_Tooltip");
|
---|
166 |
|
---|
167 | JPanel button_pane = new JPanel();
|
---|
168 | create_button = new GLIButton();
|
---|
169 | create_button.setMnemonic(KeyEvent.VK_O);
|
---|
170 | Dictionary.setBoth(create_button, "General.OK", "General.OK_Tooltip");
|
---|
171 | JButton cancel_button = new GLIButton();
|
---|
172 | cancel_button.setMnemonic(KeyEvent.VK_C);
|
---|
173 | Dictionary.setBoth(cancel_button, "General.Cancel", "General.Cancel_Tooltip");
|
---|
174 | // Connection
|
---|
175 | cancel_button.addActionListener(new CancelListener());
|
---|
176 | create_button.addActionListener(new CreateListener());
|
---|
177 | description.addKeyListener(new ColorListener(description));
|
---|
178 | description.addKeyListener(new DescriptionListener());
|
---|
179 | title.addKeyListener(new ColorListener(title));
|
---|
180 |
|
---|
181 | // Layout
|
---|
182 | title_pane.setLayout(new BorderLayout(5,0));
|
---|
183 | title_pane.add(title_label, BorderLayout.WEST);
|
---|
184 | title_pane.add(title, BorderLayout.CENTER);
|
---|
185 |
|
---|
186 | upper_pane.setLayout(new GridLayout(2,1));
|
---|
187 | upper_pane.add(instructions_label);
|
---|
188 | upper_pane.add(title_pane);
|
---|
189 |
|
---|
190 | description_pane.setLayout(new BorderLayout());
|
---|
191 | description_pane.add(description_label, BorderLayout.NORTH);
|
---|
192 | description_pane.add(new JScrollPane(description), BorderLayout.CENTER);
|
---|
193 |
|
---|
194 | base_collection_pane.setLayout(new BorderLayout(5,0));
|
---|
195 | base_collection_pane.add(base_collection_label, BorderLayout.WEST);
|
---|
196 | base_collection_pane.add(base_collection, BorderLayout.CENTER);
|
---|
197 |
|
---|
198 | center_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
|
---|
199 | center_pane.setLayout(new BorderLayout());
|
---|
200 | center_pane.add(description_pane, BorderLayout.CENTER);
|
---|
201 |
|
---|
202 | bottom_pane.setLayout(new BorderLayout());
|
---|
203 | bottom_pane.add(base_collection_pane, BorderLayout.CENTER);
|
---|
204 | bottom_pane.add(button_pane, BorderLayout.SOUTH);
|
---|
205 |
|
---|
206 | button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
|
---|
207 | button_pane.setLayout(new GridLayout(1,2));
|
---|
208 | button_pane.add(create_button);
|
---|
209 | button_pane.add(cancel_button);
|
---|
210 |
|
---|
211 | content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
212 | content_pane.setLayout(new BorderLayout());
|
---|
213 | content_pane.add(upper_pane, BorderLayout.NORTH);
|
---|
214 | content_pane.add(center_pane, BorderLayout.CENTER);
|
---|
215 | content_pane.add(bottom_pane, BorderLayout.SOUTH);
|
---|
216 | // Final dialog setup & positioning.
|
---|
217 | Dimension screen_size = Gatherer.config.screen_size;
|
---|
218 | setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
|
---|
219 | setVisible(true);
|
---|
220 | }
|
---|
221 |
|
---|
222 | public boolean isCancelled() {
|
---|
223 | return cancelled;
|
---|
224 | }
|
---|
225 |
|
---|
226 | public File getBase() {
|
---|
227 | return base_final;
|
---|
228 | }
|
---|
229 |
|
---|
230 | public String getDescription() {
|
---|
231 | return description_final;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /** Generates the collection short filename by taking the first eight characters of the title (spaces removed) and then adjusting by further truncating and then adding an unique suffix as necessary.
|
---|
235 | * @return the filename as a String
|
---|
236 | */
|
---|
237 | public String getName() {
|
---|
238 | // Retrieve the first 8 non-whitespace characters of title_final.
|
---|
239 | StringBuffer name_buffer = new StringBuffer("");
|
---|
240 | int i = 0;
|
---|
241 | while(i < title_final.length() && name_buffer.length() < 8) {
|
---|
242 | char c = title_final.charAt(i);
|
---|
243 | // Arg. We need a few more tests than just whitespace
|
---|
244 | if(Character.isLetterOrDigit(c)) {
|
---|
245 | name_buffer.append(Character.toLowerCase(c));
|
---|
246 | }
|
---|
247 | i++;
|
---|
248 | }
|
---|
249 | // We need to ensure the filename is unique
|
---|
250 | int counter = 0;
|
---|
251 | StringBuffer new_name_buffer = new StringBuffer(name_buffer.toString());
|
---|
252 | while(filenameClashes(new_name_buffer.toString())) {
|
---|
253 | new_name_buffer = new StringBuffer(name_buffer.toString());
|
---|
254 | counter++;
|
---|
255 | String suffix = String.valueOf(counter);
|
---|
256 | // If we have to truncate the namestring so as to fit the suffix
|
---|
257 | if(suffix.length() + new_name_buffer.length() > 8) {
|
---|
258 | new_name_buffer.replace(new_name_buffer.length() - suffix.length(), new_name_buffer.length(), suffix);
|
---|
259 | }
|
---|
260 | // Or just append it if that isn't necessary
|
---|
261 | else {
|
---|
262 | new_name_buffer.append(suffix);
|
---|
263 | }
|
---|
264 |
|
---|
265 | }
|
---|
266 | // All done
|
---|
267 | return new_name_buffer.toString();
|
---|
268 | }
|
---|
269 |
|
---|
270 | private boolean filenameClashes(String filename) {
|
---|
271 | File collection_directory;
|
---|
272 | if (Gatherer.GS3) {
|
---|
273 | collection_directory = new File(Utility.getCollectDir(Gatherer.config.gsdl3_path, Gatherer.config.site_name));
|
---|
274 | } else {
|
---|
275 | collection_directory = new File(Utility.getCollectDir(Gatherer.config.gsdl_path));
|
---|
276 | }
|
---|
277 |
|
---|
278 | File children[] = collection_directory.listFiles();
|
---|
279 | for(int i = 0; children != null && i < children.length; i++) {
|
---|
280 | if(children[i].getName().equals(filename)) {
|
---|
281 | return true;
|
---|
282 | }
|
---|
283 | }
|
---|
284 | return false;
|
---|
285 | }
|
---|
286 |
|
---|
287 | public String getTitle() {
|
---|
288 | return title_final;
|
---|
289 | }
|
---|
290 |
|
---|
291 | private class CancelListener
|
---|
292 | implements ActionListener {
|
---|
293 | public void actionPerformed(ActionEvent event) {
|
---|
294 | cancelled = true;
|
---|
295 | self.dispose();
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | private class ColorListener
|
---|
300 | extends KeyAdapter {
|
---|
301 | private JTextComponent component1;
|
---|
302 | private JTextComponent component2;
|
---|
303 | public ColorListener(JTextComponent component) {
|
---|
304 | this.component1 = component;
|
---|
305 | this.component2 = null;
|
---|
306 | }
|
---|
307 | public ColorListener(JTextComponent component1, JTextComponent component2) {
|
---|
308 | this.component1 = component1;
|
---|
309 | this.component2 = component2;
|
---|
310 | }
|
---|
311 | public void keyPressed(KeyEvent event) {
|
---|
312 | component1.setForeground(Gatherer.config.getColor("coloring.editable_foreground", false));
|
---|
313 | component1.setBackground(Gatherer.config.getColor("coloring.editable_background", false));
|
---|
314 | if(component2 != null) {
|
---|
315 | component2.setForeground(Gatherer.config.getColor("coloring.editable_foreground", false));
|
---|
316 | component2.setBackground(Gatherer.config.getColor("coloring.editable_background", false));
|
---|
317 | }
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | private class CreateListener
|
---|
322 | implements ActionListener {
|
---|
323 |
|
---|
324 | public void actionPerformed(ActionEvent event) {
|
---|
325 | // Validate.
|
---|
326 | title_final = title.getText();
|
---|
327 | if(title_final.length() == 0) {
|
---|
328 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Title_Error"), Dictionary.get("NewCollectionPrompt.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
329 | title.setForeground(Gatherer.config.getColor("coloring.error_foreground", false));
|
---|
330 | title.setBackground(Gatherer.config.getColor("coloring.error_background", false));
|
---|
331 | return;
|
---|
332 | }
|
---|
333 | // We must ensure that the collection title is unique. This is a pain in the nether regions as we are forced to load the collect.cfg of each other collection in turn looking for a conflicting title
|
---|
334 | else {
|
---|
335 | if(titleClashes(title_final, null)) {
|
---|
336 | if(JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Title_Clash"), Dictionary.get("General.Warning"), JOptionPane.OK_CANCEL_OPTION) == JOptionPane.NO_OPTION) {
|
---|
337 | title.setForeground(Gatherer.config.getColor("coloring.error_foreground", false));
|
---|
338 | title.setBackground(Gatherer.config.getColor("coloring.error_background", false));
|
---|
339 | return;
|
---|
340 | }
|
---|
341 | }
|
---|
342 | }
|
---|
343 |
|
---|
344 | description_final = description.getText();
|
---|
345 | if(description_final.length() == 0) {
|
---|
346 | JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Description_Error"), Dictionary.get("NewCollectionPrompt.Error"), JOptionPane.ERROR_MESSAGE);
|
---|
347 | description.setForeground(Gatherer.config.getColor("coloring.error_foreground", false));
|
---|
348 | description.setBackground(Gatherer.config.getColor("coloring.error_background", false));
|
---|
349 | return;
|
---|
350 | }
|
---|
351 | // If we got this far there are no errors.
|
---|
352 | Item item_final = (Item) base_collection.getSelectedItem();
|
---|
353 | base_final = item_final.getFile();
|
---|
354 |
|
---|
355 | cancelled = false;
|
---|
356 |
|
---|
357 | self.dispose();
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | private class DescriptionListener
|
---|
362 | extends KeyAdapter {
|
---|
363 | public void keyPressed(KeyEvent event) {
|
---|
364 | if(event.getKeyCode() == KeyEvent.VK_TAB) {
|
---|
365 | event.consume();
|
---|
366 | base_collection.grabFocus();
|
---|
367 | }
|
---|
368 | }
|
---|
369 | }
|
---|
370 |
|
---|
371 | private class Item
|
---|
372 | implements Comparable {
|
---|
373 | private BasicCollectionConfiguration config;
|
---|
374 | private File file;
|
---|
375 | private String name;
|
---|
376 | public Item(File file, BasicCollectionConfiguration config) {
|
---|
377 | this.config = config;
|
---|
378 | this.file = file;
|
---|
379 | this.name = null;
|
---|
380 | }
|
---|
381 | public Item(File file, String name) {
|
---|
382 | this.config = null;
|
---|
383 | this.file = file;
|
---|
384 | this.name = name;
|
---|
385 | }
|
---|
386 | public int compareTo(Object other) {
|
---|
387 | return toString().toLowerCase().compareTo(other.toString().toLowerCase());
|
---|
388 | }
|
---|
389 | public boolean equals(Object other) {
|
---|
390 | return compareTo(other) == 0;
|
---|
391 | }
|
---|
392 | public File getFile() {
|
---|
393 | return file;
|
---|
394 | }
|
---|
395 | public String toString() {
|
---|
396 | if(name == null && config != null) {
|
---|
397 | name = config.toString();
|
---|
398 | }
|
---|
399 | return name;
|
---|
400 | }
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 |
|
---|
405 |
|
---|