Commits

Anonymous committed e5da965

ValidateEdit is now set in the SpreadsheetCellType since the same editor can be used for different type which implement their own policy regarding validation.

Comments (0)

Files changed (7)

src/main/java/impl/org/controlsfx/spreadsheet/CellView.java

      * @param bc The SpreadsheetCell
      * @return
      */
-    @SuppressWarnings("unchecked")
     private GridCellEditor getEditor(final SpreadsheetCell cell, final SpreadsheetView spv) {
     	SpreadsheetCellType<?> cellType = cell.getCellType();
     	GridCellEditor editor = spv.getCellsViewSkin().getSpreadsheetCellEditorImpl();

src/main/java/impl/org/controlsfx/spreadsheet/GridCellEditor.java

 
 import org.controlsfx.control.spreadsheet.SpreadsheetCell;
 import org.controlsfx.control.spreadsheet.SpreadsheetCellEditor;
+import org.controlsfx.control.spreadsheet.SpreadsheetCellType;
 import org.controlsfx.control.spreadsheet.SpreadsheetView;
 
 public class GridCellEditor {
 	 */
 	public void endEdit(boolean b){
 		if(b){
-			Object value = spreadsheetCellEditor.validateEdit();
+			Object value = modelCell.getCellType().convertValue(spreadsheetCellEditor.getControlValue());
 			if(value != null && viewCell != null){
 				modelCell.setItem(value);
 				viewCell.commitEdit(modelCell);
 		spreadsheetView.disabledProperty().addListener(editorListener);
 
 		//Then we call the user editor in order for it to be ready
-		spreadsheetCellEditor.startEdit();
+		Object value = modelCell.getItem();
+		spreadsheetCellEditor.startEdit(value);
 
 		viewCell.setGraphic(spreadsheetCellEditor.getEditor());
 	}

src/main/java/org/controlsfx/control/spreadsheet/SpreadsheetCell.java

 
 import java.io.Serializable;
 import java.time.LocalDate;
-import java.time.format.DateTimeFormatter;
-import java.util.Arrays;
-import java.util.List;
 
 import javafx.beans.property.BooleanProperty;
 import javafx.beans.property.ObjectProperty;
 import javafx.collections.FXCollections;
 import javafx.collections.ObservableList;
 import javafx.collections.ObservableMap;
-import javafx.util.StringConverter;
-import javafx.util.converter.DefaultStringConverter;
-import javafx.util.converter.DoubleStringConverter;
 
 /**
  * The SpreadsheetCells serve as model for the {@link SpreadsheetView}. <br/>
 	 * @param columnSpan
 	 * @param type
 	 */
-	@SuppressWarnings("unchecked")
 	public SpreadsheetCell(final int row, final int column, final int rowSpan,
 			final int columnSpan, final SpreadsheetCellType<?> type) {
 		this.row = row;
 	 **************************************************************************/
 
 	private void updateText() {
-		this.text = type.getConverter().toString(getItem());
+		this.text = type.toString(getItem());
 	}
 }

src/main/java/org/controlsfx/control/spreadsheet/SpreadsheetCellEditor.java

 import java.time.LocalDate;
 import java.util.List;
 
-import impl.org.controlsfx.spreadsheet.GridViewSkin;
-import javafx.application.Platform;
 import javafx.beans.value.ChangeListener;
 import javafx.beans.value.ObservableValue;
 import javafx.collections.FXCollections;
 import javafx.collections.ObservableList;
 import javafx.event.EventHandler;
 import javafx.scene.control.ComboBox;
+import javafx.scene.control.Control;
 import javafx.scene.control.DatePicker;
 import javafx.scene.control.TextField;
 import javafx.scene.input.KeyCode;
 import javafx.scene.input.KeyEvent;
 
-import org.controlsfx.property.editor.PropertyEditor;
 
 /**
  * 
  * 
  * <br/>
  * <h3>Creating your editor: </h3>
- * You can of course create your own {@link SpreadsheetCellEditor} if you want to control more closely 
- * what is happening or simply for displaying other controls.<br/>
+ * You can of course create your own {@link SpreadsheetCellEditor} for displaying other controls.<br/>
  * 
  * You just have to override the three abstract methods. <b>Remember</b> that you will never call those
  * methods directly. They will be called by the {@link SpreadsheetView} when the time comes.
  * <ul>
  *   <li> {@link #startEdit()}: You can instantiate your own control.</li>
- *   <li> {@link #validateEdit()}: You can decide whether the value entered is valid for you.</li>
+ *   <li> {@link #getEditor()}: You will return which control you're using (for display).</li>
+ *   <li> {@link #getControlValue()}: You will return the value inside your editor for validation.</li>
  *   <li> {@link #end()}: When editing is finished, you can properly close your own control.</li>
  * </ul>
  * <br/>
  * @see SpreadsheetView
  * @see SpreadsheetCell
  */
-public abstract class SpreadsheetCellEditor<T> implements PropertyEditor<T>  {
+public abstract class SpreadsheetCellEditor<T>{
 	SpreadsheetView view;
     /***************************************************************************
      * * Constructor * *
      * * Public Final Methods * *
      **************************************************************************/
     /**
-     * Return the {@link SpreadsheetCell#getItem()} associated with the editor.
-     */
-    @Override
-    public final T getValue() {
-    	SpreadsheetCell cell = (SpreadsheetCell) view.getCellsViewSkin().getSpreadsheetCellEditorImpl().getModelCell();
-        return cell == null ? null : (T) cell.getItem();
-    }
-    
-    @Override
-    public final void setValue(T value) {
-        SpreadsheetCell cell = (SpreadsheetCell) view.getCellsViewSkin().getSpreadsheetCellEditorImpl().getModelCell();
-        if (cell != null) {
-    	   cell.setItem(value);
-        }
-    }
-
-    /**
-     * Return the {@link SpreadsheetCell#getProperties()} associated with
-     * the string in parameter.
-     * @param key The key which has a Object associated with in {@link SpreadsheetCell#getProperties()}
-     * @return
-     */
-    public final Object getProperties(String key){
-    	return view.getCellsViewSkin().getSpreadsheetCellEditorImpl().getModelCell().getProperties().get(key);
-    }
-    
-    /**
      * Whenever you want to stop the edition, you call that method.<br/>
      * True means you're trying to commit the value, then {@link #validateEdit()}
      * will be called in order to verify that the value is correct.<br/>
      * This method will be called when edition start.<br/>
      * You will then do all the configuration of your editor.
      */
-    public abstract void startEdit();
+    public abstract void startEdit(Object item);
     
     /**
-     * This method will be called when a commit is happening.<br/>
-     * You will then compute the value of the editor in order to determine
-     * if the current value is valid.
-     * @return null if not valid or the correct value otherwise.
+     * Return the control used for controling the input.
+     * This is called at the beginning in order to display your control
+     * in the cell.
+     * @return
      */
-    public abstract T validateEdit();
+    public abstract Control getEditor();
+   
+    /**
+     * Return the value your editor as a string.
+     * This will be used by the {@link SpreadsheetCellType#convertValue(String)}
+     * in order to compute whether the value is valid regarding
+     * the {@link SpreadsheetCellType} policy.
+     * @return
+     */
+    public abstract String getControlValue();
 	
     /**
      * This method will be called at the end of edition.<br/>
 			 * * Public Methods * *
 			 **************************************************************************/
 			@Override
-			public void startEdit() {
-				Object value = getValue();
-				if (value != null) {
+			public void startEdit(Object value) {
+				if (value instanceof String) {
 					tf.setText(value.toString());
 				}
 				tf.setMaxHeight(20);
 				tf.setOnKeyPressed(null);
 			}
 
-			@Override
+			/*@Override
 			public String validateEdit() {
 				return tf.getText();
-			}
+			}*/
 
 			@Override
 			public TextField getEditor() {
 					}
 				});
 			}
+
+			@Override
+			public String getControlValue() {
+				// TODO Auto-generated method stub
+				return null;
+			}
 		};
 	}
 	
 			 * * Public Methods * *
 			 **************************************************************************/
 			@Override
-			public void startEdit() {
-				String value = getValue();
-				if (value != null) {
-					tf.setText(value);
+			public void startEdit(Object value) {
+				
+				if (value instanceof String) {
+					tf.setText((String)value);
 				}
 				tf.setMaxHeight(20);
 				attachEnterEscapeEventHandler();
 				tf.setOnKeyPressed(null);
 			}
 
-			@Override
+			/*@Override
 			public String validateEdit() {
 				return tf.getText();
-			}
+			}*/
 
 			@Override
 			public TextField getEditor() {
 					}
 				});
 			}
+
+			@Override
+			public String getControlValue() {
+				return tf.getText();
+			}
 		};
 	}
 
 			 * * Public Methods * *
 			 **************************************************************************/
 			@Override
-			public void startEdit() {
-				if (getValue() != null) {
-					tf.setText(getValue().toString());
+			public void startEdit(Object value) {
+				if(value instanceof Double){
+					tf.setText(value.toString());
 				}
 				tf.getStyleClass().removeAll("error");
 				tf.setMaxHeight(20);
 				tf.setOnKeyPressed(null);
 			}
 
-			@Override
+			/*@Override
 			public Double validateEdit() {
 				try{
 					Double temp = Double.parseDouble(tf.getText());
 				}catch(Exception e){
 					return null;
 				}
-			}
+			}*/
 
 			@Override
 			public TextField getEditor() {
 					}
 				});
 			}
+
+			@Override
+			public String getControlValue() {
+				return tf.getText();
+			}
 		};
 	}
 
 			 **************************************************************************/
 
 			@Override
-			public void startEdit() {
-				//                super.startEdit();
-				if (getValue() != null) {
+			public void startEdit(Object value) {
+				
+				if (value instanceof String) {
 					ObservableList<String> items = FXCollections.observableList(itemList);
 					cb.setItems(items);
-					cb.setValue(getValue());
+					cb.setValue(value.toString());
 				}
 				attachEnterEscapeEventHandler();
 
 				cl = null;
 			}
 
-			@Override
+			/*@Override
 			public String validateEdit() {
 				if (cb.getSelectionModel().getSelectedIndex() != -1) {
 					return cb.getSelectionModel().getSelectedItem();
 				}
 				return null;
-			}
+			}*/
 
 			@Override
 			public ComboBox<String> getEditor() {
 					}
 				});
 			}
+
+
+			@Override
+			public String getControlValue() {
+				return cb.getSelectionModel().getSelectedItem();
+			}
 		};
 	}
 
 			 * * Public Methods * *
 			 **************************************************************************/
 			@Override
-			public void startEdit() {
-				datePicker.setValue(getValue());
+			public void startEdit(Object value) {
+				if(value instanceof LocalDate){
+					datePicker.setValue((LocalDate)value);
+				}
 				attachEnterEscapeEventHandler();
 
 				datePicker.getEditor().requestFocus();
 				datePicker.removeEventFilter(KeyEvent.KEY_PRESSED, eh);
 			}
 
-			@Override
+			/*@Override
 			public LocalDate validateEdit() {
 				return datePicker.getValue();
 			}
-
+*/
 			@Override
 			public DatePicker getEditor() {
 				return datePicker;
 					@Override
 					public void handle(KeyEvent t) {
 						if (t.getCode() == KeyCode.ENTER) {
-							final Runnable r = new Runnable() {
-								@Override
-								public void run() {
-									endEdit(true);
-								}
-							};
-							Platform.runLater(r);
+							endEdit(true);
 						} else if (t.getCode() == KeyCode.ESCAPE) {
 							endEdit(false);
 						}
 
 				datePicker.addEventFilter(KeyEvent.KEY_PRESSED, eh);
 			}
+
+			@Override
+			public String getControlValue() {
+				return datePicker.getEditor().getText();
+			}
 		};
 	}
 

src/main/java/org/controlsfx/control/spreadsheet/SpreadsheetCellType.java

  * When instantiating a {@link SpreadsheetCell}, its SpreadsheetCellType will
  * condition which value the cell can accept, and which
  * {@link SpreadsheetCellEditor} it will use.
+ * <br/>
+ * 
+ * 
+ * 
+ * 
  * 
  * 
  * @see SpreadsheetView
 			final int rowSpan, final int columnSpan, final T value);
 
 	/**
-	 * Gets this type String converter.
-	 * 
-	 * @return the converter instance
-	 */
-	public StringConverter<T> getConverter() {
-		return converter;
-	}
-
-	/**
 	 * Gets this type editor.
 	 * 
 	 * @return the editor instance
 	 */
 	public abstract SpreadsheetCellEditor<T> getEditor(SpreadsheetView view);
 
+	public abstract String toString(T item);
 	/**
 	 * Copies the value of a cell to another (copy/paste operations).
 	 * 
 	protected abstract void copy(SpreadsheetCell from, SpreadsheetCell to);
 
 	/**
+     * This method will be called when a commit is happening.<br/>
+     * You will then compute the value of the editor in order to determine
+     * if the current value is valid.
+     * @return null if not valid or the correct value otherwise.
+     */
+    public abstract T convertValue(String value);
+	
+    /**
 	 * The Object type instance.
 	 */
 	public static final SpreadsheetCellType<Object> OBJECT = new ObjectType();
 			}
 			return editor;
 		}
+
+		@Override
+		public Object convertValue(String value) {
+			return converter.fromString(value);
+		}
+
+		@Override
+		public String toString(Object item) {
+			return converter.toString(item);
+		}
+
 	};
 
 	/**
 			}
 			return editor;
 		}
+
+		@Override
+		public String convertValue(String value) {
+			return value;
+		}
+
+		@Override
+		public String toString(String item) {
+			return converter.toString(item);
+		}
+
 	};
 
 	/**
 			}
 		}
 
+		@Override
+		public Double convertValue(String value) {
+			try {
+				return converter.fromString(value);
+			} catch (Exception e) {
+				return null;
+			}
+		}
+
+		@Override
+		public String toString(Double item) {
+			return converter.toString(item);
+		}
+
 	};
 
 	public static final SpreadsheetCellType<String> LIST(
 				to.setItem(value);
 			}
 		}
+
+		@Override
+		public String convertValue(String value) {
+			return converter.fromString(value);
+		}
+
+		@Override
+		public String toString(String item) {
+			return converter.toString(item);
+		}
+
 	}
 
 	/**
 
 			}
 		}
+
+		@Override
+		public LocalDate convertValue(String value) {
+			try {
+				LocalDate temp = converter.fromString(value);
+				return temp;
+			} catch (Exception e) {
+				return null;
+			}
+		}
+
+		@Override
+		public String toString(LocalDate item) {
+			return converter.toString(item);
+		}
+
 	}
 
 }

src/main/java/org/controlsfx/control/spreadsheet/SpreadsheetView.java

 import javafx.collections.ObservableList;
 import javafx.event.ActionEvent;
 import javafx.event.EventHandler;
-import javafx.scene.Node;
 import javafx.scene.control.ContextMenu;
 import javafx.scene.control.Control;
 import javafx.scene.control.MenuItem;
 import javafx.scene.control.SelectionMode;
 import javafx.scene.control.SelectionModel;
-import javafx.scene.control.Skin;
 import javafx.scene.control.TableCell;
 import javafx.scene.control.TableColumn;
 import javafx.scene.control.TablePosition;

src/main/resources/org/controlsfx/control/spreadsheet/spreadsheet.css

 }  
 
 /* FIXED COLUMNS  */
-HorizontalHeader > Label.fixed,
-VerticalHeader > Label.fixed{
+VerticalHeader > Label.fixed,
+Label.fixed{
 	-fx-font-style : italic;
 }