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  * TaskLinkPanel.java
26  *
27  * Panel through which task constraints are entered
28  ***************************************************************************/
29  
30  package zeus.generator.task;
31  
32  import java.awt.*;
33  import java.awt.event.*;
34  import java.util.*;
35  import javax.swing.*;
36  import javax.swing.border.*;
37  import javax.swing.table.*;
38  import javax.swing.text.*;
39  import javax.swing.event.*;
40  
41  import zeus.util.*;
42  import zeus.concepts.*;
43  import zeus.generator.*;
44  import zeus.generator.util.*;
45  import zeus.gui.fields.*;
46  import zeus.gui.editors.*;
47  import zeus.gui.help.*;
48  
49  public class TaskLinkPanel extends JPanel {
50    static final String[] ERROR_MESSAGE = {
51       /* 0 */ "Please select a row before\ncalling this operation"
52    };
53  
54    protected JTable                linkTable;
55    protected TaskLinkModel         linkModel;
56    protected TaskLinkBaseTreeModel baseModel;
57    protected TaskLinkMainTreeModel mainModel;
58  
59  
60    public TaskLinkPanel(ChangeListener changeListener, TaskNodePanel nodePanel,
61                         GroupManager leftGroupManager,
62                         GroupManager rightGroupManager)  {
63  
64      baseModel = new TaskLinkBaseTreeModel(leftGroupManager,rightGroupManager);
65      mainModel = new TaskLinkMainTreeModel();
66      nodePanel.addRenameListener(mainModel);
67      nodePanel.addRenameListener(baseModel);
68      TaskLinkBaseTreePanel basePanel = new TaskLinkBaseTreePanel(baseModel);
69      TaskLinkMainTreePanel mainPanel = new TaskLinkMainTreePanel(mainModel);
70      basePanel.addLinkRootSelectionListener(mainPanel);
71  
72      linkModel = new TaskLinkModel(leftGroupManager,rightGroupManager);
73      nodePanel.addRenameListener(linkModel);
74      linkModel.addChangeListener(changeListener);
75      basePanel.addLinkNodeSelectionListener(linkModel);
76      mainPanel.addLinkNodeSelectionListener(linkModel);
77  
78      GridBagLayout gridBagLayout = new GridBagLayout();
79      GridBagConstraints gbc = new GridBagConstraints();
80      setLayout(gridBagLayout);
81      setBackground(Color.lightGray);
82      setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
83  
84      JPanel graphPanel = new JPanel();
85      gbc.gridwidth = GridBagConstraints.REMAINDER;
86      gbc.anchor = GridBagConstraints.WEST;
87      gbc.fill = GridBagConstraints.BOTH;
88      gbc.insets = new Insets(8,8,8,8);
89      gridBagLayout.setConstraints(graphPanel,gbc);
90      add(graphPanel);
91  
92      // Add the panel containing the task's applicability constraints
93      // to this panel.
94      JPanel linkPanel = new JPanel();
95      gbc.anchor = GridBagConstraints.NORTHWEST;
96      gbc.fill = GridBagConstraints.BOTH;
97      gbc.weightx = gbc.weighty = 1;
98      gbc.insets = new Insets(8,8,8,8);
99      gridBagLayout.setConstraints(linkPanel,gbc);
100     add(linkPanel);
101 
102     // create graph panel info
103     TitledBorder border;
104     border = BorderFactory.createTitledBorder("Effect-Precondition Links");
105     border.setTitlePosition(TitledBorder.TOP);
106     border.setTitleJustification(TitledBorder.RIGHT);
107     border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
108     border.setTitleColor(Color.blue);
109     graphPanel.setBorder(border);
110 
111     graphPanel.setLayout(new GridLayout(1,2,5,5));
112     graphPanel.setBackground(Color.lightGray);
113     graphPanel.add(basePanel);
114     graphPanel.add(mainPanel);
115 
116     // Add linkPanel info;
117     TableColumnModel tm;
118     TableColumn column;
119     JToolBar toolbar;
120     JScrollPane scrollPane;
121 
122     tm = new DefaultTableColumnModel();
123     column = new TableColumn(TaskLinkModel.LEFT,12);
124     column.setHeaderValue(linkModel.getColumnName(TaskLinkModel.LEFT));
125     tm.addColumn(column);
126     column = new TableColumn(TaskLinkModel.RIGHT,12);
127     column.setHeaderValue(linkModel.getColumnName(TaskLinkModel.RIGHT));
128     tm.addColumn(column);
129 
130     linkTable = new JTable(linkModel,tm);
131     linkTable.getTableHeader().setReorderingAllowed(false);
132     linkTable.setColumnSelectionAllowed(false);
133 
134     toolbar = new TaskLinkToolBar();
135 
136     border = BorderFactory.createTitledBorder("Links");
137     border.setTitlePosition(TitledBorder.TOP);
138     border.setTitleJustification(TitledBorder.RIGHT);
139     border.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
140     border.setTitleColor(Color.blue);
141     linkPanel.setBorder(border);
142 
143     gridBagLayout = new GridBagLayout();
144     linkPanel.setLayout(gridBagLayout);
145     linkPanel.setBackground(Color.lightGray);
146 
147     scrollPane = new JScrollPane(linkTable);
148     scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
149     scrollPane.setPreferredSize(new Dimension(340,150));
150     linkTable.setBackground(Color.white);
151 
152     linkTable.setMinimumSize(new Dimension(400,200));
153 
154     gbc = new GridBagConstraints();
155     gbc.anchor = GridBagConstraints.WEST;
156     gbc.fill = GridBagConstraints.NONE;
157     gbc.gridwidth = GridBagConstraints.REMAINDER;
158     gbc.insets = new Insets(0,8,0,0);
159     gridBagLayout.setConstraints(toolbar,gbc);
160     linkPanel.add(toolbar);
161 
162     gbc = new GridBagConstraints();
163     gbc.gridwidth = GridBagConstraints.REMAINDER;
164     gbc.anchor = GridBagConstraints.WEST;
165     gbc.fill = GridBagConstraints.BOTH;
166     gbc.weightx = gbc.weighty = 1;
167     gbc.insets = new Insets(8,8,8,8);
168     gridBagLayout.setConstraints(scrollPane,gbc);
169     linkPanel.add(scrollPane);
170   }
171 
172   class TaskLinkToolBar extends JToolBar implements ActionListener {
173 
174      protected HelpWindow    helpWin;
175      protected JToggleButton helpBtn;
176      protected JButton       addBtn;
177      protected JButton       deleteBtn;
178 
179      public TaskLinkToolBar() {
180         setBackground(Color.lightGray);
181         setBorder( new BevelBorder(BevelBorder.LOWERED ) );
182         setFloatable(false);
183 
184         String sep = System.getProperty("file.separator");
185         String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
186 
187         // New Button
188         addBtn = new JButton(new ImageIcon(path + "add.gif"));
189 	addBtn.setMargin(new Insets(0,0,0,0));
190         add(addBtn);
191         addBtn.setToolTipText("New");
192         addBtn.addActionListener(this);
193 
194         // Delete Button
195         deleteBtn = new JButton(new ImageIcon(path + "delete1.gif"));
196 	deleteBtn.setMargin(new Insets(0,0,0,0));	
197         add(deleteBtn);
198         deleteBtn.setToolTipText("Delete");
199         deleteBtn.addActionListener(this);
200 
201         addSeparator();
202      
203         // Help Button
204         helpBtn = new JToggleButton(new ImageIcon(path + "help.gif"));
205 	helpBtn.setMargin(new Insets(0,0,0,0));	
206         add(helpBtn);
207         helpBtn.setToolTipText("Help");
208         helpBtn.addActionListener(this);
209      }
210   
211      public void setEnabled(boolean set) {
212         addBtn.setEnabled(set);
213         deleteBtn.setEnabled(set);
214      }
215 
216      public void actionPerformed(ActionEvent e)  {
217         Object src = e.getSource();
218         if ( src == addBtn ) {
219            linkModel.addNewRow();
220         }
221         else if ( src == deleteBtn ) {
222            if ( !isRowSelected(linkTable) ) return;
223            linkModel.removeRows(linkTable.getSelectedRows());
224         }  
225         else if ( src == helpBtn ) {
226           if ( helpBtn.isSelected() ) {
227               Point dispos = getLocation();
228               helpWin = new HelpWindow(SwingUtilities.getRoot(this),
229                  dispos, "generator", "Summary Task Link Table");
230               helpWin.setSource(helpBtn);
231           }
232           else
233               helpWin.dispose();
234         }
235      }
236   }
237 
238   protected boolean isRowSelected(JTable table) {
239      int row = table.getSelectedRow();
240      if ( row == -1) {
241         errorMsg(0);
242         return false;
243      }
244      return true;
245   }
246 
247   protected void errorMsg(int tag) {
248      JOptionPane.showMessageDialog(this,ERROR_MESSAGE[tag],
249                                    "Error", JOptionPane.ERROR_MESSAGE);
250   }
251 
252   TaskLink[] getLinks() {
253      return linkModel.getData();
254   }
255 
256   void reset(TaskNode node, TaskNode[] others, TaskLink[] links) {
257      String mode = null;
258      if      ( node.isBeginNode() ) mode = TaskLinkBaseTreeModel.EFFECTS;
259      else if ( node.isEndNode()   ) mode = TaskLinkBaseTreeModel.PRECONDITIONS;
260      else                           mode = TaskLinkBaseTreeModel.BOTH;
261 
262      baseModel.reset(mode,node.getName());
263      mainModel.reset(others,node.getName());
264      linkModel.reset(node.getName(),links);
265   }
266 }