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  * FactPanel.java
26  *
27  * The Container panel for the  Facts Table
28  *****************************************************************************/
29  
30  package zeus.generator.util;
31  
32  import java.awt.*;
33  import java.util.*;
34  import javax.swing.*;
35  import javax.swing.event.*;
36  import javax.swing.border.*;
37  import javax.swing.table.*;
38  import java.awt.event.*;
39  
40  import zeus.util.*;
41  import zeus.concepts.*;
42  import zeus.gui.help.*;
43  import zeus.gui.fields.*;
44  
45  public class FactPanel extends JPanel {
46    public static final int NONE          = 0;
47    public static final int PRECONDITION  = 1;
48    public static final int POSTCONDITION = 2;
49  
50    static final String[] ERROR_MESSAGE = {
51       /* 0 */ "Please select a row before\ncalling this operation"
52    };
53  
54    protected FactToolBar    toolbar;
55    protected AttributeTable attributePanel;
56    protected AttributeModel aModel;
57    protected JTable         table;
58    protected FactModel      model;
59    protected ChangeListener editor;
60    protected Fact[]         clipboard = null;
61    protected OntologyDb     ontologyDb;
62  
63    public FactPanel(OntologyDb ontologyDb, ChangeListener editor,
64                     Fact[] facts, boolean isVariable, String label)  {
65       this(ontologyDb,editor,facts,isVariable,NONE,label);
66    }
67    public FactPanel(OntologyDb ontologyDb, ChangeListener editor,
68                     Fact[] facts, boolean isVariable, int modifierType,
69  		   String label)  {
70       this(editor, modifierType, label,
71          new FactModel(ontologyDb,new AttributeModel(),isVariable,modifierType,facts));
72  
73    }
74    public FactPanel(ChangeListener editor, String label, FactModel model)  {
75       this(editor,NONE,label,model);
76    }
77    
78    public FactPanel(ChangeListener editor, int modifierType,
79                     String label, FactModel model)  {
80  
81       this.ontologyDb = model.getOntologyDb();
82       this.editor = editor;
83       this.model = model;
84       this.aModel = model.getAttributeModel();
85  
86       attributePanel = new AttributeTable(aModel);
87       aModel.addChangeListener(editor);
88       model.addChangeListener(editor);
89  
90       TableColumnModel tm = new DefaultTableColumnModel();
91       TableColumn column;
92       column = new TableColumn(FactModel.TYPE,12);
93       column.setHeaderValue(model.getColumnName(FactModel.TYPE));
94       tm.addColumn(column);
95       column = new TableColumn(FactModel.INSTANCE,24,
96          new DefaultTableCellRenderer(),
97          new FactInstanceEditor());
98       column.setHeaderValue(model.getColumnName(FactModel.INSTANCE));
99       tm.addColumn(column);
100 
101      if ( modifierType != NONE ) {
102         column = new TableColumn(FactModel.MODIFIERS,24,
103            new FactModifiersCellRenderer(modifierType),
104            new FactModifiersEditor(modifierType));
105         column.setHeaderValue(model.getColumnName(FactModel.MODIFIERS));
106         tm.addColumn(column);
107      }
108 
109      table = new JTable(model,tm);
110      table.getTableHeader().setReorderingAllowed(false);
111      table.setColumnSelectionAllowed(false);
112 
113      ListSelectionModel selectionModel = table.getSelectionModel();
114      selectionModel.addListSelectionListener(new SymListAction());
115 
116      JScrollPane scrollPane = new JScrollPane(table);
117      scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
118      scrollPane.setPreferredSize(new Dimension(400, 100));
119      table.setBackground(Color.white);
120 
121      GridBagLayout gridBagLayout = new GridBagLayout();
122      setLayout(gridBagLayout);
123      setBackground(Color.lightGray);
124 
125      TitledBorder border = (BorderFactory.createTitledBorder(label));
126      border.setTitlePosition(TitledBorder.TOP);
127      border.setTitleJustification(TitledBorder.RIGHT);
128      border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
129      border.setTitleColor(Color.blue);
130      setBorder(border);
131 
132      GridBagConstraints gbc = new GridBagConstraints();
133      toolbar = new FactToolBar();
134 
135      gbc.gridwidth = GridBagConstraints.REMAINDER;
136      gbc.anchor = GridBagConstraints.WEST;
137      gbc.fill = GridBagConstraints.NONE;
138      gbc.insets = new Insets(0,8,0,0);
139      gridBagLayout.setConstraints(toolbar, gbc);
140      add(toolbar);
141 
142      gbc.gridwidth = GridBagConstraints.REMAINDER;
143      gbc.anchor = GridBagConstraints.WEST;
144      gbc.fill = GridBagConstraints.BOTH;
145      gbc.weightx = gbc.weighty = 1;
146      gbc.insets = new Insets(8,8,8,8);
147      gridBagLayout.setConstraints(scrollPane, gbc);
148      add(scrollPane);
149 
150      gbc.gridwidth = GridBagConstraints.REMAINDER;
151      gbc.anchor = GridBagConstraints.WEST;
152      gbc.fill = GridBagConstraints.BOTH;
153      gbc.weightx = gbc.weighty = 1;
154      gbc.insets = new Insets(8,8,8,8);
155      gridBagLayout.setConstraints(attributePanel, gbc);
156      add(attributePanel);
157   }
158 
159   protected void errorMsg(int tag) {
160      JOptionPane.showMessageDialog(this,ERROR_MESSAGE[tag],
161                                    "Error", JOptionPane.ERROR_MESSAGE);
162   }
163 
164   public void           reset(Fact[] facts) { model.reset(facts);     }
165   public Fact[]         getData()           { return model.getData(); }
166   public FactModel      getFactModel()      { return model;           }
167   public AttributeModel getAttributeModel() { return aModel;          }
168   public AttributeTable getAttributeTable() { return attributePanel;  }
169 
170   protected Fact[] getSelectedRows()  {
171     int[] srows = table.getSelectedRows();
172     Fact[] data = new Fact[srows.length];
173     for(int i = 0; i < srows.length; i++)
174        data[i] = (Fact)model.getValueAt(srows[i],FactModel.FACT);
175     return data;
176   }
177 
178   protected Fact[] cutSelectedRows()  {
179      Fact[] data = getSelectedRows();
180      model.removeRows(table.getSelectedRows());
181      return data;
182   }
183  
184   protected void deleteSelectedRow()  {
185      if ( !isRowSelected() ) return;
186      cutSelectedRows();
187   }
188 
189   protected boolean isRowSelected() {
190      int row = table.getSelectedRow();
191      if ( row == -1) {
192         errorMsg(0);
193         return false;
194      }
195      return true;
196   }
197 
198   public void setToolBarState(boolean state) {
199     toolbar.setEnabled(state);
200   }
201 
202   class SymListAction implements ListSelectionListener {
203      public void valueChanged(ListSelectionEvent e) {
204         model.selectRow(table.getSelectedRow());
205      }
206   }
207 
208   public FactInstanceEditor newInstanceEditor() {
209     return new FactInstanceEditor();
210   }
211 
212   class FactInstanceEditor extends DefaultCellEditor {
213      public FactInstanceEditor() {
214         super(new NameField());
215      }
216      public Component getTableCellEditorComponent(JTable table, Object value,
217                                                   boolean isSelected,
218                                                   int row, int column) {
219         String s = (String)value;
220         s = s.substring(1);;
221         return super.getTableCellEditorComponent(table,s,isSelected,row,column);
222      }
223   }
224 
225   class FactModifiersCellRenderer extends DefaultTableCellRenderer {
226      protected int type;
227 
228      public FactModifiersCellRenderer(int type) {
229         this.type = type;
230      }
231      public void setValue(Object value) {
232         String s = "";
233         int modifiers = ((Integer)value).intValue();
234         if (type == PRECONDITION) {
235            if ( Fact.isNegative(modifiers)   ) s += " NOT";
236            if ( Fact.isReadOnly(modifiers)   ) s += " READ_ONLY";
237            if ( Fact.isLocal(modifiers)      ) s += " LOCAL";
238            if ( Fact.isReplaced(modifiers)   ) s += " REPLACED";
239         }
240         else {
241            if ( Fact.isSideEffect(modifiers) ) s += "SIDE_EFFECT";
242         }
243         super.setValue(s);
244      }
245   }
246   class FactModifiersEditor extends DefaultCellEditor
247                             implements ActionListener, FactModifier {
248 
249     protected JButton button = new JButton("");
250     protected int row, column;
251     protected ModifierDialog dialog;
252     protected int type;
253     protected int modifier;
254  
255     public FactModifiersEditor(int modifierType) {
256       super(new JTextField());
257       setClickCountToStart(1);
258 
259       this.type = modifierType;
260 
261       dialog = new ModifierDialog((Frame)SwingUtilities.getRoot(table),
262                                   "Set Modifiers");
263 
264       button.setBackground(Color.white);
265       button.setHorizontalAlignment(JButton.LEFT);
266       button.setBorderPainted(false);
267       button.addActionListener(this);
268     }
269 
270     public void actionPerformed(ActionEvent e) {
271       Object src = e.getSource();
272       if ( src == button ) {
273          dialog.setLocationRelativeTo(button);
274          fireEditingCanceled();
275          dialog.display(this,modifier,type);
276       }
277     }
278 
279     public void factModifiersChanged(int modifier) {
280          model.setValueAt(new Integer(modifier),row,column);
281     }
282 
283     public Component getTableCellEditorComponent(JTable table, Object value,
284                                                  boolean isSelected,
285                                                  int row, int column) {
286  
287       this.row = row;
288       this.column = column; 
289       this.modifier = ((Integer)value).intValue();
290       return button;
291     }
292   }
293 
294   class FactToolBar extends JToolBar
295                     implements ActionListener,
296                                FactSelector {
297 
298      protected FactDialog    factWin;
299      protected HelpWindow    helpWin;
300      protected JToggleButton helpBtn;
301      protected JButton       newBtn;
302      protected JButton       deleteBtn;
303      protected JButton       cutBtn;
304      protected JButton       copyBtn;
305      protected JButton       pasteBtn;
306 
307      public FactToolBar() {
308         setBackground(java.awt.Color.lightGray);
309         setBorder( new BevelBorder(BevelBorder.LOWERED ) );
310         setFloatable(false);
311 
312         String sep = System.getProperty("file.separator");
313         String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
314 
315         // New Button
316         newBtn = new JButton(new ImageIcon(path + "new1.gif"));
317 	newBtn.setMargin(new Insets(0,0,0,0));
318         add(newBtn);
319         newBtn.setToolTipText("New");
320         newBtn.addActionListener(this);
321 
322         // Delete Button
323         deleteBtn = new JButton(new ImageIcon(path + "delete1.gif"));
324 	deleteBtn.setMargin(new Insets(0,0,0,0));
325         add(deleteBtn);
326         deleteBtn.setToolTipText("Delete");
327         deleteBtn.addActionListener(this);
328 
329         addSeparator();
330 
331         // Cut Button
332         cutBtn = new JButton(new ImageIcon(path + "cut.gif"));
333 	cutBtn.setMargin(new Insets(0,0,0,0));
334         add(cutBtn);
335         cutBtn.setToolTipText("Cut");
336         cutBtn.addActionListener(this);
337 
338         // Copy Button
339         copyBtn = new JButton(new ImageIcon(path + "copy.gif"));
340 	copyBtn.setMargin(new Insets(0,0,0,0));
341         add(copyBtn);
342         copyBtn.setToolTipText("Copy");
343         copyBtn.addActionListener(this);
344 
345         // Paste Button
346         pasteBtn = new JButton(new ImageIcon(path + "paste.gif"));
347 	pasteBtn.setMargin(new Insets(0,0,0,0));
348         add(pasteBtn);
349         pasteBtn.setToolTipText("Paste");
350         pasteBtn.addActionListener(this);
351 
352         addSeparator();
353 
354         // Help Button
355         helpBtn = new JToggleButton(new ImageIcon(path + "help.gif"));
356 	helpBtn.setMargin(new Insets(0,0,0,0));
357         add(helpBtn);
358         helpBtn.setToolTipText("Help");
359         helpBtn.addActionListener(this);
360 
361         factWin = new FactDialog((Frame)SwingUtilities.getRoot(this),
362                                  ontologyDb);
363      }
364 
365      public void setEnabled(boolean set) {
366         newBtn.setEnabled(set);
367         deleteBtn.setEnabled(set);
368         cutBtn.setEnabled(set);
369         copyBtn.setEnabled(set);
370         pasteBtn.setEnabled(set);
371         helpBtn.setEnabled(set);
372      }
373 
374      public void factSelected(String[] names)  {
375         // Fact Selector callback to add new entries
376         model.addNewRows(names);
377      }
378 
379      public void actionPerformed(ActionEvent e)  {
380         Object src = e.getSource();
381         if ( src == newBtn ) {
382            factWin.setLocationRelativeTo(newBtn);
383            factWin.display(this);
384         }
385         else if ( src == deleteBtn ) {
386            deleteSelectedRow();
387         }  
388         else if ( src == copyBtn ) {
389            clipboard = getSelectedRows();           
390         }
391         else if ( src == pasteBtn ) {
392            model.addRows(clipboard);
393            table.clearSelection();
394         }
395         else if ( src == cutBtn ) {
396            clipboard = cutSelectedRows();
397         }
398         else if ( src == helpBtn ) {
399           if ( helpBtn.isSelected() ) {
400               Point dispos = getLocation();
401               helpWin = new HelpWindow(SwingUtilities.getRoot(this),
402                  dispos, "generator", "Fact Table");
403               helpWin.setSource(helpBtn);
404           }
405           else
406               helpWin.dispose();
407         }
408      }
409   }
410 }