1 31 package org.objectweb.proactive.examples.binarytree; 32 33 public class SearchModel extends javax.swing.table.AbstractTableModel { 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 data = null; 41 42 public SearchModel() { 43 data = new java.util.Vector (); 44 } 45 46 public String 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 getValueAt(int row, int column) { 70 try { 71 String [] t = (String [])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 e) {} 82 return ""; 83 } 84 85 public synchronized void updateKeyValue(String [] keyValue) { 86 String key = keyValue[0]; 88 String [] 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 [])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 if (found) { 106 data.setElementAt(keyValue, index); 107 } 108 else { 110 if (numRows <= nextEmptyRow) { 111 numRows++; 113 addedRow = true; 114 } 115 index = nextEmptyRow; 116 data.addElement(keyValue); 117 } 118 119 nextEmptyRow++; 120 121 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 |