source: main/trunk/gli/src/org/greenstone/gatherer/gems/GEMS.java@ 32276

Last change on this file since 32276 was 32276, checked in by ak19, 6 years ago

Fixed a bug discovered when trying to get screenshots for Dr Nichols. I thought he needed screenshots of GEMS in French/any translated non-English language. When attempting to make the screenshots, I found I wasn't unable to launch GEMS in any other language than English. I found the infrastructure was there (strings were loaded from the Dictionary object, and GEMS strings had been translated into various languages), however, in the Dictionary object for GEMS was never initialised to use the specified locale. Fixed it now.

  • Property svn:keywords set to Author Date Id Revision
File size: 19.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 th * University of Waikato, New Zealand.
6 *
7 * <BR><BR>
8 *
9 * Author: Shaoqun Wu, Greenstone Digital Library, University of Waikato
10 *
11 * <BR><BR>
12 *
13 * Copyright (C) 2006 New Zealand Digital Library Project
14 *
15 * <BR><BR>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * <BR><BR>
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * <BR><BR>
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 *############################################## ##########################
35 */
36package org.greenstone.gatherer.gems;
37
38import java.awt.*;
39import java.awt.event.*;
40import java.util.*;
41import javax.swing.*;
42import java.lang.String;
43import javax.swing.event.*;
44import javax.swing.filechooser.*;
45import javax.swing.text.*;
46import javax.swing.tree.*;
47import java.awt.*;
48import java.awt.event.*;
49
50import java.io.File;
51
52import org.greenstone.gatherer.Configuration;
53import org.greenstone.gatherer.DebugStream;
54import org.greenstone.gatherer.Dictionary;
55import org.greenstone.gatherer.GetOpt;
56import org.greenstone.gatherer.gui.GComboBox;
57import org.greenstone.gatherer.gui.GLIButton;
58import org.greenstone.gatherer.gui.HelpFrame;
59import org.greenstone.gatherer.gui.ModalDialog;
60import org.greenstone.gatherer.gui.NonWhitespaceField;
61import org.greenstone.gatherer.util.Codec;
62import org.greenstone.gatherer.util.JarTools;
63import org.greenstone.gatherer.util.Utility;
64import org.greenstone.gatherer.util.XMLTools;
65import org.greenstone.gatherer.Dictionary;
66import org.w3c.dom.*;
67
68
69/*
70 * @author Shaoqun Wu, Greenstone Digital Library, University of Waikato
71 * @version 2.4
72 */
73public class GEMS
74 extends JFrame implements WindowFocusListener
75{
76
77 static final private Dimension SIZE = new Dimension(800,550);
78
79 // we have a card pane to switch between the no set loaded view, and the
80 // set loaded view
81 private CardLayout card_layout = null;
82 private JPanel card_pane = null;
83 /** The name of the panel containing the "set loaded" card. */
84 private String SET_LOADED_CARD = "";
85 /** The name of the panel containing the "no set loaded" placeholder */
86 private String NO_SET_LOADED_CARD = "No set loaded";
87
88 private GEMS self = null;
89
90 private OpenMetadataSetPrompt open_prompt;
91 private DeleteMetadataSetPrompt delete_prompt;
92 private NewMetadataSetPrompt new_prompt;
93
94 // this is the main panel that gets switched in once a set is loaded
95 private JSplitPane metadata_set_details_split_pane = null;
96 // this is the split with the attribute tables in it
97 private JSplitPane attribute_tables_split_pane = null;
98
99 /** Um, the size of the screen I'd guess. */
100 private Dimension screen_size = null;
101
102 private MetadataSetManager msm;
103
104 private AttributeTable attribute_table; // meta_element_table
105 private AttributeTable language_dependent_attribute_table; //lang_element_table;
106
107 private MetadataSetTree meta_set_tree;
108
109 private MetadataSetModel metadata_set_model;
110
111 private boolean stand_alone = true;
112
113 private ArrayList listeners;
114
115 static public void main(String[] args)
116 {
117 //TODO: add an option to open a paticular metadata set
118
119 // Parse arguments
120 GetOpt go = new GetOpt(args);
121
122 if (go.debug) {
123 DebugStream.enableDebugging();
124
125 Calendar now = Calendar.getInstance();
126 String debug_file_path = "debug" + now.get(Calendar.DATE) + "-" + now.get(Calendar.MONTH) + "-" + now.get(Calendar.YEAR) + ".txt";
127
128 // Debug file is created in the GLI directory
129 DebugStream.println("Debug file path: " + debug_file_path);
130 DebugStream.setDebugFile(debug_file_path);
131
132 }
133
134 new GEMS(go.gsdl_path,go.gsdl3_path,go.metadata_path,true,go.new_set);
135
136 }
137
138
139 /** Constructor.
140 */
141 public GEMS(String gsdl_path, String gsdl3_path, String metadata_path, boolean standalone, boolean new_set)
142 {
143 self = this;
144 JarTools.initialise(this);
145 screen_size = Configuration.screen_size;
146
147 // Load GLI config file: set up the Configuration so we can set up the Dictionary language
148 // Determine the GLI user directory path
149 String gli_user_directory_path = System.getProperty("user.home") + File.separator;
150 if (Utility.isWindows()) {
151 gli_user_directory_path += "Application Data" + File.separator + "Greenstone" + File.separator + "GLI" + File.separator;
152 }
153 else {
154 gli_user_directory_path += ".gli" + File.separator;
155 }
156 new Configuration(gli_user_directory_path, gsdl_path, gsdl3_path,
157 null /*gsdl3_src_path*/, null /*site_name*/, null /*fedora_info*/); // null for parameters unknown to GEMS
158
159 // Read Dictionary in the locale specified in the config.xml
160 new Dictionary(Configuration.getLocale("general.locale", true), Configuration.getFont("general.font", true));
161
162 msm = new MetadataSetManager(gsdl_path,gsdl3_path);
163 stand_alone = standalone;
164 listeners = new ArrayList();
165
166 addWindowListener(new WindowAdapter(){
167 public void windowClosing(WindowEvent e){
168 metadata_set_model.save(true);
169 if (stand_alone){
170 System.exit(0);
171 }
172 else {
173 notifyListeners();
174 setVisible(false);
175 }
176 }
177 });
178
179 setSize(SIZE);
180 setTitle(Dictionary.get("GEMS.Title"));
181 setJMenuBar(new GEMSMenuBar());
182
183 addWindowFocusListener(this);
184
185 card_layout = new CardLayout();
186 card_pane = new JPanel();
187
188 metadata_set_model = new MetadataSetModel(msm);
189 msm.setMetadataSetModel(metadata_set_model);
190 meta_set_tree = new MetadataSetTree(self);
191 metadata_set_model.addObserver(meta_set_tree);
192
193 attribute_table = new AttributeTable(false);
194 MetadataElementModel mem = new MetadataElementModel();
195 mem.addMetadataElementListener((MetadataElementListener)attribute_table);
196 attribute_table.addAttributeListener(metadata_set_model);
197
198 language_dependent_attribute_table = new AttributeTable(true);
199 mem.addMetadataElementListener((MetadataElementListener)language_dependent_attribute_table);
200 language_dependent_attribute_table.addAttributeListener(metadata_set_model);
201
202 MetadataSetInfo msti= new MetadataSetInfo();
203 msti.addMetadataSetListener((MetadataSetListener)attribute_table);
204 msti.addMetadataSetListener((MetadataSetListener)language_dependent_attribute_table);
205 msti.setMetadataSetModel(metadata_set_model);
206
207 open_prompt = new OpenMetadataSetPrompt(self,msm);
208 open_prompt.addMetadataSetListener((MetadataSetListener)metadata_set_model);
209 open_prompt.addMetadataSetListener((MetadataSetListener)attribute_table);
210 open_prompt.addMetadataSetListener((MetadataSetListener)language_dependent_attribute_table);
211
212 delete_prompt = new DeleteMetadataSetPrompt(self,msm);
213 delete_prompt.addMetadataSetListener((MetadataSetListener)metadata_set_model);
214 delete_prompt.addMetadataSetListener((MetadataSetListener)attribute_table);
215 delete_prompt.addMetadataSetListener((MetadataSetListener)language_dependent_attribute_table);
216
217 new_prompt = new NewMetadataSetPrompt(self,msm);
218 new_prompt.addMetadataSetListener((MetadataSetListener)metadata_set_model);
219 new_prompt.addMetadataSetListener((MetadataSetListener)attribute_table);
220 new_prompt.addMetadataSetListener((MetadataSetListener)language_dependent_attribute_table);
221
222
223 // load the initial metadataset
224 if (metadata_path !=null && !metadata_path.equals("")){
225 open_prompt.openMetadataSet(metadata_path);
226 }
227 else{
228 if (new_set) new_prompt.display();
229 }
230
231 // the set tree
232 JScrollPane treePane = new JScrollPane(meta_set_tree);
233 // the language independent attributes
234 JScrollPane tablePane = new JScrollPane(attribute_table);
235 // the language dependent attributes
236 JScrollPane langTablePane = new JScrollPane(language_dependent_attribute_table);
237
238 // no set loaded pane
239 JPanel no_set_loaded_pane = new JPanel();
240 no_set_loaded_pane.setBackground(Color.lightGray);
241 JLabel no_set_loaded_label = new JLabel(Dictionary.get("GEMS.No_Set_Loaded"));
242 no_set_loaded_label.setHorizontalAlignment(JLabel.CENTER);
243 no_set_loaded_label.setVerticalAlignment(JLabel.CENTER);
244
245 no_set_loaded_pane.setLayout(new BorderLayout());
246 no_set_loaded_pane.add(no_set_loaded_label, BorderLayout.CENTER);
247
248 JPanel buttonPane = new JPanel(new GridLayout(1,2));
249
250 JButton arrow_up_button = new GLIButton(Dictionary.get("GEMS.Move_Up"));
251 arrow_up_button.setIcon(JarTools.getImage("arrow-up.gif"));
252
253 JButton arrow_down_button = new GLIButton(Dictionary.get("GEMS.Move_Down"));
254 arrow_down_button.setIcon(JarTools.getImage("arrow-down.gif"));
255
256 arrow_up_button.setActionCommand(GEMSConstants.MOVE_UP);
257 arrow_up_button.addActionListener(meta_set_tree);
258 arrow_down_button.addActionListener(meta_set_tree);
259 arrow_down_button.setActionCommand(GEMSConstants.MOVE_DOWN);
260
261 buttonPane.add(arrow_up_button);
262 buttonPane.add(arrow_down_button);
263
264 JPanel leftPane = new JPanel(new BorderLayout());
265 leftPane.add(treePane,BorderLayout.CENTER);
266 leftPane.add(buttonPane,BorderLayout.SOUTH);
267
268 JLabel selected_language_label = new JLabel(Dictionary.get("GEMS.SelectedLanguage"));
269 selected_language_label.setOpaque(true);
270
271 Vector language_vector = new Vector(msm.getLanguageList());
272 language_vector.add(0, Dictionary.get("GEMS.Language"));
273 JComboBox language_combo = new JComboBox(language_vector);
274
275 language_combo.addActionListener(new ActionListener(){
276 public void actionPerformed(ActionEvent e){
277 JComboBox language_combo = (JComboBox) e.getSource();
278 String langInfo = (String)language_combo.getSelectedItem();
279 if (!langInfo.equals(Dictionary.get("GEMS.Language"))) {
280 String lang = langInfo.split("\\s")[0];
281 language_dependent_attribute_table.addNewLanguage(lang);
282 }
283 }
284
285 });
286
287 JLabel language_label = new JLabel(Dictionary.get("GEMS.LanguageDependent"));
288 language_label.setOpaque(true);
289
290
291 JPanel selectLangPane = new JPanel(new BorderLayout(5,5));
292 selectLangPane.add(selected_language_label,BorderLayout.WEST);
293 selectLangPane.add(language_combo, BorderLayout.CENTER);
294
295
296 JPanel languagePane = new JPanel(new BorderLayout(5,5));
297 languagePane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
298 languagePane.add(language_label,BorderLayout.NORTH);
299 languagePane.add(selectLangPane,BorderLayout.CENTER);
300
301
302 JPanel languageAttributePane = new JPanel(new BorderLayout());
303 languageAttributePane.add(languagePane,BorderLayout.NORTH);
304 languageAttributePane.add(langTablePane,BorderLayout.CENTER);
305
306 JLabel attribute_label = new JLabel(Dictionary.get("GEMS.Attribute_Table"));
307 attribute_label.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
308 JPanel mainAttributePane = new JPanel(new BorderLayout());
309 mainAttributePane.add(attribute_label, BorderLayout.NORTH);
310 mainAttributePane.add(tablePane, BorderLayout.CENTER);
311
312 attribute_tables_split_pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,mainAttributePane,languageAttributePane);
313
314 metadata_set_details_split_pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPane,attribute_tables_split_pane);
315
316 card_pane.setLayout(card_layout);
317 card_pane.add(no_set_loaded_pane, NO_SET_LOADED_CARD);
318 card_pane.add(metadata_set_details_split_pane, SET_LOADED_CARD);
319
320 getContentPane().add(card_pane,BorderLayout.CENTER);
321 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
322 if (stand_alone)
323 setVisible(true);
324 }
325
326 public void setVisible(boolean visible) {
327 super.setVisible(visible);
328 if (visible) {
329 attribute_tables_split_pane.setDividerLocation(0.3);
330 }
331 }
332
333 // called from GLI
334 public void displayMetadataSet(String metadata_path){
335 open_prompt.openMetadataSet(metadata_path);
336 updateCardLayout(true);
337 setVisible(true);
338 }
339
340 // called from GLI
341 public void newMetadataSet(){
342 new_prompt.display();
343 if (!new_prompt.isCancelled()) {
344 updateCardLayout(true);
345 setVisible(true);
346 }
347
348 }
349
350 public void addGEMSListener(GEMSListener listener){
351 listeners.add(listener);
352 }
353
354 public void removeGEMSListener(GEMSListener listener){
355 listeners.remove(listener);
356 }
357
358 public void notifyListeners(){
359 for (int i=0;i<listeners.size();i++){
360 GEMSListener listener = (GEMSListener) listeners.get(i);
361 listener.gemsIsClosed();
362 }
363 }
364
365
366 public void exit(){
367 //clean up things
368 cleanUpListeners();
369 }
370
371 protected void cleanUpListeners() {
372 MetadataSetInfo msti= new MetadataSetInfo();
373 msti.removeAllMetadataSetListeners();
374 MetadataElementModel mem= new MetadataElementModel();
375 mem.removeAllMetadataElementListeners();
376
377 }
378
379 public void windowGainedFocus(WindowEvent e) {
380 //System.out.println("windowGainedFocus.");
381 }
382
383 public void windowLostFocus(WindowEvent e) {
384 //System.out.println("windowLostFocus.");
385
386 //save stuff
387
388 }
389
390 protected void updateCardLayout(boolean set_loaded) {
391 if (set_loaded) {
392 card_layout.show(card_pane, SET_LOADED_CARD);
393 } else {
394 card_layout.show(card_pane, NO_SET_LOADED_CARD);
395 }
396 }
397
398 private class GEMSMenuBar
399 extends JMenuBar
400 implements ActionListener
401 {
402 private JMenu file = null;
403 private JMenu edit = null;
404
405 public JMenuItem file_exit = null;
406 public JMenuItem file_new = null;
407 public JMenuItem file_open = null;
408 public JMenuItem file_close = null;
409 public JMenuItem file_save = null;
410 public JMenuItem edit_copy = null;
411 public JMenuItem edit_cut = null;
412 public JMenuItem edit_paste = null;
413 public JMenuItem help_help = null;
414 public JMenuItem file_delete = null;
415 //public JMenuItem file_preferences = null;
416 public GEMSMenuBar()
417 {
418 file = new JMenu();
419 file.setText(Dictionary.get("Menu.File"));
420
421 file_exit = new JMenuItem(Dictionary.get("Menu.File_Exit"));
422 file_exit.addActionListener(this);
423
424 file_new = new JMenuItem(Dictionary.get("Menu.File_New"));
425 file_new.addActionListener(this);
426
427 file_open = new JMenuItem(Dictionary.get("Menu.File_Open"));
428 file_open.addActionListener(this);
429
430 file_close = new JMenuItem(Dictionary.get("Menu.File_Close"));
431 file_close.addActionListener(this);
432
433 file_save = new JMenuItem(Dictionary.get("Menu.File_Save"));
434 file_save.addActionListener(this);
435
436 file_delete = new JMenuItem(Dictionary.get("Menu.File_Delete"));
437 file_delete.addActionListener(this);
438
439 //file_preferences = new JMenuItem(Dictionary.get("Menu.File_Options"));
440 //file_preferences.addActionListener(this);
441
442 if (!stand_alone) {
443 // when running from GLI, we disable most of the menu so
444 // that we have control over what sets the user is editing
445 file_new.setEnabled(false);
446 file_open.setEnabled(false);
447 file_close.setEnabled(false);
448 file_delete.setEnabled(false);
449 }
450
451 // Layout (file menu)
452 file.add(file_new);
453 file.add(file_open);
454 file.add(file_close);
455 file.add(file_save);
456 file.add(file_delete);
457 file.add(new JSeparator());
458 //file.add(file_preferences);
459 //file.add(new JSeparator());
460 file.add(file_exit);
461
462 // Edit menu
463 edit = new JMenu();
464 edit.setText(Dictionary.get("Menu.Edit"));
465
466 String modkey = "ctrl";
467 if(Utility.isMac()) { // on Mac, we now use the Apple key instead of Ctrl for GLI/GEMS edit shortcuts
468 // http://stackoverflow.com/questions/5585919/creating-unicode-character-from-its-number
469 char appleKeyCodepoint = 0x2318; // applekey symbol unicode codepoint: U+2318 (\u2318)
470 String appleKeySymbol = String.valueOf(appleKeyCodepoint);
471 modkey = appleKeySymbol;
472 }
473
474 edit_cut = new JMenuItem(Dictionary.get("Menu.Edit_Cut", modkey));
475 edit_cut.addActionListener(this);
476
477 edit_copy = new JMenuItem(Dictionary.get("Menu.Edit_Copy", modkey));
478 edit_copy.addActionListener(this);
479
480 edit_paste = new JMenuItem(Dictionary.get("Menu.Edit_Paste", modkey));
481 edit_paste.addActionListener(this);
482
483 // Layout (edit menu)
484 edit.add(edit_cut);
485 edit.add(edit_copy);
486 edit.add(edit_paste);
487
488
489 // Layout (menu bar)
490 this.add(file);
491 this.add(Box.createHorizontalStrut(15));
492 this.add(edit);
493 this.add(Box.createHorizontalGlue());
494 //this.add(help);
495 }
496
497
498 public void actionPerformed(ActionEvent event)
499 {
500 Object event_source = event.getSource();
501
502 // File -> New
503 if (event_source == file_new) {
504 new_prompt.display();
505 if (!new_prompt.isCancelled()) {
506 updateCardLayout(true);
507 }
508 return;
509 }
510
511 // File -> Open
512 if (event_source == file_open) {
513 open_prompt.display();
514 if (!open_prompt.isCancelled()) {
515 updateCardLayout(true);
516 }
517 return;
518 }
519
520 // File -> Close
521 if (event_source == file_close) {
522 metadata_set_model.save(true);
523 updateCardLayout(false);
524 return;
525 }
526 // File -> Delete
527 if (event_source == file_delete) {
528 delete_prompt.display();
529 return;
530 }
531
532
533 // File -> Save
534 if (event_source == file_save) {
535 metadata_set_model.save(false);
536 return;
537 }
538
539 // File -> Exit
540 if (event_source == file_exit) {
541 metadata_set_model.save(true);
542 if (stand_alone){
543 System.exit(0);
544 }
545 else {
546 self.notifyListeners();
547 self.setVisible(false);
548 return;
549 }
550 }
551
552 /// File -> Preferences
553 // if(event_source == file_preferences){
554 // GEMSPreferences GemsPreferences = new GEMSPreferences();
555 //}
556
557 // Edit -> Cut
558 if (event_source == edit_cut) {
559 try {
560 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
561 // Get the component with selected text as a JTextComponent
562 JTextComponent text = (JTextComponent) kfm.getPermanentFocusOwner();
563 // Cut the text to the clipboard
564 text.cut();
565 }
566 catch (ClassCastException cce) {
567 // If the component is not a text component ignore the cut command
568 DebugStream.println(cce.toString());
569 }
570 return;
571 }
572
573 // Edit -> Copy
574 if (event_source == edit_copy) {
575 try {
576 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
577 // Get the component with selected text as a JTextComponent
578 JTextComponent text = (JTextComponent) kfm.getPermanentFocusOwner();//getFocusOwner();
579 text.copy();
580 }
581 catch (Exception cce) {
582 // If the component is not a text component ignore the copy command
583 DebugStream.println(cce.toString());
584 }
585 return;
586 }
587
588 // Edit -> Paste
589 if (event_source == edit_paste) {
590 try {
591 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
592 // Get the component with selected text as a JTextComponent
593 JTextComponent text = (JTextComponent) kfm.getPermanentFocusOwner();
594 // Cut the text to the clipboard
595 text.paste();
596 }
597 catch (ClassCastException cce) {
598 // If the component is not a text component ignore the paste command
599 DebugStream.println(cce.toString());
600 }
601 return;
602 }
603
604 }
605 }
606
607}
Note: See TracBrowser for help on using the repository browser.