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  * AttributeTablePanel.java
26  *
27  * The Container panel for the Attribute Table
28  *****************************************************************************/
29  
30  package zeus.ontology.attributes;
31  
32  import java.awt.*;
33  import java.util.*;
34  import javax.swing.*;
35  import javax.swing.table.*;
36  import javax.swing.border.*;
37  import java.awt.event.*;
38  
39  import zeus.util.*;
40  import zeus.gui.help.*;
41  import zeus.gui.fields.*;
42  import zeus.gui.editors.*;
43  import zeus.concepts.*;
44  import zeus.ontology.*;
45  
46  
47  public class AttributeTablePanel extends JPanel {
48    protected JTable              table;
49    protected TitledBorder        border;
50    protected AttributeToolBar    toolbar;
51    protected OntologyEditor      parent;
52  
53    protected String              currentName = null;
54    protected boolean             showState   = false;
55    protected AttributeTableModel model;
56    protected String[][]          clipboard = null;
57  
58    static final String[] ERROR_MESSAGE = {
59       "Please select a fact before\ncalling this operation",
60       "Please select a row before\ncalling this operation"
61    };
62  
63    public AttributeTablePanel(OntologyEditor editor, OntologyDb ontologyDb) {
64      parent = editor;
65      model = new AttributeTableModel(ontologyDb);
66  
67      TableColumnModel tm = new DefaultTableColumnModel();
68      TableColumn column;
69  
70      column = new TableColumn(AttributeTableModel.NAME,12,
71         new NameCellRenderer(),
72         new DefaultCellEditor(new NameField()));
73      column.setHeaderValue(model.getColumnName(AttributeTableModel.NAME));
74      tm.addColumn(column);
75      column = new TableColumn(AttributeTableModel.TYPE,12,
76         new ValidatingCellRenderer(model),
77         new TypeCellEditor(ontologyDb));
78      column.setHeaderValue(model.getColumnName(AttributeTableModel.TYPE));
79      tm.addColumn(column);
80      column = new TableColumn(AttributeTableModel.RESTRICTION,24,
81         new ValidatingCellRenderer(model),
82         new ExpressionCellEditor(model));
83      column.setHeaderValue(model.getColumnName(AttributeTableModel.RESTRICTION));
84      tm.addColumn(column);
85      column = new TableColumn(AttributeTableModel.DEFAULT,24,
86         new ValidatingCellRenderer(model),
87         new ExpressionCellEditor(model));
88      column.setHeaderValue(model.getColumnName(AttributeTableModel.DEFAULT));
89      tm.addColumn(column);
90  
91      table = new JTable(model,tm);
92  
93      table.getTableHeader().setReorderingAllowed(false);
94      table.setColumnSelectionAllowed(false);
95      table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
96  
97      JScrollPane scrollPane = new JScrollPane(table);
98      scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
99      scrollPane.setPreferredSize(new Dimension(420, 100));
100     table.setBackground(Color.white);
101 
102     setLayout(new BorderLayout());
103     //setBackground(Color.lightGray);
104     setBorder(new CompoundBorder(new BevelBorder(BevelBorder.LOWERED),
105                                  new EmptyBorder(15,15,15,15)));
106 
107     JPanel innerPanel = new JPanel();
108     border = (BorderFactory.createTitledBorder("Fact Attributes"));
109     border.setTitlePosition(TitledBorder.TOP);
110     border.setTitleJustification(TitledBorder.RIGHT);
111     border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
112     border.setTitleColor(Color.blue);
113     innerPanel.setBorder(border);
114 
115     GridBagLayout gridBagLayout = new GridBagLayout();
116     innerPanel.setLayout(gridBagLayout);
117     GridBagConstraints gbc = new GridBagConstraints();
118 
119     toolbar = new AttributeToolBar();
120     gbc = new GridBagConstraints();
121     gbc.anchor = GridBagConstraints.WEST;
122     gbc.gridwidth = GridBagConstraints.REMAINDER;
123     gbc.fill = GridBagConstraints.NONE;
124     gbc.insets = new Insets(0,16,0,0);
125     gridBagLayout.setConstraints(toolbar, gbc);
126     innerPanel.add(toolbar);
127 
128 
129     gbc.gridwidth = GridBagConstraints.REMAINDER;
130     gbc.anchor = GridBagConstraints.WEST;
131     gbc.fill = GridBagConstraints.BOTH;
132     gbc.weightx = gbc.weighty = 1;
133     gbc.insets = new Insets(16,16,16,16);
134     gridBagLayout.setConstraints(scrollPane, gbc);
135     innerPanel.add(scrollPane);
136 
137     add("Center", innerPanel);
138   }
139 
140   class TypeCellEditor extends DefaultCellEditor
141                        implements ActionListener, TypeSelector {
142 
143     protected JButton button = new JButton("");
144     protected int row, column;
145     protected TypeDialog dialog;
146 
147     public TypeCellEditor(OntologyDb ontologyDb) {
148       super(new JTextField());
149       setClickCountToStart(2);
150       dialog = new TypeDialog((Frame)SwingUtilities.getRoot(table),
151          ontologyDb,TypeTreeModel.FACT);
152 
153       button.setBackground(Color.white);
154       button.setHorizontalAlignment(JButton.LEFT);
155       button.setBorderPainted(false);
156       button.addActionListener(this);
157     }
158 
159     public void actionPerformed(ActionEvent e) {
160       Object src = e.getSource();
161       if ( src == button ) {
162          dialog.setLocationRelativeTo(button);
163          fireEditingCanceled();
164          dialog.display(this);
165       }
166     }
167     public void typeSelected(String value) {
168        model.setValueAt(value,row,column);
169     }
170     public Component getTableCellEditorComponent(JTable table, Object value,
171                                                  boolean isSelected,
172                                                  int row, int column) {
173 
174       this.row = row;
175       this.column = column;
176       button.setText(value.toString());
177       return button;
178     }
179   }
180 
181   class NameCellRenderer extends DefaultTableCellRenderer {
182      public Component getTableCellRendererComponent(JTable table, Object value,
183         boolean isSelected, boolean hasFocus, int row, int column) {
184 
185         // show attributes not specific to this class in red
186         if ( model.isCellEditable(row,column) )
187            setForeground(Color.black);
188         else
189            setForeground(Color.green);
190 
191         return super.getTableCellRendererComponent(table,value,isSelected,
192            hasFocus, row, column);
193      }
194   }
195 
196 class AttributeToolBar extends JToolBar implements ActionListener {
197   protected HelpWindow    helpWin = null;
198   protected JButton       newBtn;
199   protected JButton       deleteBtn;
200   protected JButton       cutBtn;
201   protected JButton       copyBtn;
202   protected JButton       pasteBtn;
203   protected JToggleButton helpBtn;
204   protected JToggleButton showBtn;
205 
206   public AttributeToolBar() {
207     setBorder( new BevelBorder(BevelBorder.LOWERED ) );
208     setFloatable(false);
209 
210     String sep = System.getProperty("file.separator");
211     String path = SystemProps.getProperty("gif.dir") + "ontology" + sep;
212 
213     // New Button
214     newBtn = new JButton(new ImageIcon(path + "new.gif"));
215     newBtn.setMargin(new Insets(0,0,0,0));
216     add(newBtn);
217     newBtn.setToolTipText("New");
218     newBtn.addActionListener(this);
219 
220     // Delete Button
221     deleteBtn = new JButton(new ImageIcon(path + "delete.gif"));
222     deleteBtn.setMargin(new Insets(0,0,0,0));
223     add(deleteBtn);
224     deleteBtn.setToolTipText("Delete");
225     deleteBtn.addActionListener(this);
226 
227     addSeparator();
228 
229     // Cut Button
230     cutBtn = new JButton(new ImageIcon(path + "cut.gif"));
231     cutBtn.setMargin(new Insets(0,0,0,0));
232     add(cutBtn);
233     cutBtn.setToolTipText("Cut");
234     cutBtn.addActionListener(this);
235 
236     // Copy Button
237     copyBtn = new JButton(new ImageIcon(path + "copy.gif"));
238     copyBtn.setMargin(new Insets(0,0,0,0));
239     add(copyBtn);
240     copyBtn.setToolTipText("Copy");
241     copyBtn.addActionListener(this);
242 
243     // Paste Button
244     pasteBtn = new JButton(new ImageIcon(path + "paste.gif"));
245     pasteBtn.setMargin(new Insets(0,0,0,0));
246     add(pasteBtn);
247     pasteBtn.setToolTipText("Paste");
248     pasteBtn.addActionListener(this);
249 
250     // Attribute Toggle Button
251     showBtn = new JToggleButton(new ImageIcon(path + "row1.gif"), false);
252     showBtn.setMargin(new Insets(0,0,0,0));
253     showBtn.setSelectedIcon(new ImageIcon(path + "row2.gif"));
254     add(showBtn);
255     showBtn.setToolTipText("Toggle shown attributes");
256     showBtn.addActionListener(this);
257 
258     addSeparator();
259 
260     // Help Button
261     helpBtn = new JToggleButton(new ImageIcon(path + "help.gif"));
262     helpBtn.setMargin(new Insets(0,0,0,0));
263     add(helpBtn);
264     helpBtn.setToolTipText("Help");
265     helpBtn.addActionListener(this);
266 
267     addSeparator();
268 
269   }
270   public void activate(boolean state) {
271      newBtn.setEnabled(state);
272      deleteBtn.setEnabled(state);
273      cutBtn.setEnabled(state);
274      pasteBtn.setEnabled(state);
275   }
276   public void actionPerformed(ActionEvent e) {
277     Object src = e.getSource();
278 
279     if ( src == newBtn )
280        addNewRow();
281     else if ( src == deleteBtn )
282        deleteSelected();
283     else if ( src == showBtn )
284        updateDisplay(showBtn.isSelected());
285     else if ( src == copyBtn )
286        clipboard = getSelectedRows();
287     else if ( src == pasteBtn ) {
288        if ( clipboard != null )
289           model.addRows(clipboard);
290     }
291     else if ( src == cutBtn )
292        clipboard = cutSelectedRows();
293     else if ( src == helpBtn ) {
294        if ( helpBtn.isSelected() ) {
295           Point dispos = getLocation();
296           helpWin = new HelpWindow(SwingUtilities.getRoot(helpBtn),
297              dispos, "ontology", "Attribute Table");
298           helpWin.setSource(helpBtn);
299        }
300        else {
301           helpWin.dispose();
302        }
303     }
304   }
305 }
306 
307 
308   public void displayAttributes(String name) {
309     currentName = name;
310     updateDisplay(showState);
311   }
312 
313   void updateDisplay(boolean state) {
314     showState = state;
315     if (showState) {
316       border.setTitle("All Attributes of '" + currentName + "'");
317       model.refreshAllAttributes(currentName);
318     }
319     else {
320       border.setTitle("The Attributes specific to '" + currentName + "'");
321       model.refreshAttributes(currentName);
322     }
323     toolbar.activate(model.isNodeEditable());
324     invalidate();
325     validate();
326     repaint();
327   }
328 
329   public void clear() {
330     currentName = null;
331     border.setTitle("No Attributes Shown");
332     displayAttributes(currentName);
333     validate();
334     repaint();
335   }
336 
337   AttributeTableModel getModel() { return model; }
338 
339   private void errorMsg(int tag) {
340      JOptionPane.showMessageDialog(this,ERROR_MESSAGE[tag],
341                                    "Error", JOptionPane.ERROR_MESSAGE);
342   }
343 
344   void addNewRow() {
345     if ( currentName == null )  {
346        errorMsg(0);
347        return;
348     }
349     model.addNewRow();
350   }
351 
352   void addRows(String[][] rows) {
353     if ( currentName == null )  {
354        errorMsg(0);
355        return;
356     }
357     model.addRows(rows);
358   }
359 
360   void deleteSelected() {
361     if ( currentName == null )  {
362        errorMsg(0);
363        return;
364     }
365     else if ( table.getSelectedRow() == -1 ) {
366        errorMsg(1);
367        return;
368     }
369     int[] srows = table.getSelectedRows();
370     model.deleteRows(srows);
371   }
372 
373   String[][] getSelectedRows() {
374     if ( currentName == null )  {
375        errorMsg(0);
376        return new String[0][0];
377     }
378     else if ( table.getSelectedRow() == -1 ) {
379        errorMsg(1);
380        return new String[0][0];
381     }
382     int[] srows = table.getSelectedRows();
383     return model.getRows(srows);
384   }
385 
386   String[][] cutSelectedRows() {
387     if ( currentName == null )  {
388        errorMsg(0);
389        return new String[0][0];
390     }
391     else if ( table.getSelectedRow() == -1 ) {
392        errorMsg(1);
393        return new String[0][0];
394     }
395     String[][] data = getSelectedRows();
396     deleteSelected();
397     return data;
398   }
399 
400 }