source: gli/branches/rtl-gli/src/org/greenstone/gatherer/gui/NewCollectionDetailsPrompt.java@ 18297

Last change on this file since 18297 was 18297, checked in by kjdon, 15 years ago

interface updated to display right to left for rtl languages. This code is thanks to Amin Hejazi. It seems to be only partially complete. Amin was working with Greenstone 3 so might have missed some panels

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