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

1    package javagroup.tools.processmanager; 
2     
3    import javagroup.process.JProcess; 
4    import javagroup.process.ProcessEventListener; 
5    import javagroup.process.ProcessManager; 
6    import javagroup.process.ProcessManagerHolder; 
7     
8    import javax.swing.table.AbstractTableModel; 
9    import java.util.Enumeration; 
10   import java.util.Vector; 
11    
12   public class ProcessDataModel extends AbstractTableModel 
13           implements ProcessEventListener { 
14    
15       protected Vector _processTableEntries; 
16    
17       private static String __NAME = 
18               "Process name (including argument list)"; 
19    
20       /** 
21        * column numbers 
22        */ 
23       public static final int C_PID = 0, C_NAME = 1; 
24    
25       public int numColumns = 2; 
26    
27       public ProcessDataModel() { 
28    
29           _processTableEntries = new Vector(); 
30    
31           ProcessManager manager = ProcessManagerHolder.getProcessManager(); 
32           Enumeration processes = manager.getProcesses(); 
33    
34           while (processes.hasMoreElements()) 
35               createProcessTableEntry((JProcess) processes.nextElement()); 
36    
37           manager.addProcessEventListener(this); 
38    
39       } 
40    
41       protected void createProcessTableEntry(JProcess process) { 
42    
43           ProcessTableEntry entry = new ProcessTableEntry(process); 
44           _processTableEntries.addElement(entry); 
45    
46       } 
47    
48       public int getColumnCount() { 
49           return numColumns; 
50       } 
51    
52       public int getRowCount() { 
53           return _processTableEntries.size(); 
54       } 
55    
56       public Object getValueAt(int rowIndex, int colIndex) { 
57           if (rowIndex >= getRowCount()) 
58               return ""; 
59    
60           ProcessTableEntry entry = 
61                   (ProcessTableEntry) _processTableEntries.elementAt( 
62                           rowIndex); 
63           if (colIndex == C_PID) 
64               return String.valueOf(entry._pid); 
65           if (colIndex == C_NAME) 
66               return entry._name; 
67    
68           return "NO DATA"; 
69    
70       } 
71    
72       public boolean isCellEditable(int rowIndex, int columnIndex) { 
73    
74           if (columnIndex == C_PID) 
75               return true; 
76           else 
77               return false; 
78    
79       } 
80    
81       public void setValueAt(Object aValue, 
82                              int rowIndex, int colIndex) { 
83           if (rowIndex > getRowCount()) 
84               return; 
85    
86           ProcessTableEntry entry = 
87                   (ProcessTableEntry) _processTableEntries.elementAt( 
88                           rowIndex); 
89           ProcessManagerHolder.getProcessManager().kill(entry._pid); 
90           fireTableDataChanged(); 
91       } 
92    
93       /** 
94        * This method _is_ in TableDataModel. 
95        */ 
96       public String getColumnName(int columnIndex) { 
97           switch (columnIndex) { 
98               case C_PID: 
99                   return "PID"; 
100              case C_NAME: 
101                  return __NAME; 
102              default: 
103                  return "Undefined"; 
104          } 
105      } 
106   
107      public Class getColumnClass(int c) { 
108          return getValueAt(0, c).getClass(); 
109      } 
110   
111      public void processCreated(JProcess process) { 
112          createProcessTableEntry(process); 
113          fireTableDataChanged(); 
114      } 
115   
116      public void processDestroyed(JProcess process) { 
117          _processTableEntries.removeElement(new ProcessTableEntry(process)); 
118          fireTableDataChanged(); 
119      } 
120   
121  } 
122   
123  final class ProcessTableEntry { 
124   
125      protected String _name; 
126      protected long _pid; 
127   
128      public ProcessTableEntry(JProcess process) { 
129          _name = process.getName(); 
130          _pid = process.getPid(); 
131      } 
132   
133      public boolean equals(Object o) { 
134          if (o instanceof ProcessTableEntry) { 
135              ProcessTableEntry entry = (ProcessTableEntry) o; 
136              return _pid == entry._pid; 
137          } 
138          return super.equals(o); 
139      } 
140   
141      public int hashCode() { 
142          return (int) _pid; 
143      } 
144   
145  } 
146   
147   
148