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.plansch;
25  
26  
27  import javax.swing.*;
28  import javax.swing.border.*;
29  import javax.swing.event.*;
30  import javax.swing.table.*;
31  import java.awt.*;
32  import java.awt.event.*;
33  import java.util.*;
34  
35  
36  import zeus.agentviewer.*;
37  import zeus.util.*;
38  import zeus.gui.fields.*;
39  import zeus.actors.PlanRecord;
40  import zeus.concepts.*;
41  
42  
43  public class PlanSchTableUI extends ZeusInternalFrame
44     implements ActionListener, MouseListener, ChangeListener  {
45  
46     private JPanel  contentPane;
47     private JTable  table;
48     private JTextArea textarea;
49     private JScrollPane topPane;
50  
51     final int TOP_PANE_MIN_HEIGHT = 27;
52     final int TOP_PANE_MIN_WIDTH = 500;
53     final int BOTTOM_PANE_MIN_WIDTH = 500;
54     final int BOTTOM_PANE_MIN_HEIGHT = 30;
55  
56  
57     private static int NUMBER_DISPLAYED = 0;
58     private InternalFramesPanel deskTop;
59     PlanSchModel planSchBuffer;
60     WholeNumberField startTime;
61     JButton setBtn,resetBtn;
62     
63     
64     public PlanSchTableUI(InternalFramesPanel deskTop,
65                             PlanSchModel planSchBuffer)
66       {
67          
68          super(" ",true,true,true,true);
69          try {
70          setTitle("Planner & Scheduler:" + (++NUMBER_DISPLAYED));
71          String sep = System.getProperty("file.separator");
72          String gifpath = SystemProps.getProperty("gif.dir") + "agentviewer" + sep;
73          ImageIcon icon = new ImageIcon(gifpath + ViewerNames.PLANSCH_IMG);
74          setFrameIcon(icon);
75          this.deskTop = deskTop;
76          this.planSchBuffer = planSchBuffer;
77  
78          buildUI();
79          startTime.setText(Integer.toString(planSchBuffer.getFrom()));
80          deskTop.addInternalFrame(this);
81          planSchBuffer.addChangeListener(this);
82          planSchBuffer.setTable(table);
83          setSize(500,350);
84          setDoubleBuffered(true);
85          setVisible(true);
86          } catch (Exception e) { 
87              e.printStackTrace(); }
88  
89        }
90  
91  
92        class KeyLabel extends JPanel {
93          
94             public KeyLabel(String text, Color bColor) {
95              try { 
96                GridBagLayout gridBagLayout = new GridBagLayout();
97                GridBagConstraints gbc = new GridBagConstraints();
98                setLayout(gridBagLayout);
99            
100               // Add the colour panel
101               JPanel colorPanel = new JPanel();
102               colorPanel.setPreferredSize(new Dimension(10,10));
103               colorPanel.setBackground(bColor);
104               gbc.gridwidth = 1;
105               gbc.anchor = GridBagConstraints.WEST;
106               gbc.fill = GridBagConstraints.NONE;
107               gbc.insets = new Insets(0,0,0,0);
108               gbc.weightx = gbc.weighty = 0;
109               gridBagLayout.setConstraints(colorPanel,gbc);
110               add(colorPanel);
111 
112               // Add the text 
113               JLabel label = new JLabel(text);
114               gbc.gridwidth = GridBagConstraints.REMAINDER;
115               gbc.anchor = GridBagConstraints.WEST;
116               gbc.fill = GridBagConstraints.HORIZONTAL;
117               gbc.insets = new Insets(0,5,0,0);
118               gbc.weightx = gbc.weighty = 1;
119               gridBagLayout.setConstraints(label,gbc);
120               add(label);
121                } catch (Exception e) { 
122             e.printStackTrace(); }
123 
124            }
125       }
126 //-------------------------------------------------------------------------
127       private JPanel getColorPanel() {
128         try {
129          KeyLabel lb;
130          JPanel skeyPanel = new JPanel();
131          skeyPanel.setLayout(new GridLayout(3,3,5,2));
132          for(int i = 0; i < PlanRecord.state_string.length; i++ ) {
133             lb = new KeyLabel(PlanRecord.state_string[i],PlanRecord.color[i]);
134             skeyPanel.add(lb);
135          }
136    
137          JPanel keyPanel = new JPanel();
138          GridBagLayout gb = new GridBagLayout();
139          GridBagConstraints gbc = new GridBagConstraints();
140          keyPanel.setLayout(gb);
141    
142          gbc.insets = new Insets(10,5,0,0);
143          gbc.anchor = GridBagConstraints.NORTHWEST;
144          gbc.fill = GridBagConstraints.NONE;
145          gbc.gridwidth = GridBagConstraints.REMAINDER;
146          JLabel keyLb = new JLabel("Key",SwingConstants.LEFT);
147          keyLb.setFont(new Font("Courier",Font.BOLD,14));
148          keyLb.setForeground(Color.black);
149          gb.setConstraints(keyLb,gbc);
150          keyPanel.add(keyLb);
151          gbc.insets = new Insets(5,5,5,5);
152          gbc.weightx = gbc.weighty = 1;
153          gb.setConstraints(skeyPanel,gbc);
154          keyPanel.add(skeyPanel);
155       
156 
157          return keyPanel;
158             } catch (Exception e) { 
159             e.printStackTrace(); }
160             return null;
161         
162       }
163 
164 
165       private void buildUI() {
166         try {
167           table = new JTable(planSchBuffer);
168           planSchBuffer.setTable(table); 
169           table.addMouseListener(this);
170           table.getSelectionModel().setSelectionMode(
171              ListSelectionModel.SINGLE_SELECTION);
172           table.setCellSelectionEnabled(true);
173           DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {
174              public void setValue(Object value) {
175                 if ( value == null ) {
176                 super.setValue("Free");
177                    setBackground(PlanRecord.color[PlanRecord.FREE]);
178                 }
179                 else {
180                    PrimitiveTask task = ((PlanRecord)value).getTask();
181                    Goal g = ((PlanRecord)value).getGoal();
182                    super.setValue(task.getName() + ": " + g.getFactType());
183                    setBackground(PlanRecord.color[((PlanRecord)value).getState()]);
184                 }
185        }
186     };
187 
188           table.setDefaultRenderer(Object.class,renderer);
189 
190           topPane = new JScrollPane(table);
191           topPane.getViewport().setBackground(Color.white);
192 //          topPane.setPreferredSize(new Dimension(TOP_PANE_MIN_WIDTH,
193 //             TOP_PANE_MIN_HEIGHT*planSchBuffer.getProcessors()));
194 
195           textarea = new JTextArea(3,40);
196 
197              textarea.setBorder(BorderFactory.createTitledBorder(
198              BorderFactory.createLineBorder(Color.gray),
199              "Diagnostic Information",
200              TitledBorder.LEFT, TitledBorder.DEFAULT_POSITION,
201              new Font("Courier",Font.BOLD,14), Color.black)
202           );
203 
204     textarea.setEditable(false);
205           textarea.setLineWrap(true);
206           textarea.setWrapStyleWord(true);
207 
208 //          textarea.setPreferredSize(new Dimension(BOTTOM_PANE_MIN_WIDTH,
209 //                                                    BOTTOM_PANE_MIN_HEIGHT));
210           JScrollPane bottomSP = new JScrollPane(textarea);
211 
212           JPanel centerPanel = new JPanel(new BorderLayout());
213           centerPanel.add(BorderLayout.CENTER,topPane);
214           centerPanel.add(BorderLayout.SOUTH, bottomSP);
215 
216           JPanel rangePanel = new JPanel();
217           rangePanel.setLayout(new BoxLayout(rangePanel, BoxLayout.X_AXIS));
218           rangePanel.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.gray));
219           rangePanel.add(Box.createRigidArea(new Dimension(15,10)));
220           JLabel text = new JLabel("Display times from ");
221           text.setAlignmentY(Component.CENTER_ALIGNMENT);
222           text.setForeground(Color.black);
223           rangePanel.add(text);
224           //global
225           startTime = new WholeNumberField();
226           startTime.setPreferredSize(new Dimension(100,20));
227           startTime.setMinimumSize(new Dimension(100,20));
228           startTime.setAlignmentY(Component.CENTER_ALIGNMENT);
229           rangePanel.add(startTime);
230 
231           setBtn = new JButton("Set");
232           setBtn.setAlignmentY(Component.CENTER_ALIGNMENT);
233           setBtn.addActionListener(this);
234           setBtn.setToolTipText("Click to set columns");
235           setBtn.setForeground(Color.red);
236           setBtn.setBorder(BorderFactory.createRaisedBevelBorder());
237 
238           resetBtn = new JButton("Reset");
239           resetBtn.setBorder(BorderFactory.createRaisedBevelBorder());
240           resetBtn.setForeground(Color.blue);
241           resetBtn.setToolTipText("Click to reset columns");
242           resetBtn.setAlignmentY(Component.CENTER_ALIGNMENT);
243           resetBtn.addActionListener(this);
244           JPanel btnPanel = new JPanel(new GridLayout(1,2));
245           btnPanel.add(setBtn);
246           btnPanel.add(resetBtn);
247           rangePanel.add(btnPanel);
248           rangePanel.add(Box.createRigidArea(new Dimension(15,10)));
249 
250           contentPane = (JPanel) getContentPane();
251           contentPane.setLayout(new BorderLayout(10,10));
252           contentPane.add(BorderLayout.CENTER,centerPanel);
253           contentPane.add(BorderLayout.NORTH, rangePanel);
254           contentPane.add(BorderLayout.SOUTH, getColorPanel());
255           pack();
256          } catch (Exception e) { 
257             e.printStackTrace(); }
258 
259       }
260 
261 
262 
263         public void stateChanged(ChangeEvent c) {
264             try { 
265                 startTime.setValue(planSchBuffer.getFrom());
266                 textarea.setText("  ");
267                 table.repaint(); 
268               //  reSize();
269               } catch (Exception e) { 
270             e.printStackTrace(); }
271 
272         }
273 
274 
275     public void mouseClicked(MouseEvent e) {
276        int iMouseX = e.getX();
277        int iMouseY = e.getY();
278 
279        int sCol = table.columnAtPoint(new Point(iMouseX,iMouseY));
280        int sRow = table.rowAtPoint(new Point(iMouseX,iMouseY));
281        PlanRecord pr = (PlanRecord) table.getValueAt(sRow,sCol);
282        if (pr != null && pr.diagnostic() != null )
283           textarea.setText(pr.diagnostic());
284        else
285           textarea.setText("");
286     }
287 //-------------------------------------------------------------------------
288     public void mouseEntered(MouseEvent e) {}
289 //-------------------------------------------------------------------------
290     public void mousePressed(MouseEvent e) {}
291 //-------------------------------------------------------------------------
292     public void mouseExited(MouseEvent e) {}
293 //-------------------------------------------------------------------------
294     public void mouseReleased(MouseEvent e) {}
295 //-------------------------------------------------------------------------
296      public void actionPerformed(ActionEvent event) {
297         try {
298           Object source = event.getSource();
299 
300           if ( source == setBtn) {
301              Long value = startTime.getValue();
302              if ( value != null ) planSchBuffer.setFrom(value.intValue());
303           }
304           else if (source == resetBtn) {
305              planSchBuffer.reSetFrom();
306           }
307          } catch (Exception e) { 
308             e.printStackTrace(); }
309 
310      }
311 //-------------------------------------------------------------------------
312      void reSize() {
313         try {
314         setSize(getWidth()+1,getHeight());
315         setSize(getWidth()-1,getHeight());
316          } catch (Exception e) { 
317             e.printStackTrace(); }
318 
319      }
320 //------------------------------------------------------------------------
321 }
322 
323 
324