1 26 package org.objectweb.util.explorer.swing.lib; 27 28 import java.awt.Component ; 29 import java.awt.Point ; 30 import java.awt.event.MouseAdapter ; 31 import java.awt.event.MouseEvent ; 32 import java.net.URL ; 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.explorer.api.Entry; 42 import org.objectweb.util.explorer.api.IconProvider; 43 import org.objectweb.util.explorer.api.Tree; 44 import org.objectweb.util.explorer.core.common.api.Description; 45 import org.objectweb.util.explorer.core.common.api.ExplorerConstants; 46 import org.objectweb.util.explorer.core.menu.lib.DefaultMenuItemTreeView; 47 import org.objectweb.util.explorer.core.naming.lib.DefaultEntry; 48 import org.objectweb.util.explorer.interpreter.api.DescriptionInterpreter; 49 import org.objectweb.util.explorer.resolver.api.PropertyResolver; 50 import org.objectweb.util.explorer.swing.icon.IconFileProvider; 51 import org.objectweb.util.explorer.swing.icon.ObjectIconProvider; 52 53 61 public class DynamicTable 62 extends JTable 63 { 64 65 71 protected TableCellRenderer tableCellRenderer_; 72 protected PropertyResolver propertyResolver_; 73 protected DescriptionInterpreter descriptionInterpreter_; 74 protected Tree tree_; 75 protected Icon objectIcon_; 76 77 83 public DynamicTable(){ 84 super(); 85 tableCellRenderer_ = new MyTableCellRenderer(); 86 addMouseListener(new MyMouseAdapter()); 87 setColumnSelectionAllowed(true); 88 setRowSelectionAllowed(true); 89 objectIcon_ = (Icon )(new ObjectIconProvider()).newIcon(null); 90 } 91 92 93 99 protected PropertyResolver getPropertyResolver(){ 100 return propertyResolver_; 101 } 102 103 protected DescriptionInterpreter getDescriptionInterpreter(){ 104 return descriptionInterpreter_; 105 } 106 107 protected Tree getTree(){ 108 return tree_; 109 } 110 111 protected Icon getImageIcon(Entry entry){ 112 if(getPropertyResolver()!=null){ 113 Description desc = getPropertyResolver().resolve(ExplorerConstants.ICON_PROPERTY, entry); 114 IconProvider iconProvider = (IconProvider) getDescriptionInterpreter().interprete(desc, null); 115 if(iconProvider!=null){ 116 Object object = iconProvider.newIcon(entry.getValue()); 117 if(object instanceof Icon ){ 118 return (Icon )object; 119 } else if(object instanceof String ) { 120 return (Icon )(new IconFileProvider((String )object)).newIcon(entry.getValue()); 121 } else if (object instanceof URL ) { 122 return (Icon )(new IconFileProvider((URL )object)).newIcon(entry.getValue()); 123 } 124 } 125 } 126 return null; 127 } 128 129 135 public TableCellRenderer getCellRenderer(int row, int col) { 136 return tableCellRenderer_; 137 } 138 139 public void setPropertyResolver(PropertyResolver propertyResolver){ 140 propertyResolver_ = propertyResolver; 142 } 143 144 public void setDescriptionInterpreter(DescriptionInterpreter descriptionInterpreter){ 145 descriptionInterpreter_ = descriptionInterpreter; 147 } 148 149 public void setTree(Tree tree){ 150 tree_ = tree; 152 } 153 154 public void setData(String [] headers, Object [][] values){ 155 setModel(new TableModel (headers, values)); 156 } 157 158 164 protected class TableModel 165 extends AbstractTableModel { 166 167 protected String [] headers_; 168 protected Object [][] values_; 169 170 public TableModel(String [] headers, Object [][] values){ 171 headers_ = headers; 172 values_ = new Object [values.length][0]; 173 if(values_!=null){ 174 for(int i = 0 ; i < values.length ; i++){ 175 values_[i] = new Object [values[i].length]; 176 for(int j = 0 ; j < values[i].length ; j++){ 177 Object value = values[i][j]; 178 if(value!=null){ 179 if(value instanceof Entry){ 180 values_[i][j] = (Entry)value; 181 } else { 182 Entry newEntry = new DefaultEntry(value.toString(), value); 184 values_[i][j] = newEntry; 185 } 186 } 187 } 188 } 189 } 190 } 191 192 196 public String getColumnName(int column) { 197 if(headers_!=null) 198 return headers_[column]; 199 return super.getColumnName(column); 200 } 201 202 206 public int getColumnCount() { 207 if(headers_!=null) 208 return headers_.length; 209 return 0; 210 } 211 212 216 public int getRowCount() { 217 if(values_ != null){ 218 return values_.length; 219 } 220 return 0; 221 } 222 223 227 public Object getValueAt(int rowIndex, int columnIndex) { 228 if(values_ != null) 229 return values_[rowIndex][columnIndex]; 230 return null; 231 } 232 233 234 } 235 236 240 protected final class MyMouseAdapter extends MouseAdapter { 241 public void mousePressed(MouseEvent e) { 242 popupLayout(e); 243 } 244 public void mouseReleased(MouseEvent e) { 245 popupLayout(e); 246 } 247 private void popupLayout(MouseEvent e) { 248 if (e.isPopupTrigger()) { 249 Point point = e.getPoint(); 250 int row = rowAtPoint(point); 251 int col = columnAtPoint(point); 252 if((row != -1) && (col != -1)) { 253 Entry object = (Entry)getValueAt(row,col); 254 if(getPropertyResolver()!=null && getDescriptionInterpreter()!=null){ 255 Description desc = getPropertyResolver().resolve(ExplorerConstants.MENU_PROPERTY, object, null); 256 JPopupMenu menu = (JPopupMenu ) getDescriptionInterpreter().interprete(desc, new DefaultMenuItemTreeView(getTree(),object,null)); 257 if (menu != null) { 258 changeSelection(row,col,false,false); 259 menu.show(DynamicTable.this,(int) (point.getX()),(int) (point.getY())); 260 } 261 } 262 } 263 } 264 } 265 } 266 267 272 protected final class MyTableCellRenderer extends DefaultTableCellRenderer { 273 274 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { 275 super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 276 if(value != null) { 277 Entry entry = (Entry)value; 278 setText(entry.getName().toString()); 279 Icon icon = getImageIcon(entry); 280 setIcon(icon==null?objectIcon_:icon); 281 } else { 282 setIcon(null); 283 } 284 return this; 285 } 286 287 } 288 289 } 290 291 292 | Popular Tags |