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

Last change on this file since 11791 was 11791, checked in by mdewsnip, 18 years ago

Removed some dead code leftover from that ridiculous functionality where the title/description fields would be coloured bright red if not filled out.

  • Property svn:keywords set to Author Date Id Revision
File size: 13.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 DescriptionListener());
161
162 // Layout
163 title_pane.setLayout(new BorderLayout(5,0));
164 title_pane.add(title_label, BorderLayout.WEST);
165 title_pane.add(title, BorderLayout.CENTER);
166
167 upper_pane.setLayout(new GridLayout(2,1));
168 upper_pane.add(instructions_label);
169 upper_pane.add(title_pane);
170
171 description_pane.setLayout(new BorderLayout());
172 description_pane.add(description_label, BorderLayout.NORTH);
173 description_pane.add(new JScrollPane(description), BorderLayout.CENTER);
174
175 base_collection_pane.setLayout(new BorderLayout(5,0));
176 base_collection_pane.add(base_collection_label, BorderLayout.WEST);
177 base_collection_pane.add(base_collection, BorderLayout.CENTER);
178
179 center_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
180 center_pane.setLayout(new BorderLayout());
181 center_pane.add(description_pane, BorderLayout.CENTER);
182
183 bottom_pane.setLayout(new BorderLayout());
184 bottom_pane.add(base_collection_pane, BorderLayout.CENTER);
185 bottom_pane.add(button_pane, BorderLayout.SOUTH);
186
187 button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
188 button_pane.setLayout(new GridLayout(1,2));
189 button_pane.add(create_button);
190 button_pane.add(cancel_button);
191
192 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
193 content_pane.setLayout(new BorderLayout());
194 content_pane.add(upper_pane, BorderLayout.NORTH);
195 content_pane.add(center_pane, BorderLayout.CENTER);
196 content_pane.add(bottom_pane, BorderLayout.SOUTH);
197 // Final dialog setup & positioning.
198 Dimension screen_size = Configuration.screen_size;
199 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
200 setVisible(true);
201 }
202
203 public boolean isCancelled() {
204 return cancelled;
205 }
206
207 public File getBase() {
208 return base_final;
209 }
210
211 public String getDescription() {
212 return description_final;
213 }
214
215 /** 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.
216 * @return the filename as a String
217 */
218 public String getName()
219 {
220 // Retrieve the first 8 non-whitespace ASCII characters of title_final.
221 StringBuffer name_buffer = new StringBuffer("");
222 for (int i = 0, u = 0; (i < title_final.length() && u < 8); i++) {
223 // To be safe we make sure the internal collection name (folder name) contains only ASCII characters
224 char c = title_final.charAt(i);
225 if ((int) c < 128 && Character.isLetterOrDigit(c)) {
226 name_buffer.append(Character.toLowerCase(c));
227 u++;
228 }
229 }
230
231 // Use "col" as the base collection name if we have nothing left
232 if (name_buffer.length() == 0) {
233 name_buffer = new StringBuffer("col");
234 }
235
236 // We need to ensure the filename is unique
237 int counter = 0;
238 StringBuffer new_name_buffer = new StringBuffer(name_buffer.toString());
239 while(filenameClashes(new_name_buffer.toString())) {
240 new_name_buffer = new StringBuffer(name_buffer.toString());
241 counter++;
242 String suffix = String.valueOf(counter);
243 // If we have to truncate the namestring so as to fit the suffix
244 if(suffix.length() + new_name_buffer.length() > 8) {
245 new_name_buffer.replace(new_name_buffer.length() - suffix.length(), new_name_buffer.length(), suffix);
246 }
247 // Or just append it if that isn't necessary
248 else {
249 new_name_buffer.append(suffix);
250 }
251
252 }
253 // All done
254 return new_name_buffer.toString();
255 }
256
257 private boolean filenameClashes(String filename)
258 {
259 File collect_directory = new File(Gatherer.getCollectDirectoryPath());
260 File children[] = collect_directory.listFiles();
261 for(int i = 0; children != null && i < children.length; i++) {
262 if(children[i].getName().equals(filename)) {
263 return true;
264 }
265 }
266 return false;
267 }
268
269 public String getTitle() {
270 return title_final;
271 }
272
273 private void addCollectionsToModel(Vector base_collection_model, File collect_directory, String site) {
274 File[] possible_collections = collect_directory.listFiles();
275 for (int i = 0; possible_collections != null && i < possible_collections.length; i++) {
276 // If the directory has a etc/collect.cfg file then it looks like a collection
277 File collect_cfg_file = new File(possible_collections[i], Utility.CONFIG_FILE);
278 if (collect_cfg_file.exists()) {
279 // If the directory has a metadata/ then the collection can be used as a base
280 File metadata_directory = new File(possible_collections[i], "metadata");
281 if (metadata_directory.exists()) {
282 // Add to model
283 BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(collect_cfg_file);
284 if (Gatherer.GS3 && site != null) {
285 collect_cfg.setSite(site);
286 }
287 Item item = new Item(possible_collections[i], collect_cfg);
288 if (!base_collection_model.contains(item)) {
289 base_collection_model.add(item);
290 }
291 }
292 }
293 }
294
295 }
296
297 private class CancelListener
298 implements ActionListener {
299 public void actionPerformed(ActionEvent event) {
300 cancelled = true;
301 self.dispose();
302 }
303 }
304
305
306 private class CreateListener
307 implements ActionListener {
308
309 public void actionPerformed(ActionEvent event) {
310 // Validate.
311 title_final = title.getText();
312 if(title_final.length() == 0) {
313 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Title_Error"), Dictionary.get("NewCollectionPrompt.Error"), JOptionPane.ERROR_MESSAGE);
314 return;
315 }
316 // 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
317 else {
318 if(titleClashes(title_final, null)) {
319 if (JOptionPane.showConfirmDialog(Gatherer.g_man, Dictionary.get("NewCollectionPrompt.Title_Clash"), Dictionary.get("General.Warning"), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
320 return;
321 }
322 }
323 }
324
325 description_final = description.getText();
326
327 // If we got this far there are no errors.
328 Item item_final = (Item) base_collection.getSelectedItem();
329 base_final = item_final.getFile();
330
331 cancelled = false;
332
333 self.dispose();
334 }
335 }
336
337 private class DescriptionListener
338 extends KeyAdapter {
339 public void keyPressed(KeyEvent event) {
340 if(event.getKeyCode() == KeyEvent.VK_TAB) {
341 event.consume();
342 base_collection.grabFocus();
343 }
344 }
345 }
346
347 private class Item
348 implements Comparable {
349 private BasicCollectionConfiguration config;
350 private File file;
351 private String name;
352 public Item(File file, BasicCollectionConfiguration config) {
353 this.config = config;
354 this.file = file;
355 this.name = null;
356 }
357 public Item(File file, String name) {
358 this.config = null;
359 this.file = file;
360 this.name = name;
361 }
362 public int compareTo(Object other) {
363 return toString().toLowerCase().compareTo(other.toString().toLowerCase());
364 }
365 public boolean equals(Object other) {
366 return compareTo(other) == 0;
367 }
368 public File getFile() {
369 return file;
370 }
371 public String toString() {
372 if(name == null && config != null) {
373 name = config.toString();
374 }
375 return name;
376 }
377 }
378}
379
380
381
Note: See TracBrowser for help on using the repository browser.