source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/gems/GEMS.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

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