View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  *
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  /******************************************************************************
25  * AgentGenerator.java
26  *
27  * Main frame of the Zeus Agent Generator
28  *****************************************************************************/
29  
30  package zeus.generator;
31  
32  import java.util.*;
33  import java.io.*;
34  import java.awt.*;
35  import java.awt.event.*;
36  import javax.swing.*;
37  import javax.swing.border.*;
38  import javax.swing.event.*;
39  
40  import zeus.util.*;
41  import zeus.concepts.*;
42  import zeus.gui.*;
43  import zeus.gui.help.*;
44  import zeus.ontology.*;
45  import zeus.generator.code.CodeGenerator;
46  import zeus.generator.code.GenerationPlan;
47  import zeus.generator.agent.*;
48  import zeus.generator.task.*;
49  
50  public class AgentGenerator extends javax.swing.JFrame implements ChangeListener, ActionListener
51  {
52    protected FileHandler       filer;
53    protected CodeGenerator     codeUI = null;
54  
55    protected File              projectFile  = null;
56    protected boolean           projectSaveNeeded = false;
57  
58    protected Hashtable         agentEditorList = new Hashtable();
59    protected Hashtable         taskEditorList = new Hashtable();
60  
61    protected OntologyDb        ontology;
62    protected GeneratorModel    genmodel;
63    protected GenerationPlan    genplan;
64    protected OntologyEditor    ontologyEditor = null;
65    protected SocietyEditor     societyEditor  = null;
66  
67    protected JMenuBar  mainMenuBar;
68    protected JMenu     menu1;
69    protected JMenuItem miNew;
70    protected JMenuItem miOpen;
71    protected JMenuItem miSave;
72    protected JMenuItem miSaveAs;
73    protected JMenuItem miGen;
74    protected JMenuItem miExit;
75    protected JMenu     menu2;
76    protected JMenuItem miNewOnt;
77    protected JMenuItem miEditOnt;
78    protected JMenuItem miLoadOnt;
79    protected JMenuItem miSaveOnt;
80    protected JMenu     menu3;
81    protected JMenuItem miAbout;
82  
83    protected JLabel projectInfoLabel;
84    protected JLabel ontologyInfoLabel;
85  
86    static final String PROJECT_FILE_EXT = "*.def";
87  
88    static final String[] MESSAGE = {
89       /* 0 */ "Save file?",
90       /* 1 */ "Save needed",
91       /* 2 */ "File already exists.\nOverwrite saved file?",
92       /* 3 */ "Save file as",
93       /* 4 */ "Error",
94       /* 5 */ "Warning",
95       /* 6 */ "Agent is currently being edited.\nClose agent editor?",
96       /* 7 */ "Remove agent",
97       /* 8 */ "Are you sure?",
98       /* 9 */ "FREE SLOT",
99  
100      /* 10 */ "Project: ",
101      /* 11 */ "Using ontology: ",
102      /* 12 */ "FREE SLOT",
103      /* 13 */ "FREE SLOT",
104      /* 14 */ "FREE SLOT",
105      /* 15 */ "FREE SLOT",
106      /* 16 */ "FREE SLOT",
107      /* 17 */ "FREE SLOT",
108      /* 18 */ "FREE SLOT",
109      /* 19 */ "ZEUS v2.0\n BT 1999-2004\n Consult the ZEUS Realisation Guide for instructions\n For more info on Zeus Open Source effort see http://www.sourceforge.net/projects/zeusagent" target="alexandria_uri">http://www.sourceforge.net/projects/zeusagent\n For more on the IS Lab see http://www.labs.bt.com/projects/agents.htm",
110 
111      /* 20 */ "About ZEUS",
112      /* 21 */ "No project loaded",
113      /* 22 */ "Task is currently being edited.\nClose task editor?",
114      /* 23 */ "Remove task",
115      /* 24 */ "Task is currently being edited.\nSave task?",
116      /* 25 */ "Clone Task",
117      /* 26 */ "Agent is currently being edited.\nSave agent?",
118      /* 27 */ "Clone Agent",
119      /* 28 */ " is currently being edited.\nSave agent?",
120      /* 29 */ " is currently being edited.\nSave task?",
121      /* 30 */ "New Project"
122   };
123 
124   public AgentGenerator() {
125     // debug classpath problems for lamers. 
126     // added by simon 21/08/00
127     try {
128          Class c = Class.forName("java.lang.Object"); 
129     }
130     catch (ClassNotFoundException cnfe) { 
131        System.out.println("Java cannot find java.lang.Object.\n This indicates that the rt.jar file is not in your classpath.\n Ensure that $java_install_dir//jre//rt.jar is present in the classpath and then continue");
132             cnfe.printStackTrace();}
133     try {
134          Class c = Class.forName("zeus.gui.help.HelpWindow"); 
135     }
136     catch (ClassNotFoundException cnfe) { 
137        System.out.println("Java cannot find a zeus class.\n This indicates that the zeus.jar file is not in your classpath.\n Ensure that zeus_install_dir//lib//zeus.jar is present in the classpath and then continue");
138             cnfe.printStackTrace();}      
139       
140 
141         
142     GenSym genSym = new GenSym("AgentGenerator");
143     ontology = new OntologyDb(genSym);
144     genmodel = new GeneratorModel(ontology);
145     genplan = new GenerationPlan(genmodel,ontology);
146     filer = new FileHandler(ontology,genmodel,genplan);
147 
148     String sep = System.getProperty("file.separator");
149     String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
150     ImageIcon icon = new ImageIcon(path + "tool.gif");
151     setIconImage(icon.getImage());
152     getContentPane().setBackground(Color.lightGray);
153     getContentPane().setLayout(new BorderLayout());
154 
155     JPanel mainPanel = new JPanel();
156     getContentPane().add(mainPanel, BorderLayout.CENTER);
157     genmodel.addChangeListener(this);
158     genplan.addChangeListener(this);
159 
160     mainPanel.setBackground(Color.lightGray);
161     Border b1 = new CompoundBorder(new BevelBorder(BevelBorder.LOWERED), new EmptyBorder(15,15,15,15));
162     mainPanel.setBorder(b1);
163 
164     GridBagLayout gridBagLayout = new GridBagLayout();
165     mainPanel.setLayout(gridBagLayout);
166     GridBagConstraints gbc = new GridBagConstraints();
167 
168     gbc.gridwidth = GridBagConstraints.REMAINDER;
169     gbc.anchor = GridBagConstraints.NORTH;
170     gbc.fill = GridBagConstraints.NONE;
171     gbc.weightx = gbc.weighty = 0;
172     gbc.insets = new Insets(0,8,8,8);
173 
174     JLabel header = new JLabel(new ImageIcon(path + "zeus.gif"));
175     header.setBorder(new BevelBorder(BevelBorder.RAISED));
176 
177     gridBagLayout.setConstraints(header, gbc);
178     mainPanel.add(header);
179 
180     gbc.anchor = GridBagConstraints.WEST;
181     gbc.fill = GridBagConstraints.HORIZONTAL;
182     gbc.weightx = gbc.weighty = 0;
183     gbc.insets = new Insets(8,8,0,8);
184 
185     JPanel projectPanel = new JPanel();
186     gridBagLayout.setConstraints(projectPanel, gbc);
187     mainPanel.add(projectPanel);
188 
189     JPanel ontologyPanel = new JPanel();
190     gridBagLayout.setConstraints(ontologyPanel, gbc);
191     mainPanel.add(ontologyPanel);
192 
193     gbc.fill = GridBagConstraints.BOTH;
194     gbc.weightx = gbc.weighty = 1;
195 
196     JPanel agentPanel = new JPanel();
197     gridBagLayout.setConstraints(agentPanel, gbc);
198     mainPanel.add(agentPanel);
199 
200     JPanel taskPanel = new JPanel();
201     gridBagLayout.setConstraints(taskPanel, gbc);
202     mainPanel.add(taskPanel);
203 
204    // ----- Create Project Panel ----
205     projectPanel.setBackground(Color.lightGray);
206     TitledBorder b2 = (BorderFactory.createTitledBorder("Project Options"));
207     b2.setTitlePosition(TitledBorder.TOP);
208     b2.setTitleJustification(TitledBorder.RIGHT);
209     b2.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
210     b2.setTitleColor(Color.blue);
211     projectPanel.setBorder(b2);
212    
213     JToolBar toolbar = new ProjectToolBar();
214     projectInfoLabel = new JLabel();
215     projectInfoLabel.setFont(new Font("Helvetica", Font.BOLD, 12));
216     projectInfoLabel.setText(MESSAGE[21]);
217 
218     gridBagLayout = new GridBagLayout();
219     projectPanel.setLayout(gridBagLayout);
220     gbc = new GridBagConstraints();
221 
222     gbc.gridwidth = GridBagConstraints.REMAINDER;
223     gbc.anchor = GridBagConstraints.WEST;
224     gbc.fill = GridBagConstraints.NONE;
225     gbc.insets = new Insets(0,8,0,0);
226     gridBagLayout.setConstraints(toolbar, gbc);
227     projectPanel.add(toolbar);
228 
229     gbc.anchor = GridBagConstraints.WEST;
230     gbc.fill = GridBagConstraints.BOTH;
231     gbc.weightx = gbc.weighty = 1;
232     gbc.insets = new Insets(4,8,4,8);
233     gridBagLayout.setConstraints(projectInfoLabel, gbc);
234     projectPanel.add(projectInfoLabel);
235 
236     // ---- Create Ontology Panel ----
237     ontologyPanel.setBackground(Color.lightGray);
238     b2 = (BorderFactory.createTitledBorder("Ontology Options"));
239     b2.setTitlePosition(TitledBorder.TOP);
240     b2.setTitleJustification(TitledBorder.RIGHT);
241     b2.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
242     b2.setTitleColor(Color.blue);
243     ontologyPanel.setBorder(b2);
244 
245     toolbar = new OntologyToolBar();
246     ontologyInfoLabel = new JLabel();
247     ontologyInfoLabel.setText(MESSAGE[16]);
248     ontologyInfoLabel.setFont(new Font("Helvetica", Font.BOLD, 12));
249 
250     gridBagLayout = new GridBagLayout();
251     ontologyPanel.setLayout(gridBagLayout);
252     gbc = new GridBagConstraints();
253 
254     gbc.gridwidth = GridBagConstraints.REMAINDER;
255     gbc.anchor = GridBagConstraints.WEST;
256     gbc.fill = GridBagConstraints.NONE;
257     gbc.insets = new Insets(0,8,0,0);
258     gridBagLayout.setConstraints(toolbar, gbc);
259     ontologyPanel.add(toolbar);
260 
261     gbc.anchor = GridBagConstraints.WEST;
262     gbc.fill = GridBagConstraints.BOTH;
263     gbc.weightx = gbc.weighty = 1;
264     gbc.insets = new Insets(4,8,4,8);
265     gridBagLayout.setConstraints(ontologyInfoLabel, gbc);
266     ontologyPanel.add(ontologyInfoLabel);
267 
268     // ---- Create Agent Panel ----
269     agentPanel.setBackground(Color.lightGray);
270     b2 = (BorderFactory.createTitledBorder("Agent Options"));
271     b2.setTitlePosition(TitledBorder.TOP);
272     b2.setTitleJustification(TitledBorder.RIGHT);
273     b2.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
274     b2.setTitleColor(Color.blue);
275     agentPanel.setBorder(b2);
276 
277     AgentTableUI agentTable = new AgentTableUI(this,genmodel);
278     toolbar = new AgentToolBar(agentTable);
279 
280     gridBagLayout = new GridBagLayout();
281     agentPanel.setLayout(gridBagLayout);
282     gbc = new GridBagConstraints();
283 
284     gbc.gridwidth = GridBagConstraints.REMAINDER;
285     gbc.anchor = GridBagConstraints.WEST;
286     gbc.fill = GridBagConstraints.NONE;
287     gbc.insets = new Insets(0,8,8,0);
288     gridBagLayout.setConstraints(toolbar, gbc);
289     agentPanel.add(toolbar);
290 
291     gbc.anchor = GridBagConstraints.WEST;
292     gbc.fill = GridBagConstraints.BOTH;
293     gbc.weightx = gbc.weighty = 1;
294     gbc.insets = new Insets(4,8,4,8);
295     gridBagLayout.setConstraints(agentTable, gbc);
296     agentPanel.add(agentTable);
297 
298     // ---- Create Task Panel ----
299     taskPanel.setBackground(Color.lightGray);
300     b2 = (BorderFactory.createTitledBorder("Task Options"));
301     b2.setTitlePosition(TitledBorder.TOP);
302     b2.setTitleJustification(TitledBorder.RIGHT);
303     b2.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
304     b2.setTitleColor(Color.blue);
305     taskPanel.setBorder(b2);
306    
307     TaskTableUI taskTable = new TaskTableUI(this,genmodel);
308     toolbar = new TaskToolBar(taskTable);
309 
310     gridBagLayout = new GridBagLayout();
311     taskPanel.setLayout(gridBagLayout);
312     gbc = new GridBagConstraints();
313 
314     gbc.gridwidth = GridBagConstraints.REMAINDER;
315     gbc.anchor = GridBagConstraints.WEST;
316     gbc.fill = GridBagConstraints.NONE;
317     gbc.insets = new Insets(0,8,8,0);
318     gridBagLayout.setConstraints(toolbar, gbc);
319     taskPanel.add(toolbar);
320 
321     gbc.anchor = GridBagConstraints.WEST;
322     gbc.fill = GridBagConstraints.BOTH;
323     gbc.weightx = gbc.weighty = 1;
324     gbc.insets = new Insets(4,8,4,8);
325     gridBagLayout.setConstraints(taskTable, gbc);
326     taskPanel.add(taskTable);
327 
328     // ---- Create OntologyEditor ----
329     ontologyEditor = new OntologyEditor(ontology, this, genmodel, ontologyInfoLabel);
330     // now passes in generator model - Jaron 1/11/00
331 
332     // ---- Set Frame title/menus/etc ----
333     setTitle("ZEUS Agent Generator " + SystemProps.getProperty("version.id"));
334     initMenus();
335 
336     // ---- Register Listeners ----
337     SymWindow aSymWindow = new SymWindow();
338     this.addWindowListener(aSymWindow);
339 
340     setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
341     setBackground(Color.lightGray);
342     setVisible(true);
343     pack();
344     // force compiler to except ClassNotFoundException
345    
346 
347   }
348 
349   protected void initMenus() {
350      mainMenuBar = new JMenuBar();
351      menu1 = new JMenu("Project");
352      miNew = new JMenuItem("New Project");
353      miNew.addActionListener(this);
354      menu1.add(miNew);
355      
356      miOpen = new JMenuItem("Load Project...");
357      miOpen.addActionListener(this);
358      menu1.add(miOpen);
359      
360      miSave = new JMenuItem("Save Project...");
361      miSave.addActionListener(this);
362      menu1.add(miSave);
363      
364      miSaveAs = new JMenuItem("Save Project As...");
365      miSaveAs.addActionListener(this);
366      menu1.add(miSaveAs);
367      menu1.addSeparator();
368      
369      miGen = new JMenuItem("Generate Code");
370      miGen.addActionListener(this);
371      menu1.add(miGen);
372      menu1.addSeparator();
373      
374      miExit = new JMenuItem("Exit");
375      miExit.addActionListener(this);
376      menu1.add(miExit);
377      mainMenuBar.add(menu1);
378      
379      menu2 = new JMenu("Ontology");
380      miNewOnt = new JMenuItem("New Ontology");
381      miNewOnt.addActionListener(this);
382      menu2.add(miNewOnt);
383      miLoadOnt = new JMenuItem("Load Ontology");
384      miLoadOnt.addActionListener(this);
385      menu2.add(miLoadOnt);
386      miSaveOnt = new JMenuItem("Save Ontology");
387      miSaveOnt.addActionListener(this);
388      menu2.add(miSaveOnt);
389      miEditOnt = new JMenuItem("Edit Ontology");
390      miEditOnt.addActionListener(this);
391      menu2.add(miEditOnt);
392 
393      mainMenuBar.add(menu2);
394      menu3 = new JMenu("Help");
395 //     mainMenuBar.setHelpMenu(menu3);
396      miAbout = new JMenuItem("About..");
397      miAbout.addActionListener(this);
398      menu3.add(miAbout);
399      mainMenuBar.add(menu3);
400      setJMenuBar(mainMenuBar);
401        
402   }
403 
404   
405   // ---- Project Options ----  
406   
407   public boolean openFile(File f) {
408     Assert.notNull(f);
409     Frame frame = (Frame)SwingUtilities.getRoot(this);
410     Cursor lastCursor = frame.getCursor();
411     frame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
412     int status = filer.openFile(f);
413     frame.setCursor(lastCursor);
414     if ( (status & FileHandler.ERROR_MASK) != 0 ) {
415        JOptionPane.showMessageDialog(this,filer.getError(),
416           MESSAGE[4],JOptionPane.ERROR_MESSAGE);
417        projectSaveNeeded = false;
418        return newProject();
419     }
420     else if ( (status & FileHandler.WARNING_MASK) != 0 ) {
421       JOptionPane.showMessageDialog(this,filer.getWarning(),
422          MESSAGE[5],JOptionPane.WARNING_MESSAGE);
423 
424          if (FileHandler.WARNING_MASK == 1)
425            projectSaveNeeded = true; // autosave perhaps?
426     }
427     else
428       projectSaveNeeded = false;
429     projectFile = f;
430     projectInfoLabel.setText(MESSAGE[10] + f.getName());
431     ontologyInfoLabel.setText(MESSAGE[11] + ontology.getFilename());
432     return true;
433   }
434 
435   public boolean saveFile(File f) {
436     Assert.notNull(f);
437     Frame frame = (Frame)SwingUtilities.getRoot(this);
438     Cursor lastCursor = frame.getCursor();
439 
440     frame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
441     if ( !ontologyEditor.Save() ) {
442        frame.setCursor(lastCursor);
443        return false;
444     }
445     int status = filer.saveFile(f);
446     frame.setCursor(lastCursor);
447 
448     if ( (status & FileHandler.ERROR_MASK) != 0 ) {
449        JOptionPane.showMessageDialog(this,filer.getError(),
450           MESSAGE[4],JOptionPane.ERROR_MESSAGE);
451        return false;
452     }
453     else if ( (status & FileHandler.WARNING_MASK) != 0 ) {
454        JOptionPane.showMessageDialog(this,filer.getWarning(),
455           MESSAGE[5],JOptionPane.WARNING_MESSAGE);
456     }
457 
458     projectInfoLabel.setText(MESSAGE[10] + f.getName());
459     projectSaveNeeded = false;
460     projectFile = f;
461     return true;
462   }
463 
464   protected void resetProject() {
465     genmodel.clear();
466     genplan.reset();
467     ontologyEditor.closeFile();
468     projectFile = null;
469     projectInfoLabel.setText(MESSAGE[21]);
470     projectSaveNeeded = false;
471 
472     Enumeration enum = agentEditorList.elements();
473     while (enum.hasMoreElements()) {
474       JFrame frame = (JFrame)enum.nextElement();
475       frame.dispose();
476     }
477     agentEditorList.clear();
478 
479     enum = taskEditorList.elements();
480     while (enum.hasMoreElements()) {
481       JFrame frame = (JFrame)enum.nextElement();
482       frame.dispose();
483     }
484     taskEditorList.clear();
485   }
486 
487   public void loadProject() {
488     if ( !newProject() ) return;
489 
490     File f = getFile(FileDialog.LOAD,projectFile,PROJECT_FILE_EXT);
491     if (f != null)
492        openFile(f);
493   }
494 
495   protected boolean editorsHaveChanged() {
496     Enumeration enum = agentEditorList.elements();
497     while( enum.hasMoreElements()) {
498       AgentEditor editor = (AgentEditor)enum.nextElement();
499       if ( editor.hasChanged() ) return true;
500     }
501 
502     enum = taskEditorList.elements();
503     while ( enum.hasMoreElements()) {
504       TaskEditor editor = (TaskEditor)enum.nextElement();
505       if ( editor.hasChanged() ) return true;
506     }
507     return false;
508   }
509 
510   public boolean saveProject() {
511     // store information from open editor windows
512     Enumeration enum = agentEditorList.elements();
513     while (enum.hasMoreElements()) {
514       AgentEditor editor = (AgentEditor)enum.nextElement();
515       if ( editor.hasChanged() ) {
516          int answer = JOptionPane.showConfirmDialog(this, "Agent " +
517             editor.getObjectName() + MESSAGE[28],
518             MESSAGE[30],JOptionPane.YES_NO_CANCEL_OPTION);
519          if ( answer == JOptionPane.YES_OPTION )
520             editor.save();
521          else if ( answer == JOptionPane.CANCEL_OPTION )
522             return false;
523       }
524     }
525 
526     enum = taskEditorList.elements();
527     while (enum.hasMoreElements()) {
528       TaskEditor editor = (TaskEditor)enum.nextElement();
529       if ( editor.hasChanged() ) {
530          int answer = JOptionPane.showConfirmDialog(this, "Task " +
531             editor.getObjectName() + MESSAGE[29],
532             MESSAGE[30], JOptionPane.YES_NO_CANCEL_OPTION);
533          if ( answer == JOptionPane.YES_OPTION )
534             editor.save();
535          else if ( answer == JOptionPane.CANCEL_OPTION )
536             return false;
537       }
538     }
539 
540     if ( projectFile == null )
541        return saveProjectAs();
542     else
543        return saveFile(projectFile);
544   }
545 
546   protected boolean saveProjectAs() {
547     File f = getFile(FileDialog.SAVE,projectFile,PROJECT_FILE_EXT);
548     if ( f != null ) {
549        if ( f.exists() && projectFile != null && !f.equals(projectFile) ) {
550           int answer = JOptionPane.showConfirmDialog(this,MESSAGE[2],
551                        MESSAGE[3],JOptionPane.YES_NO_OPTION);
552           if ( answer == JOptionPane.NO_OPTION )
553              return false;
554        }
555        return saveFile(f);
556     }
557     return false;
558   }
559 
560   protected boolean newProject() {
561     if ( projectSaveNeeded || editorsHaveChanged() ) {
562        int answer = JOptionPane.showConfirmDialog(this,MESSAGE[0],
563                        MESSAGE[1],JOptionPane.YES_NO_CANCEL_OPTION);
564 
565        if ( answer == JOptionPane.YES_OPTION && !saveProject() )
566           return false;
567        if ( answer == JOptionPane.CANCEL_OPTION )
568           return false;
569     }
570     resetProject();
571     return true;
572   }
573 
574   public void generateCode() {
575     if ( codeUI == null )
576        codeUI = new CodeGenerator(genmodel,genplan,true);
577 
578     if ( codeUI.isShowing() )
579        codeUI.toFront();
580     else
581        codeUI.setVisible(true);
582   }
583 
584   public void showSociety() {
585     if ( societyEditor == null )
586        societyEditor = new SocietyEditor(this,genmodel);
587 
588     if ( societyEditor.isShowing() )
589        societyEditor.toFront();
590     else
591        societyEditor.setVisible(true);
592   }
593 
594   public void exitGenerator() {
595      if ( !newProject() ) return;
596 
597      dispose();
598      if ( codeUI != null ) codeUI.dispose();
599      if ( societyEditor != null ) societyEditor.dispose();
600      System.exit(0);
601   }
602   
603   // ---- Ontology Options -----
604 
605   public boolean newOntology() {
606      return ontologyEditor.Close();
607   }
608 
609 
610   public void loadOntology() {
611      ontologyEditor.Open();
612   }
613 
614 
615   public boolean saveOntology() {
616      return ontologyEditor.Save();
617   }
618 
619 
620   public void editOntology() {
621     ontologyEditor.showFrame();
622   }
623 
624   // ---- About Options -----
625 
626   public void about() {
627       JOptionPane.showMessageDialog(this,MESSAGE[19],MESSAGE[20],
628                                     JOptionPane.INFORMATION_MESSAGE);
629   }
630 
631   // ---- Agent Options -----
632 
633   public void editAgent(String id) {
634     if ( agentEditorList.containsKey(id) ) {
635       AgentEditor editor = (AgentEditor)agentEditorList.get(id);
636       editor.toFront();
637     }
638     else {
639       AgentDescription agent = genmodel.getAgent(id);
640       AgentEditor editor = new AgentEditor(this,genmodel,ontology,agent);
641       agentEditorList.put(id,editor);
642       editor.show();
643     }
644   }
645   
646   
647   public void removeAgent(String id) {
648      if ( agentEditorList.containsKey(id) ) {
649         int answer = JOptionPane.showConfirmDialog(this,MESSAGE[6],
650                         MESSAGE[7],JOptionPane.YES_NO_CANCEL_OPTION);
651         if ( answer == JOptionPane.YES_OPTION ) {
652            AgentEditor editor = (AgentEditor)agentEditorList.get(id);
653            editor.dispose();
654            genmodel.removeAgent(id);
655         }
656         else
657            return;
658      }
659      else {
660         int answer = JOptionPane.showConfirmDialog(this,MESSAGE[8],
661                         MESSAGE[7],JOptionPane.YES_NO_CANCEL_OPTION);
662         if ( answer == JOptionPane.YES_OPTION ) {
663            genmodel.removeAgent(id);
664         }
665      }
666   }
667 
668 
669   public void cloneAgent(String id) {
670      if ( agentEditorList.containsKey(id) ) {
671         int answer = JOptionPane.showConfirmDialog(this,MESSAGE[26],
672                         MESSAGE[27],JOptionPane.YES_NO_CANCEL_OPTION);
673         if ( answer == JOptionPane.YES_OPTION ) {
674            AgentEditor editor = (AgentEditor)agentEditorList.get(id);
675            editor.save();
676         }
677         else if ( answer == JOptionPane.CANCEL_OPTION ) {
678            return;
679         }
680      }
681      genmodel.cloneAgent(id);
682   }
683 
684 
685   public void agentEditorClosed(String name) {
686     agentEditorList.remove(name);
687   }
688 
689   // ---- Task Options -----
690 
691   public void editTask(String id) {
692     if ( taskEditorList.containsKey(id) ) {
693       TaskEditor editor = (TaskEditor)taskEditorList.get(id);
694       editor.toFront();
695     }
696     else {
697       AbstractTask task = genmodel.getTask(id);
698       TaskEditor editor = new TaskEditor(this,genmodel,ontology,task);
699       taskEditorList.put(id,editor);
700       editor.show();
701     }
702   }
703 
704 
705   public void removeTask(String id) {
706      if ( taskEditorList.containsKey(id) ) {
707         int answer = JOptionPane.showConfirmDialog(this,MESSAGE[22],
708                         MESSAGE[23],JOptionPane.YES_NO_CANCEL_OPTION);
709         if ( answer == JOptionPane.YES_OPTION ) {
710            TaskEditor editor = (TaskEditor)taskEditorList.get(id);
711            editor.dispose();
712            genmodel.removeTask(id);
713         }
714         else
715            return;
716      }
717      else {
718         int answer = JOptionPane.showConfirmDialog(this,MESSAGE[8],
719                         MESSAGE[23],JOptionPane.YES_NO_CANCEL_OPTION);
720         if ( answer == JOptionPane.YES_OPTION ) {
721            genmodel.removeTask(id);
722         }
723      }
724   }
725   
726   
727   public void cloneTask(String id) {
728      if ( taskEditorList.containsKey(id) ) {
729         int answer = JOptionPane.showConfirmDialog(this,MESSAGE[24],
730                         MESSAGE[25],JOptionPane.YES_NO_CANCEL_OPTION);
731         if ( answer == JOptionPane.YES_OPTION ) {
732            TaskEditor editor = (TaskEditor)taskEditorList.get(id);
733            editor.save();
734         }
735         else if ( answer == JOptionPane.CANCEL_OPTION ) {
736            return;
737         }
738      }
739      genmodel.cloneTask(id);
740   }
741 
742   public void taskEditorClosed(String name) {
743     taskEditorList.remove(name);
744   }
745 
746   // ---- Utilities ----
747 
748   protected File getFile(int type, File f1, String filter) {
749     FileDialog f = new FileDialog(this, "AgentGenerator: Select File", type);
750     if (f1 != null ) {
751        f.setFile(f1.getName());
752        f.setDirectory(f1.getParent());
753     }
754     else
755        f.setFile(filter);
756 
757     f.pack();
758     f.setVisible(true);
759 
760     return  f.getFile()==null ? null : new File(f.getDirectory(),f.getFile());
761   }
762   
763   
764 
765   // ---- Initialisation Methods ----
766   
767   protected static void usage() {
768     System.err.println("Usage: java AgentGenerator [-f <file>] [-h] [-v]");
769     System.exit(0);
770   }
771   
772   protected static void version() {
773     System.err.println("Zeus Agent Generator version: " +
774                        SystemProps.getProperty("version.id"));
775     System.exit(0);
776   }
777 
778   public static void main(String arg[]) {
779     String filename = null;
780 
781     for( int i = 0; i < arg.length; i++ ) {
782       if ( arg[i].equals("-f") && ++i < arg.length )
783         filename = arg[i];
784       else if ( arg[i].equals("-h") )
785         usage();
786       else if ( arg[i].equals("-v") )
787         version();
788       else
789         usage();
790     }
791 
792     AgentGenerator generator = new AgentGenerator();
793 
794     if ( filename != null ) {
795        String dir = System.getProperty("user.dir") +
796                     System.getProperty("file.separator");
797        generator.openFile(new File(dir + filename));
798     }
799   }
800   
801 //---------------------------------------------------------------------------
802 
803 
804   public void stateChanged(ChangeEvent evt) {
805      projectSaveNeeded = true;
806   }
807 
808   public void actionPerformed(ActionEvent event) {
809      Object object = event.getSource();
810      if (object == miNew)
811        newProject();
812      else if (object == miOpen)
813        loadProject();
814      else if (object == miSave)
815        saveProject();
816      else if (object == miSaveAs)
817        saveProjectAs();
818      else if (object == miGen)
819        generateCode();
820      else if (object == miExit)
821        exitGenerator();
822 
823      else if (object == miNewOnt)
824        newOntology();
825      else if (object == miLoadOnt)
826        loadOntology();
827      else if (object == miSaveOnt)
828        saveOntology();
829      else if (object == miEditOnt)
830        editOntology();
831 
832      else if (object == miAbout)
833        about();
834 
835    repaint(); 
836 
837   }
838 
839 
840   class SymWindow extends WindowAdapter {
841     public void windowClosing(WindowEvent event) {
842        exitGenerator();
843     }
844   }
845 
846   class ProjectToolBar extends JToolBar implements ActionListener {
847      protected HelpWindow    helpWin;
848      protected JButton       newBtn;
849      protected JButton       loadBtn;
850      protected JButton       saveBtn;
851      protected JButton       viewBtn;
852      protected JButton       genBtn;
853      protected JToggleButton helpBtn;
854 
855      public ProjectToolBar() {
856        setBackground(Color.lightGray);
857        setBorder( new BevelBorder(BevelBorder.LOWERED ) );
858        setFloatable(false);
859 
860        String sep = System.getProperty("file.separator");
861        String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
862 
863        newBtn = new JButton(new ImageIcon(path + "new.gif"));
864        add(newBtn);
865        newBtn.setToolTipText("Create New Project");
866        newBtn.addActionListener(this);
867 
868        loadBtn = new JButton(new ImageIcon(path + "load.gif"));
869        add(loadBtn);
870        loadBtn.setToolTipText("Load Existing Project");
871        loadBtn.addActionListener(this);
872 
873        saveBtn = new JButton(new ImageIcon(path + "savedisk.gif"));
874        add(saveBtn);
875        saveBtn.setToolTipText("Save current Project to disk");
876        saveBtn.addActionListener(this);
877 
878        addSeparator();
879 
880        viewBtn = new JButton(new ImageIcon(path + "view.gif"));
881        add(viewBtn);
882        viewBtn.setToolTipText("View Agent Society");
883        viewBtn.addActionListener(this);
884 
885        addSeparator();
886 
887        genBtn = new JButton(new ImageIcon(path + "code.gif"));
888        add(genBtn);
889        genBtn.setToolTipText("Generate Agent Code");
890        genBtn.addActionListener(this);
891 
892        addSeparator();
893 
894 
895        helpBtn = new JToggleButton(new ImageIcon(path + "info.gif"));
896        helpBtn.setMargin(new Insets(0,0,0,0));
897        add(helpBtn);
898        helpBtn.setToolTipText("Help");
899        helpBtn.addActionListener(this);
900      }
901 
902      public void actionPerformed(ActionEvent e) {
903        Object src = e.getSource();
904        if ( src == newBtn )
905           newProject();
906        else if ( src == loadBtn )
907          loadProject();
908        else if ( src == saveBtn )
909          saveProject();
910        else if ( src == viewBtn )
911          showSociety();
912        else if ( src == genBtn )
913          generateCode();
914        else if ( src == helpBtn ) {
915          if (helpBtn.isSelected()) {
916            Point dispos = getLocation();
917            helpWin = new HelpWindow(SwingUtilities.getRoot(this), dispos,
918                                     "generator", "Project Options");
919            helpWin.setSource(helpBtn);
920          }
921          else
922            helpWin.dispose();
923        }
924      }
925    }
926 
927    class OntologyToolBar extends JToolBar implements ActionListener {
928      protected HelpWindow    helpWin;
929      protected JButton       newBtn;
930      protected JButton       loadBtn;
931      protected JButton       editBtn;
932      protected JButton       saveBtn;
933      protected JToggleButton helpBtn;
934 
935      public OntologyToolBar() {
936        setBackground(Color.lightGray);
937        setBorder( new BevelBorder(BevelBorder.LOWERED ) );
938        setFloatable(false);
939 
940        String sep = System.getProperty("file.separator");
941        String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
942 
943        newBtn = new JButton(new ImageIcon(path + "new.gif"));
944        add(newBtn);
945        newBtn.setToolTipText("Create New Ontology");
946        newBtn.addActionListener(this);
947 
948        loadBtn = new JButton(new ImageIcon(path + "load.gif"));
949        add(loadBtn);
950        loadBtn.setToolTipText("Load Existing Ontology");
951        loadBtn.addActionListener(this);
952 
953        saveBtn = new JButton(new ImageIcon(path + "savedisk.gif"));
954        add(saveBtn);
955        saveBtn.setToolTipText("Save current Ontology to disk");
956        saveBtn.addActionListener(this);
957 
958        addSeparator();
959 
960        editBtn = new JButton(new ImageIcon(path + "edit.gif"));
961        add(editBtn);
962        editBtn.setToolTipText("Edit Current Ontology");
963        editBtn.addActionListener(this);
964 
965        addSeparator();
966 
967        helpBtn = new JToggleButton(new ImageIcon(path + "info.gif"));
968        helpBtn.setMargin(new Insets(0,0,0,0));
969        add(helpBtn);
970        helpBtn.setToolTipText("Help");
971        helpBtn.addActionListener(this);
972      }
973    
974      public void actionPerformed(ActionEvent e) {
975        Object src = e.getSource();
976        if ( src == newBtn )
977          newOntology();
978        else if ( src == loadBtn )
979          loadOntology();
980        else if ( src == editBtn )
981          editOntology();
982        else if ( src == saveBtn )
983          saveOntology();
984        else if ( src == helpBtn ) {
985          if (helpBtn.isSelected()) {
986            Point dispos = getLocation();
987            helpWin = new HelpWindow(SwingUtilities.getRoot(this), dispos,
988                                     "generator", "Ontology Options");
989            helpWin.setSource(helpBtn);
990          }
991          else
992            helpWin.dispose();
993        }
994      }
995    }
996 
997 
998    class AgentToolBar extends JToolBar implements ActionListener {
999      protected JButton        newBtn;
1000      protected JButton        editBtn;
1001      protected JButton        deleteBtn;
1002      protected JButton        cloneBtn;
1003      protected JButton        renameBtn;
1004      protected JToggleButton  helpBtn;
1005      protected HelpWindow     helpWin;
1006      protected AgentTableUI   agentTable;
1007      protected JPopupMenu     popup;
1008      protected JMenuItem      renameAgentMenuItem;
1009      protected JMenuItem      modifyTaskListMenuItem;
1010 
1011      public AgentToolBar(AgentTableUI agentTable) {
1012        this.agentTable = agentTable;
1013        setBackground(Color.lightGray);
1014        setBorder( new BevelBorder(BevelBorder.LOWERED ) );
1015        setFloatable(false);
1016 
1017        String sep = System.getProperty("file.separator");
1018        String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
1019 
1020        newBtn = new JButton(new ImageIcon(path + "new.gif"));
1021        add(newBtn);
1022        newBtn.setToolTipText("Create new agent");
1023        newBtn.addActionListener(this);
1024 
1025        renameBtn = new JButton(new ImageIcon(path + "rename.gif"));
1026        add(renameBtn);
1027        renameBtn.setToolTipText("Modify this agent's name or its task list");
1028        renameBtn.addActionListener(this);
1029 
1030        deleteBtn = new JButton(new ImageIcon(path + "delete.gif"));
1031        add(deleteBtn);
1032        deleteBtn.setToolTipText("Delete this agent");
1033        deleteBtn.addActionListener(this);
1034 
1035        cloneBtn = new JButton(new ImageIcon(path + "clone.gif"));
1036        add(cloneBtn);
1037        cloneBtn.setToolTipText("Clone this agent");
1038        cloneBtn.addActionListener(this);
1039 
1040        addSeparator();
1041 
1042        editBtn = new JButton(new ImageIcon(path + "edit.gif"));
1043        add(editBtn);
1044        editBtn.setToolTipText("Edit this agent");
1045        editBtn.addActionListener(this);
1046 
1047        addSeparator();
1048 
1049        helpBtn = new JToggleButton(new ImageIcon(path + "info.gif"));
1050        helpBtn.setMargin(new Insets(0,0,0,0));
1051        add(helpBtn);
1052        helpBtn.setToolTipText("Help");
1053        helpBtn.addActionListener(this);
1054 
1055        // ---- Popup Menu for Modifications ----
1056        popup = new JPopupMenu();
1057        popup.add(new JLabel ("Modify"));
1058        popup.addSeparator();
1059 
1060        renameAgentMenuItem = new JMenuItem("Rename agent");
1061        renameAgentMenuItem.addActionListener(this);
1062        popup.add(renameAgentMenuItem);
1063 
1064        modifyTaskListMenuItem  = new JMenuItem("Modify task list");
1065        modifyTaskListMenuItem.addActionListener(this);
1066        popup.add(modifyTaskListMenuItem);
1067 
1068        CompoundBorder cbr = new CompoundBorder(new EtchedBorder(),
1069                                                new EmptyBorder(5,5,5,5));
1070        popup.setBorder(cbr);
1071 
1072      }
1073 
1074      public void actionPerformed(ActionEvent e) {
1075        Object src = e.getSource();
1076 
1077        if ( src == newBtn )
1078           agentTable.addNewAgent();
1079        else if ( src == editBtn )
1080           agentTable.editAgent();
1081        else if ( src == deleteBtn )
1082           agentTable.removeAgent();
1083        else if ( src == cloneBtn )
1084           agentTable.cloneAgent();
1085        else if ( src == renameBtn ) {
1086           popup.pack();
1087           popup.show(renameBtn,0,0);
1088        }
1089        else if ( src == renameAgentMenuItem )
1090           agentTable.renameAgent();
1091        else if ( src == modifyTaskListMenuItem )
1092           agentTable.modifyTaskList();
1093        else if ( src == helpBtn ) {
1094          if ( helpBtn.isSelected() ) {
1095            Point dispos = getLocation();
1096            helpWin = new HelpWindow(SwingUtilities.getRoot(this),dispos,
1097                                     "generator", "Agent Options");
1098            helpWin.setSource(helpBtn);
1099          }
1100          else
1101            helpWin.dispose();
1102        }
1103      }
1104    }
1105 
1106    class TaskToolBar extends JToolBar implements ActionListener {
1107      protected JButton        newBtn;
1108      protected JButton        editBtn;
1109      protected JButton        cloneBtn;
1110      protected JButton        deleteBtn;
1111      protected JButton        renameBtn;
1112      protected JToggleButton  helpBtn;
1113      protected HelpWindow     helpWin;
1114      protected TaskTableUI    taskTable;
1115      protected JPopupMenu     popup;
1116      protected JMenuItem      primitiveMenuItem;
1117      protected JMenuItem      summaryMenuItem;
1118      protected JMenuItem      behaviourMenuItem;
1119      protected JMenuItem      scriptMenuItem;
1120 
1121      public TaskToolBar(TaskTableUI taskTable) {
1122        this.taskTable = taskTable;
1123        setBackground(Color.lightGray);
1124        setBorder( new BevelBorder(BevelBorder.LOWERED ) );
1125        setFloatable(false);
1126 
1127        String sep = System.getProperty("file.separator");
1128        String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
1129 
1130        newBtn = new JButton(new ImageIcon(path + "new.gif"));
1131        add(newBtn);
1132        newBtn.setToolTipText("Create new task");
1133        newBtn.addActionListener(this);
1134 
1135        renameBtn = new JButton(new ImageIcon(path + "rename.gif"));
1136        add(renameBtn);
1137        renameBtn.setToolTipText("Rename task");
1138        renameBtn.addActionListener(this);
1139 
1140        deleteBtn = new JButton(new ImageIcon(path + "delete.gif"));
1141        add(deleteBtn);
1142        deleteBtn.setToolTipText("Delete this task");
1143        deleteBtn.addActionListener(this);
1144 
1145        cloneBtn = new JButton(new ImageIcon(path + "clone.gif"));
1146        add(cloneBtn);
1147        cloneBtn.setToolTipText("Clone this task");
1148        cloneBtn.addActionListener(this);
1149 
1150        addSeparator();
1151 
1152        editBtn = new JButton(new ImageIcon(path + "edit.gif"));
1153        add(editBtn);
1154        editBtn.setToolTipText("Edit this task");
1155        editBtn.addActionListener(this);
1156 
1157        addSeparator();
1158 
1159        helpBtn = new JToggleButton(new ImageIcon(path + "info.gif"));
1160        helpBtn.setMargin(new Insets(0,0,0,0));
1161        add(helpBtn);
1162        helpBtn.setToolTipText("Help");
1163        helpBtn.addActionListener(this);
1164 
1165        // ---- Popup Menu for Task Types ----
1166        popup = new JPopupMenu();
1167        popup.add(new JLabel ("Task Type"));
1168        popup.addSeparator();
1169 
1170        primitiveMenuItem = new JMenuItem(
1171           AbstractTask.getTypeName(AbstractTask.PRIMITIVE));
1172        primitiveMenuItem.addActionListener(this);
1173        popup.add(primitiveMenuItem);
1174 
1175        summaryMenuItem  = new JMenuItem(
1176           AbstractTask.getTypeName(AbstractTask.SUMMARY));
1177        summaryMenuItem.addActionListener(this);
1178        popup.add(summaryMenuItem);
1179 
1180        behaviourMenuItem = new JMenuItem(
1181           AbstractTask.getTypeName(AbstractTask.BEHAVIOUR));
1182        behaviourMenuItem.addActionListener(this);
1183        popup.add(behaviourMenuItem);
1184 
1185        scriptMenuItem  = new JMenuItem(
1186           AbstractTask.getTypeName(AbstractTask.SCRIPT));
1187        scriptMenuItem.addActionListener(this);
1188        popup.add(scriptMenuItem);
1189 
1190        CompoundBorder cbr = new CompoundBorder(new EtchedBorder(),
1191                                                new EmptyBorder(5,5,5,5));
1192        popup.setBorder(cbr);
1193      }
1194 
1195      public void actionPerformed(ActionEvent e) {
1196        Object src = e.getSource();
1197 
1198        if ( src == newBtn ) {
1199           popup.pack();
1200           popup.show(newBtn,0,0);
1201        }
1202        else if ( src == primitiveMenuItem )
1203           taskTable.addNewTask(AbstractTask.PRIMITIVE);
1204        else if ( src == summaryMenuItem )
1205           taskTable.addNewTask(AbstractTask.SUMMARY);
1206        else if ( src == behaviourMenuItem )
1207           taskTable.addNewTask(AbstractTask.BEHAVIOUR);
1208        else if ( src == scriptMenuItem )
1209           taskTable.addNewTask(AbstractTask.SCRIPT);
1210        else if ( src == editBtn )
1211           taskTable.editTask();
1212        else if ( src == deleteBtn )
1213           taskTable.removeTask();
1214        else if ( src == cloneBtn )
1215           taskTable.cloneTask();
1216        else if ( src == renameBtn )
1217           taskTable.renameTask();
1218        else if ( src == helpBtn ) {
1219          if ( helpBtn.isSelected() ) {
1220            Point dispos = getLocation();
1221            helpWin = new HelpWindow(SwingUtilities.getRoot(this),dispos,
1222                                     "generator", "Task Options");
1223            helpWin.setSource(helpBtn);
1224          }
1225          else
1226            helpWin.dispose();
1227        }
1228      }
1229    }
1230 
1231 
1232 }
1233