source: trunk/gli/src/org/greenstone/gatherer/gui/NewCollectionDetailsPrompt.java@ 8257

Last change on this file since 8257 was 8243, checked in by mdewsnip, 20 years ago

Removed all occurrences of classes explicitly importing other classes in the same package.

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