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  * GenerationPanel.java
26  *
27  * The 'summary' panel for the code generation process
28  *****************************************************************************/
29  
30  package zeus.generator.code;
31  
32  import java.io.*;
33  import java.awt.*;
34  import java.awt.event.*;
35  import java.util.*;
36  import javax.swing.*;
37  import javax.swing.event.*;
38  import javax.swing.table.*;
39  import javax.swing.border.*;
40  
41  import zeus.gui.help.*;
42  import zeus.util.*;
43  import zeus.generator.*;
44  
45  public class GenerationPanel extends JPanel
46                               implements ActionListener,
47                                          ChangeListener,
48                                          FocusListener {
49  
50    static final String[] ERROR_MESSAGE = {
51       /* 0 */ "Please select a row before\ncalling this operation"
52    };
53  
54    protected JTable               table;
55    protected GenerationTableModel model;
56  
57    protected JTextField      dirField;
58    protected JRadioButton    win;
59    protected JRadioButton    unix;
60    protected JRadioButton    zsh; 
61    protected JButton         selectBtn;
62    protected GenerationPlan  genplan;
63    protected JTextArea       textArea;
64  
65    public GenerationPanel(GenerationPlan genplan) {
66      this.genplan = genplan;
67      genplan.addChangeListener(this);
68  
69      setBackground(Color.lightGray);
70  
71      GridBagLayout gridBagLayout = new GridBagLayout();
72      setLayout(gridBagLayout);
73      GridBagConstraints gbc = new GridBagConstraints();
74  
75      JPanel dirPanel = new JPanel();
76      gbc = new GridBagConstraints();
77      gbc.gridwidth = GridBagConstraints.REMAINDER;
78      gbc.anchor = GridBagConstraints.WEST;
79      gbc.fill = GridBagConstraints.HORIZONTAL;
80      gbc.insets = new Insets(8,8,0,8);
81      gridBagLayout.setConstraints(dirPanel,gbc);
82      add(dirPanel);
83  
84      JPanel topPanel = new JPanel();
85      gbc = new GridBagConstraints();
86      gbc.gridwidth = GridBagConstraints.REMAINDER;
87      gbc.anchor = GridBagConstraints.WEST;
88      gbc.fill = GridBagConstraints.BOTH;
89      gbc.insets = new Insets(8,8,0,8);
90      gbc.weightx = gbc.weighty = 1;
91      gridBagLayout.setConstraints(topPanel,gbc);
92      add(topPanel);
93  
94      JPanel bottomPanel = new JPanel();
95      gbc = new GridBagConstraints();
96      gbc.gridwidth = GridBagConstraints.REMAINDER;
97      gbc.anchor = GridBagConstraints.WEST;
98      gbc.fill = GridBagConstraints.BOTH;
99      gbc.insets = new Insets(8,8,0,8);
100     gbc.weightx = gbc.weighty = 1;
101     gridBagLayout.setConstraints(bottomPanel,gbc);
102     add(bottomPanel);
103 
104     // create top panel
105     topPanel.setBackground(Color.lightGray);
106     TitledBorder b1 = (BorderFactory.createTitledBorder("Generation Plan"));
107     b1.setTitlePosition(TitledBorder.TOP);
108     b1.setTitleJustification(TitledBorder.RIGHT);
109     b1.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
110     b1.setTitleColor(Color.blue);
111     topPanel.setBorder(b1);
112 
113     model = new GenerationTableModel(genplan);
114     JToolBar toolbar = new GenerationToolBar();
115 
116     TableColumnModel tm = new DefaultTableColumnModel();
117     TableColumn column;
118 
119     column = new TableColumn(GenerationInfo.NAME,24);
120     column.setHeaderValue(model.getColumnName(GenerationInfo.NAME));
121     tm.addColumn(column);
122 
123     column = new TableColumn(GenerationInfo.TYPE,24);
124     column.setHeaderValue(model.getColumnName(GenerationInfo.TYPE));
125     tm.addColumn(column);
126 
127     column = new TableColumn(GenerationInfo.COMMAND,24);
128     column.setHeaderValue(model.getColumnName(GenerationInfo.COMMAND));
129     tm.addColumn(column);
130 
131     table = new JTable(model,tm);
132     table.getTableHeader().setReorderingAllowed(false);
133     table.setColumnSelectionAllowed(false);
134     table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
135 
136     JScrollPane scrollPane = new JScrollPane(table);
137     scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
138     scrollPane.setPreferredSize(new Dimension(300,160));
139     table.setBackground(Color.white);
140 
141     gridBagLayout = new GridBagLayout();
142     gbc = new GridBagConstraints();
143     topPanel.setLayout(gridBagLayout);
144 
145     gbc.anchor = GridBagConstraints.WEST;
146     gbc.fill = GridBagConstraints.NONE;
147     gbc.insets = new Insets(0,8,0,0);
148     gbc.gridwidth = GridBagConstraints.REMAINDER;
149     gridBagLayout.setConstraints(toolbar,gbc);
150     topPanel.add(toolbar);
151 
152     gbc.gridwidth = GridBagConstraints.REMAINDER;
153     gbc.anchor = GridBagConstraints.WEST;
154     gbc.fill = GridBagConstraints.BOTH;
155     gbc.weightx = gbc.weighty = 1;
156     gbc.insets = new Insets(8,8,8,8);
157     gridBagLayout.setConstraints(scrollPane, gbc);
158     topPanel.add(scrollPane);
159 
160     // create top panel
161     bottomPanel.setBackground(Color.lightGray);
162     b1 = (BorderFactory.createTitledBorder("Generation Messages"));
163     b1.setTitlePosition(TitledBorder.TOP);
164     b1.setTitleJustification(TitledBorder.RIGHT);
165     b1.setTitleFont(new Font("Helvetica", Font.BOLD, 14));
166     b1.setTitleColor(Color.blue);
167     bottomPanel.setBorder(b1);
168 
169     textArea = new JTextArea(12,80);
170     textArea.setEditable(false);
171     textArea.setLineWrap(true);
172     textArea.setBackground(Color.white);
173 
174     scrollPane = new JScrollPane(textArea);
175     scrollPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
176     scrollPane.setPreferredSize(new Dimension(300,160));
177 
178     gridBagLayout = new GridBagLayout();
179     gbc = new GridBagConstraints();
180     bottomPanel.setLayout(gridBagLayout);
181 
182     gbc.anchor = GridBagConstraints.NORTHWEST;
183     gbc.fill = GridBagConstraints.BOTH;
184     gbc.gridwidth = GridBagConstraints.REMAINDER;
185     gbc.weightx = gbc.weighty = 1;
186     gbc.insets = new Insets(8,8,8,8);
187     gridBagLayout.setConstraints(scrollPane,gbc);
188     bottomPanel.add(scrollPane);
189 
190     // directory panel info
191     dirPanel.setBackground(Color.lightGray);
192     BevelBorder b2 = new BevelBorder(BevelBorder.LOWERED);
193     dirPanel.setBorder(b2);
194 
195     gridBagLayout = new GridBagLayout();
196     gbc = new GridBagConstraints();
197     dirPanel.setLayout(gridBagLayout);
198 
199     dirField = new JTextField(20);
200     dirField.addActionListener(this);
201     dirField.addFocusListener(this);
202     dirField.setText(genplan.getDirectory());
203 
204     String path = SystemProps.getProperty("gif.dir") + "generator" +
205                   System.getProperty("file.separator");
206 
207     selectBtn = new JButton("Choose Target Directory", new ImageIcon(path + "open.gif"));
208     selectBtn.setToolTipText("Choose where source code will be written");
209     selectBtn.addActionListener(this);
210 
211     gbc.anchor = GridBagConstraints.WEST;
212     gbc.fill = GridBagConstraints.NONE;
213     gbc.weightx = gbc.weighty = 0;
214     gbc.insets = new Insets(4,4,4,4);
215     gridBagLayout.setConstraints(selectBtn, gbc);
216     dirPanel.add(selectBtn);
217 
218     gbc.gridwidth = GridBagConstraints.REMAINDER;
219     gbc.anchor = GridBagConstraints.WEST;
220     gbc.fill = GridBagConstraints.HORIZONTAL;
221     gbc.weightx = gbc.weighty = 1;
222     gbc.insets = new Insets(4,4,4,4);
223     gridBagLayout.setConstraints(dirField, gbc);
224     dirPanel.add(dirField);
225 
226     JLabel groupLbl = new JLabel("Create Scripts for ...", JLabel.CENTER);
227     gbc.gridwidth = 1;
228     gbc.anchor = GridBagConstraints.WEST;
229     gbc.fill = GridBagConstraints.NONE;
230     gbc.weightx = gbc.weighty = 0;
231     gbc.insets = new Insets(4,4,4,0);
232     gridBagLayout.setConstraints(groupLbl, gbc);
233     dirPanel.add(groupLbl);
234 
235     win = new JRadioButton(GenerationPlan.WINDOWS,false);
236     unix = new JRadioButton(GenerationPlan.UNIX,false);
237     zsh = new JRadioButton (GenerationPlan.ZSH,false); 
238     
239     unix.setBackground(Color.lightGray);
240     win.setBackground(Color.lightGray);
241     zsh.setBackground(Color.lightGray); 
242     
243     unix.addActionListener(this);
244     win.addActionListener(this);
245     zsh.addActionListener(this); 
246 
247     if ( genplan.getPlatform().equals(GenerationPlan.WINDOWS) )
248       win.setSelected(true);
249     else
250       unix.setSelected(true);
251     ButtonGroup platform = new ButtonGroup();
252     platform.add(win);
253     platform.add(unix);
254     
255    
256 
257     gbc.anchor = GridBagConstraints.WEST;
258     gbc.fill = GridBagConstraints.NONE;
259     gbc.weightx = gbc.weighty = 0;
260     gbc.insets = new Insets(4,4,4,4);
261     gridBagLayout.setConstraints(win,gbc);
262     dirPanel.add(win);
263 
264     gbc.anchor = GridBagConstraints.WEST;
265     gbc.fill = GridBagConstraints.NONE;
266     gbc.weightx = gbc.weighty = 0;
267     gbc.insets = new Insets(4,4,4,4);
268     gridBagLayout.setConstraints(unix,gbc);
269     dirPanel.add(unix);
270     
271     gbc.anchor = GridBagConstraints.WEST;
272     gbc.fill = GridBagConstraints.NONE;
273     gbc.weightx = gbc.weighty = 0;
274     gbc.insets = new Insets(4,4,4,4);
275     gridBagLayout.setConstraints(zsh,gbc);
276     dirPanel.add(zsh);
277   }
278 
279    protected class GenerationToolBar extends JToolBar
280                                      implements ActionListener {
281    
282      protected JToggleButton agentBtn;
283      protected JToggleButton taskBtn;
284      protected JToggleButton utilityBtn;
285      protected JButton       deleteBtn;
286      public    JToggleButton helpBtn;
287      protected HelpWindow    helpWin;
288    
289      public GenerationToolBar()  {
290        setBackground(Color.lightGray);
291        setBorder(new BevelBorder(BevelBorder.LOWERED));
292        setFloatable(false);
293    
294        String sep = System.getProperty("file.separator");
295        String path = SystemProps.getProperty("gif.dir") + "generator" + sep;
296    
297        utilityBtn = new JToggleButton(new ImageIcon(path + "showutility.gif"), true);
298        add(utilityBtn);
299        utilityBtn.setToolTipText("Hide/Show Utility Agents");
300        utilityBtn.setMargin(new Insets(0,0,0,0));
301        utilityBtn.addActionListener(this);
302    
303        agentBtn = new JToggleButton(new ImageIcon(path + "showagents.gif"), true);
304        add(agentBtn);
305        agentBtn.setToolTipText("Hide/Show Task Agents");
306        agentBtn.setMargin(new Insets(0,0,0,0));
307        agentBtn.addActionListener(this);
308    
309        taskBtn = new JToggleButton(new ImageIcon(path + "showtasks.gif"), true);
310        add(taskBtn);
311        taskBtn.setToolTipText("Hide/Show Tasks");
312        taskBtn.setMargin(new Insets(0,0,0,0));
313        taskBtn.addActionListener(this);
314    
315        deleteBtn = new JButton(new ImageIcon(path + "delete1.gif"));
316        add(deleteBtn);
317        deleteBtn.setToolTipText("Delete selected entry");
318        deleteBtn.setMargin(new Insets(0,0,0,0));
319        deleteBtn.addActionListener(this);
320    
321        addSeparator();
322    
323        helpBtn = new JToggleButton(new ImageIcon(path + "help.gif"));
324        add(helpBtn);
325        helpBtn.setToolTipText("Help");
326        helpBtn.setMargin(new Insets(0,0,0,0));
327        helpBtn.addActionListener(this);
328      }
329      public void actionPerformed(ActionEvent e) {
330        Object src = e.getSource();
331    
332        if ( src == agentBtn )
333          model.setFilter(GenerationTableModel.AGENT_FILTER);
334        else if ( src == taskBtn )
335          model.setFilter(GenerationTableModel.TASK_FILTER);
336        else if ( src == utilityBtn )
337          model.setFilter(GenerationTableModel.UTILITY_FILTER);
338        else if ( src == deleteBtn ) {
339          if (table.getSelectedRow() == -1) {
340             errorMsg(0);
341             return;
342          }
343          else
344            model.removeRows(table.getSelectedRows());
345        }
346        else if ( src == helpBtn ) {
347          if ( helpBtn.isSelected() ) {
348             helpWin = new HelpWindow(SwingUtilities.getRoot(this),
349               getLocation(), "generator", "Generation Table");
350             helpWin.setSource(helpBtn);
351          }
352          else
353             helpWin.dispose();
354        }
355      }
356    }
357    
358    protected void errorMsg(int tag) {
359       JOptionPane.showMessageDialog(this,ERROR_MESSAGE[tag],
360                                     "Error", JOptionPane.ERROR_MESSAGE);
361    }
362    
363    public void    setWriteDir(String dir) { dirField.setText(dir); }
364    public String  getWriteDir()           { return dirField.getText(); }
365    public boolean isWindowsPlatform()     { return win.isSelected(); }
366    public boolean isUnixPlatform()        { return unix.isSelected(); }
367    
368    public boolean isZsh() { return zsh.isSelected();}
369    
370    
371    protected String getDirectory() {
372      String path = getWriteDir();
373      FileDialog f = new FileDialog((Frame)SwingUtilities.getRoot(this),
374         "Select (a file within) Target Directory", FileDialog.LOAD);
375      f.setDirectory(path);
376      f.pack();
377      f.setVisible(true);
378      return f.getDirectory();
379    }
380    
381    protected JTextArea getTextArea() {
382      return textArea;
383    }
384    
385    
386    public void actionPerformed(ActionEvent e)  {
387      Object src = e.getSource();
388      if ( src == selectBtn ) {
389         String dir = getDirectory();
390         genplan.setDirectory(dir);
391         setWriteDir(dir);
392      }
393      else if ( src == dirField ) {
394         String dir = getWriteDir();
395         if ( !dir.equals(genplan.getDirectory()) )
396            genplan.setDirectory(dir);
397      }
398      else if ( src == unix ) {
399         if ( !genplan.getPlatform().equals(GenerationPlan.UNIX) )
400            genplan.setPlatform(GenerationPlan.UNIX);
401      }
402      else if ( src == win ) {
403         if ( !genplan.getPlatform().equals(GenerationPlan.WINDOWS) )
404            genplan.setPlatform(GenerationPlan.WINDOWS);
405      }
406      else if (src == zsh) { 
407          if (!genplan.getShell().equals(GenerationPlan.ZSH))
408             genplan.setShell(GenerationPlan.ZSH); 
409          else 
410             genplan.setShell (GenerationPlan.NONE); 
411      }
412      
413    }
414    
415    
416    public void focusGained(FocusEvent e) {
417    }
418    
419    
420    public void focusLost(FocusEvent e) {
421       if ( e.getSource() == dirField ) {
422         String dir = getWriteDir();
423         if ( !dir.equals(genplan.getDirectory()) )
424            genplan.setDirectory(dir);
425       }
426    }
427 
428    public void stateChanged(ChangeEvent e) {
429       if ( e.getSource() == genplan ) {
430          String dir = getWriteDir();
431          String gen_dir = genplan.getDirectory();
432          if ( !dir.equals(gen_dir) )
433             setWriteDir(gen_dir);
434    
435          String gen_platform = genplan.getPlatform();
436          if ( gen_platform.equals(GenerationPlan.UNIX) &&
437               !unix.isSelected() )
438             unix.setSelected(true);
439          else if ( gen_platform.equals(GenerationPlan.WINDOWS) &&
440               !win.isSelected() )
441             win.setSelected(true);
442       }
443    }
444 }