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  package zeus.agentviewer.resources;
25  
26  import javax.swing.*;
27  import javax.swing.table.*;
28  import javax.swing.event.*;
29  import javax.swing.border.*;
30  import java.awt.*;
31  import java.awt.event.*;
32  import java.util.*;
33  
34  
35  import zeus.agentviewer.*;
36  import zeus.util.*;
37  
38  
39  
40  public class ResourceTableUI extends ZeusInternalFrame implements ListSelectionListener,
41                                                                 ActionListener,
42                                                                 ChangeListener
43  
44  {
45     String fsep = System.getProperty("file.separator");
46     String IMAGEPATH = SystemProps.getProperty("gif.dir") + "agentviewer" + fsep;
47     private JPanel  contentPane;
48     private JTable  table;
49     private JTextArea bottomPane;
50     private JScrollPane topPane;
51  
52     final int IMGw = 20;
53     final int IMGh = 20;
54     final int TOP_PANE_MIN_HEIGHT = 120;
55     final int TOP_PANE_MIN_WIDTH = 500;
56     final int BOTTOM_PANE_MIN_WIDTH = 500;
57     final int BOTTOM_PANE_MIN_HEIGHT = 100;
58  
59     private static int NUMBER_DISPLAYED = 0;
60     private InternalFramesPanel deskTop;
61     ResourceTableModel resourceBuffer;
62     private FactAttributesTableModel attributesTableModel;
63     JTable attributesTable;
64  
65  
66  
67  
68  //------------------------------------------------------------------------------
69       public ResourceTableUI(InternalFramesPanel deskTop,
70                              ResourceTableModel resourceBuffer)
71       {
72          super(" ",true,true,true,true);
73          setTitle("Resource Database:" + (++NUMBER_DISPLAYED) );
74          ImageIcon icon = new ImageIcon(IMAGEPATH + ViewerNames.RESDB_IMG);
75          setFrameIcon(icon);
76          this.deskTop = deskTop;
77          this.resourceBuffer =resourceBuffer;
78          attributesTableModel = new FactAttributesTableModel(resourceBuffer);
79          buildUI();
80          deskTop.addInternalFrame(this);
81          setVisible(true);
82       }
83  
84  //------------------------------------------------------------------------------
85       Icon getIcon(String imgFile, int w, int h){
86  
87         String  imgStr = new String(IMAGEPATH + imgFile);
88         Image aImg = Toolkit.getDefaultToolkit().getImage(imgStr);
89         aImg = aImg.getScaledInstance(w,h,Image.SCALE_SMOOTH);
90         Icon aIcon = new ImageIcon(aImg);
91         return  aIcon;
92       }
93  //------------------------------------------------------------------------------
94      public void stateChanged(ChangeEvent c){
95        bottomPane.setText("  ");
96      }
97  //------------------------------------------------------------------------------
98       public void actionPerformed(ActionEvent e){
99           String cmd = e.getActionCommand();
100          int sRow;
101 
102          if (cmd.equals("delResBtn")) {
103             sRow = table.getSelectedRow();
104             if (sRow == -1) {
105                JOptionPane.showMessageDialog(this," Select resource",
106                   "Resource not selected",JOptionPane.ERROR_MESSAGE);
107               return;
108             }
109             else {
110               // resourceBuffer.deleteFact(sRow);
111               // attributesTableModel.setFact(null);
112               // bottomPane.setText("  ");
113             }
114 
115          }
116     }
117 //------------------------------------------------------------------------------
118     JToolBar getToolBar(){
119 
120          JButton  delResBtn;
121          String img;
122          JToolBar toolBar = new JToolBar();
123 
124          delResBtn = new JButton(getIcon(ViewerNames.DELETE_IMG,IMGw,IMGh));
125          delResBtn.setToolTipText("Delete resource");
126          delResBtn.addActionListener(this);
127          delResBtn.setActionCommand("delResBtn");
128          toolBar.add(delResBtn);
129 
130          toolBar.setFloatable(false);
131 
132          return toolBar;
133     }
134 //------------------------------------------------------------------------------
135    private TitledBorder makeBorder(String title){
136           TitledBorder border = (BorderFactory.createTitledBorder(title));
137         	border.setTitlePosition(TitledBorder.TOP);
138 	        border.setTitleJustification(TitledBorder.RIGHT);
139 	        border.setTitleFont(new Font("Helvetica", Font.BOLD, 12));
140 	        border.setTitleColor(Color.black);
141           return border;
142 
143    }
144 //------------------------------------------------------------------------------
145    private void buildUI(){
146 
147           table = new JTable(resourceBuffer);
148           table.setPreferredScrollableViewportSize(new Dimension(TOP_PANE_MIN_WIDTH,
149                                                                  TOP_PANE_MIN_HEIGHT));
150           topPane = new JScrollPane(table);
151           table.getSelectionModel().addListSelectionListener(this );
152           table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
153 
154           bottomPane = new JTextArea();
155           bottomPane.setEditable(false);
156           bottomPane.setLineWrap(true);
157           bottomPane.setWrapStyleWord(true);
158           bottomPane.setPreferredSize(new Dimension(BOTTOM_PANE_MIN_WIDTH,
159                                                     BOTTOM_PANE_MIN_HEIGHT));
160           JScrollPane bottomSP = new JScrollPane(bottomPane);
161           bottomSP.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.gray));
162 
163           //attributes
164           attributesTable = new JTable(attributesTableModel);
165           attributesTable.setColumnSelectionAllowed(false);
166           attributesTable = new JTable(attributesTableModel);
167           attributesTable.setPreferredScrollableViewportSize(new Dimension(BOTTOM_PANE_MIN_WIDTH,
168                                                                BOTTOM_PANE_MIN_HEIGHT));
169 
170           JPanel attributesPanel = new JPanel(new BorderLayout());
171           attributesPanel.setBorder(makeBorder("Attributes"));
172           attributesPanel.add(BorderLayout.CENTER,new JScrollPane(attributesTable));
173 
174           JPanel centerPanel = new JPanel(new BorderLayout());
175           centerPanel.add(BorderLayout.NORTH,topPane);
176           centerPanel.add(BorderLayout.CENTER,attributesPanel);
177           //centerPanel.add(bottomSP);
178 
179           contentPane = (JPanel) getContentPane();
180           contentPane.setLayout(new BorderLayout());
181           //contentPane.add(BorderLayout.NORTH,getToolBar());
182           contentPane.add(BorderLayout.CENTER,centerPanel);
183 
184 
185           pack();
186 
187       }
188 //------------------------------------------------------------------------------
189    void displayAttributes(){
190 
191      int  row = table.getSelectedRow();
192      if (row >= 0  && row < resourceBuffer.getRowCount()) {
193         attributesTableModel.setFact(resourceBuffer.getAttributesOf(row));
194         reSize();
195      }
196 
197    }
198 //------------------------------------------------------------------------------
199       public void valueChanged(ListSelectionEvent e) {
200           int row;
201           if (e.getSource() == table.getSelectionModel() ) {
202 
203                row = table.getSelectedRow();
204                if (row >= 0  && row < resourceBuffer.getRowCount()) {
205                   //bottomPane.setText(resourceBuffer.getFactContent(row));
206                   //bottomPane.validate();
207                   displayAttributes();
208 
209                }
210           }
211 
212       }
213 
214 //------------------------------------------------------------------------------
215      void reSize(){
216         setSize(getWidth()+1,getHeight());
217         setSize(getWidth()-1,getHeight());
218      }
219 
220 }