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

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

when run from GLI, we disable most of the menu so hat we have control over what sets the user is editing

  • Property svn:keywords set to Author Date Id Revision
File size: 18.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 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 if (visible) {
311 attribute_tables_split_pane.setDividerLocation(0.3);
312 }
313 }
314
315 // called from GLI
316 public void displayMetadataSet(String metadata_path){
317 open_prompt.openMetadataSet(metadata_path);
318 updateCardLayout(true);
319 setVisible(true);
320 }
321
322 // called from GLI
323 public void newMetadataSet(){
324 new_prompt.display();
325 if (!new_prompt.isCancelled()) {
326 updateCardLayout(true);
327 setVisible(true);
328 }
329
330 }
331
332 public void addGEMSListener(GEMSListener listener){
333 listeners.add(listener);
334 }
335
336 public void removeGEMSListener(GEMSListener listener){
337 listeners.remove(listener);
338 }
339
340 public void notifyListeners(){
341 for (int i=0;i<listeners.size();i++){
342 GEMSListener listener = (GEMSListener) listeners.get(i);
343 listener.gemsIsClosed();
344 }
345 }
346
347
348 public void exit(){
349 //clean up things
350 cleanUpListeners();
351 }
352
353 protected void cleanUpListeners() {
354 MetadataSetInfo msti= new MetadataSetInfo();
355 msti.removeAllMetadataSetListeners();
356 MetadataElementModel mem= new MetadataElementModel();
357 mem.removeAllMetadataElementListeners();
358
359 }
360
361 public void windowGainedFocus(WindowEvent e) {
362 //System.out.println("windowGainedFocus.");
363 }
364
365 public void windowLostFocus(WindowEvent e) {
366 //System.out.println("windowLostFocus.");
367
368 //save stuff
369
370 }
371
372 protected void updateCardLayout(boolean set_loaded) {
373 if (set_loaded) {
374 card_layout.show(card_pane, SET_LOADED_CARD);
375 } else {
376 card_layout.show(card_pane, NO_SET_LOADED_CARD);
377 }
378 }
379
380 private class GEMSMenuBar
381 extends JMenuBar
382 implements ActionListener
383 {
384 private JMenu file = null;
385 private JMenu edit = null;
386
387 public JMenuItem file_exit = null;
388 public JMenuItem file_new = null;
389 public JMenuItem file_open = null;
390 public JMenuItem file_close = null;
391 public JMenuItem file_save = null;
392 public JMenuItem edit_copy = null;
393 public JMenuItem edit_cut = null;
394 public JMenuItem edit_paste = null;
395 public JMenuItem help_help = null;
396 public JMenuItem file_delete = null;
397 //public JMenuItem file_preferences = null;
398 public GEMSMenuBar()
399 {
400 file = new JMenu();
401 file.setText(Dictionary.get("Menu.File"));
402
403 file_exit = new JMenuItem(Dictionary.get("Menu.File_Exit"));
404 file_exit.addActionListener(this);
405
406 file_new = new JMenuItem(Dictionary.get("Menu.File_New"));
407 file_new.addActionListener(this);
408
409 file_open = new JMenuItem(Dictionary.get("Menu.File_Open"));
410 file_open.addActionListener(this);
411
412 file_close = new JMenuItem(Dictionary.get("Menu.File_Close"));
413 file_close.addActionListener(this);
414
415 file_save = new JMenuItem(Dictionary.get("Menu.File_Save"));
416 file_save.addActionListener(this);
417
418 file_delete = new JMenuItem(Dictionary.get("Menu.File_Delete"));
419 file_delete.addActionListener(this);
420
421 //file_preferences = new JMenuItem(Dictionary.get("Menu.File_Options"));
422 //file_preferences.addActionListener(this);
423
424 if (!stand_alone) {
425 // when running from GLI, we disable most of the menu so
426 // that we have control over what sets the user is editing
427 file_new.setEnabled(false);
428 file_open.setEnabled(false);
429 file_close.setEnabled(false);
430 file_delete.setEnabled(false);
431 }
432
433 // Layout (file menu)
434 file.add(file_new);
435 file.add(file_open);
436 file.add(file_close);
437 file.add(file_save);
438 file.add(file_delete);
439 file.add(new JSeparator());
440 //file.add(file_preferences);
441 //file.add(new JSeparator());
442 file.add(file_exit);
443
444 // Edit menu
445 edit = new JMenu();
446 edit.setText(Dictionary.get("Menu.Edit"));
447
448 edit_cut = new JMenuItem(Dictionary.get("Menu.Edit_Cut"));
449 edit_cut.addActionListener(this);
450
451 edit_copy = new JMenuItem(Dictionary.get("Menu.Edit_Copy"));
452 edit_copy.addActionListener(this);
453
454 edit_paste = new JMenuItem(Dictionary.get("Menu.Edit_Paste"));
455 edit_paste.addActionListener(this);
456
457 // Layout (edit menu)
458 edit.add(edit_cut);
459 edit.add(edit_copy);
460 edit.add(edit_paste);
461
462
463 // Layout (menu bar)
464 this.add(file);
465 this.add(Box.createHorizontalStrut(15));
466 this.add(edit);
467 this.add(Box.createHorizontalGlue());
468 //this.add(help);
469 }
470
471
472 public void actionPerformed(ActionEvent event)
473 {
474 Object event_source = event.getSource();
475
476 // File -> New
477 if (event_source == file_new) {
478 new_prompt.display();
479 if (!new_prompt.isCancelled()) {
480 updateCardLayout(true);
481 }
482 return;
483 }
484
485 // File -> Open
486 if (event_source == file_open) {
487 open_prompt.display();
488 if (!open_prompt.isCancelled()) {
489 updateCardLayout(true);
490 }
491 return;
492 }
493
494 // File -> Close
495 if (event_source == file_close) {
496 metadata_set_model.save(true);
497 updateCardLayout(false);
498 return;
499 }
500 // File -> Delete
501 if (event_source == file_delete) {
502 delete_prompt.display();
503 return;
504 }
505
506
507 // File -> Save
508 if (event_source == file_save) {
509 metadata_set_model.save(false);
510 return;
511 }
512
513 // File -> Exit
514 if (event_source == file_exit) {
515 metadata_set_model.save(true);
516 if (stand_alone){
517 System.exit(0);
518 }
519 else {
520 self.notifyListeners();
521 self.setVisible(false);
522 return;
523 }
524 }
525
526 /// File -> Preferences
527 // if(event_source == file_preferences){
528 // GEMSPreferences GemsPreferences = new GEMSPreferences();
529 //}
530
531 // Edit -> Cut
532 if (event_source == edit_cut) {
533 try {
534 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
535 // Get the component with selected text as a JTextComponent
536 JTextComponent text = (JTextComponent) kfm.getPermanentFocusOwner();
537 // Cut the text to the clipboard
538 text.cut();
539 }
540 catch (ClassCastException cce) {
541 // If the component is not a text component ignore the cut command
542 DebugStream.println(cce.toString());
543 }
544 return;
545 }
546
547 // Edit -> Copy
548 if (event_source == edit_copy) {
549 try {
550 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
551 // Get the component with selected text as a JTextComponent
552 JTextComponent text = (JTextComponent) kfm.getPermanentFocusOwner();//getFocusOwner();
553 text.copy();
554 }
555 catch (Exception cce) {
556 // If the component is not a text component ignore the copy command
557 DebugStream.println(cce.toString());
558 }
559 return;
560 }
561
562 // Edit -> Paste
563 if (event_source == edit_paste) {
564 try {
565 KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
566 // Get the component with selected text as a JTextComponent
567 JTextComponent text = (JTextComponent) kfm.getPermanentFocusOwner();
568 // Cut the text to the clipboard
569 text.paste();
570 }
571 catch (ClassCastException cce) {
572 // If the component is not a text component ignore the paste command
573 DebugStream.println(cce.toString());
574 }
575 return;
576 }
577
578 }
579 }
580
581}
Note: See TracBrowser for help on using the repository browser.