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  * ConstraintsPanel.java
26  *
27  * Panel through which task constraints 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.table.*;
38  import javax.swing.text.*;
39  import javax.swing.event.*;
40  
41  import zeus.util.*;
42  import zeus.concepts.*;
43  import zeus.generator.*;
44  import zeus.generator.util.*;
45  import zeus.gui.fields.*;
46  import zeus.gui.editors.*;
47  import zeus.gui.help.*;
48  
49  public class ConstraintsPanel extends JPanel {
50    static final String[] ERROR_MESSAGE = {
51       /* 0 */ "Please select a row before\ncalling this operation"
52    };
53  
54    protected Task             task;
55    protected JTable           constraintsTable;
56    protected JTable           orderingTable;
57    protected OrderingModel    orderingModel;
58    protected ConstraintsModel constraintsModel;
59    protected BasicFactModel   precondsModel;
60    protected BasicFactModel   postcondsModel;
61  
62    public ConstraintsPanel(AgentGenerator generator,
63                            GeneratorModel genmodel,
64                            OntologyDb ontologyDb,
65                            TaskEditor editor,
66                            Task task,
67                            BasicFactModel precondsModel,
68                            BasicFactModel postcondsModel)  {
69  
70      this.task = task;
71      this.precondsModel = precondsModel;
72      this.postcondsModel = postcondsModel;
73  
74      GridBagLayout gridBagLayout = new GridBagLayout();
75      GridBagConstraints gbc = new GridBagConstraints();
76      setLayout(gridBagLayout);
77      setBackground(Color.lightGray);
78      setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
79  
80      // Add the control panel
81      ControlPanel controlPane =
82         new ControlPanel(editor,"Task Constraints",false,true);
83      gbc.gridwidth = GridBagConstraints.REMAINDER;
84      gbc.anchor = GridBagConstraints.NORTHWEST;
85      gbc.fill = GridBagConstraints.HORIZONTAL;
86      gbc.insets = new Insets(8,8,8,8);
87      gridBagLayout.setConstraints(controlPane,gbc);
88      add(controlPane);
89  
90      JPanel orderingPanel = null;
91      if ( task.isPrimitive() ) {
92         orderingPanel = new JPanel();
93         gbc.gridwidth = GridBagConstraints.REMAINDER;
94         gbc.anchor = GridBagConstraints.WEST;
95         gbc.fill = GridBagConstraints.BOTH;
96         gbc.insets = new Insets(8,8,8,8);
97         gridBagLayout.setConstraints(orderingPanel,gbc);
98         add(orderingPanel);
99      }
100 
101     // Add the panel containing the task's applicability constraints
102     // to this panel.
103     JPanel constraintsPanel = new JPanel();
104     gbc.anchor = GridBagConstraints.NORTHWEST;
105     gbc.fill = GridBagConstraints.BOTH;
106     gbc.weightx = gbc.weighty = 1;
107        gbc.insets = new Insets(8,8,8,8);
108     gridBagLayout.setConstraints(constraintsPanel,gbc);
109     add(constraintsPanel);
110 
111     TableColumnModel tm;
112     TableColumn column;
113     ValidatingCellRenderer renderer;
114     TitledBorder border;
115     JToolBar toolbar;
116     JScrollPane scrollPane;
117 
118     // Ordering panel info
119 
120     if ( task.isPrimitive() ) {
121        orderingModel = new OrderingModel(precondsModel,((PrimitiveTask)task).getOrdering());
122        orderingModel.addChangeListener(editor);
123 
124        tm = new DefaultTableColumnModel();
125        renderer = new ValidatingCellRenderer(orderingModel);
126        OrderingCellEditor cellEditor = new OrderingCellEditor();
127        column = new TableColumn(OrderingModel.BEFORE,12,renderer,cellEditor);
128        column.setHeaderValue(orderingModel.getColumnName(OrderingModel.BEFORE));
129        tm.addColumn(column);
130        column = new TableColumn(OrderingModel.AFTER,12,renderer,cellEditor);
131        column.setHeaderValue(orderingModel.getColumnName(OrderingModel.AFTER));
132        tm.addColumn(column);
133 
134        orderingTable = new JTable(orderingModel,tm);
135        orderingTable.getTableHeader().setReorderingAllowed(false);
136        orderingTable.setColumnSelectionAllowed(false);
137 
138        toolbar = new OrderingToolBar();
139 
140        border = BorderFactory.createTitledBorder("Preconditions Ordering Constraints");
141        border.setTitlePosition(TitledBorder.TOP);
142        border.setTitleJustification(TitledBorder.RIGHT);
143        border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
144        border.setTitleColor(Color.blue);
145        orderingPanel.setBorder(border);
146 
147        gridBagLayout = new GridBagLayout();
148        orderingPanel.setLayout(gridBagLayout);
149        orderingPanel.setBackground(Color.lightGray);
150 
151        scrollPane = new JScrollPane(orderingTable);
152        scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
153        scrollPane.setPreferredSize(new Dimension(340,150));
154        orderingTable.setBackground(Color.white);
155 
156        orderingTable.setMinimumSize(new Dimension(400,200));
157 
158        gbc = new GridBagConstraints();
159        gbc.anchor = GridBagConstraints.WEST;
160        gbc.fill = GridBagConstraints.NONE;
161        gbc.gridwidth = GridBagConstraints.REMAINDER;
162        gbc.insets = new Insets(0,8,0,0);
163        gridBagLayout.setConstraints(toolbar,gbc);
164        orderingPanel.add(toolbar);
165 
166        gbc = new GridBagConstraints();
167        gbc.gridwidth = GridBagConstraints.REMAINDER;
168        gbc.anchor = GridBagConstraints.WEST;
169        gbc.fill = GridBagConstraints.BOTH;
170        gbc.weightx = gbc.weighty = 1;
171        gbc.insets = new Insets(8,8,8,8);
172        gridBagLayout.setConstraints(scrollPane,gbc);
173        orderingPanel.add(scrollPane);
174     }
175 
176     // Add constraintsPanel info;
177 
178     constraintsModel = new ConstraintsModel(
179        precondsModel,postcondsModel,task.getConstraints());
180     constraintsModel.addChangeListener(editor);
181 
182     ExpressionCellEditor cellEditor1 = new ExpressionCellEditor(constraintsModel);
183     cellEditor1.addMouseListener(new SymMouseAction());
184     renderer = new ValidatingCellRenderer(constraintsModel);
185     tm = new DefaultTableColumnModel();
186     column = new TableColumn(ConstraintsModel.CONSTRAINT,12,
187        renderer, cellEditor1);
188     column.setHeaderValue(constraintsModel.getColumnName(ConstraintsModel.CONSTRAINT));
189     tm.addColumn(column);
190 
191     constraintsTable = new JTable(constraintsModel,tm);
192     constraintsTable.getTableHeader().setReorderingAllowed(false);
193     constraintsTable.setColumnSelectionAllowed(false);
194 
195     toolbar = new ConstraintsToolBar();
196 
197     border = BorderFactory.createTitledBorder("Task Applicability Constraints");
198     border.setTitlePosition(TitledBorder.TOP);
199     border.setTitleJustification(TitledBorder.RIGHT);
200     border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
201     border.setTitleColor(Color.blue);
202     constraintsPanel.setBorder(border);
203 
204     gridBagLayout = new GridBagLayout();
205     constraintsPanel.setLayout(gridBagLayout);
206     constraintsPanel.setBackground(Color.lightGray);
207 
208     scrollPane = new JScrollPane(constraintsTable);
209     scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
210     scrollPane.setPreferredSize(new Dimension(340,150));
211     constraintsTable.setBackground(Color.white);
212 
213     constraintsTable.setMinimumSize(new Dimension(400,200));
214 //    constraintsTable.setPreferredSize(new Dimension(400,200));
215 
216     gbc = new GridBagConstraints();
217     gbc.anchor = GridBagConstraints.WEST;
218     gbc.fill = GridBagConstraints.NONE;
219     gbc.gridwidth = GridBagConstraints.REMAINDER;
220     gbc.insets = new Insets(0,8,0,0);
221     gridBagLayout.setConstraints(toolbar,gbc);
222     constraintsPanel.add(toolbar);
223 
224     gbc = new GridBagConstraints();
225     gbc.gridwidth = GridBagConstraints.REMAINDER;
226     gbc.anchor = GridBagConstraints.WEST;
227     gbc.fill = GridBagConstraints.BOTH;
228     gbc.weightx = gbc.weighty = 1;
229     gbc.insets = new Insets(8,8,8,8);
230     gridBagLayout.setConstraints(scrollPane,gbc);
231     constraintsPanel.add(scrollPane);
232   }
233 
234   class OrderingToolBar extends JToolBar
235                         implements ActionListener, OrderingSelector {
236 
237      protected HelpWindow     helpWin;
238      protected JToggleButton  helpBtn;
239      protected JButton        newBtn;
240      protected JButton        deleteBtn;
241      protected OrderingDialog dialog;
242 
243      public OrderingToolBar() {
244         setBackground(java.awt.Color.lightGray);
245         setBorder( new BevelBorder(BevelBorder.LOWERED ) );
246         setFloatable(false);
247 
248         String sep = System.getProperty("file.separator");
249         String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
250 
251         // New Button
252         newBtn = new JButton(new ImageIcon(path + "new1.gif"));
253 	newBtn.setMargin(new Insets(0,0,0,0));
254         add(newBtn);
255         newBtn.setToolTipText("New");    
256         newBtn.addActionListener(this);
257 
258         // Delete Button
259         deleteBtn = new JButton(new ImageIcon(path + "delete1.gif"));
260 	deleteBtn.setMargin(new Insets(0,0,0,0));	
261         add(deleteBtn);
262         deleteBtn.setToolTipText("Delete");
263         deleteBtn.addActionListener(this);
264 
265         addSeparator();
266      
267         // Help Button
268         helpBtn = new JToggleButton(new ImageIcon(path + "help.gif"));
269 	helpBtn.setMargin(new Insets(0,0,0,0));	
270         add(helpBtn);
271         helpBtn.setToolTipText("Help");
272         helpBtn.addActionListener(this);
273 
274         dialog = new OrderingDialog((Frame)SwingUtilities.getRoot(this),
275                                     "Select ordering", precondsModel);
276      }
277   
278      public void setEnabled(boolean set) {
279         newBtn.setEnabled(set);
280         deleteBtn.setEnabled(set);
281      }
282 
283      public void orderingSelected(String left, String[] right) {
284         orderingModel.addNewRows(left,right);
285      }
286 
287      public void actionPerformed(ActionEvent e)  {
288         Object src = e.getSource();
289         if ( src == newBtn ) {
290            dialog.setLocationRelativeTo(newBtn);	
291            dialog.display(this);
292         }
293         else if ( src == deleteBtn ) {
294            if ( !isRowSelected(orderingTable) ) return;
295            orderingModel.removeRows(orderingTable.getSelectedRows());
296         }  
297         else if ( src == helpBtn ) {
298           if ( helpBtn.isSelected() ) {
299               Point dispos = getLocation();
300               helpWin = new HelpWindow(SwingUtilities.getRoot(this),
301                  dispos, "generator", "Preconditions Ordering Table");
302               helpWin.setSource(helpBtn);
303           }
304           else
305               helpWin.dispose();
306         }
307      }
308   }
309 
310   class ConstraintsToolBar extends JToolBar implements ActionListener {
311 
312      protected HelpWindow    helpWin;
313      protected JToggleButton helpBtn;
314      protected JButton       newBtn;
315      protected JButton       deleteBtn;
316     
317      public ConstraintsToolBar() {
318         setBackground(java.awt.Color.lightGray);
319         setBorder( new BevelBorder(BevelBorder.LOWERED ) );
320         setFloatable(false);
321     
322         String sep = System.getProperty("file.separator");
323         String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
324 
325         // New Button
326         newBtn = new JButton(new ImageIcon(path + "new1.gif"));
327 	newBtn.setMargin(new Insets(0,0,0,0));
328         add(newBtn);
329         newBtn.setToolTipText("New");    
330         newBtn.addActionListener(this);
331 
332         // Delete Button
333         deleteBtn = new JButton(new ImageIcon(path + "delete1.gif"));
334 	deleteBtn.setMargin(new Insets(0,0,0,0));	
335         add(deleteBtn);
336         deleteBtn.setToolTipText("Delete");
337         deleteBtn.addActionListener(this);
338 
339         addSeparator();
340      
341         // Help Button
342         helpBtn = new JToggleButton(new ImageIcon(path + "help.gif"));
343 	helpBtn.setMargin(new Insets(0,0,0,0));	
344         add(helpBtn);
345         helpBtn.setToolTipText("Help");
346         helpBtn.addActionListener(this);
347      }
348   
349      public void setEnabled(boolean set) {
350         newBtn.setEnabled(set);
351         deleteBtn.setEnabled(set);
352      }
353 
354      public void actionPerformed(ActionEvent e)  {
355         Object src = e.getSource();
356         if ( src == newBtn ) {
357            constraintsModel.addNewRow();
358         }
359         else if ( src == deleteBtn ) {
360            if ( !isRowSelected(constraintsTable) ) return;
361            constraintsModel.removeRows(constraintsTable.getSelectedRows());
362         }  
363         else if ( src == helpBtn ) {
364           if ( helpBtn.isSelected() ) {
365               Point dispos = getLocation();
366               helpWin = new HelpWindow(SwingUtilities.getRoot(this),
367                  dispos, "generator", "Task Applicability Constraints Table");
368               helpWin.setSource(helpBtn);
369           }
370           else
371               helpWin.dispose();
372         }
373      }
374   }
375 
376   class OrderingCellEditor extends DefaultCellEditor {
377      public OrderingCellEditor() {
378         super( new JComboBox() {
379                       public void contentsChanged(ListDataEvent e) {
380                          selectedItemReminder = null; 
381                          super.contentsChanged( e );
382                }
383            }
384         );
385 	setClickCountToStart(2);
386      }
387 
388      public Component getTableCellEditorComponent(JTable table, Object value,
389                                                   boolean isSelected, 
390                                                   int row, int column) {
391 
392         JComboBox combo = ((JComboBox)editorComponent);
393         Fact[] facts = precondsModel.getData();
394         String[] items = new String[facts.length];
395         for(int i = 0; i < facts.length; i++ )
396            items[i] = facts[i].getId();
397 
398         if ( combo.getItemCount() != 0 )
399 	   combo.removeAllItems();
400         for(int i = 0; i < items.length; i++ ) {
401            combo.addItem(items[i]);
402         }
403         combo.setSelectedItem(value);
404         return super.getTableCellEditorComponent(table,value,isSelected,row,column);
405      }
406   }
407 
408   class SymMouseAction extends MouseAdapter implements AttributeSelector {
409      protected JTextComponent field = null;
410      protected AttributeDialog dialog = null;
411      protected AttributeTreeModel attributeTreeModel = null;
412 
413      public SymMouseAction() {
414         attributeTreeModel = new AttributeTreeModel();
415         attributeTreeModel.setFactModels(precondsModel,postcondsModel);
416      }
417      public void mouseClicked(MouseEvent e) {
418         if ( SwingUtilities.isRightMouseButton(e) ) {
419            field = (JTextComponent)e.getSource();
420            if ( dialog == null )
421               dialog = new AttributeDialog(
422 	         (Frame)SwingUtilities.getRoot(field),attributeTreeModel);
423            dialog.setLocationRelativeTo(field);
424            dialog.display(this);
425         }
426      }
427      public void attributeSelected(String attribute) {
428         try {
429 	   Document doc = field.getDocument();
430            int length = doc.getLength();
431            AttributeSet a = doc.getDefaultRootElement().getAttributes();
432            doc.insertString(length,attribute,a);
433 	}
434 	catch(BadLocationException e) {
435 	}
436      }
437   }
438 
439   protected boolean isRowSelected(JTable table) {
440      int row = table.getSelectedRow();
441      if ( row == -1) {
442         errorMsg(0);
443         return false;
444      }
445      return true;
446   }
447 
448   protected void errorMsg(int tag) {
449      JOptionPane.showMessageDialog(this,ERROR_MESSAGE[tag],
450                                    "Error", JOptionPane.ERROR_MESSAGE);
451   }
452 
453   void save() {
454      task.setConstraints(constraintsModel.getData());
455      if ( task.isPrimitive() )
456         ((PrimitiveTask)task).setOrdering(orderingModel.getData());
457   }
458 }