/Users/lyon/j4p/src/javagroup/tools/processmanager/ProcessWatcherPanel.java

1    package javagroup.tools.processmanager; 
2     
3    import javagroup.process.ProcessManagerHolder; 
4     
5    import javax.swing.*; 
6    import javax.swing.table.DefaultTableCellRenderer; 
7    import javax.swing.table.TableColumn; 
8    import java.awt.*; 
9    import java.awt.event.ActionEvent; 
10   import java.awt.event.ActionListener; 
11    
12   public class ProcessWatcherPanel extends JPanel implements ActionListener { 
13    
14       /** 
15        * Table containing process info * 
16        */ 
17       protected JTable _processTable; 
18       protected JButton _killButton; 
19       protected ProcessDataModel _dataModel; 
20    
21       private static final int __MINIMUM_HEIGHT = 200; 
22       public static final int CLICK_DURATION = 1000; 
23    
24       public ProcessWatcherPanel() { 
25    
26           ProcessDataModel myDataModel = new ProcessDataModel(); 
27           _dataModel = myDataModel; 
28           JTable table = new JTable(myDataModel); 
29           _processTable = table; 
30    
31           table.setRowSelectionAllowed(true); 
32           table.setColumnSelectionAllowed(false); 
33           table.setCellSelectionEnabled(false); 
34           table.setSelectionMode(0); // SINGLE_SELECTION 
35           JCheckBox cb = new JCheckBox(); 
36           TableColumn tCol = table.getColumn( 
37                   table.getModel().getColumnName(ProcessDataModel.C_PID)); 
38    
39           tCol.setCellRenderer(new DefaultTableCellRenderer()); 
40           tCol.setCellEditor(new DefaultCellEditor(cb)); 
41    
42           //Create the scroll pane and add the table to it. 
43           JScrollPane scrollPane = new JScrollPane(table); 
44    
45           //Make the table heads be the non-scrolling column header. 
46           //JViewport columnHeading = new JViewport(); 
47           //columnHeading.setView(table.getTableHeader()); 
48           //columnHeading.setLayout(new BoxLayout(columnHeading,    //HACK 
49           //            BoxLayout.X_AXIS)); 
50           //scrollPane.setColumnHeader(columnHeading); 
51    
52           _killButton = new JButton("Kill process"); 
53           _killButton.addActionListener(this); 
54    
55           this.setLayout(new BorderLayout()); 
56           this.add("Center", scrollPane); 
57           this.add("South", _killButton); 
58       } 
59    
60       public Dimension getPreferredSize() { 
61           Dimension pref_size = super.getPreferredSize(); 
62           return 
63                   new Dimension(pref_size.width, 
64                           Math.max(__MINIMUM_HEIGHT, pref_size.height)); 
65       } 
66    
67       protected void killCurrentSelection() { 
68           if (_processTable.getSelectedRow() >= _processTable.getRowCount()) 
69               return; 
70    
71           try { 
72               long pid; 
73               pid = 
74                       Long.parseLong((String) _dataModel 
75                       .getValueAt(_processTable.getSelectedRow(), 
76                               ProcessDataModel.C_PID)); 
77               ProcessManagerHolder.getProcessManager().kill(pid); 
78           } catch (Exception e) { 
79               e.printStackTrace(); 
80           } 
81       } 
82    
83       public void actionPerformed(ActionEvent event) { 
84    
85           killCurrentSelection(); 
86    
87       } 
88    
89   } 
90    
91