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

Last change on this file since 12732 was 12732, checked in by kjdon, 18 years ago

MetadataSetTree now knows what its parent frame is, so the new element prompt can be modal

  • Property svn:keywords set to Author Date Id Revision
File size: 18.1 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 new GEMS(go.gsdl_path,go.metadata_path,true,go.new_set);
134 }
135
136
137 /** Constructor.
138 */
139 public GEMS(String gsdl_path, String metadata_path, boolean standalone, boolean new_set)
140 {
141 self = this;
142 JarTools.initialise(this);
143 screen_size = Configuration.screen_size;
144 msm = new MetadataSetManager(gsdl_path);
145 stand_alone = standalone;
146 listeners = new ArrayList();
147
148 addWindowListener(new WindowAdapter(){
149 public void windowClosing(WindowEvent e){
150 metadata_set_model.save(true);
151 if (stand_alone){
152 System.exit(0);
153 }
154 else {
155 notifyListeners();
156 setVisible(false);
157 }
158 }
159 });
160
161 setSize(SIZE);
162 setTitle(Dictionary.get("GEMS.Title"));
163 setJMenuBar(new GEMSMenuBar());
164
165 addWindowFocusListener(this);
166
167 card_layout = new CardLayout();
168 card_pane = new JPanel();
169
170 metadata_set_model = new MetadataSetModel(msm);
171 msm.setMetadataSetModel(metadata_set_model);
172 meta_set_tree = new MetadataSetTree(self);
173 metadata_set_model.addObserver(meta_set_tree);
174
175 attribute_table = new AttributeTable(false);
176 MetadataElementModel mem = new MetadataElementModel();
177 mem.addMetadataElementListener((MetadataElementListener)attribute_table);
178 attribute_table.addAttributeListener(metadata_set_model);
179
180 language_dependent_attribute_table = new AttributeTable(true);
181 mem.addMetadataElementListener((MetadataElementListener)language_dependent_attribute_table);
182 language_dependent_attribute_table.addAttributeListener(metadata_set_model);
183
184 MetadataSetInfo msti= new MetadataSetInfo();
185 msti.addMetadataSetListener((MetadataSetListener)attribute_table);
186 msti.addMetadataSetListener((MetadataSetListener)language_dependent_attribute_table);
187 msti.setMetadataSetModel(metadata_set_model);
188
189 open_prompt = new OpenMetadataSetPrompt(self,msm);
190 open_prompt.addMetadataSetListener((MetadataSetListener)metadata_set_model);
191 open_prompt.addMetadataSetListener((MetadataSetListener)attribute_table);
192 open_prompt.addMetadataSetListener((MetadataSetListener)language_dependent_attribute_table);
193
194 delete_prompt = new DeleteMetadataSetPrompt(self,msm);
195 delete_prompt.addMetadataSetListener((MetadataSetListener)metadata_set_model);
196 delete_prompt.addMetadataSetListener((MetadataSetListener)attribute_table);
197 delete_prompt.addMetadataSetListener((MetadataSetListener)language_dependent_attribute_table);
198
199 new_prompt = new NewMetadataSetPrompt(self,msm);
200 new_prompt.addMetadataSetListener((MetadataSetListener)metadata_set_model);
201 new_prompt.addMetadataSetListener((MetadataSetListener)attribute_table);
202 new_prompt.addMetadataSetListener((MetadataSetListener)language_dependent_attribute_table);
203
204
205 // load the initial metadataset
206 if (metadata_path !=null && !metadata_path.equals("")){
207 open_prompt.openMetadataSet(metadata_path);
208 }
209 else{
210 if (new_set) new_prompt.display();
211 }
212
213 // the set tree
214 JScrollPane treePane = new JScrollPane(meta_set_tree);
215 // the language independent attributes
216 JScrollPane tablePane = new JScrollPane(attribute_table);
217 // the language dependent attributes
218 JScrollPane langTablePane = new JScrollPane(language_dependent_attribute_table);
219
220 // no set loaded pane
221 JPanel no_set_loaded_pane = new JPanel();
222 no_set_loaded_pane.setBackground(Color.lightGray);
223 JLabel no_set_loaded_label = new JLabel(Dictionary.get("GEMS.No_Set_Loaded"));
224 no_set_loaded_label.setHorizontalAlignment(JLabel.CENTER);
225 no_set_loaded_label.setVerticalAlignment(JLabel.CENTER);
226
227 no_set_loaded_pane.setLayout(new BorderLayout());
228 no_set_loaded_pane.add(no_set_loaded_label, BorderLayout.CENTER);
229
230 JPanel buttonPane = new JPanel(new GridLayout(1,2));
231
232 JButton arrow_up_button = new GLIButton(Dictionary.get("GEMS.Move_Up"));
233 arrow_up_button.setIcon(JarTools.getImage("arrow-up.gif"));
234
235 JButton arrow_down_button = new GLIButton(Dictionary.get("GEMS.Move_Down"));
236 arrow_down_button.setIcon(JarTools.getImage("arrow-down.gif"));
237
238 arrow_up_button.setActionCommand(GEMSConstants.MOVE_UP);
239 arrow_up_button.addActionListener(meta_set_tree);
240 arrow_down_button.addActionListener(meta_set_tree);
241 arrow_down_button.setActionCommand(GEMSConstants.MOVE_DOWN);
242
243 buttonPane.add(arrow_up_button);
244 buttonPane.add(arrow_down_button);
245
246 JPanel leftPane = new JPanel(new BorderLayout());
247 leftPane.add(treePane,BorderLayout.CENTER);
248 leftPane.add(buttonPane,BorderLayout.SOUTH);
249
250 JLabel selected_language_label = new JLabel(Dictionary.get("GEMS.SelectedLanguage"));
251 selected_language_label.setOpaque(true);
252
253 Vector language_vector = new Vector(msm.getLanguageList());
254 language_vector.add(0, Dictionary.get("GEMS.Language"));
255 JComboBox language_combo = new JComboBox(language_vector);
256
257 language_combo.addActionListener(new ActionListener(){
258 public void actionPerformed(ActionEvent e){
259 JComboBox language_combo = (JComboBox) e.getSource();
260 String langInfo = (String)language_combo.getSelectedItem();
261 if (!langInfo.equals(Dictionary.get("GEMS.Language"))) {
262 String lang = langInfo.split("\\s")[0];
263 language_dependent_attribute_table.addNewLanguage(lang);
264 }
265 }
266
267 });
268
269 JLabel language_label = new JLabel(Dictionary.get("GEMS.LanguageDependent"));
270 language_label.setOpaque(true);
271
272
273 JPanel selectLangPane = new JPanel(new BorderLayout(5,5));
274 selectLangPane.add(selected_language_label,BorderLayout.WEST);
275 selectLangPane.add(language_combo, BorderLayout.CENTER);
276
277
278 JPanel languagePane = new JPanel(new BorderLayout(5,5));
279 languagePane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
280 languagePane.add(language_label,BorderLayout.NORTH);
281 languagePane.add(selectLangPane,BorderLayout.CENTER);
282
283
284 JPanel languageAttributePane = new JPanel(new BorderLayout());
285 languageAttributePane.add(languagePane,BorderLayout.NORTH);
286 languageAttributePane.add(langTablePane,BorderLayout.CENTER);
287
288 JLabel attribute_label = new JLabel(Dictionary.get("GEMS.Attribute_Table"));
289 attribute_label.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
290 JPanel mainAttributePane = new JPanel(new BorderLayout());
291 mainAttributePane.add(attribute_label, BorderLayout.NORTH);
292 mainAttributePane.add(tablePane, BorderLayout.CENTER);
293
294 attribute_tables_split_pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,mainAttributePane,languageAttributePane);
295
296 metadata_set_details_split_pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPane,attribute_tables_split_pane);
297
298 card_pane.setLayout(card_layout);
299 card_pane.add(no_set_loaded_pane, NO_SET_LOADED_CARD);
300 card_pane.add(metadata_set_details_split_pane, SET_LOADED_CARD);
301
302 getContentPane().add(card_pane,BorderLayout.CENTER);
303 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
304 if (stand_alone)
305 setVisible(true);
306 }
307
308 public void setVisible(boolean visible) {
309 super.setVisible(visible);
310 attribute_tables_split_pane.setDividerLocation(0.3);
311 }
312
313 // called from GLI
314 public void displayMetadataSet(String metadata_path){
315 open_prompt.openMetadataSet(metadata_path);
316 updateCardLayout(true);
317 setVisible(true);
318 }
319
320 // called from GLI
321 public void newMetadataSet(){
322 new_prompt.display();
323 if (!new_prompt.isCancelled()) {
324 updateCardLayout(true);
325 setVisible(true);
326 }
327
328 }
329
330 public void addGEMSListener(GEMSListener listener){
331 listeners.add(listener);
332 }
333
334 public void removeGEMSListener(GEMSListener listener){
335 listeners.remove(listener);
336 }
337
338 public void notifyListeners(){
339 for (int i=0;i<listeners.size();i++){
340 GEMSListener listener = (GEMSListener) listeners.get(i);
341 listener.gemsIsClosed();
342 }
343 }
344
345
346 public void exit(){
347 //clean up things
348 cleanUpListeners();
349 }
350
351 protected void cleanUpListeners() {
352 MetadataSetInfo msti= new MetadataSetInfo();
353 msti.removeAllMetadataSetListeners();
354 MetadataElementModel mem= new MetadataElementModel();
355 mem.removeAllMetadataElementListeners();
356
357 }
358
359 public void windowGainedFocus(WindowEvent e) {
360 //System.out.println("windowGainedFocus.");
361 }
362
363 public void windowLostFocus(WindowEvent e) {
364 //System.out.println("windowLostFocus.");
365
366 //save stuff
367
368 }
369
370 protected void updateCardLayout(boolean set_loaded) {
371 if (set_loaded) {
372 card_layout.show(card_pane, SET_LOADED_CARD);
373 } else {
374 card_layout.show(card_pane, NO_SET_LOADED_CARD);
375 }
376 }
377
378 private class GEMSMenuBar
379 extends JMenuBar
380 implements ActionListener
381 {
382 private JMenu file = null;
383 private JMenu edit = null;
384
385 public JMenuItem file_exit = null;
386 public JMenuItem file_new = null;
387 public JMenuItem file_open = null;
388 public JMenuItem file_close = null;
389 public JMenuItem file_save = null;
390 public JMenuItem edit_copy = null;
391 public JMenuItem edit_cut = null;
392 public JMenuItem edit_paste = null;
393 public JMenuItem help_help = null;
394 public JMenuItem file_delete = null;
395 //public JMenuItem file_preferences = null;
396 public GEMSMenuBar()
397 {
398 file = new JMenu();
399 file.setText(Dictionary.get("Menu.File"));
400
401 file_exit = new JMenuItem(Dictionary.get("Menu.File_Exit"));
402 file_exit.addActionListener(this);
403
404 file_new = new JMenuItem(Dictionary.get("Menu.File_New"));
405 file_new.addActionListener(this);
406
407 file_open = new JMenuItem(Dictionary.get("Menu.File_Open"));
408 file_open.addActionListener(this);
409
410 file_close = new JMenuItem(Dictionary.get("Menu.File_Close"));
411 file_close.addActionListener(this);
412
413 file_save = new JMenuItem(Dictionary.get("Menu.File_Save"));
414 file_save.addActionListener(this);
415
416 file_delete = new JMenuItem(Dictionary.get("Menu.File_Delete"));
417 file_delete.addActionListener(this);
418
419 //file_preferences = new JMenuItem(Dictionary.get("Menu.File_Options"));
420 //file_preferences.addActionListener(this);
421
422 // Layout (file menu)
423 file.add(file_new);
424 file.add(file_open);
425 file.add(file_close);
426 file.add(file_save);
427 file.add(file_delete);
428 file.add(new JSeparator());
429 //file.add(file_preferences);
430 //file.add(new JSeparator());
431 file.add(file_exit);
432
433 // Edit menu
434 edit = new JMenu();
435 edit.setText(Dictionary.get("Menu.Edit"));
436
437 edit_cut = new JMenuItem(Dictionary.get("Menu.Edit_Cut"));
438 edit_cut.addActionListener(this);
439
440 edit_copy = new JMenuItem(Dictionary.get("Menu.Edit_Copy"));
441 edit_copy.addActionListener(this);
442
443 edit_paste = new JMenuItem(Dictionary.get("Menu.Edit_Paste"));
444 edit_paste.addActionListener(this);
445
446 // Layout (edit menu)
447 edit.add(edit_cut);
448 edit.add(edit_copy);
449 edit.add(edit_paste);
450
451
452 // Layout (menu bar)
453 this.add(file);
454 this.add(Box.createHorizontalStrut(15));
455 this.add(edit);
456 this.add(Box.createHorizontalGlue());
457 //this.add(help);
458 }
459
460
461 public void actionPerformed(ActionEvent event)
462 {
463 Object event_source = event.getSource();
464
465 // File -> New
466 if (event_source == file_new) {
467 new_prompt.display();
468 if (!new_prompt.isCancelled()) {
469 updateCardLayout(true);
470 }
471 return;
472 }
473
474 // File -> Open
475 if (event_source == file_open) {
476 open_prompt.display();
477 if (!open_prompt.isCancelled()) {
478 updateCardLayout(true);
479 }
480 return;
481 }
482
483 // File -> Close
484 if (event_source == file_close) {
485 metadata_set_model.save(true);
486 updateCardLayout(false);
487 return;
488 }
489 // File -> Delete
490 if (event_source == file_delete) {
491 delete_prompt.display();
492 return;
493 }
494
495
496 // File -> Save
497 if (event_source == file_save) {
498 metadata_set_model.save(false);
499 return;
500 }
501
502 // File -> Exit
503 if (event_source == file_exit) {
504 metadata_set_model.save(true);
505 if (stand_alone){
506 System.exit(0);
507 }
508 else{
509 notifyListeners();
510 setVisible(false);
511 }
512 }
513
514 /// File -> Preferences
515 // if(event_source == file_preferences){
516 // GEMSPreferences GemsPreferences = new GEMSPreferences();
517 //}
518
519 // Edit -> Cut
520 if (event_source == edit_cut) {
521 try {
522 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
523 // Get the component with selected text as a JTextComponent
524 JTextComponent text = (JTextComponent) kfm.getPermanentFocusOwner();
525 // Cut the text to the clipboard
526 text.cut();
527 }
528 catch (ClassCastException cce) {
529 // If the component is not a text component ignore the cut command
530 DebugStream.println(cce.toString());
531 }
532 return;
533 }
534
535 // Edit -> Copy
536 if (event_source == edit_copy) {
537 try {
538 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
539 // Get the component with selected text as a JTextComponent
540 JTextComponent text = (JTextComponent) kfm.getPermanentFocusOwner();//getFocusOwner();
541 text.copy();
542 }
543 catch (Exception cce) {
544 // If the component is not a text component ignore the copy command
545 DebugStream.println(cce.toString());
546 }
547 return;
548 }
549
550 // Edit -> Paste
551 if (event_source == edit_paste) {
552 try {
553 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
554 // Get the component with selected text as a JTextComponent
555 JTextComponent text = (JTextComponent) kfm.getPermanentFocusOwner();
556 // Cut the text to the clipboard
557 text.paste();
558 }
559 catch (ClassCastException cce) {
560 // If the component is not a text component ignore the paste command
561 DebugStream.println(cce.toString());
562 }
563 return;
564 }
565
566 }
567 }
568
569}
Note: See TracBrowser for help on using the repository browser.