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

Last change on this file since 9888 was 9753, checked in by mdewsnip, 19 years ago

Made it so you don't have to enter a description when creating a new collection.

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