1 19 20 package org.netbeans.modules.db.explorer.dlg; 21 22 23 import java.util.Vector ; 24 25 import javax.swing.event.TableModelEvent ; 26 import javax.swing.table.AbstractTableModel ; 27 28 import org.openide.util.NbBundle; 29 30 34 public class DataModel extends AbstractTableModel 35 { 36 37 private Vector data; 38 39 transient private Vector primaryKeys = new Vector (); 40 transient private Vector uniqueKeys = new Vector (); 41 42 static final long serialVersionUID =4162743695966976536L; 43 44 public DataModel() 45 { 46 super(); 47 data = new Vector (1); 48 } 49 50 public Vector getData() 51 { 52 return data; 53 } 54 55 public int getColumnCount() 56 { 57 return ColumnItem.getProperties().size(); 58 } 59 60 public int getRowCount() 61 { 62 return data.size(); 63 } 64 65 public Object getValue(String pname, int row) 66 { 67 ColumnItem xcol = (ColumnItem)data.elementAt(row); 68 return xcol.getProperty(pname); 69 } 70 71 public Object getValueAt(int row, int col) 72 { 73 return getValue((String )ColumnItem.getColumnNames().elementAt(col), row); 74 } 75 76 public void setValue(Object val, String pname, int row) 77 { 78 if( row < getRowCount() ) { 79 int srow = row, erow = row; 80 ColumnItem xcol = (ColumnItem)data.elementAt(row); 81 xcol.setProperty(pname, val); 82 if (pname.equals(ColumnItem.PRIMARY_KEY) && val.equals(Boolean.TRUE)) { 83 84 if (xcol.allowsNull()) xcol.setProperty(ColumnItem.NULLABLE, Boolean.FALSE); 85 if (!xcol.isIndexed()) xcol.setProperty(ColumnItem.INDEX, Boolean.TRUE); 86 if (!xcol.isUnique()) xcol.setProperty(ColumnItem.UNIQUE, Boolean.TRUE); 87 94 primaryKeys.add(xcol); 95 } 96 97 if (pname.equals(ColumnItem.PRIMARY_KEY) && val.equals(Boolean.FALSE)) { 98 primaryKeys.remove((ColumnItem)data.elementAt(row)); 99 } 100 101 if (pname.equals(ColumnItem.NULLABLE)) { 102 if (val.equals(Boolean.TRUE)) { 103 xcol.setProperty(ColumnItem.PRIMARY_KEY, Boolean.FALSE); 106 primaryKeys.remove((ColumnItem)data.elementAt(row)); 107 } 108 } 109 110 if (pname.equals(ColumnItem.INDEX)) { 111 if (val.equals(Boolean.FALSE)) { 112 if (xcol.isUnique()) xcol.setProperty(ColumnItem.UNIQUE, Boolean.FALSE); 113 if (xcol.isPrimaryKey()) { 114 xcol.setProperty(ColumnItem.PRIMARY_KEY, Boolean.FALSE); 115 primaryKeys.remove((ColumnItem)data.elementAt(row)); 116 } 117 } } 121 122 if (pname.equals(ColumnItem.UNIQUE)) { 123 if (val.equals(Boolean.TRUE)) { 124 if (!xcol.isIndexed()) xcol.setProperty(ColumnItem.INDEX, Boolean.TRUE); 125 } else { 126 xcol.setProperty(ColumnItem.PRIMARY_KEY, Boolean.FALSE); 127 primaryKeys.remove((ColumnItem)data.elementAt(row)); 128 xcol.setProperty(ColumnItem.INDEX, Boolean.FALSE); 129 } 130 } 131 132 fireTableRowsUpdated(srow, erow); 133 } 134 } 135 136 public void setValueAt(Object val, int row, int col) { 137 if (row == -1 || col == -1) 139 return; 140 141 if (row < getRowCount() && col < getColumnCount()) { 142 String pname = (String ) ColumnItem.getColumnNames().elementAt(col); 143 setValue(val, pname, row); 144 } 145 } 146 147 public String getColumnName(int col) { 148 return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("CreateTable_" + col); } 150 151 public Class getColumnClass(int c) 152 { 153 return getValueAt(0,c).getClass(); 154 } 155 156 public boolean isCellEditable(int row, int col) 157 { 158 return true; 159 } 160 161 public boolean isTablePrimaryKey() 162 { 163 return primaryKeys.size()>1; 164 } 165 166 public Vector getTablePrimaryKeys() 167 { 168 return primaryKeys; 169 } 170 171 public Vector getTableUniqueKeys() 172 { 173 return uniqueKeys; 174 } 175 176 public boolean isTableUniqueKey() 177 { 178 return uniqueKeys.size()>1; 179 } 180 181 186 public void addRow(Object object) 187 { 188 data.addElement(object); 189 fireTableChanged(new TableModelEvent (this, getRowCount()-1, getRowCount()-1, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT)); 190 } 191 192 199 public void insertRow(int row, Object object) 200 { 201 data.insertElementAt(object, row); 202 fireTableRowsInserted(row, row); 203 } 204 205 211 public void removeRow(int row) 212 { 213 if (row < data.size()) { 214 data.removeElementAt(row); 215 fireTableRowsDeleted(row, row); 216 } 217 } 218 } 219 | Popular Tags |