1 14 package org.wings; 15 16 import org.wings.session.SessionManager; 17 import org.wings.table.STableCellEditor; 18 19 import javax.swing.event.CellEditorListener ; 20 import javax.swing.event.ChangeEvent ; 21 import javax.swing.event.EventListenerList ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.util.EventObject ; 25 26 33 public class SDefaultCellEditor 34 implements STableCellEditor { 35 38 private static final SIcon OK_BUTTON_ICON = (SIcon) SessionManager.getSession() 39 .getCGManager().getObject("SDefaultCellEditor.okIcon", SIcon.class); 40 41 44 private static final SIcon CANCEL_BUTTON_ICON = (SIcon) SessionManager.getSession() 45 .getCGManager().getObject("SDefaultCellEditor.cancelIcon", SIcon.class); 46 47 51 protected final SLabel messageLabel; 52 53 56 protected final SPanel editorPanel; 57 58 62 protected final SButton ok; 63 64 67 protected final SButton cancel; 68 69 72 protected final EventListenerList listenerList; 73 74 79 private final ActionListener fireEventListener = new ActionListener () { 80 public void actionPerformed(ActionEvent e) { 81 if (e.getSource() == ok) { 82 stopCellEditing(); 83 } else if (e.getSource() == cancel) { 84 cancelCellEditing(); 85 } 86 } 87 }; 88 89 94 private boolean fastEditSupport = true; 95 96 protected EditorDelegate delegate; 97 98 protected SComponent editorComponent; 99 100 110 protected SDefaultCellEditor(SComponent editorComponent, 111 boolean initializeButtons) { 112 this.messageLabel = new SLabel(); 113 this.editorPanel = new SPanel(new SFlowLayout()); 114 this.ok = new SButton(); 115 this.cancel = new SButton(); 116 this.listenerList = new EventListenerList (); 117 this.editorComponent = editorComponent; 118 119 editorPanel.add(messageLabel); 120 editorPanel.add(editorComponent); 121 if (initializeButtons) { 122 initButtons(); 123 } 124 } 125 126 131 public SDefaultCellEditor(STextField x) { 132 this(x, true); 133 this.delegate = new EditorDelegate() { 134 public void setValue(Object v) { 135 super.setValue(v); 136 ((STextField) editorComponent).setText(v != null ? v.toString() : null); 137 } 138 139 public Object getCellEditorValue() { 140 String text = ((STextField) editorComponent).getText(); 141 return "".equals(text) ? null : text; 142 } 143 144 public boolean stopCellEditing() { 145 return true; 146 } 147 148 public boolean shouldSelectCell(EventObject anEvent) { 149 return true; 150 } 151 }; 152 } 153 154 159 public SDefaultCellEditor(SCheckBox x) { 160 this(x, true); 161 this.delegate = new EditorDelegate() { 162 public void setValue(Object v) { 163 boolean bool; 165 if (v instanceof Boolean ) { 166 bool = ((Boolean ) v).booleanValue(); 167 } else if (v instanceof String ) { 168 Boolean b = Boolean.valueOf((String ) v); 169 bool = b.booleanValue(); 170 } else { 171 bool = false; 172 } 173 174 if (fastEditSupport) { 175 ((SCheckBox) editorComponent).setSelected(!bool); 176 SDefaultCellEditor.this.stopCellEditing(); 177 } else { 178 ((SCheckBox) editorComponent).setSelected(bool); 179 } 180 } 181 182 public Object getCellEditorValue() { 183 return Boolean.valueOf(((SCheckBox) editorComponent).isSelected()); 184 } 185 186 public boolean stopCellEditing() { 187 return true; 188 } 189 190 public boolean shouldSelectCell(EventObject anEvent) { 191 return false; 192 } 193 }; 194 } 195 196 199 protected void initButtons() { 200 ok.addActionListener(fireEventListener); 201 ok.setIcon(OK_BUTTON_ICON); 202 ok.setToolTipText("ok"); 203 204 cancel.addActionListener(fireEventListener); 205 cancel.setIcon(CANCEL_BUTTON_ICON); 206 cancel.setToolTipText("cancel"); 207 208 editorPanel.add(ok); 209 editorPanel.add(cancel); 210 } 211 212 217 public final SComponent getComponent() { 218 return editorComponent; 219 } 220 221 public final SButton getOKButton() { 222 return ok; 223 } 224 225 public final SButton getCancelButton() { 226 return cancel; 227 } 228 229 236 public final void setFastEdit(boolean b) { 237 fastEditSupport = b; 238 } 239 240 246 public final boolean getFastEdit() { 247 return fastEditSupport; 248 } 249 250 public Object getCellEditorValue() { 251 return delegate.getCellEditorValue(); 252 } 253 254 public boolean isCellEditable(EventObject anEvent) { 255 return delegate.isCellEditable(anEvent); 256 } 257 258 public boolean shouldSelectCell(EventObject anEvent) { 259 return delegate.shouldSelectCell(anEvent); 260 } 261 262 public boolean stopCellEditing() { 263 if (delegate.stopCellEditing()) { 264 fireEditingStopped(); 265 return true; 266 } 267 268 return false; 269 } 270 271 public void cancelCellEditing() { 272 delegate.cancelCellEditing(); 273 fireEditingCanceled(); 274 } 275 276 public void addCellEditorListener(CellEditorListener l) { 277 listenerList.add(CellEditorListener .class, l); 278 } 279 280 public void removeCellEditorListener(CellEditorListener l) { 281 listenerList.remove(CellEditorListener .class, l); 282 } 283 284 private ChangeEvent changeEvent = null; 285 286 293 protected void fireEditingStopped() { 294 Object [] listeners = listenerList.getListenerList(); 295 for (int i = listeners.length - 2; i >= 0; i -= 2) { 296 if (listeners[i] == CellEditorListener .class) { 297 if (changeEvent == null) 298 changeEvent = new ChangeEvent (this); 299 ((CellEditorListener ) listeners[i + 1]).editingStopped(changeEvent); 300 } 301 } 302 } 303 304 305 312 protected void fireEditingCanceled() { 313 Object [] listeners = listenerList.getListenerList(); 314 for (int i = listeners.length - 2; i >= 0; i -= 2) { 315 if (listeners[i] == CellEditorListener .class) { 316 if (changeEvent == null) 317 changeEvent = new ChangeEvent (this); 318 ((CellEditorListener ) listeners[i + 1]).editingCanceled(changeEvent); 319 } 320 } 321 } 322 323 public SComponent getTreeCellEditorComponent(STree tree, Object value, 324 boolean isSelected, 325 boolean expanded, 326 boolean leaf, int row) { 327 328 delegate.setValue(value); 329 return editorPanel; 330 } 331 332 public SComponent getTableCellEditorComponent(STable table, Object value, 333 boolean isSelected, 334 int row, int column) { 335 delegate.setValue(value); 336 337 return editorPanel; 338 } 339 340 341 345 349 protected class EditorDelegate { 350 protected Object value; 351 352 359 public Object getCellEditorValue() { 360 return value; 361 } 362 363 370 public void setValue(Object x) { 371 this.value = x; 372 } 373 374 public boolean isCellEditable(EventObject anEvent) { 375 return true; 376 } 377 378 public boolean stopCellEditing() { 379 return true; 380 } 381 382 public void cancelCellEditing() { 383 } 384 385 public boolean shouldSelectCell(EventObject anEvent) { 386 return true; 387 } 388 } 389 } 390 | Popular Tags |