1 /***
2 * ProcessInfoReport.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2003 Denis Bregeon
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * contact information: dbregeon@sourceforge.net
25 */
26 package org.jcreme.tutorial;
27
28 import java.util.Arrays;
29 import java.util.HashSet;
30 import java.util.Iterator;
31 import java.util.Vector;
32
33 import org.jcreme.filters.ReportFilter;
34 import org.jcreme.reporting.DataChangeEvent;
35 import org.jcreme.reporting.DataChangeListener;
36 import org.jcreme.reporting.Report;
37 import org.jcreme.reporting.ReportChangeEvent;
38 import org.jcreme.reporting.ReportChangeListener;
39
40 /***
41 *
42 * @author $Author: dbregeon $
43 * @version $Revision: 1.1 $
44 */
45 public class ProcessInfoReport implements Report, DataChangeListener {
46 protected String[] columnNames = new String[] { "Id", "Process Name",
47 "CPU", "Memory" };
48
49 protected ProcessInfoSource processInfoSource = null;
50
51 protected Vector rows = new Vector();
52
53 protected HashSet listeners = new HashSet();
54
55 /***
56 * @see DataChangeListener#dataChanged(DataChangeEvent)
57 */
58 public void dataChanged(DataChangeEvent e) {
59 if (e.getSource() == this.processInfoSource) {
60 synchronized (this) {
61 if (!this.rows.contains(e.getData())) {
62 this.rows.add(e.getData());
63 fireReportChangeEvent(ReportChangeEvent.INSERT, this.rows
64 .size() - 1, this.rows.size() - 1);
65 } else if (this.processInfoSource.isValid((ProcessInfo) e
66 .getData())) {
67 int line = this.rows.indexOf(e.getData());
68 fireReportChangeEvent(ReportChangeEvent.UPDATE, line, line);
69 } else {
70 int line = this.rows.indexOf(e.getData());
71 this.rows.remove(e.getData());
72 fireReportChangeEvent(ReportChangeEvent.DELETE, line, line);
73 }
74 }
75 }
76 }
77
78 /***
79 * @see org.jcreme.reporting.Report#getTitle()
80 */
81 public String getTitle() {
82 return "Process Report";
83 }
84
85 /***
86 * @see org.jcreme.reporting.Report#getColumnNames()
87 */
88 public String[] getColumnNames() {
89 return this.columnNames;
90 }
91
92 /***
93 * @see org.jcreme.reporting.Report#getColumnIndex(java.lang.String)
94 */
95 public int getColumnIndex(String columnName) {
96 int result = -1;
97 if (columnName != null) {
98 for (int i = 0; ((i < this.columnNames.length) && (result < 0)); i++) {
99 if (columnName.equals(this.columnNames[i])) {
100 result = i;
101 }
102 }
103 }
104 return 0;
105 }
106
107 /***
108 * @see org.jcreme.reporting.Report#getColumnName(int)
109 */
110 public String getColumnName(int index) {
111 String result = null;
112 if ((index >= 0) && (index < this.columnNames.length)) {
113 result = this.columnNames[index];
114 }
115 return result;
116 }
117
118 /***
119 * @see org.jcreme.reporting.Report#getColumnCount()
120 */
121 public int getColumnCount() {
122 return this.columnNames.length;
123 }
124
125 /***
126 * @see org.jcreme.reporting.Report#getRowCount()
127 */
128 public int getRowCount() {
129 return this.rows.size();
130 }
131
132 /***
133 * @see org.jcreme.reporting.Report#getValueAt(int, int)
134 */
135 public Object getValueAt(int row, int column) {
136 Object result = null;
137 if ((row >= 0) && (row < this.rows.size())) {
138 try {
139 ProcessInfo value = (ProcessInfo) this.rows.get(row);
140 switch (column) {
141 case 0:
142 result = value.getProcessId();
143 break;
144 case 1:
145 result = value.getProcessName();
146 break;
147 case 2:
148 result = value.getCPUUsage();
149 break;
150 case 3:
151 result = value.getMemoryUsage();
152 break;
153 }
154 } catch (ArrayIndexOutOfBoundsException e) {
155 e.printStackTrace();
156 }
157
158 }
159 return result;
160 }
161
162 /***
163 * @see org.jcreme.reporting.Report#addReportChangeListener(org.jcreme.reporting.ReportChangeListener)
164 */
165 public void addReportChangeListener(ReportChangeListener listener) {
166 if (listener != null) {
167 synchronized (this.listeners) {
168 this.listeners.add(listener);
169 }
170 }
171 }
172
173 /***
174 * @see org.jcreme.reporting.Report#removeReportChangeListener(org.jcreme.reporting.ReportChangeListener)
175 */
176 public void removeReportChangeListener(ReportChangeListener listener) {
177 if (listener != null) {
178 synchronized (this.listeners) {
179 this.listeners.remove(listener);
180 }
181 }
182 }
183
184 /***
185 * @see org.jcreme.reporting.Report#clear()
186 */
187 public void clear() {
188 synchronized (this.rows) {
189 this.rows.clear();
190 fireReportChangeEvent(ReportChangeEvent.DELETE, 0,
191 this.rows.size() - 1);
192 }
193 }
194
195 /***
196 * @see org.jcreme.reporting.Report#getFilter()
197 */
198 public ReportFilter getFilter() {
199 return null;
200 }
201
202 /***
203 * @see org.jcreme.reporting.Report#setFilter(org.jcreme.filters.ReportFilter)
204 */
205 public void setFilter(ReportFilter filter) {
206
207 }
208
209 /***
210 * @see org.jcreme.reporting.Report#getColumnClass(int)
211 */
212 public Class getColumnClass(int columnIndex) {
213 Class result = null;
214 switch (columnIndex) {
215 case 0:
216 result = Integer.class;
217 break;
218 case 1:
219 result = String.class;
220 break;
221 case 2:
222 result = Double.class;
223 break;
224 case 3:
225 result = Double.class;
226 break;
227 }
228 return result;
229 }
230
231 /***
232 *
233 * @param source
234 */
235 public void setProcessInfoSource(ProcessInfoSource source) {
236 if (this.processInfoSource != null) {
237 this.processInfoSource.removeDataChangeListener(this);
238 }
239 this.processInfoSource = source;
240 clear();
241 if (this.processInfoSource != null) {
242 synchronized (this) {
243 this.rows.addAll(Arrays.asList(this.processInfoSource
244 .getProcessInfos()));
245 fireReportChangeEvent(ReportChangeEvent.INSERT, 0, this.rows
246 .size() - 1);
247 }
248 this.processInfoSource.addDataChangeListener(this);
249 }
250 }
251
252 protected void fireReportChangeEvent(int type, int firstRow, int lastRow) {
253 synchronized (this.listeners) {
254 ReportChangeEvent evt = new ReportChangeEvent(this, firstRow,
255 lastRow, 0, this.columnNames.length - 1, type);
256 Iterator iter = this.listeners.iterator();
257 while (iter.hasNext()) {
258 ((ReportChangeListener) iter.next()).reportChanged(evt);
259 }
260 }
261 }
262
263 }