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.task;
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  import zeus.agentviewer.*;
35  import zeus.util.*;
36  import zeus.gui.fields.*;
37  
38  
39  public class TaskTableUI extends ZeusInternalFrame
40                           implements ListSelectionListener  {
41  
42     final int TOP_PANE_MIN_HEIGHT = 43;
43     final int TOP_PANE_MIN_WIDTH = 500;
44     final int BOTTOM_PANE_MIN_WIDTH = 100;
45     final int BOTTOM_PANE_MIN_HEIGHT = 100;
46  
47     private JPanel  contentPane;
48     private JTable  taskTable, preCondTable,
49                     preAttrTable,effectsTable, effectsAttrTable,
50                     orderingTable, constraintsTable;
51  
52  
53  
54     private static int NUMBER_DISPLAYED = 0;
55     private InternalFramesPanel deskTop;
56  
57     TaskTableModel taskBuffer;
58     ConditionsAttributeTableModel preAttrBuffer, effectsAttrBuffer;
59     TaskConditionsTableModel effectsBuffer, preCondBuffer;
60     ConstraintsModel constraintsBuffer;
61     OrderingModel orderingBuffer;
62     LargeTextField timefield, costfield;
63  
64  //------------------------------------------------------------------------------
65       public TaskTableUI(InternalFramesPanel deskTop,
66                          TaskTableModel taskBuffer,
67                          TaskConditionsTableModel preCondBuffer,
68                          ConditionsAttributeTableModel preAttrBuffer,
69                          TaskConditionsTableModel effectsBuffer,
70                          ConditionsAttributeTableModel effectsAttrBuffer,
71                          ConstraintsModel constraintsBuffer,
72                          OrderingModel orderingBuffer)
73       {
74          super("Task Database",true,true,true,true);
75          setTitle("Task Database:" + (++NUMBER_DISPLAYED));
76          String sep = System.getProperty("file.separator");
77          String gifpath = SystemProps.getProperty("gif.dir") + "agentviewer" + sep;
78          ImageIcon icon = new ImageIcon(gifpath + ViewerNames.TASKPLAN_IMG);
79          setFrameIcon(icon);
80          this.deskTop = deskTop;
81          this.taskBuffer =taskBuffer;
82          this.preCondBuffer = preCondBuffer;
83          this.preAttrBuffer = preAttrBuffer;
84          this.effectsBuffer = effectsBuffer;
85          this.effectsAttrBuffer =effectsAttrBuffer;
86          this.constraintsBuffer = constraintsBuffer;
87          this.orderingBuffer = orderingBuffer;
88  
89          buildUI();
90          deskTop.addInternalFrame(this);
91          setVisible(true);
92        }
93  //------------------------------------------------------------------------------
94        private void buildUI() {
95            contentPane = (JPanel) getContentPane();
96            contentPane.setLayout(new BorderLayout());
97  
98            JPanel conditionsPanel = new JPanel(new GridLayout(1,2,10,10));
99            conditionsPanel.setBorder(
100              BorderFactory.createEtchedBorder(Color.gray,Color.black));
101           conditionsPanel.add(getPreConditionsPanel());
102           conditionsPanel.add(getEffectsPanel());
103 
104           JPanel centerPanel = new JPanel(new BorderLayout(5,15));
105 
106           JPanel taskPanel = getTaskPanel();
107           JPanel costPanel = getCostPanel();
108 
109           GridBagLayout gridBagLayout = new GridBagLayout();
110           GridBagConstraints gbc = new GridBagConstraints();
111           centerPanel.setLayout(gridBagLayout);
112           
113           // Add the taskPanel 
114           gbc.gridwidth = GridBagConstraints.REMAINDER;
115           gbc.anchor = GridBagConstraints.NORTHWEST;
116           gbc.fill = GridBagConstraints.BOTH;
117           gbc.insets = new Insets(8,8,0,8);
118           gbc.weightx = gbc.weighty = 1;
119           gridBagLayout.setConstraints(taskPanel,gbc);
120           centerPanel.add(taskPanel);
121       
122           // Add the conditionsPanel
123           JPanel flowPanel = new JPanel();
124           gbc.gridwidth = GridBagConstraints.REMAINDER;
125           gbc.anchor = GridBagConstraints.WEST;
126           gbc.fill = GridBagConstraints.BOTH;
127           gbc.insets = new Insets(8,8,0,8);
128           gbc.weightx = gbc.weighty = 1;
129           gridBagLayout.setConstraints(conditionsPanel,gbc);
130           centerPanel.add(conditionsPanel);
131           
132           // Add the panel containing the task's cost and time to the panel.
133           gbc.gridwidth = GridBagConstraints.REMAINDER;
134           gbc.anchor = GridBagConstraints.SOUTHWEST;
135           gbc.fill = GridBagConstraints.HORIZONTAL;
136           gbc.insets = new Insets(8,8,8,8);
137           gbc.weightx = gbc.weighty = 0;
138           gridBagLayout.setConstraints(costPanel,gbc);
139           centerPanel.add(costPanel);
140 
141           JTabbedPane tabbedPane = new JTabbedPane();
142 
143           tabbedPane.addTab("Preconditions and Effects", centerPanel);
144           tabbedPane.addTab("Constraints", getConstraintsPanel());
145           tabbedPane.setSelectedIndex(0);
146 
147           tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
148           contentPane.add(tabbedPane,BorderLayout.CENTER);
149 
150           pack();
151 
152       }
153 //------------------------------------------------------------------------------
154       JPanel getConstraintsPanel() {
155           JPanel panel = new JPanel(new BorderLayout());
156 
157           JPanel north = new JPanel(new BorderLayout());
158           north.setBorder(makeBorder("PreConditions Ordering Constraints"));
159           orderingTable = new JTable(orderingBuffer);
160           orderingTable.setPreferredScrollableViewportSize(
161              new Dimension(TOP_PANE_MIN_WIDTH, TOP_PANE_MIN_HEIGHT));
162           JScrollPane orderingTableSP = new JScrollPane(orderingTable);
163           north.add(BorderLayout.CENTER,orderingTableSP);
164           panel.add(BorderLayout.NORTH, north);
165 
166           JPanel south =  new JPanel(new BorderLayout());
167           south.setBorder(makeBorder("Task Applicability Constraints"));
168           constraintsTable = new JTable(constraintsBuffer);
169           constraintsTable.setPreferredScrollableViewportSize(
170              new Dimension(TOP_PANE_MIN_WIDTH, TOP_PANE_MIN_HEIGHT));
171           JScrollPane constraintsTableSP = new JScrollPane(constraintsTable);
172           south.add(BorderLayout.CENTER,constraintsTableSP);
173           panel.add(BorderLayout.CENTER, south);
174 
175           return panel;
176 
177       }
178 //------------------------------------------------------------------------------
179       JPanel getTaskPanel() {
180          JPanel north =  new JPanel(new BorderLayout());
181          north.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.gray));
182          taskTable = new JTable(taskBuffer);
183          taskTable.setPreferredScrollableViewportSize(
184              new Dimension(TOP_PANE_MIN_WIDTH, TOP_PANE_MIN_HEIGHT));
185          JScrollPane taskTableSP = new JScrollPane(taskTable);
186          taskTable.getSelectionModel().addListSelectionListener(this );
187          taskTable.getSelectionModel().setSelectionMode(
188             ListSelectionModel.SINGLE_SELECTION);
189          north.add(BorderLayout.CENTER,taskTableSP);
190 
191 
192          return north;
193       }
194 //------------------------------------------------------------------------------
195       JPanel getPreConditionsPanel() {
196           JPanel centerLeft =  new JPanel(new GridLayout(2,1,2,6));
197           Border redline = BorderFactory.createLineBorder(Color.red);
198           centerLeft.setBorder(makeBorder("PreConditions"));
199 
200           preCondTable = new JTable(preCondBuffer);
201           preCondTable.setPreferredScrollableViewportSize(
202              new Dimension(TOP_PANE_MIN_WIDTH/2, TOP_PANE_MIN_HEIGHT));
203           JScrollPane preCondTableSP = new JScrollPane(preCondTable);
204           preCondTable.getSelectionModel().addListSelectionListener(this );
205           preCondTable.getSelectionModel().setSelectionMode(
206              ListSelectionModel.SINGLE_SELECTION);
207           preCondTable.setBorder(BorderFactory.createLineBorder(Color.black));
208           centerLeft.add(preCondTableSP);
209 
210 
211           preAttrTable = new JTable(preAttrBuffer);
212           preAttrTable.setPreferredScrollableViewportSize(
213              new Dimension(TOP_PANE_MIN_WIDTH/2, TOP_PANE_MIN_HEIGHT));
214           JScrollPane preAttrTableSP = new JScrollPane(preAttrTable);
215           preAttrTable.getSelectionModel().addListSelectionListener(this );
216           preAttrTable.getSelectionModel().setSelectionMode(
217              ListSelectionModel.SINGLE_SELECTION);
218           preAttrTable.setBorder(BorderFactory.createLineBorder(Color.black));
219           centerLeft.add(preAttrTableSP);
220 
221 
222           return centerLeft;
223       }
224 //------------------------------------------------------------------------------
225       JPanel getEffectsPanel() {
226           JPanel centerRight =  new JPanel(new GridLayout(2,1,2,6));
227           centerRight.setBorder(makeBorder("Effects"));
228           effectsTable = new JTable(effectsBuffer);
229           effectsTable.setPreferredScrollableViewportSize(
230              new Dimension(TOP_PANE_MIN_WIDTH/2, TOP_PANE_MIN_HEIGHT));
231           JScrollPane effectsTableSP = new JScrollPane(effectsTable);
232           effectsTable.getSelectionModel().addListSelectionListener(this );
233           effectsTable.getSelectionModel().setSelectionMode(
234              ListSelectionModel.SINGLE_SELECTION);
235           effectsTable.setBorder(BorderFactory.createLineBorder(Color.black));
236           centerRight.add(effectsTableSP);
237 
238 
239           effectsAttrTable = new JTable(effectsAttrBuffer);
240           effectsAttrTable.setPreferredScrollableViewportSize(
241              new Dimension(TOP_PANE_MIN_WIDTH/2, TOP_PANE_MIN_HEIGHT));
242           JScrollPane effectsAttrTableSP = new JScrollPane(effectsAttrTable);
243           effectsAttrTable.getSelectionModel().addListSelectionListener(this );
244           effectsAttrTable.getSelectionModel().setSelectionMode(
245              ListSelectionModel.SINGLE_SELECTION);
246           effectsAttrTable.setBorder(BorderFactory.createLineBorder(Color.black));
247           centerRight.add(effectsAttrTableSP);
248 
249 
250           return centerRight;
251       }
252 //------------------------------------------------------------------------------
253       private TitledBorder makeBorder(String title) {
254           TitledBorder border = (BorderFactory.createTitledBorder(title));
255           border.setTitlePosition(TitledBorder.TOP);
256 	  border.setTitleJustification(TitledBorder.RIGHT);
257 	  border.setTitleFont(new Font("Helvetica", Font.BOLD, 12));
258 	  border.setTitleColor(Color.black);
259 
260           return border;
261 
262      }
263 //------------------------------------------------------------------------------
264    JPanel getCostPanel() {
265 
266     JPanel costPanel = new JPanel(new BorderLayout());
267 
268 
269     // Cost Panel information
270     TitledBorder border =
271        BorderFactory.createTitledBorder("Task Cost and Time");
272     border.setTitlePosition(TitledBorder.TOP);
273     border.setTitleJustification(TitledBorder.RIGHT);
274     border.setTitleFont(new Font("Helvetica", Font.BOLD, 12));
275     border.setTitleColor(Color.black);
276     costPanel.setBorder(border);
277 
278     JPanel labelPanel = new JPanel(new GridLayout(2,1,5,10));
279 
280     JLabel label = new JLabel("Cost:");
281     label.setToolTipText("Default cost of performing this task");
282     labelPanel.add(label);
283     label = new JLabel("Time:");
284     label.setToolTipText("Default duration of this task");
285     labelPanel.add(label);
286     costPanel.add(BorderLayout.WEST,labelPanel);
287 
288 
289     JPanel fieldsPanel = new JPanel(new GridLayout(2,1,5,10));
290     costfield = new LargeTextField(2,15);
291     costfield.setLineWrap(true);
292     costfield.setText("");
293     costfield.setEditable(false);
294     fieldsPanel.add(costfield);
295 
296     timefield = new LargeTextField(2,15);
297     timefield.setLineWrap(true);
298     timefield.setText("");
299     timefield.setEditable(false);
300     fieldsPanel.add(timefield);
301 
302     costPanel.add(BorderLayout.CENTER,fieldsPanel);
303 
304     return costPanel;
305 
306    }
307 //------------------------------------------------------------------------------
308       public void valueChanged(ListSelectionEvent e) {
309           int row;
310           if (e.getSource() == taskTable.getSelectionModel() ) {
311 
312                row = taskTable.getSelectedRow();
313                costfield.setText(taskBuffer.getCost(row));
314                timefield.setText(taskBuffer.getTime(row));
315                if (row >= 0  && row < taskBuffer.getRowCount()) {
316                     preCondBuffer.setFacts(taskBuffer.getPreConditions(row));
317                     preCondTable.validate();
318                     effectsBuffer.setFacts(taskBuffer.getEffects(row));
319                     effectsTable.validate();
320                }
321           }
322           else if (e.getSource() == preCondTable.getSelectionModel() ) {
323               row = preCondTable.getSelectedRow();
324               if (row >= 0  && row < preCondBuffer.getRowCount()) {
325                    preAttrBuffer.setFact(preCondBuffer.getFact(row));
326                    preAttrTable.validate();
327                }
328           }
329           else if (e.getSource() == effectsTable.getSelectionModel() ) {
330               row = effectsTable.getSelectedRow();
331               if (row >= 0  && row < effectsBuffer.getRowCount()) {
332                   effectsAttrBuffer.setFact(effectsBuffer.getFact(row));
333                   effectsAttrTable.validate();
334                }
335           }
336       }
337 
338 //------------------------------------------------------------------------------
339      void reSize() {
340         setSize(getWidth()+1,getHeight());
341         setSize(getWidth()-1,getHeight());
342      }
343 }