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  * ConditionsPanel.java
24  *
25  * Panel through which task attributes are entered
26  ***************************************************************************/
27  
28  package zeus.generator.task;
29  
30  import java.awt.*;
31  import java.awt.event.*;
32  import java.util.*;
33  import javax.swing.*;
34  import javax.swing.border.*;
35  import javax.swing.text.*;
36  import javax.swing.event.*;
37  
38  import zeus.util.*;
39  import zeus.concepts.*;
40  import zeus.concepts.fn.*;
41  import zeus.generator.*;
42  import zeus.generator.event.*;
43  import zeus.generator.util.*;
44  import zeus.gui.fields.*;
45  
46  /*****************************************************************************
47  * ConditionsPanel.java
48  *
49  * Panel through which task attributes are entered
50  ***************************************************************************/
51  public class ConditionsPanel extends JPanel {
52    protected PrimitiveTask  task;
53    protected LargeTextField costfield;
54    protected LargeTextField timefield;
55    protected FactPanel      preconditionsPanel;
56    protected FactPanel      postconditionsPanel;
57    protected FactModel      postconditionsModel;
58    protected FactModel      preconditionsModel;
59  
60  
61    public ConditionsPanel(AgentGenerator generator,
62                           GeneratorModel genmodel,
63                           OntologyDb ontologyDb,
64                           TaskEditor editor,
65                           PrimitiveTask task)  {
66  
67      this.task = task;
68  
69      GridBagLayout gridBagLayout = new GridBagLayout();
70      GridBagConstraints gbc = new GridBagConstraints();
71      setLayout(gridBagLayout);
72      setBackground(Color.lightGray);    
73      setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
74      
75      // Add the control panel 
76      ControlPanel controlPane =
77         new ControlPanel(editor,"Task Preconditions and Effects",true,false);
78      gbc.gridwidth = GridBagConstraints.REMAINDER;
79      gbc.anchor = GridBagConstraints.NORTHWEST;
80      gbc.fill = GridBagConstraints.HORIZONTAL;
81      gbc.insets = new Insets(8,8,8,8);
82      gridBagLayout.setConstraints(controlPane,gbc);
83      add(controlPane);
84  
85      // Add the panel show the flow metaphor to this panel.
86      JPanel flowPanel = new JPanel();
87      gbc.gridwidth = GridBagConstraints.REMAINDER;
88      gbc.anchor = GridBagConstraints.WEST;
89      gbc.fill = GridBagConstraints.HORIZONTAL;
90      gbc.insets = new Insets(8,8,8,8);
91      gridBagLayout.setConstraints(flowPanel,gbc);
92      add(flowPanel);
93      
94      // Add the panel containing the task's facts to this panel.   
95      JPanel factPanel = new JPanel();
96      gbc.anchor = GridBagConstraints.NORTHWEST;
97      gbc.fill = GridBagConstraints.BOTH;
98      gbc.weightx = gbc.weighty = 1;
99      gridBagLayout.setConstraints(factPanel,gbc);
100     add(factPanel);
101 
102     // Add the panel containing the task's cost and time to the panel.
103     JPanel costPanel = new JPanel();
104     gbc.gridwidth = GridBagConstraints.REMAINDER;
105     gbc.anchor = GridBagConstraints.SOUTHWEST;
106     gbc.fill = GridBagConstraints.HORIZONTAL;
107     gbc.insets = new Insets(8,8,8,8);
108     gbc.weightx = gbc.weighty = 0;
109     gridBagLayout.setConstraints(costPanel,gbc);
110     add(costPanel);
111 
112     // Cost Panel information
113     TitledBorder border =
114        BorderFactory.createTitledBorder("Task Cost and Time");
115     border.setTitlePosition(TitledBorder.TOP);
116     border.setTitleJustification(TitledBorder.RIGHT);
117     border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
118     border.setTitleColor(Color.blue);
119     costPanel.setBorder(border);
120 
121     gridBagLayout = new GridBagLayout();
122     costPanel.setLayout(gridBagLayout);
123     costPanel.setBackground(Color.lightGray);
124 
125     JLabel label = new JLabel("Cost:");
126     label.setToolTipText("Default cost of performing this task");
127     gbc = new GridBagConstraints();
128     gbc.anchor = GridBagConstraints.WEST;
129     gbc.fill = GridBagConstraints.NONE;
130     gbc.gridwidth = 1;
131     gbc.insets = new Insets(0,16,0,0);
132     gridBagLayout.setConstraints(label,gbc);
133     costPanel.add(label);
134 
135     costfield = new LargeTextField(2,15);
136     costfield.setLineWrap(true);
137     costfield.setText(task.getCostFn().toString());
138     costfield.addChangeListener(editor);
139 
140     gbc = new GridBagConstraints();
141     gbc.gridwidth = GridBagConstraints.REMAINDER;
142     gbc.anchor = GridBagConstraints.WEST;
143     gbc.fill = GridBagConstraints.HORIZONTAL;
144     gbc.weightx = gbc.weighty = 1;
145     gbc.insets = new Insets(0,16,0,16);
146     gridBagLayout.setConstraints(costfield,gbc);
147     costPanel.add(costfield);
148 
149     label = new JLabel("Time:");
150     label.setToolTipText("Default duration of this task");
151     gbc = new GridBagConstraints();
152     gbc.anchor = GridBagConstraints.WEST;
153     gbc.fill = GridBagConstraints.NONE;
154     gbc.gridwidth = 1;
155     gbc.insets = new Insets(16,16,16,0);
156     gridBagLayout.setConstraints(label,gbc);
157     costPanel.add(label);
158 
159     timefield = new LargeTextField(2,15);
160     timefield.setLineWrap(true);
161     timefield.setText(task.getTimeFn().toString());
162     timefield.addChangeListener(editor);
163 
164     gbc = new GridBagConstraints();
165     gbc.gridwidth = GridBagConstraints.REMAINDER;
166     gbc.anchor = GridBagConstraints.WEST;
167     gbc.fill = GridBagConstraints.HORIZONTAL;
168     gbc.weightx = gbc.weighty = 1;
169     gbc.insets = new Insets(16,16,16,16);
170     gridBagLayout.setConstraints(timefield,gbc);
171     costPanel.add(timefield);
172 
173     // Flow panel info
174     String sep = System.getProperty("file.separator");
175     String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
176 
177     gridBagLayout = new GridBagLayout();
178     flowPanel.setLayout(gridBagLayout);
179     flowPanel.setBackground(Color.lightGray);
180 
181     label = new JLabel(new ImageIcon(path + "uparrow.gif"));
182     gbc = new GridBagConstraints();
183     gbc.weightx = gbc.weighty = 1;
184     gbc.anchor = GridBagConstraints.WEST;
185     gbc.fill = GridBagConstraints.BOTH;
186     gbc.insets = new Insets(0,0,0,0);
187     gridBagLayout.setConstraints(label,gbc);
188     flowPanel.add(label);
189 
190     label = new JLabel(new ImageIcon(path + "arrow.gif"));
191     gbc.insets = new Insets(0,0,0,0);
192     gridBagLayout.setConstraints(label,gbc);
193     flowPanel.add(label);
194 
195     label = new JLabel(new ImageIcon(path + "body.gif"));
196     gbc.insets = new Insets(0,0,0,0);
197     gridBagLayout.setConstraints(label,gbc);
198     flowPanel.add(label);
199 
200     label = new JLabel(new ImageIcon(path + "arrow.gif"));
201     gbc.insets = new Insets(0,0,0,0);
202     gridBagLayout.setConstraints(label,gbc);
203     flowPanel.add(label);
204 
205     label = new JLabel(new ImageIcon(path + "downarrow.gif"));
206     gbc.insets = new Insets(0,0,0,0);
207     gridBagLayout.setConstraints(label,gbc);
208     flowPanel.add(label);
209 
210     // Add factPanel info
211     factPanel.setLayout(new GridLayout(1,2,5,5));
212     factPanel.setBackground(Color.lightGray);
213 
214     preconditionsPanel = new FactPanel(
215        ontologyDb,editor,task.getPreconditions(),Fact.VARIABLE,
216        FactPanel.PRECONDITION,"Task Inputs/Preconditions");
217     postconditionsPanel = new FactPanel(
218        ontologyDb,editor,task.getPostconditions(),Fact.VARIABLE,
219        FactPanel.POSTCONDITION,"Task Effects/Outputs");
220 
221     preconditionsModel = preconditionsPanel.getFactModel();
222     postconditionsModel = postconditionsPanel.getFactModel();
223 
224     AttributeModel preAttrModel = preconditionsPanel.getAttributeModel();
225     AttributeModel postAttrModel = postconditionsPanel.getAttributeModel();
226 
227     AttributeTable preAttrTable = preconditionsPanel.getAttributeTable();
228     AttributeTable postAttrTable = postconditionsPanel.getAttributeTable();
229 
230     // for supporting attribute tree creation
231     preAttrTable.setFactModels(preconditionsModel,postconditionsModel);
232     postAttrTable.setFactModels(preconditionsModel,postconditionsModel);
233 
234     // for uniqueness checking
235     preconditionsModel.addRelatedModel(postconditionsModel);
236     postconditionsModel.addRelatedModel(preconditionsModel);
237 
238     // for renaming cost/time expressions
239     SymRenameAction rs = new SymRenameAction();
240     preconditionsModel.addRenameListener(rs);
241     postconditionsModel.addRenameListener(rs);
242 
243     // for pre/post attribute renaming
244     preconditionsModel.addRenameListener(preAttrModel);
245     preconditionsModel.addRenameListener(postAttrModel);
246 
247     postconditionsModel.addRenameListener(preAttrModel);
248     postconditionsModel.addRenameListener(postAttrModel);
249 
250     // for validating cost/time expressions
251     SymFocusAction fs = new SymFocusAction();
252     costfield.addFocusListener(fs);
253     timefield.addFocusListener(fs);
254 
255     // for supporting attribute tree creation for cost/time fields
256     SymMouseAction ms  = new SymMouseAction();
257     costfield.addMouseListener(ms);
258     timefield.addMouseListener(ms);
259 
260     factPanel.add(preconditionsPanel);
261     factPanel.add(postconditionsPanel);
262   }
263 
264   class SymFocusAction implements FocusListener {
265     public void focusLost(FocusEvent e) {
266        LargeTextField field = (LargeTextField)e.getSource();
267        validate(field);
268     }
269     public void focusGained(FocusEvent e) {
270        LargeTextField field = (LargeTextField)e.getSource();
271        field.setForeground(Color.black);
272     }
273     protected void validate(LargeTextField field) {
274        String s = field.getText();
275        ValueFunction fn = ZeusParser.Expression(s);
276        if ( fn != null && (fn instanceof NumericFn || fn instanceof ElseFn) )
277           field.setForeground(Color.black);
278        else
279           field.setForeground(Color.red);
280     }
281   }
282 
283   class SymMouseAction extends MouseAdapter implements AttributeSelector {
284      protected JTextComponent field = null;
285      protected AttributeDialog dialog = null;
286      protected AttributeTreeModel attributeTreeModel = null;
287 
288      public SymMouseAction() {
289         attributeTreeModel = new AttributeTreeModel();
290         attributeTreeModel.setFactModels(preconditionsModel,
291 	                                 postconditionsModel);
292      }
293      public void mouseClicked(MouseEvent e) {
294         if ( SwingUtilities.isRightMouseButton(e) ) {
295            field = (JTextComponent)e.getSource();
296            if ( dialog == null )
297               dialog = new AttributeDialog(
298 	         (Frame)SwingUtilities.getRoot(field),attributeTreeModel);
299            field = (JTextComponent)e.getSource();
300            dialog.setLocationRelativeTo(field);
301            dialog.display(this);
302         }
303      }
304      public void attributeSelected(String attribute) {
305         try {
306 	   Document doc = field.getDocument();
307            int length = doc.getLength();
308            AttributeSet a = doc.getDefaultRootElement().getAttributes();
309            doc.insertString(length,attribute,a);
310 	}
311 	catch(BadLocationException e) {
312 	}
313      }
314   }
315 
316   class SymRenameAction implements RenameListener {
317     public void nameChanged(RenameEvent e) {
318        String prev = (String)e.getOriginal();
319        String curr = (String)e.getCurrent();
320 
321        String s;
322        s = costfield.getText();
323        costfield.setText(Misc.substitute(s,prev,curr));
324        s = timefield.getText();
325        timefield.setText(Misc.substitute(s,prev,curr));
326     }
327   }
328 
329   FactModel getPostconditionsModel() { return postconditionsModel; }
330   FactModel getPreconditionsModel()  { return preconditionsModel; }
331 
332   void save() {
333      task.setCostFn(costfield.getText());
334      task.setTimeFn(timefield.getText());
335      task.setPreconditions(preconditionsPanel.getData());
336      task.setPostconditions(postconditionsPanel.getData());
337   }
338 }