KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > binarytree > SearchModel


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
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 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
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.binarytree;
32
33 public class SearchModel extends javax.swing.table.AbstractTableModel JavaDoc {
34     
35     protected static int NUM_COLUMNS = 3;
36     protected static int START_NUM_ROWS = 17;
37     protected int nextEmptyRow = 0;
38     protected int numRows = 0;
39     
40     protected java.util.Vector JavaDoc data = null;
41
42     public SearchModel() {
43     data = new java.util.Vector JavaDoc();
44     }
45
46     public String JavaDoc getColumnName(int column) {
47         switch (column) {
48     case 0:
49             return "Key";
50     case 1:
51             return "Value";
52     case 2:
53         return "Order";
54         }
55         return "";
56     }
57
58     public synchronized int getColumnCount() {
59         return NUM_COLUMNS;
60     }
61
62     public synchronized int getRowCount() {
63         if (numRows < START_NUM_ROWS)
64             return START_NUM_ROWS;
65         else
66             return numRows;
67     }
68
69     public synchronized Object JavaDoc getValueAt(int row, int column) {
70     try {
71             String JavaDoc[] t = (String JavaDoc[])data.elementAt(row);
72             switch (column) {
73         case 0:
74         return t[0];
75         case 1:
76         return t[1];
77         case 2:
78         return t[2];
79             }
80         }
81     catch (Exception JavaDoc e) {}
82         return "";
83     }
84
85     public synchronized void updateKeyValue(String JavaDoc[] keyValue) {
86     // Find the key
87
String JavaDoc key = keyValue[0];
88     String JavaDoc[] kv = null;
89     int index = -1;
90         boolean found = false;
91         boolean addedRow = false;
92         
93         int i = 0;
94     
95         while (!found && (i < nextEmptyRow)) {
96             kv = (String JavaDoc[])data.elementAt(i);
97             if ((kv != null) && (key.compareTo(kv[0]) == 0)) {
98                 found = true;
99                 index = i;
100             } else {
101                 i++;
102             }
103         }
104     // Update old row
105
if (found) {
106             data.setElementAt(keyValue, index);
107         }
108     // Add new row
109
else {
110              if (numRows <= nextEmptyRow) {
111         // Add a row
112
numRows++;
113                 addedRow = true;
114             }
115             index = nextEmptyRow;
116             data.addElement(keyValue);
117         }
118     
119         nextEmptyRow++;
120
121         // Notify listeners that the data changed.
122
if (addedRow)
123             fireTableRowsInserted(index, index);
124     else
125             fireTableRowsUpdated(index, index);
126     }
127
128     public synchronized void clear() {
129         int oldNumRows = numRows;
130     
131         numRows = 0;
132         data.removeAllElements();
133         nextEmptyRow = 0;
134     
135         if (oldNumRows > START_NUM_ROWS) {
136             fireTableRowsDeleted(START_NUM_ROWS, oldNumRows - 1);
137         }
138         fireTableRowsUpdated(0, START_NUM_ROWS - 1);
139     }
140 }
141
Popular Tags