1 26 27 package org.objectweb.util.browser.core.common; 28 29 import java.awt.Component ; 30 import java.awt.Point ; 31 import java.awt.event.MouseAdapter ; 32 import java.awt.event.MouseEvent ; 33 34 import javax.swing.Icon ; 35 import javax.swing.JPopupMenu ; 36 import javax.swing.JTable ; 37 import javax.swing.table.AbstractTableModel ; 38 import javax.swing.table.DefaultTableCellRenderer ; 39 import javax.swing.table.TableCellRenderer ; 40 41 import org.objectweb.util.browser.api.Entry; 42 import org.objectweb.util.browser.core.api.Decoder; 43 import org.objectweb.util.browser.core.api.TableConfiguration; 44 45 import org.objectweb.util.browser.core.naming.DefaultEntry; 46 import org.objectweb.util.browser.core.naming.DefaultName; 47 48 56 public class DynamicTable 57 extends JTable 58 implements TableConfiguration { 59 60 66 protected AdminCustomization custom_; 67 68 protected TableCellRenderer tableCellRenderer_; 69 70 protected Decoder decoder_; 71 72 78 public DynamicTable(Decoder currentDecoder){ 79 super(); 80 tableCellRenderer_ = new MyTableCellRenderer(); 81 decoder_ = currentDecoder; 82 addMouseListener(new MyMouseAdapter()); 83 setColumnSelectionAllowed(true); 84 setRowSelectionAllowed(true); 85 } 86 87 public DynamicTable(){ 88 this(null); 89 } 90 91 97 103 public TableCellRenderer getCellRenderer(int row, int col) { 104 return tableCellRenderer_; 105 } 106 107 113 public void setAdminCustomization(AdminCustomization custom) { 114 custom_ = custom; 115 } 116 117 public void setData(String [] headers, Object [][] values){ 118 setModel(new TableModel (headers, values)); 119 } 120 121 127 protected class TableModel 128 extends AbstractTableModel { 129 130 protected String [] headers_; 131 protected Object [][] values_; 132 133 public TableModel(String [] headers, Object [][] values){ 134 headers_ = headers; 135 values_ = values; 136 if(values_!=null && decoder_!=null){ 137 for(int i = 0 ; i < values_.length ; i++){ 138 for(int j = 0 ; j < values_[i].length ; j++){ 139 Object value = getValueAt(i,j); 140 if(value!=null){ 141 if(Entry.class.isAssignableFrom(value.getClass())){ 142 Entry currentEntry = (Entry)value; 143 Entry newEntry = new DefaultEntry(decoder_.decode(currentEntry.getValue()),new DefaultName(currentEntry.getName().toString()),null); 144 values_[i][j] = newEntry; 145 } else { 146 values_[i][j] = decoder_.decode(value); 147 } 148 } 149 } 150 } 151 } 152 } 153 154 158 public String getColumnName(int column) { 159 if(headers_!=null) 160 return headers_[column]; 161 return super.getColumnName(column); 162 } 163 164 168 public int getColumnCount() { 169 if(headers_!=null) 170 return headers_.length; 171 return 0; 172 } 173 174 178 public int getRowCount() { 179 if(values_ != null){ 180 return values_.length; 181 } 182 return 0; 183 } 184 185 189 public Object getValueAt(int rowIndex, int columnIndex) { 190 if(values_ != null) 191 return values_[rowIndex][columnIndex]; 192 return null; 193 } 194 195 196 } 197 198 202 protected final class MyMouseAdapter extends MouseAdapter { 203 public void mousePressed(MouseEvent e) { 204 popupLayout(e); 205 } 206 public void mouseReleased(MouseEvent e) { 207 popupLayout(e); 208 } 209 private void popupLayout(MouseEvent e) { 210 if (e.isPopupTrigger()) { 211 Point point = e.getPoint(); 212 int row = rowAtPoint(point); 213 int col = columnAtPoint(point); 214 if((row != -1) && (col != -1)) { 215 Object object = getValueAt(row,col); 216 if (custom_ != null) { 217 JPopupMenu menu = custom_.getMenu(object,AdminCustomization.TABLE_TARGET); 218 if (menu != null) { 219 changeSelection(row,col,false,false); 220 menu.show(DynamicTable.this,(int) (point.getX()),(int) (point.getY())); 221 } 222 } 223 } 224 } 225 } 226 } 227 228 233 protected final class MyTableCellRenderer extends DefaultTableCellRenderer { 234 235 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { 236 super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 237 if(value != null) { 238 if(Entry.class.isAssignableFrom(value.getClass())) 239 setText(((Entry)value).getName().toString()); 240 else 241 setText(value.toString()); 242 243 if (custom_ != null) { 244 Icon icon = custom_.getIcon(value); 245 if (icon != null) 246 setIcon(icon); 247 else 248 setIcon(null); 249 } 250 } else { 251 setIcon(null); 252 } 253 return this; 254 255 } 256 257 } 258 259 260 } | Popular Tags |