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  * RestrictionTableUI.java
26  *
27  * The Viewer/Controller for displaying and editing restrictions
28  *****************************************************************************/
29  
30  package zeus.ontology.restrictions;
31  
32  import javax.swing.*;
33  import javax.swing.table.*;
34  import javax.swing.border.*;
35  import javax.swing.event.*;
36  
37  import java.awt.*;
38  import java.awt.event.*;
39  import java.util.*;
40  
41  import zeus.util.*;
42  import zeus.concepts.*;
43  import zeus.ontology.*;
44  import zeus.gui.help.*;
45  import zeus.gui.editors.*;
46  import zeus.gui.fields.*;
47  
48  
49  public class RestrictionTableUI extends JPanel {
50    static final String[] ERROR_MESSAGE = {
51       "Please select a row before\ncalling this operation"
52    };
53  
54    protected JTable                table;
55    protected RestrictionTableModel model;
56    protected String[][]            clipboard = null;
57  
58    public RestrictionTableUI(OntologyDb ontologyDb) {
59  
60      model = new RestrictionTableModel(ontologyDb);
61  
62      TableColumnModel tm = new DefaultTableColumnModel();
63      TableColumn column;
64  
65      column = new TableColumn(RestrictionTableModel.NAME,12,
66         new DefaultTableCellRenderer(),
67         new DefaultCellEditor(new NameField()));
68      column.setHeaderValue(model.getColumnName(RestrictionTableModel.NAME));
69      tm.addColumn(column);
70      column = new TableColumn(RestrictionTableModel.TYPE,12,
71         new ValidatingCellRenderer(model),
72         new TypeCellEditor(ontologyDb));
73      column.setHeaderValue(model.getColumnName(RestrictionTableModel.TYPE));
74      tm.addColumn(column);
75      column = new TableColumn(RestrictionTableModel.RESTRICTION,24,
76         new ValidatingCellRenderer(model),
77         new ExpressionCellEditor(model));
78      column.setHeaderValue(model.getColumnName(RestrictionTableModel.RESTRICTION));
79      tm.addColumn(column);
80  
81      table = new JTable(model,tm);
82  
83      table.getTableHeader().setReorderingAllowed(false);
84      table.setColumnSelectionAllowed(false);
85  
86      JScrollPane scrollPane = new JScrollPane(table);
87      scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
88      scrollPane.setPreferredSize(new Dimension(420, 100));
89      table.setBackground(Color.white);
90  
91      GridBagLayout gridBagLayout = new GridBagLayout();
92      setLayout(gridBagLayout);
93     // setBackground(Color.lightGray);
94  
95      TitledBorder border = (BorderFactory.createTitledBorder("Known Restrictions"));
96      border.setTitlePosition(TitledBorder.TOP);
97      border.setTitleJustification(TitledBorder.RIGHT);
98      border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
99      border.setTitleColor(Color.blue);
100     setBorder(border);
101 
102     GridBagConstraints gbc = new GridBagConstraints();
103 
104     RestrictionToolBar resToolbar = new RestrictionToolBar();
105     gbc = new GridBagConstraints();
106     gbc.anchor = GridBagConstraints.WEST;
107     gbc.gridwidth = GridBagConstraints.REMAINDER;
108     gbc.fill = GridBagConstraints.NONE;
109     gbc.insets = new Insets(0,16,0,0);
110     gridBagLayout.setConstraints(resToolbar, gbc);
111     add(resToolbar);
112 
113     gbc.gridwidth = GridBagConstraints.REMAINDER;
114     gbc.anchor = GridBagConstraints.WEST;
115     gbc.fill = GridBagConstraints.BOTH;
116     gbc.weightx = gbc.weighty = 1;
117     gbc.insets = new Insets(16,16,16,16);
118     gridBagLayout.setConstraints(scrollPane, gbc);
119     add(scrollPane);
120   }
121 
122   class TypeCellEditor extends DefaultCellEditor
123                        implements ActionListener, TypeSelector {
124 
125     protected JButton button = new JButton("");
126     protected int row, column;
127     protected TypeDialog dialog;
128 
129     public TypeCellEditor(OntologyDb ontologyDb) {
130       super(new JTextField());
131       setClickCountToStart(2);
132       dialog = new TypeDialog((Frame)SwingUtilities.getRoot(table),
133          ontologyDb, TypeTreeModel.RESTRICTION);
134 
135       button.setBackground(Color.white);
136       button.setHorizontalAlignment(JButton.LEFT);
137       button.setBorderPainted(false);
138       button.addActionListener(this);
139     }
140 
141     public void actionPerformed(ActionEvent e) {
142       Object src = e.getSource();
143       if ( src == button ) {
144          dialog.setLocationRelativeTo(button);
145          fireEditingCanceled();
146          dialog.display(this);
147       }
148     }
149     public void typeSelected(String value) {
150        model.setValueAt(value,row,column);
151     }
152     public Component getTableCellEditorComponent(JTable table, Object value,
153                                                  boolean isSelected,
154                                                  int row, int column) {
155 
156       this.row = row;
157       this.column = column;
158       button.setText(value.toString());
159       return button;
160     }
161   }
162 
163 public class RestrictionToolBar extends JToolBar implements ActionListener {
164   protected HelpWindow         helpWin;
165   protected JButton            newBtn;
166   protected JButton            deleteBtn;
167   protected JButton            cutBtn;
168   protected JButton            copyBtn;
169   protected JButton            pasteBtn;
170   protected JToggleButton      helpBtn;
171 
172   public RestrictionToolBar() {
173     setBorder(new BevelBorder(BevelBorder.LOWERED));
174     setFloatable(false);
175 
176     String sep = System.getProperty("file.separator");
177     String path = SystemProps.getProperty("gif.dir") + "ontology" + sep;
178 
179     // New Button
180     newBtn = new JButton(new ImageIcon(path + "new.gif"));
181     newBtn.setMargin(new Insets(0,0,0,0));
182     add(newBtn);
183     newBtn.setToolTipText("New");
184     newBtn.addActionListener(this);
185 
186     // Delete Button
187     deleteBtn = new JButton(new ImageIcon(path + "delete.gif"));
188     deleteBtn.setMargin(new Insets(0,0,0,0));
189     add(deleteBtn);
190     deleteBtn.setToolTipText("Delete");
191     deleteBtn.addActionListener(this);
192 
193     addSeparator();
194 
195     // Cut Button
196     cutBtn = new JButton(new ImageIcon(path + "cut.gif"));
197     cutBtn.setMargin(new Insets(0,0,0,0));
198     add(cutBtn);
199     cutBtn.setToolTipText("Cut");
200     cutBtn.addActionListener(this);
201 
202     // Copy Button
203     copyBtn = new JButton(new ImageIcon(path + "copy.gif"));
204     copyBtn.setMargin(new Insets(0,0,0,0));
205     add(copyBtn);
206     copyBtn.setToolTipText("Copy");
207     copyBtn.addActionListener(this);
208 
209     // Paste Button
210     pasteBtn = new JButton(new ImageIcon(path + "paste.gif"));
211     pasteBtn.setMargin(new Insets(0,0,0,0));
212     add(pasteBtn);
213     pasteBtn.setToolTipText("Paste");
214     pasteBtn.addActionListener(this);
215 
216     addSeparator();
217 
218     // Help Button
219     helpBtn = new JToggleButton(new ImageIcon(path + "help.gif"));
220     helpBtn.setMargin(new Insets(0,0,0,0));
221     add(helpBtn);
222     helpBtn.setToolTipText("Help");
223     helpBtn.addActionListener(this);
224   }
225 
226   public void actionPerformed(ActionEvent e) {
227     Object src = e.getSource();
228     if ( src == newBtn )
229        model.addNewRow();
230     else if ( src == deleteBtn )
231        deleteSelected();
232     else if ( src == copyBtn )
233        clipboard = getSelectedRows();
234     else if ( src == pasteBtn )
235        addRows(clipboard);
236     else if ( src == cutBtn )
237        clipboard = cutSelectedRows();
238     else if ( src == helpBtn ) {
239        if ( helpBtn.isSelected() ) {
240           helpWin = new HelpWindow(SwingUtilities.getRoot(this),
241 	     getLocation(),"ontology","Restriction Table");
242           helpWin.setSource(helpBtn);
243        }
244        else {
245           helpWin.dispose();
246        }
247     }
248   }
249 }
250   private void errorMsg(int tag) {
251      JOptionPane.showMessageDialog(this,ERROR_MESSAGE[tag],
252                                    "Error", JOptionPane.ERROR_MESSAGE);
253   }
254 
255   void addRows(String[][] rows) {
256     model.addRows(rows);
257   }
258 
259   void deleteSelected() {
260     if ( table.getSelectedRow() == -1 ) {
261        errorMsg(1);
262        return;
263     }
264     int[] srows = table.getSelectedRows();
265     model.deleteRows(srows);
266   }
267 
268   String[][] getSelectedRows() {
269     if ( table.getSelectedRow() == -1 ) {
270        errorMsg(1);
271        return new String[0][0];
272     }
273     int[] srows = table.getSelectedRows();
274     return model.getRows(srows);
275   }
276 
277   String[][] cutSelectedRows() {
278     String[][] result = getSelectedRows();
279     if ( result.length > 0 ) deleteSelected();
280     return result;
281   }
282 }