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

Last change on this file since 11778 was 11778, checked in by kjdon, 18 years ago

initialised title_final - seems to stop a bug the prevents it from displaying using apple java 1.5 on a mac

  • Property svn:keywords set to Author Date Id Revision
File size: 14.5 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 {
222 // Retrieve the first 8 non-whitespace ASCII characters of title_final.
223 StringBuffer name_buffer = new StringBuffer("");
224 for (int i = 0, u = 0; (i < title_final.length() && u < 8); i++) {
225 // To be safe we make sure the internal collection name (folder name) contains only ASCII characters
226 char c = title_final.charAt(i);
227 if ((int) c < 128 && Character.isLetterOrDigit(c)) {
228 name_buffer.append(Character.toLowerCase(c));
229 u++;
230 }
231 }
232
233 // Use "col" as the base collection name if we have nothing left
234 if (name_buffer.length() == 0) {
235 name_buffer = new StringBuffer("col");
236 }
237
238 // We need to ensure the filename is unique
239 int counter = 0;
240 StringBuffer new_name_buffer = new StringBuffer(name_buffer.toString());
241 while(filenameClashes(new_name_buffer.toString())) {
242 new_name_buffer = new StringBuffer(name_buffer.toString());
243 counter++;
244 String suffix = String.valueOf(counter);
245 // If we have to truncate the namestring so as to fit the suffix
246 if(suffix.length() + new_name_buffer.length() > 8) {
247 new_name_buffer.replace(new_name_buffer.length() - suffix.length(), new_name_buffer.length(), suffix);
248 }
249 // Or just append it if that isn't necessary
250 else {
251 new_name_buffer.append(suffix);
252 }
253
254 }
255 // All done
256 return new_name_buffer.toString();
257 }
258
259 private boolean filenameClashes(String filename)
260 {
261 File collect_directory = new File(Gatherer.getCollectDirectoryPath());
262 File children[] = collect_directory.listFiles();
263 for(int i = 0; children != null && i < children.length; i++) {
264 if(children[i].getName().equals(filename)) {
265 return true;
266 }
267 }
268 return false;
269 }
270
271 public String getTitle() {
272 return title_final;
273 }
274
275 private void addCollectionsToModel(Vector base_collection_model, File collect_directory, String site) {
276 File[] possible_collections = collect_directory.listFiles();
277 for (int i = 0; possible_collections != null && i < possible_collections.length; i++) {
278 // If the directory has a etc/collect.cfg file then it looks like a collection
279 File collect_cfg_file = new File(possible_collections[i], Utility.CONFIG_FILE);
280 if (collect_cfg_file.exists()) {
281 // If the directory has a metadata/ then the collection can be used as a base
282 File metadata_directory = new File(possible_collections[i], "metadata");
283 if (metadata_directory.exists()) {
284 // Add to model
285 BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(collect_cfg_file);
286 if (Gatherer.GS3 && site != null) {
287 collect_cfg.setSite(site);
288 }
289 Item item = new Item(possible_collections[i], collect_cfg);
290 if (!base_collection_model.contains(item)) {
291 base_collection_model.add(item);
292 }
293 }
294 }
295 }
296
297 }
298
299 private class CancelListener
300 implements ActionListener {
301 public void actionPerformed(ActionEvent event) {
302 cancelled = true;
303 self.dispose();
304 }
305 }
306
307 private class ColorListener
308 extends KeyAdapter {
309 private JTextComponent component1;
310 private JTextComponent component2;
311 public ColorListener(JTextComponent component) {
312 this.component1 = component;
313 this.component2 = null;
314 }
315 public ColorListener(JTextComponent component1, JTextComponent component2) {
316 this.component1 = component1;
317 this.component2 = component2;
318 }
319 public void keyPressed(KeyEvent event) {
320 component1.setForeground(Configuration.getColor("coloring.editable_foreground", false));
321 component1.setBackground(Configuration.getColor("coloring.editable_background", false));
322 if(component2 != null) {
323 component2.setForeground(Configuration.getColor("coloring.editable_foreground", false));
324 component2.setBackground(Configuration.getColor("coloring.editable_background", false));
325 }
326 }
327 }
328
329 private class CreateListener
330 implements ActionListener {
331
332 public void actionPerformed(ActionEvent event) {
333 // Validate.
334 title_final = title.getText();
335 if(title_final.length() == 0) {
336 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Title_Error"), Dictionary.get("NewCollectionPrompt.Error"), JOptionPane.ERROR_MESSAGE);
337 return;
338 }
339 // 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
340 else {
341 if(titleClashes(title_final, null)) {
342 if (JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Title_Clash"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
343 return;
344 }
345 }
346 }
347
348 description_final = description.getText();
349
350 // If we got this far there are no errors.
351 Item item_final = (Item) base_collection.getSelectedItem();
352 base_final = item_final.getFile();
353
354 cancelled = false;
355
356 self.dispose();
357 }
358 }
359
360 private class DescriptionListener
361 extends KeyAdapter {
362 public void keyPressed(KeyEvent event) {
363 if(event.getKeyCode() == KeyEvent.VK_TAB) {
364 event.consume();
365 base_collection.grabFocus();
366 }
367 }
368 }
369
370 private class Item
371 implements Comparable {
372 private BasicCollectionConfiguration config;
373 private File file;
374 private String name;
375 public Item(File file, BasicCollectionConfiguration config) {
376 this.config = config;
377 this.file = file;
378 this.name = null;
379 }
380 public Item(File file, String name) {
381 this.config = null;
382 this.file = file;
383 this.name = name;
384 }
385 public int compareTo(Object other) {
386 return toString().toLowerCase().compareTo(other.toString().toLowerCase());
387 }
388 public boolean equals(Object other) {
389 return compareTo(other) == 0;
390 }
391 public File getFile() {
392 return file;
393 }
394 public String toString() {
395 if(name == null && config != null) {
396 name = config.toString();
397 }
398 return name;
399 }
400 }
401}
402
403
404
Note: See TracBrowser for help on using the repository browser.