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

Last change on this file since 18130 was 18130, checked in by ak19, 15 years ago

Ok button is made the default button so that it is activated on pressing Enter

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