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.database;
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 TablesTableUI extends JPanel 
50  {
51    static final String[] ERROR_MESSAGE = {
52       "Please select a row before\ncalling this operation"
53    };
54  
55    protected JTable         table;
56  
57    protected TablesTableModel  model;
58    protected ColumnsTableModel cmodel;
59  
60    protected int selRow = -1;
61  
62  
63    public TablesTableUI(ColumnsTableModel cm)
64    {
65      cmodel = cm;
66      model = new TablesTableModel();
67  
68      TableColumnModel tm = new DefaultTableColumnModel();
69      TableColumn column;
70      column = new TableColumn(0);
71      column.setHeaderValue(model.getColumnName(0));
72      tm.addColumn(column);
73      table = new JTable(model,tm);
74  
75      table.getTableHeader().setReorderingAllowed(false);
76      table.setColumnSelectionAllowed(false);
77  
78      JScrollPane scrollPane = new JScrollPane(table);
79      scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
80      scrollPane.setPreferredSize(new Dimension(140, 180));
81      table.setBackground(Color.white);
82  
83      GridBagLayout gridBagLayout = new GridBagLayout();
84      setLayout(gridBagLayout);
85     // setBackground(Color.lightGray);
86  
87      GridBagConstraints gbc = new GridBagConstraints();
88  
89      gbc.gridwidth = GridBagConstraints.REMAINDER;
90      gbc.anchor = GridBagConstraints.WEST;
91      gbc.fill = GridBagConstraints.BOTH;
92      gbc.weightx = gbc.weighty = 1;
93      gbc.insets = new Insets(16,16,16,16);
94      gridBagLayout.setConstraints(scrollPane, gbc);
95      add(scrollPane);
96  
97      ListSelectionModel selectionModel = table.getSelectionModel();
98      selectionModel.addListSelectionListener(new SymListAction());
99    }
100 
101   public TablesTableModel getModel() { return model; }
102 
103 
104   private void errorMsg(int tag) {
105      JOptionPane.showMessageDialog(this,ERROR_MESSAGE[tag],
106                                    "Error", JOptionPane.ERROR_MESSAGE);
107   }
108 
109   public String getSelectedRow()
110   {
111     if (selRow == -1) {
112       return null;
113     }
114     return (String)model.getValueAt(selRow, 0);
115   }
116 
117   class SymListAction implements ListSelectionListener {
118     public void valueChanged(ListSelectionEvent e)
119     {
120       int row = table.getSelectedRow();
121       if (row != selRow)
122       {
123         // avoids opening unnecessary DB connections
124         selRow = row;
125         String name = (String)model.getValueAt(row, 0);
126         //System.out.println("Select: " + name);
127         cmodel.refreshColumns(name);
128       }
129     }
130   }
131 }