1 30 package com.genimen.djeneric.tools.administrator.editors; 31 32 import java.awt.Component ; 33 import java.util.ArrayList ; 34 import java.util.Vector ; 35 36 import javax.swing.DefaultComboBoxModel ; 37 import javax.swing.JComboBox ; 38 import javax.swing.JTable ; 39 import javax.swing.table.TableCellEditor ; 40 41 import com.genimen.djeneric.language.Messages; 42 import com.genimen.djeneric.repository.DjModelView; 43 import com.genimen.djeneric.repository.DjPersistenceManager; 44 import com.genimen.djeneric.repository.DjUser; 45 import com.genimen.djeneric.tools.modeler.ModelEditor; 46 import com.genimen.djeneric.ui.DjCellEditor; 47 import com.genimen.djeneric.ui.DjModelColumn; 48 import com.genimen.djeneric.ui.DjTableModel; 49 import com.genimen.djeneric.util.DjLogger; 50 51 public class ModelViewTableModel extends DjTableModel 52 { 53 private static final long serialVersionUID = 1L; 54 private final int _colCount = 1; 55 56 private DjPersistenceManager _mgr; 57 58 ArrayList _allViews; 59 60 private final int IDX_VIEW = 0; 61 62 public ModelViewTableModel(DjPersistenceManager mgr, ArrayList allViews, DjUser user) 63 { 64 _mgr = mgr; 65 _allViews = allViews; 66 _columns = new DjModelColumn[_colCount]; 67 68 _columns[IDX_VIEW] = new DjModelColumn(Messages.getString("global.View"), 200, Messages 69 .getString("ModelViewTableModel.ViewsUserHasAccess2")); 70 } 71 72 public TableCellEditor getColumnEditor(int column) 73 { 74 if (column == IDX_VIEW) 75 { 76 return new DjCellEditor(new JComboBox ()) 77 { 78 private static final long serialVersionUID = 1L; 79 80 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) 81 { 82 JComboBox cbb = (JComboBox ) editorComponent; 83 try 84 { 85 DjModelView[] views = _mgr.getViews(); 86 Vector v = new Vector (); 87 DjModelView c = (DjModelView) _allViews.get(row); 88 if (c != null) v.add(c); 89 90 for (int i = 0; i < views.length; i++) 91 { 92 boolean canAdd = true; 93 for (int j = 0; j < _allViews.size(); j++) 94 { 95 c = (DjModelView) _allViews.get(j); 96 if (c != null && c.equals(views[i])) canAdd = false; 97 } 98 if (canAdd) v.add(views[i]); 99 } 100 cbb.setModel(new DefaultComboBoxModel (v)); 101 } 102 catch (Exception x) 103 { 104 DjLogger.log(x); 105 } 106 delegate.setValue(value); 107 return editorComponent; 108 } 109 }; 110 } 111 return super.getColumnEditor(column); 112 } 113 114 public int getRowCount() 115 { 116 if (_allViews == null) return 0; 117 118 return _allViews.size(); 119 } 120 121 public int getColumnCount() 122 { 123 return _colCount; 124 } 125 126 public Object getValueAt(int nRow, int nCol) 127 { 128 if (nRow < 0 || nRow >= getRowCount()) return ""; 129 130 DjModelView c = (DjModelView) _allViews.get(nRow); 131 if (nCol == IDX_VIEW) return c; 132 else return Messages.getString("global.InvalidColumn", String.valueOf(nCol)); 133 } 134 135 public void setValueAt(Object aValue, int rowIndex, int columnIndex) 136 { 137 if (rowIndex < 0 || rowIndex >= getRowCount()) return; 138 if (aValue == null) return; 139 notifyModified(); 140 141 try 142 { 143 if (columnIndex == IDX_VIEW) _allViews.set(rowIndex, aValue); 144 setStatusMessage(ModelEditor.OK_MSG); 145 } 146 catch (Exception x) 147 { 148 setStatusMessage(x); 149 } 150 151 } 152 153 int counter = 1; 154 155 public int insertRow(int atIdx) 156 { 157 try 158 { 159 _allViews.add(atIdx, null); 160 notifyModified(); 161 setStatusMessage(ModelEditor.OK_MSG); 162 return atIdx; 163 } 164 catch (Exception x) 165 { 166 setStatusMessage(x.getMessage(), false); 167 } 168 return -1; 169 } 170 171 public boolean deleteRow(int atIdx) 172 { 173 try 174 { 175 _allViews.remove(atIdx); 176 notifyModified(); 177 setStatusMessage(ModelEditor.OK_MSG); 178 return true; 179 } 180 catch (Exception x) 181 { 182 setStatusMessage(x.getMessage(), false); 183 } 184 return false; 185 } 186 187 } | Popular Tags |