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  * TaskNodePanel.java
26  *
27  * Panel through which task attributes are entered
28  ***************************************************************************/
29  
30  package zeus.generator.task;
31  
32  import java.awt.*;
33  import java.awt.event.*;
34  import java.util.*;
35  import javax.swing.*;
36  import javax.swing.border.*;
37  import javax.swing.text.*;
38  import javax.swing.event.*;
39  
40  import zeus.util.*;
41  import zeus.concepts.*;
42  import zeus.concepts.fn.*;
43  import zeus.generator.*;
44  import zeus.generator.event.*;
45  import zeus.generator.util.*;
46  import zeus.gui.fields.*;
47  import zeus.gui.help.*;
48  
49  public class TaskNodePanel extends JPanel {
50    static final String[] ERROR_MESSAGE = {
51       /* 0 */ "Invalid name"
52    };
53    protected EventListenerList nameListeners = new EventListenerList();
54    protected boolean           isConditionalNode;
55    protected NameField         namefield;
56    protected FactPanel         preconditionsPanel, postconditionsPanel;
57    protected JPanel            factPanel, leftPanel, rightPanel;
58    protected GroupManager      leftGroupManager, rightGroupManager;
59    protected JComboBox         leftCombo, rightCombo;
60    protected GroupToolBar      toolbar;
61    protected Hashtable         nameTable;
62    protected String            previousName = null;
63    protected AttributeModel    preAttrModel, postAttrModel;
64    protected AttributeTable    preAttrTable, postAttrTable;
65  
66    public TaskNodePanel(OntologyDb ontologyDb, ChangeListener parent)  {
67      GridBagLayout gridBagLayout = new GridBagLayout();
68      GridBagConstraints gbc = new GridBagConstraints();
69      setLayout(gridBagLayout);
70      setBackground(Color.lightGray);
71      setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
72  
73      // Add the panel show that shows the node name to this panel.
74      JPanel namePanel = new JPanel();
75      gbc.gridwidth = GridBagConstraints.REMAINDER;
76      gbc.anchor = GridBagConstraints.WEST;
77      gbc.fill = GridBagConstraints.HORIZONTAL;
78      gbc.insets = new Insets(8,8,8,8);
79      gridBagLayout.setConstraints(namePanel,gbc);
80      add(namePanel);
81  
82      // Add the panel containing the task's facts to this panel.
83      factPanel = new JPanel();
84      gbc.anchor = GridBagConstraints.NORTHWEST;
85      gbc.fill = GridBagConstraints.BOTH;
86      gbc.weightx = gbc.weighty = 1;
87      gridBagLayout.setConstraints(factPanel,gbc);
88      add(factPanel);
89  
90      // Cost Panel information
91      TitledBorder border =
92         BorderFactory.createTitledBorder("Node Name");
93      border.setTitlePosition(TitledBorder.TOP);
94      border.setTitleJustification(TitledBorder.RIGHT);
95      border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
96      border.setTitleColor(Color.blue);
97      namePanel.setBorder(border);
98  
99      gridBagLayout = new GridBagLayout();
100     namePanel.setLayout(gridBagLayout);
101     namePanel.setBackground(Color.lightGray);
102 
103     JLabel label = new JLabel("Name:");
104     label.setToolTipText("Name of this node");
105     gbc = new GridBagConstraints();
106     gbc.anchor = GridBagConstraints.WEST;
107     gbc.fill = GridBagConstraints.NONE;
108     gbc.gridwidth = 1;
109     gbc.insets = new Insets(0,8,0,0);
110     gridBagLayout.setConstraints(label,gbc);
111     namePanel.add(label);
112 
113     namefield = new NameField(20);
114     namefield.addChangeListener(parent);
115     namefield.addFocusListener(new SymFocusAction());
116 
117     gbc = new GridBagConstraints();
118     gbc.gridwidth = GridBagConstraints.REMAINDER;
119     gbc.anchor = GridBagConstraints.WEST;
120     gbc.fill = GridBagConstraints.NONE;
121     gbc.weightx = gbc.weighty = 1;
122     gbc.insets = new Insets(0,8,8,8);
123     gridBagLayout.setConstraints(namefield,gbc);
124     namePanel.add(namefield);
125 
126     // Add factPanel info
127     factPanel.setLayout(new GridLayout(1,2,5,5));
128     factPanel.setBackground(Color.lightGray);
129 
130     leftGroupManager = new GroupManager(ontologyDb, new AttributeModel(),
131        Fact.VARIABLE, FactPanel.PRECONDITION, new Fact[0]);
132     rightGroupManager = new GroupManager(ontologyDb, new AttributeModel(),
133        Fact.VARIABLE, FactPanel.POSTCONDITION, new Fact[0]);
134 
135     preconditionsPanel = new FactPanel(parent, FactPanel.PRECONDITION,
136        "Node Preconditions", leftGroupManager);
137     postconditionsPanel = new FactPanel(parent, FactPanel.POSTCONDITION,
138        "Node Effects", rightGroupManager);
139 
140     preAttrModel = preconditionsPanel.getAttributeModel();
141     postAttrModel = postconditionsPanel.getAttributeModel();
142 
143     preAttrTable = preconditionsPanel.getAttributeTable();
144     postAttrTable = postconditionsPanel.getAttributeTable();
145 
146     // for pre/post attribute renaming
147     leftGroupManager.addRenameListener(preAttrModel);
148     leftGroupManager.addRenameListener(postAttrModel);
149 
150     rightGroupManager.addRenameListener(preAttrModel);
151     rightGroupManager.addRenameListener(postAttrModel);
152 
153     // for pre/post conditions renaming
154     SymRenameAction symRenameAction = new SymRenameAction();
155     leftGroupManager.addRenameListener(symRenameAction);
156     rightGroupManager.addRenameListener(symRenameAction);
157 
158     nameTable = new Hashtable();
159 
160     // left and right panels
161     leftPanel = new JPanel();
162     gridBagLayout = new GridBagLayout();
163     leftPanel.setLayout(gridBagLayout);
164     leftPanel.setBackground(Color.lightGray);
165 
166     label = new JLabel("Group:");
167     label.setToolTipText("Name of this preconditions group");
168     gbc = new GridBagConstraints();
169     gbc.anchor = GridBagConstraints.WEST;
170     gbc.fill = GridBagConstraints.NONE;
171     gbc.gridwidth = 1;
172     gbc.insets = new Insets(0,8,0,0);
173     gridBagLayout.setConstraints(label,gbc);
174     leftPanel.add(label);
175 
176     leftCombo = new JComboBox(leftGroupManager.getComboBoxModel()) {
177        public void contentsChanged(ListDataEvent e) {
178           selectedItemReminder = null;
179           super.contentsChanged(e);
180        }
181     };
182 
183     leftCombo.setEnabled(false);
184     leftCombo.addActionListener(leftGroupManager);
185 
186     gbc = new GridBagConstraints();
187     gbc.gridwidth = GridBagConstraints.REMAINDER;
188     gbc.anchor = GridBagConstraints.WEST;
189     gbc.fill = GridBagConstraints.NONE;
190     gbc.insets = new Insets(0,8,0,8);
191     gridBagLayout.setConstraints(leftCombo,gbc);
192     leftPanel.add(leftCombo);
193 
194     gbc = new GridBagConstraints();
195     gbc.gridwidth = GridBagConstraints.REMAINDER;
196     gbc.anchor = GridBagConstraints.WEST;
197     gbc.fill = GridBagConstraints.BOTH;
198     gbc.weightx = gbc.weighty = 1;
199     gbc.insets = new Insets(8,8,8,8);
200     gridBagLayout.setConstraints(preconditionsPanel,gbc);
201     leftPanel.add(preconditionsPanel);
202 
203     rightPanel = new JPanel();
204     gridBagLayout = new GridBagLayout();
205     rightPanel.setLayout(gridBagLayout);
206     rightPanel.setBackground(Color.lightGray);
207 
208     label = new JLabel("Group:");
209     label.setToolTipText("Name of this postconditions group");
210     gbc = new GridBagConstraints();
211     gbc.anchor = GridBagConstraints.WEST;
212     gbc.fill = GridBagConstraints.NONE;
213     gbc.gridwidth = 1;
214     gbc.insets = new Insets(0,8,0,0);
215     gridBagLayout.setConstraints(label,gbc);
216     rightPanel.add(label);
217 
218     rightCombo = new JComboBox(rightGroupManager.getComboBoxModel()) {
219        public void contentsChanged(ListDataEvent e) {
220           selectedItemReminder = null;
221           super.contentsChanged(e);
222        }
223     };
224 
225     rightCombo.setEditable(true);
226     rightCombo.addActionListener(rightGroupManager);
227 
228     gbc = new GridBagConstraints();
229     gbc.anchor = GridBagConstraints.WEST;
230     gbc.fill = GridBagConstraints.NONE;
231     gbc.gridwidth = 1;
232     gbc.insets = new Insets(0,8,0,0);
233     gridBagLayout.setConstraints(rightCombo,gbc);
234     rightPanel.add(rightCombo);
235 
236     toolbar = new GroupToolBar(rightGroupManager);
237     gbc = new GridBagConstraints();
238     gbc.gridwidth = GridBagConstraints.REMAINDER;
239     gbc.anchor = GridBagConstraints.WEST;
240     gbc.fill = GridBagConstraints.NONE;
241     gbc.insets = new Insets(0,8,0,8);
242     gridBagLayout.setConstraints(toolbar,gbc);
243     rightPanel.add(toolbar);
244 
245     gbc = new GridBagConstraints();
246     gbc.gridwidth = GridBagConstraints.REMAINDER;
247     gbc.anchor = GridBagConstraints.WEST;
248     gbc.fill = GridBagConstraints.BOTH;
249     gbc.weightx = gbc.weighty = 1;
250     gbc.insets = new Insets(8,8,8,8);
251     gridBagLayout.setConstraints(postconditionsPanel,gbc);
252     rightPanel.add(postconditionsPanel);
253   }
254 
255   protected class GroupToolBar extends JToolBar
256                     implements ActionListener {
257 
258      protected HelpWindow    helpWin;
259      protected JToggleButton helpBtn;
260      protected JButton       newBtn;
261      protected JButton       deleteBtn;
262      protected JButton       copyBtn;
263      protected GroupManager  groupManager;
264 
265      public GroupToolBar(GroupManager groupManager) {
266         this.groupManager = groupManager;
267 
268         setBackground(Color.lightGray);
269         setBorder(new BevelBorder(BevelBorder.LOWERED));
270         setMargin(new Insets(0,0,0,0));
271         setFloatable(false);
272 
273         String sep = System.getProperty("file.separator");
274         String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
275 
276         // New Button
277         newBtn = new JButton(new ImageIcon(path + "new1.gif"));
278 	newBtn.setMargin(new Insets(0,0,0,0));
279         add(newBtn);
280         newBtn.setToolTipText("New group");
281         newBtn.addActionListener(this);
282 
283         // Delete Button
284         deleteBtn = new JButton(new ImageIcon(path + "delete1.gif"));
285 	deleteBtn.setMargin(new Insets(0,0,0,0));
286         add(deleteBtn);
287         deleteBtn.setToolTipText("Delete group");
288         deleteBtn.addActionListener(this);
289 
290         // Clone Button
291         copyBtn = new JButton(new ImageIcon(path + "copy.gif"));
292 	copyBtn.setMargin(new Insets(0,0,0,0));
293         add(copyBtn);
294         copyBtn.setToolTipText("Copy group");
295         copyBtn.addActionListener(this);
296 
297         addSeparator();
298 
299         // Help Button
300         helpBtn = new JToggleButton(new ImageIcon(path + "help.gif"));
301 	helpBtn.setMargin(new Insets(0,0,0,0));
302         add(helpBtn);
303         helpBtn.setToolTipText("Help");
304         helpBtn.addActionListener(this);
305      }
306 
307      public void setEnabled(boolean set) {
308         newBtn.setEnabled(set);
309         deleteBtn.setEnabled(set);
310         copyBtn.setEnabled(set);
311         helpBtn.setEnabled(set);
312      }
313 
314      public void actionPerformed(ActionEvent e)  {
315         Object src = e.getSource();
316         if ( src == newBtn )
317            groupManager.newGroup();
318         else if ( src == deleteBtn )
319            groupManager.deleteGroup();
320         else if ( src == copyBtn )
321            groupManager.copyGroup();
322         else if ( src == helpBtn ) {
323           if ( helpBtn.isSelected() ) {
324               Point pt = getLocation();
325               helpWin = new HelpWindow(SwingUtilities.getRoot(this),
326                  pt, "generator", "Task Node Conditions Group");
327               helpWin.setSource(helpBtn);
328           }
329           else
330               helpWin.dispose();
331         }
332      }
333   }
334 
335   protected class SymRenameAction implements RenameListener {
336      public void nameChanged(RenameEvent e) {
337         Object src = e.getSource();
338         if ( src == leftGroupManager || src == rightGroupManager ) {
339            nameTable.put(e.getOriginal(),e.getCurrent());
340         }
341      }
342   }
343   protected class SymFocusAction implements FocusListener, ActionListener {
344      public void focusGained(FocusEvent e) {
345      }
346      public void focusLost(FocusEvent e) {
347         this.update(e);
348      }
349      public void actionPerformed(ActionEvent e) {
350         this.update(e);
351      }
352      public void update(AWTEvent e) {
353         Object src = e.getSource();
354         if ( src == namefield ) {
355            if ( previousName != null ) {
356               String name = namefield.getText();
357               if ( name == null ) {
358                  errorMsg(0);
359                  namefield.setText(previousName);
360               }
361               name = name.trim();
362               if ( name.equals("") ) {
363                  errorMsg(0);
364                  namefield.setText(previousName);
365               }
366               if ( !previousName.equals(name) ) {
367                  nameTable.put(previousName,name);
368                  fireRenameAction(namefield,previousName,name);
369               }
370            }
371         }
372      }
373   }
374 
375   protected void errorMsg(int tag) {
376      JOptionPane.showMessageDialog(this,ERROR_MESSAGE[tag],
377                                    "Error", JOptionPane.ERROR_MESSAGE);
378   }
379 
380   GroupManager getPostconditionsManager() { return rightGroupManager; }
381   GroupManager getPreconditionsManager()  { return leftGroupManager; }
382 
383   TaskNode getNode() {
384      TaskNode node;
385      if ( isConditionalNode )
386         node = new ConditionalNode(namefield.getText());
387      else
388         node = new TaskNode(namefield.getText());
389      node.setPreconditions(leftGroupManager.getManagerData());
390      node.setPostconditions(rightGroupManager.getManagerData());
391      return node;
392   }
393 
394   Hashtable getNameTable() {
395      return nameTable;
396   }
397 
398   void reset(TaskNode node) {
399      isConditionalNode = false;
400      previousName = node.getName();
401      namefield.setText(previousName);
402 
403      factPanel.removeAll();
404      nameTable.clear();
405 
406      leftGroupManager.removeRelatedModel(rightGroupManager);
407      rightGroupManager.removeRelatedModel(leftGroupManager);
408      leftGroupManager.removeFactModelListener(rightGroupManager);
409      leftGroupManager.resetManager(node.getAllPreconditions());
410      rightGroupManager.resetManager(node.getAllPostconditions());
411 
412      postconditionsPanel.setToolBarState(true);
413      rightGroupManager.setEditable(true);
414 
415      if ( node.isBeginNode() ) {
416         namefield.setEditable(false);
417         factPanel.add(rightPanel);
418         toolbar.setEnabled(false);
419         rightCombo.setEnabled(false);
420         postAttrTable.setFactModel(rightGroupManager);
421      }
422      else if ( node.isEndNode() ) {
423         namefield.setEditable(false);
424         factPanel.add(leftPanel);
425         preAttrTable.setFactModel(leftGroupManager);
426      }
427      else if ( node.isConditionalNode() ) {
428         isConditionalNode = true;
429 
430         namefield.setEditable(true);
431         factPanel.add(leftPanel);
432         factPanel.add(rightPanel);
433         toolbar.setEnabled(true);
434         rightCombo.setEnabled(true);
435         postconditionsPanel.setToolBarState(false);
436         rightGroupManager.setEditable(false);
437 	leftGroupManager.addFactModelListener(rightGroupManager);
438 
439         preAttrTable.setFactModel(leftGroupManager);
440         postAttrTable.setFactModel(leftGroupManager);
441 
442         rightGroupManager.resetManager(node.getAllPostconditions(),
443            node.getPreconditions());
444      }
445      else {
446         namefield.setEditable(true);
447         factPanel.add(leftPanel);
448         factPanel.add(rightPanel);
449         toolbar.setEnabled(false);
450         rightCombo.setEnabled(false);
451 
452         leftGroupManager.addRelatedModel(rightGroupManager);
453         rightGroupManager.addRelatedModel(leftGroupManager);
454 
455         preAttrTable.setFactModels(leftGroupManager,rightGroupManager);
456         postAttrTable.setFactModels(leftGroupManager,rightGroupManager);
457      }
458   }
459 
460   public void addRenameListener(RenameListener x) {
461      nameListeners.add(RenameListener.class, x);
462      leftGroupManager.addRenameListener(x);
463      rightGroupManager.addRenameListener(x);
464   }
465   public void removeRenameListener(RenameListener x) {
466      nameListeners.remove(RenameListener.class, x);
467      leftGroupManager.removeRenameListener(x);
468      rightGroupManager.removeRenameListener(x);
469   }
470 
471   protected void fireRenameAction(Object src, Object prev, Object curr) {
472      RenameEvent e = new RenameEvent(this,src,prev,curr);
473      Object[] listeners = nameListeners.getListenerList();
474      for(int i = listeners.length-2; i >= 0; i -= 2) {
475         if (listeners[i] == RenameListener.class) {
476            RenameListener l = (RenameListener)listeners[i+1];
477            l.nameChanged(e);
478         }
479      }
480   }
481 }