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  * TaskEditor.java
26  *
27  * Main Frame for the task editing panels
28  ***************************************************************************/
29  
30  package zeus.generator.task;
31  
32  import java.util.*;
33  import java.awt.*;
34  import java.awt.event.*;
35  import javax.swing.*;
36  import javax.swing.border.*;
37  import javax.swing.event.*;
38  
39  import zeus.util.*;
40  import zeus.concepts.*;
41  import zeus.gui.help.*;
42  import zeus.generator.*;
43  import zeus.generator.util.*;
44  import zeus.generator.task.rulebase.RuleUI;
45  
46  /***
47   *TaskEditor is the panel that is used as an editor for the various types of task.
48   *dependent on the task type being edited the panel will take on the right 
49   *characteristics to get the information that is appropriate for that task
50   *Change Log
51   *Simon added an Information Panel to the Primative task editor to allow information 
52   *for DAML-S service descriptions to be edited
53   */
54  public class TaskEditor extends JFrame
55                          implements Editor {
56  
57    protected JTabbedPane       tabbedPane;
58    protected AgentGenerator    generator;
59    protected GeneratorModel    genmodel;
60    protected OntologyDb        ontologyDb;
61    protected HelpWindow        helpWin;
62    protected boolean           changed;
63    protected EventListenerList changeListeners = new EventListenerList();
64    protected AbstractTask      currentTask;
65  
66    // Panels used for primitive tasks
67    protected ConditionsPanel   conditionsPanel;
68    protected ConstraintsPanel  constraintsPanel;
69    protected InformationPanel infoPanel;
70  
71    // Panels used for summary tasks and plan scripts
72    protected NodesPanel        nodesPanel;
73  
74    // Panels used for rulebases
75    protected RuleUI            rulebasePanel;
76  
77    static final String[] MESSAGE = {
78       /* 0 */ "Save task?",
79       /* 1 */ "Save needed",
80       /* 2 */ "Task Editor: "
81    };
82  
83  
84    public TaskEditor(AgentGenerator generator, GeneratorModel genmodel,
85                      OntologyDb ontologyDb, AbstractTask task) {
86  
87      this.generator = generator;
88      this.genmodel = genmodel;
89      this.currentTask = task;
90      this.ontologyDb = ontologyDb;
91  
92      changed = false;
93      genmodel.addChangeListener(this);
94  
95      getContentPane().setBackground(Color.lightGray);
96      getContentPane().setLayout(new BorderLayout());
97  
98      String sep = System.getProperty("file.separator");
99      String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
100     ImageIcon icon = new ImageIcon(path + "edit.gif");
101     setIconImage(icon.getImage());
102 
103     tabbedPane = new JTabbedPane();
104     if ( task.isPrimitive() ) {
105        conditionsPanel = new ConditionsPanel(
106           generator,genmodel,ontologyDb,this,(PrimitiveTask)task);
107        constraintsPanel = new ConstraintsPanel(
108           generator,genmodel,ontologyDb,this,(Task)task,
109           conditionsPanel.getPreconditionsModel(),
110           conditionsPanel.getPostconditionsModel());
111        infoPanel = new InformationPanel((Task)task); 
112        
113        tabbedPane.addTab("Preconditions and Effects", conditionsPanel);
114        tabbedPane.addTab("Constraints", constraintsPanel);
115        tabbedPane.addTab("Information", infoPanel); 
116     
117     }
118     else if ( task.isSummary() || task.isScript() ) {
119        nodesPanel = new NodesPanel(
120           generator,genmodel,ontologyDb,this,(SummaryTask)task);
121        constraintsPanel = new ConstraintsPanel(
122           generator,genmodel,ontologyDb,this,(Task)task,
123 	  nodesPanel.getPreconditionsModel(),
124 	  nodesPanel.getPostconditionsModel());
125        tabbedPane.addTab("Decomposition Graph", nodesPanel);
126        tabbedPane.addTab("Constraints", constraintsPanel);
127     }
128     else if ( task.isBehaviour() ) {
129        rulebasePanel = new RuleUI(ontologyDb,this,(ReteKB)task, new Vector());
130        tabbedPane.addTab("Behaviour Rulebase", rulebasePanel);
131     }
132 
133     tabbedPane.setSelectedIndex(0);
134     tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
135     getContentPane().add(tabbedPane,BorderLayout.CENTER);
136 
137     this.addWindowListener(new WindowAdapter() {
138           public void windowClosing(WindowEvent evt) { closeDown(); }
139        }
140     );
141     String name = genmodel.getTaskName(task.getName());
142     setTitle(task.getTypeName() + " " + MESSAGE[2] + name);
143     pack();
144     this.repaint();
145 
146     genmodel.addChangeListener(new SymChangeListener());
147   }
148 
149   public void previous() {
150     if ( (tabbedPane.getSelectedIndex() > 0) )
151        tabbedPane.setSelectedIndex(tabbedPane.getSelectedIndex() - 1);
152   }
153   public void next() {
154     if ( (tabbedPane.getSelectedIndex() + 1 < tabbedPane.getTabCount() ) )
155        tabbedPane.setSelectedIndex(tabbedPane.getSelectedIndex() + 1);
156   }
157   public void help(AbstractButton helpBtn) {
158     if ( helpBtn.isSelected() ) {
159        Point pt = getLocation();
160        helpWin = new HelpWindow(this, pt, "generator", "Task Specification");
161        helpWin.setSource(helpBtn);
162     }
163     else
164        helpWin.dispose();
165   }
166 
167   public void save() {
168     if ( currentTask.isPrimitive() ) {
169        conditionsPanel.save();
170        constraintsPanel.save();
171     }
172     else if ( currentTask.isSummary() || currentTask.isScript() ) {
173        nodesPanel.save();
174        constraintsPanel.save();
175     }
176     else if ( currentTask.isBehaviour() ) {
177        rulebasePanel.save();
178     }
179     genmodel.updateTask(currentTask);
180     changed = false;
181     fireChanged();
182   }
183 
184   public AbstractTask getCurrentTask() {
185      return currentTask;
186   }
187   public String getObjectName() {
188      return genmodel.getTaskName(currentTask.getName());
189   }
190 
191   public boolean hasChanged() {
192      return changed;
193   }
194 
195   public void stateChanged(ChangeEvent evt) {
196     if ( evt.getSource() != genmodel )
197        changed = true;
198     fireChanged();
199   }
200 
201   public void closeDown() {
202     if ( changed ) {
203        int answer = JOptionPane.showConfirmDialog(this,MESSAGE[0],
204           MESSAGE[1],JOptionPane.YES_NO_CANCEL_OPTION);
205        if ( answer == JOptionPane.YES_OPTION )
206           save();
207        else if ( answer == JOptionPane.CANCEL_OPTION ) {
208           setVisible(true);
209           return;
210        }
211     }
212     generator.taskEditorClosed(currentTask.getName());
213     dispose();
214   }
215 
216   protected class SymChangeListener implements ChangeListener {
217     public void stateChanged(ChangeEvent e) {
218        String taskName = genmodel.getTaskName(currentTask.getName());
219        setTitle(currentTask.getTypeName() + " " + MESSAGE[2] + taskName);
220        fireChanged();
221     }
222   }
223 
224   // -- ChangeListener Methods
225   public void addChangeListener(ChangeListener x) {
226     changeListeners.add(ChangeListener.class, x);
227   }
228   public void removeChangeListener(ChangeListener x) {
229     changeListeners.remove(ChangeListener.class, x);
230   }
231 
232   protected void fireChanged() {
233     ChangeEvent c = new ChangeEvent(this);
234     Object[] listeners = changeListeners.getListenerList();
235     for(int i = listeners.length-2; i >= 0; i -=2) {
236        if (listeners[i] == ChangeListener.class) {
237           ChangeListener cl = (ChangeListener)listeners[i+1];
238           cl.stateChanged(c);
239       }
240     }
241   }
242 }