source: trunk/gli/src/org/greenstone/gatherer/cdm/GeneralManager.java@ 12641

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

Changed all access to the static strings through CollectionConfiguration to directly use StaticStrings.

  • Property svn:keywords set to Author Date Id Revision
File size: 18.7 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 Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 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.cdm;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.io.*;
32import java.util.regex.*;
33import javax.swing.*;
34import javax.swing.filechooser.*;
35import javax.swing.event.*;
36import org.greenstone.gatherer.Configuration;
37import org.greenstone.gatherer.DebugStream;
38import org.greenstone.gatherer.Dictionary;
39import org.greenstone.gatherer.Gatherer;
40import org.greenstone.gatherer.gui.DesignPaneHeader;
41import org.greenstone.gatherer.gui.EmailField;
42import org.greenstone.gatherer.gui.GLIButton;
43import org.greenstone.gatherer.gui.GUIUtils;
44import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
45import org.greenstone.gatherer.util.StaticStrings;
46
47/** This class provides a graphical interface for editing general metadata for the collection, such as name, description, icons etc.
48*/
49public class GeneralManager {
50
51 /** The controls used to modify the general options. */
52 private Control controls;
53 /** Constructor. */
54 public GeneralManager() {
55 }
56
57 /** Destructor. */
58 public void destroy() {
59 if (controls != null) {
60 controls.destroy();
61 controls = null;
62 }
63 }
64
65 public void loseFocus() {
66 }
67
68 public void gainFocus() {
69
70 }
71 /** This class is resposible for generating the controls for the editing of general options.
72 * @return the Control for editing the general options
73 */
74 public Control getControls() {
75 if (controls == null) {
76 controls = new GeneralControl();
77 }
78 return controls;
79 }
80
81
82 /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
83 * @param mode the new mode as an int
84 */
85 public void modeChanged(int mode) {
86
87 }
88
89 /** This class represents the visual component of the general options stored in the CollectionDesignManager. */
90 private class GeneralControl
91 extends JPanel
92 implements Control {
93
94 //private boolean has_been_warned = false;
95 private boolean ready = false;
96 private CollectionMeta collection_extra_collectionmeta;
97 private CollectionMeta collection_name_collectionmeta;
98 private CollectionMeta creator_collectionmeta;
99 private CollectionMeta icon_collection_collectionmeta;
100 private CollectionMeta icon_collection_small_collectionmeta;
101 private CollectionMeta maintainer_collectionmeta;
102 private CollectionMeta public_collectionmeta;
103 /** The creators email. */
104 private EmailField creator_emailfield;
105 /** The maintainers email. */
106 private EmailField maintainer_emailfield;
107 /** Button to browse for the collection about page icon. */
108 private JButton browse_about_icon_button;
109 /** Button to browse for the collection home page icon. */
110 private JButton browse_home_icon_button;
111 /** The checkbox controlling public access to the collection. */
112 private JCheckBox public_checkbox;
113 private JLabel creator_label;
114 private JLabel description_label;
115 private JLabel icon_label;
116 private JLabel maintainer_label;
117 private JLabel name_label;
118 private JLabel small_icon_label;
119 /** The text field used to edit the file name of the collections icon. */
120 private JTextField icon_textfield;
121 /** The text field used to edit the collections title. */
122 private JTextField name_textfield;
123 /** The text field used to edit the file name of the collections small icon. */
124 private JTextField small_icon_textfield;
125 /** A text area used to modify the collection description. */
126 private JTextArea description_textarea;
127 /** Constructor. */
128 public GeneralControl() {
129 super();
130 // Retrieve some of the model elements, those we know aren't language dependant
131 public_collectionmeta = new CollectionMeta(CollectionDesignManager.collect_config.getPublic());
132
133 // Creation
134 JPanel header_panel = new DesignPaneHeader("CDM.GUI.General", "generalsettings");
135
136 JPanel all_details_panel = new JPanel();
137 JPanel details_panel = new JPanel();
138 JPanel fields_panel = new JPanel();
139
140 creator_label = new JLabel(Dictionary.get("CDM.General.Email.Creator"));
141
142 creator_emailfield = new EmailField(Configuration.getColor("coloring.error_background", false));
143 creator_emailfield.setToolTipText(Dictionary.get("CDM.General.Email.Creator_Tooltip"));
144
145 maintainer_label = new JLabel(Dictionary.get("CDM.General.Email.Maintainer"));
146
147 maintainer_emailfield = new EmailField(Configuration.getColor("coloring.error_background", false));
148 maintainer_emailfield.setToolTipText(Dictionary.get("CDM.General.Email.Maintainer_Tooltip"));
149
150 name_label = new JLabel(Dictionary.get("CDM.General.Collection_Name"));
151 name_textfield = new JTextField();
152 name_textfield.setToolTipText(Dictionary.get("CDM.General.Collection_Name_Tooltip"));
153
154 JLabel short_name_label = new JLabel(Dictionary.get("NewCollectionPrompt.Collection_Name"));
155 JTextField short_name_textfield = new JTextField(Gatherer.c_man.getCollection().getName());
156 short_name_textfield.setEditable(false);
157 short_name_textfield.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
158 JPanel icon_panel = new JPanel();
159 icon_label = new JLabel(Dictionary.get("CDM.General.Icon_Collection"));
160 icon_textfield = new JTextField();
161 icon_textfield.setToolTipText(Dictionary.get("CDM.General.Icon_Collection_Tooltip"));
162 browse_about_icon_button = new GLIButton(Dictionary.get("General.Browse"));
163 JPanel small_icon_panel = new JPanel();
164 small_icon_label = new JLabel(Dictionary.get("CDM.General.Icon_Collection_Small"));
165 small_icon_textfield = new JTextField();
166 small_icon_textfield.setToolTipText(Dictionary.get("CDM.General.Icon_Collection_Small_Tooltip"));
167 browse_home_icon_button = new GLIButton(Dictionary.get("General.Browse"));
168
169 // public
170 JPanel box_panel = new JPanel();
171 public_checkbox = new JCheckBox(Dictionary.get("CDM.General.Access"), public_collectionmeta.getValue(CollectionMeta.TEXT).equals(StaticStrings.TRUE_STR));
172
173 JPanel description_panel = new JPanel();
174 description_label = new JLabel(Dictionary.get("CDM.General.Collection_Extra"));
175
176 description_textarea = new JTextArea();
177 description_textarea.setBackground(Configuration.getColor("coloring.editable_background", false));
178 description_textarea.setToolTipText(Dictionary.get("CDM.General.Collection_Extra_Tooltip"));
179
180 // Connection
181 BrowseListener browse_listener = new BrowseListener(StaticStrings.IMAGES_PATH_RELATIVE_TO_GSDL_PREFIX);
182 browse_about_icon_button.addActionListener(browse_listener);
183 browse_home_icon_button.addActionListener(browse_listener);
184 browse_listener = null;
185 public_checkbox.addActionListener(CollectionDesignManager.change_listener);
186 public_checkbox.addActionListener(CollectionDesignManager.buildcol_change_listener);
187 creator_emailfield.getDocument().addDocumentListener(CollectionDesignManager.change_listener);
188 creator_emailfield.getDocument().addDocumentListener(CollectionDesignManager.buildcol_change_listener);
189 description_textarea.getDocument().addDocumentListener(CollectionDesignManager.change_listener);
190 description_textarea.getDocument().addDocumentListener(CollectionDesignManager.buildcol_change_listener);
191 icon_textfield.getDocument().addDocumentListener(CollectionDesignManager.change_listener);
192 icon_textfield.getDocument().addDocumentListener(CollectionDesignManager.buildcol_change_listener);
193 maintainer_emailfield.getDocument().addDocumentListener(CollectionDesignManager.change_listener);
194 maintainer_emailfield.getDocument().addDocumentListener(CollectionDesignManager.buildcol_change_listener);
195 name_textfield.getDocument().addDocumentListener(CollectionDesignManager.change_listener);
196 name_textfield.getDocument().addDocumentListener(new CollectionTitleUpdater());
197 name_textfield.getDocument().addDocumentListener(CollectionDesignManager.buildcol_change_listener);
198 small_icon_textfield.getDocument().addDocumentListener(CollectionDesignManager.change_listener);
199 small_icon_textfield.getDocument().addDocumentListener(CollectionDesignManager.buildcol_change_listener);
200 //Note: unfortunately just loading the General Design panel fires the buildcol_change_listener (even if nothing is changed).
201
202 // Layout
203 fields_panel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
204 fields_panel.setLayout(new BorderLayout(5,2));
205
206 JPanel fields_label_panel = new JPanel();
207 fields_label_panel.setLayout(new GridLayout(6,1));
208
209 JPanel fields_box_panel = new JPanel();
210 fields_box_panel.setLayout(new GridLayout(6,1));
211
212 // creator
213 fields_label_panel.add(creator_label);
214 fields_box_panel.add(creator_emailfield);
215
216 // maintainer
217 fields_label_panel.add(maintainer_label);
218 fields_box_panel.add(maintainer_emailfield);
219
220 // title
221 fields_label_panel.add(name_label);
222 fields_box_panel.add(name_textfield);
223
224 // collection short name
225 fields_label_panel.add(short_name_label);
226 fields_box_panel.add(short_name_textfield);
227
228 // icon
229 fields_label_panel.add(icon_label);
230 fields_box_panel.add(icon_panel);
231
232 // small icon
233 fields_label_panel.add(small_icon_label);
234 fields_box_panel.add(small_icon_panel);
235
236 fields_panel.add(fields_label_panel, BorderLayout.WEST);
237 fields_panel.add(fields_box_panel, BorderLayout.CENTER);
238
239 icon_panel.setLayout(new BorderLayout());
240 icon_panel.add(icon_textfield, BorderLayout.CENTER);
241 icon_panel.add(browse_about_icon_button, BorderLayout.EAST);
242
243 small_icon_panel.setLayout(new BorderLayout());
244 small_icon_panel.add(small_icon_textfield, BorderLayout.CENTER);
245 small_icon_panel.add(browse_home_icon_button, BorderLayout.EAST);
246
247 box_panel.setLayout(new GridLayout(1,1,5,2));
248 box_panel.add(public_checkbox);
249
250 description_panel.setLayout(new BorderLayout());
251 description_panel.add(description_label, BorderLayout.NORTH);
252 description_panel.add(new JScrollPane(description_textarea), BorderLayout.CENTER);
253
254 details_panel.setLayout(new BorderLayout());
255 details_panel.add(fields_panel, BorderLayout.NORTH);
256 details_panel.add(box_panel, BorderLayout.CENTER);
257
258 all_details_panel.setLayout(new BorderLayout());
259 all_details_panel.add(details_panel, BorderLayout.NORTH);
260 all_details_panel.add(description_panel, BorderLayout.CENTER);
261
262 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
263 setLayout(new BorderLayout());
264 add(header_panel, BorderLayout.NORTH);
265 add(all_details_panel, BorderLayout.CENTER);
266 }
267
268 /** Destructor. */
269 public void destroy() {
270 }
271
272 /** Called to refresh the components. */
273 public void gainFocus() {
274 // Retrieve all of the elements that are dependant on default language.
275 collection_extra_collectionmeta = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR);
276 collection_name_collectionmeta = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR);
277
278 creator_collectionmeta = new CollectionMeta(CollectionDesignManager.collect_config.getCreator());
279 icon_collection_collectionmeta = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_ICONCOLLECTION_STR);
280 icon_collection_small_collectionmeta = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_ICONCOLLECTIONSMALL_STR);
281 maintainer_collectionmeta = new CollectionMeta(CollectionDesignManager.collect_config.getMaintainer());
282 // Make sure the components are up to date
283 creator_emailfield.setText(creator_collectionmeta.getValue(CollectionMeta.TEXT));
284 creator_emailfield.setCaretPosition(0);
285 description_textarea.setText(collection_extra_collectionmeta.getValue(CollectionMeta.TEXT));
286 description_textarea.setCaretPosition(0);
287 icon_textfield.setText(icon_collection_collectionmeta.getValue(CollectionMeta.TEXT));
288 icon_textfield.setCaretPosition(0);
289 maintainer_emailfield.setText(maintainer_collectionmeta.getValue(CollectionMeta.TEXT));
290 maintainer_emailfield.setCaretPosition(0);
291 name_textfield.setText(collection_name_collectionmeta.getValue(CollectionMeta.TEXT));
292 name_textfield.setCaretPosition(0);
293 small_icon_textfield.setText(icon_collection_small_collectionmeta.getValue(CollectionMeta.TEXT));
294 small_icon_textfield.setCaretPosition(0);
295 public_checkbox.setSelected(public_collectionmeta.getValue(CollectionMeta.TEXT).equals(StaticStrings.TRUE_STR));
296 ready = true;
297 }
298
299 /** Called to store the current value of the components. */
300 public void loseFocus() {
301 // String values. Have to test if this component has actually ever recieved focus anyway.
302 if(ready) {
303 // Boolean values
304 public_collectionmeta.setValue((public_checkbox.isSelected() ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
305 String creator_email_str = creator_emailfield.getText();
306 creator_collectionmeta.setValue(creator_email_str);
307 // If the email is currently empty, store this email
308 if(Configuration.getEmail() == null) {
309 Configuration.setEmail(creator_email_str); // Store the email address in the configuration
310 }
311 collection_extra_collectionmeta.setValue(description_textarea.getText());
312 icon_collection_collectionmeta.setValue(icon_textfield.getText());
313 maintainer_collectionmeta.setValue(maintainer_emailfield.getText());
314 icon_collection_small_collectionmeta.setValue(small_icon_textfield.getText());
315 collection_name_collectionmeta.setValue(name_textfield.getText());
316 ready = false;
317 }
318
319 }
320
321 /** Listens for a click on either browse button, and when detected opens a file browser window initially pointed at the images folder of the collection. Once a user has selected a path, does it's best to construct the best address to the resource, such as _httpcollection_/images/about.gif */
322 private class BrowseListener
323 implements ActionListener {
324
325 private String prefix;
326
327 public BrowseListener(String prefix_raw) {
328 this.prefix = prefix_raw.replaceAll(StaticStrings.COLNAME_PATTERN, Gatherer.c_man.getCollection().getName());
329 }
330
331 public void actionPerformed(ActionEvent event) {
332 // Open an almost standard file browser to the images folder of the current collection
333 File images_folder = new File(Gatherer.c_man.getCollectionImagesDirectoryPath());
334 // If images isn't already there, create it
335 if(!images_folder.exists()) {
336 images_folder.mkdirs();
337 }
338 JFileChooser file_chooser = new JFileChooser(images_folder);
339 file_chooser.setAcceptAllFileFilterUsed(false);
340 file_chooser.setDialogTitle(Dictionary.get("CDM.General.Browser_Title"));
341 file_chooser.setFileFilter(new ImageFilter());
342 file_chooser.setSize(400,300);
343 GUIUtils.disableRename(file_chooser);
344 int value = file_chooser.showOpenDialog(Gatherer.g_man);
345 // If the user hasn't cancelled, retrieve the file path selected
346 if(value == JFileChooser.APPROVE_OPTION) {
347 // If the file isn't in the images folder, then ask the user if they want to copy it there
348 File file = file_chooser.getSelectedFile();
349 if (!file.getParentFile().equals(images_folder)) {
350 // Copy the file
351 try {
352 File collection_image_file = new File(images_folder, file.getName());
353 Gatherer.f_man.getQueue().copyFile(file, collection_image_file, true);
354
355 // If we're using a remote Greenstone server, upload the image
356 if (Gatherer.isGsdlRemote) {
357 RemoteGreenstoneServer.uploadCollectionFile(Gatherer.c_man.getCollection().getName(), collection_image_file);
358 }
359 }
360 catch(Exception exception) {
361 DebugStream.printStackTrace(exception);
362 // Show warning
363 String[] args = new String[] {file.getAbsolutePath(), images_folder.getAbsolutePath()};
364 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.General.Image_Copy_Failed", args), Dictionary.get("General.Error"), JOptionPane.ERROR_MESSAGE);
365 // Nothing else we can do.
366 return;
367 }
368 }
369
370 // Create the path starting _httpcollection_/images/<filename>
371 String path = prefix + file.getName();
372 if(event.getSource() == browse_about_icon_button) {
373 icon_textfield.setText(path);
374 }
375 else {
376 small_icon_textfield.setText(path);
377 }
378 path = null;
379 }
380 }
381
382 /** ImageFilter.java is a 1.4 example used by FileChooserDemo2.java. */
383 private class ImageFilter
384 extends javax.swing.filechooser.FileFilter {
385
386 private Pattern pattern = null;
387
388 public ImageFilter() {
389 pattern = Pattern.compile(".*\\.(gif|png|jpe?g)");
390 }
391
392 // Accept all directories and all .col files
393 public boolean accept(File f) {
394 String filename = f.getName().toLowerCase();
395 Matcher matcher = pattern.matcher(filename);
396 return f.isDirectory() || matcher.matches();
397 }
398
399 // The description of this filter
400 public String getDescription() {
401 return Dictionary.get("CDM.General.Image_Filter");
402 }
403 }
404 }
405
406 private class CollectionTitleUpdater
407 implements DocumentListener {
408 /** Gives notification that an attribute or set of attributes changed. */
409 public void changedUpdate(DocumentEvent e) {
410 setTitle();
411 }
412 /** Gives notification that there was an insert into the document. */
413 public void insertUpdate(DocumentEvent e) {
414 setTitle();
415 }
416 /** Gives notification that a portion of the document has been removed. */
417 public void removeUpdate(DocumentEvent e) {
418 setTitle();
419 }
420
421 private void setTitle() {
422 // Set the title
423 String collection_title = name_textfield.getText();
424 String collection_name = Gatherer.c_man.getCollection().getName();
425 Gatherer.g_man.setTitle(collection_title, collection_name);
426 collection_title = null;
427 collection_name = null;
428 }
429 }
430 }
431}
Note: See TracBrowser for help on using the repository browser.