18 package org.turro.zkoss.grid;
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.List;
24 import org.turro.string.Strings;
25 import org.turro.elephant.impl.util.StringParser;
26 import org.turro.elephant.util.BooleanFormats;
27 import org.turro.elephant.util.DateFormats;
28 import org.turro.elephant.util.DecimalFormats;
29 import org.turro.elephant.util.Messages;
30 import org.turro.i18n.I_;
31 import org.turro.zkoss.input.*;
32 import org.zkoss.zk.ui.Component;
33 import org.zkoss.zk.ui.HtmlBasedComponent;
34 import org.zkoss.zk.ui.event.Event;
35 import org.zkoss.zk.ui.event.EventListener;
36 import org.zkoss.zk.ui.event.Events;
37 import org.zkoss.zk.ui.ext.AfterCompose;
38 import org.zkoss.zul.*;
39 import org.zkoss.zul.impl.InputElement;
40 import org.zkoss.zul.impl.XulElement;
54 selectInput =
true, readOnly =
false,
61 private boolean onlyRenderer =
false;
64 setSclass(
"editableGrid");
68 return allowDeletions;
72 this.allowDeletions = allowDeletions;
96 this.readOnly = readOnly;
104 this.selectInput = selectInput;
112 this.showZeros = showZeros;
123 getFoot().getChildren().clear();
129 getRows().getChildren().clear();
135 Iterator it =
getRows().getChildren().iterator();
136 while(it.hasNext()) {
137 Row r = (Row) it.next();
138 if(
isValid((V) r.getValue())) {
152 List<V> l =
new ArrayList<V>();
153 for(Component row :
getRows().getChildren()) {
154 if(
isValid((V) ((Row)row).getValue())) {
155 l.add((V) ((Row)row).getValue());
162 List<V> l =
new ArrayList<V>();
163 for(Component row :
getRows().getChildren()) {
164 l.add((V) ((Row)row).getValue());
215 if(editableCell ==
null || !
lastRow.equals(editableCell.
getRow())) {
216 Events.postEvent(
new Event(Events.ON_CHANGE,
this, editableCell));
223 if(editableCell !=
null) {
239 hbc.setSclass(
"grid-editor");
244 hbc.setWidgetListener(
"onKeyDown",
"if(event.keyCode == 9 && !(event.ctrlKey || event.shiftKey)) event.keyCode = 13;");
245 ((XulElement) hbc).setCtrlKeys(
247 (hbc instanceof Listbox ?
"" :
"#down#up"));
249 if(hbc instanceof Datebox) {
250 ((Datebox) hbc).setButtonVisible(
false);
251 ((InputElement) hbc).setReadonly(
true);
252 }
else if(hbc instanceof InputElement) {
253 ((InputElement) hbc).setReadonly(
true);
254 }
else if(hbc instanceof Listbox) {
255 ((Listbox) hbc).setDisabled(
true);
256 }
else if(hbc instanceof Checkbox) {
257 ((Checkbox) hbc).setDisabled(
true);
264 Row row = (Row)
getRows().getLastChild();
265 if(row !=
null && !
isValid((V) row.getValue())) {
273 row.setSclass(
"invalid");
274 if(
getRows().appendChild(row)) {
277 initiateRowListeners(row);
288 Events.postEvent(
new Event(Events.ON_CHANGE,
EditableGrid.this));
295 Row newRow =
new Row();
296 newRow.setSclass(
"invalid");
297 getRows().insertBefore(newRow, row);
299 initiateRowListeners(newRow);
305 if(cols !=
null && cellIndex < cols.getChildren().size()) {
306 return (Column) cols.getChildren().get(cellIndex);
313 processRowSclass(row);
315 for(Component c : (List<Component>) row.getChildren()) {
316 if(!(c instanceof Detail)) {
322 onlyRenderer =
false;
327 for(Component row :
getRows().getChildren()) {
333 String format,
int scale,
boolean onlyDate,
boolean readOnly) {
343 ec.setAlign(
"right");
344 if(Strings.isBlank(format)) {
353 String format,
int scale,
boolean onlyDate,
boolean readOnly)
throws ClassNotFoundException {
363 ec.setAlign(
"right");
364 if(Strings.isBlank(format)) {
373 String format,
int scale,
boolean onlyDate,
boolean readOnly) {
376 ec.setTooltiptext(tooltip);
384 ec.setAlign(
"right");
385 if(Strings.isBlank(format)) {
394 String format,
int scale,
boolean onlyDate,
boolean readOnly)
throws ClassNotFoundException {
397 ec.setTooltiptext(tooltip);
405 ec.setAlign(
"right");
406 if(Strings.isBlank(format)) {
416 if(editableCell.
getRow().getValue() !=
null) {
419 HtmlBasedComponent hbc = (HtmlBasedComponent) editableCell.
getRenderer();
420 if(hbc instanceof Checkbox) {
421 return ((Checkbox) hbc).isChecked();
422 }
else if(hbc instanceof Label) {
423 return ((Label) hbc).getValue();
431 HtmlBasedComponent hbc = (HtmlBasedComponent) editableCell.
getEditor();
432 if(hbc instanceof Checkbox) {
433 return ((Checkbox) hbc).isChecked();
436 }
else if(hbc instanceof Decimalbox) {
440 }
else if(hbc instanceof Longbox) {
442 }
else if(hbc instanceof Datebox) {
443 return ((Datebox) hbc).getValue();
444 }
else if(hbc instanceof Timebox) {
445 return ((Timebox) hbc).getValue();
446 }
else if(hbc instanceof
KnowsID) {
447 return ((
KnowsID) hbc).getObjectId();
454 }
else if(hbc instanceof Listbox) {
455 return ((Listbox) hbc).getSelectedItem().getValue();
456 }
else if(hbc instanceof Textbox) {
457 return ((Textbox) hbc).getValue();
466 if(editableCell.
getProperty() !=
null && editableCell.
getRow().getValue() !=
null) {
471 HtmlBasedComponent hbc = (HtmlBasedComponent) editableCell.
getRenderer();
472 if(hbc instanceof Checkbox) {
473 ((Checkbox) hbc).setChecked((Boolean) value);
474 }
else if(hbc instanceof Label) {
475 ((Label) hbc).setValue((String)
formatCell(editableCell, value));
484 if(value instanceof Boolean) {
486 }
else if(value instanceof Number) {
487 if(((Number) value).doubleValue() == 0 && showZeros ==
false) {
490 return editableCell.
getFormat() !=
null ?
493 }
else if(value instanceof Date) {
499 }
else if(value instanceof Enum) {
500 return I_.
byKey(((Enum) value).toString());
501 }
else if(editableCell.
getScale() > 0) {
504 return (String) value;
512 Checkbox cb =
new Checkbox();
516 cb.setChecked(((Checkbox)editableCell.
getRenderer()).isChecked());
518 cb.setChecked(value !=
null ? (Boolean) value :
false);
523 }
else if(editableCell.
isNumber()) {
536 }
else if(editableCell.
isDate()) {
538 Timebox tb =
new Timebox((Date) value);
541 tb.setButtonVisible(
false);
550 }
else if(editableCell.
getScale() > 0) {
552 mb.setReadonly(editableCell.
isReadOnly() || mb.isReadonly());
555 Textbox tb =
new Textbox((String) value);
558 tb.setMaxlength(255);
566 if(value instanceof Boolean) {
567 Checkbox cb =
new Checkbox();
568 cb.setChecked((Boolean) value);
571 Label l =
new Label(
formatCell(editableCell, value));
580 Events.postEvent(
new Event(Events.ON_CHANGING,
this, editableCell));
612 private void initiateListeners() {
615 for(Component row :
getRows().getChildren()) {
616 initiateRowListeners(((Row)row));
620 private void initiateRowListeners(Row r) {
622 for(Component c :
getColumns(
true).getChildren()) {
623 if(c instanceof EditableColumn) {
628 for(Component c : (List<Component>) r.getChildren()) {
631 }
else if(c instanceof Detail) {
633 }
else if(c instanceof Checkbox) {
641 private void processRowSclass(Row row) {
642 if(row.getValue() !=
null &&
isValid((V) row.getValue())) {
645 row.setSclass(
"invalid");
649 private void makeItEditable(HtmlBasedComponent hbc) {
650 ((Label) hbc).setStyle(
"display:block;");
651 if (Strings.isBlank(((Label) hbc).getValue())) {
652 ((Label) hbc).setValue(
"*****");
653 ((Label) hbc).setStyle(
"color:#eee;display:block;");
static void setValueFrom(String pathToValue, Object object, Class javaClass, Object value)
static Object getValueFrom(String pathToValue, Object object)
static Object convertToClass(Class javaClass, Object value)
static Messages confirmDeletion()
static String byKey(String key)
void setReadOnly(boolean readOnly)
void setJavaClass(Class javaClass)
void setOnlyDate(boolean onlyDate)
void setFormat(String format)
void setProperty(String property)
void setCellValue(EditableCell editableCell, Object value)
EventListener getNextListener()
boolean canDeleteRow(Row row)
void processEdition(EditableCell editableCell)
void setAllowDeletions(boolean allowDeletions)
EditableColumn addColumn(String image, String tooltip, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
boolean canEditRow(Row row)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
EditableColumn addColumn(String image, String tooltip, String javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
NextEventListener nextListener
EventListener getKeyListener()
void setAllowInsertions(boolean allowInsertions)
abstract boolean deleteRow(Row row)
Object getEditorValue(EditableCell editableCell)
KeyEventListener keyListener
boolean isAllowInsertions()
abstract void rowChanged(Row row)
void cellChanged(EditableCell editableCell, Object value)
abstract void initiateRow(Row row, V value)
void setShowZeros(boolean showZeros)
BlurEventListener blurListener
void deleteSelectedRow(final Row row)
boolean isAllowDeletions()
Object getCellValue(EditableCell editableCell)
EditableCell getCurrentCell()
CancelEventListener cancelListener
EditableColumn addColumn(String label, String javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
EditEventListener editListener
boolean isCellValid(EditableCell editableCell, Object value)
EventListener getEditListener()
EventListener getCancelListener()
void setReadOnly(boolean readOnly)
void insertInSelectedRow(Row row)
HtmlBasedComponent createRenderer(EditableCell editableCell)
EventListener getBlurListener()
String formatCell(EditableCell editableCell, Object value)
void setSelectInput(boolean selectInput)
HtmlBasedComponent getEditor(EditableCell editableCell)
HtmlBasedComponent createEditor(EditableCell editableCell)
abstract boolean isValid(V v)
Column getEditableColumn(int cellIndex)
Foot getFoot(boolean create)
Columns getColumns(boolean create)
Rows getRows(boolean create)
void setRowCount(int rows)