How to do per-keystroke validation of TextFieldTableCell? - javafx-2

I am trying to do per-keystroke validation in a JavaFX TextFieldTableCell but I do not know how to capture the text-changed events from the embedded TextField control.
If the object in question were simply a TextField, then textField.textProperty().addListener(myChangeListener) would do the trick. TextFieldTableCell also exposes textProperty(), but this property behaves quite differently on TextFieldTableCell. It does not generate change events on a per-keystroke basis. Rather, I see lots of events when the TableView is first displayed, and I see one event each time I begin editing in a cell.

First of all, about textProperty().
Look here to see :
http://docs.oracle.com/javafx/2/api/index.html
TextProperty() is a property of labeled parent class, you will learn nothing from it, because it is not used. It is tricky thing : cell - inheritant of labeled control. TextField, which you see, when start editing, it is a graphic node of cell (graphicProperty()) (as far as I remember documentation).
And, this graphic node is assigned by a text field, only when editing starts.
AFAIK, there is no direct access to editable node.
The way to solve the issue - implement editable cell by your self.
Let me talk to developer, to learn more...
Supposing, you have DataItem class, which contains String, and supposing that TableView has encapsulated data type DataItem, and the only column has the same encapsulated data type, you may use this implementation as basis :
public class TextFieldTableCell extends TableCell<DataItem, DataItem> {
private TextField textField;
public TextFieldTableCell() {
}
#Override
public void startEdit() {
super.startEdit();
if (isEmpty()) {
return;
}
if (textField == null) {
createTextBox();
} else {
textField.setText(new CellCustomStringConverter().toString(getItem()));
}
setGraphic(textField);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
textField.requestFocus();
textField.selectAll();
}
#Override
public void cancelEdit() {
super.cancelEdit();
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
#Override
public void updateItem(DataItem item, boolean empty) {
super.updateItem(item, empty);
if (!isEmpty()) {
if (textField != null) {
textField.setText(new CellCustomStringConverter().toString(item));
}
setText(item.toString());
}
}
private void createTextBox() {
textField = new TextField(new CellCustomStringConverter().toString(getItem()));
textField.setId(TABLE_EDIT_ID);
textField.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
commitEdit(new DataItem(textField.getText()));
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
}
}
});
}
}

Related

Cannot capture javafx CheckBoxTableCell CellEditEvent

I have defined a CheckBoc TableColumn as
#FXML private TableColumn<Batch, Boolean> sltd;
And have defined the CellValueFactory & CellFactory
sltd.setCellValueFactory(new PropertyValueFactory<Batch, Boolean>("pr"));
sltd.setCellFactory(CheckBoxTableCell.forTableColumn(sltd));
My problem is i am not able to capture the edit column event for the checkbox. I use the following code:
sltd.setOnEditStart(new EventHandler<TableColumn.CellEditEvent<Batch, Boolean>>() {
#Override
public void handle(TableColumn.CellEditEvent<Batch, Boolean> t) {
//System.out.println("CheckBox clicked.");
}
});
I don't think the check boxes in the CheckBoxTableCell invoke the startEdit(...) method on the table.
The only thing that can happen in an edit is that the boolean property of one of the items in the table changes from true to false, or vice versa. So you can check for this just by listening directly to those boolean properties.
If you want a single listener that will catch changes to any of the properties, you can create an observableList with an "extractor" and register a list change listener with the list. This looks like:
ObservableList<Batch> items = FXCollections.observableArrayList(new Callback<Batch, Observable[]>() {
#Override
public Observable[] call(Batch batch) {
return new Observable[] { batch.prProperty() } ;
}
}
// populate items
table.setItems(items);
items.addListener(new ListChangeListener<Batch>() {
#Override
public void onChanged(Change<? extends Batch> change) {
while (change.hasNext()) {
if (change.wasUpdated()) {
System.out.println("Item at "+change.getFrom()+" changed value");
}
}
}
});

Post render event in JavaFX

I'm trying to add a click event listener to the label of all column-headers of a TableView, as follows:
for (final Node header : tblView.lookupAll(".column-header > .label")) {
if ((header != null) && (header instanceof Label)) {
final Label headerLabel = (Label) header;
// ...
}
}
Now, the problem is that if I do this in the initialize()-function of the Controller, the Scenegraph is not yet rendered and the above code won't work. Hence my question: Is there some kind of a post-render event?
many thanks in advance.
There is a WINDOW_SHOWN event in javafx.stage.WindowEvents. That is not (imo) "Post render event" but you can utilize it in similar manner, by adding an event handler to the Stage (which extends from Window).
In the initialize method of controller class, get the primary stage and then:
stage.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent window) {
Platform.runLater(new Runnable() {
#Override
public void run() {
addListenerToColumnHeaders();
}
});
}
});
Hope this helps, since didn't try myself.

How can I get a filter to work with an Event Calendar webpart?

I've created a custom event document that extends the fields of the normal event document. I've added a field that can keep 0 to many category Ids in a pipe delimited list. Categories are stored in a custom table.
Here is my filter code:
public partial class CMSGlobalFiles_EventCategoryFilter : CMSAbstractDataFilterControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnInit(EventArgs e)
{
SetupControl();
base.OnInit(e);
}
protected override void OnPreRender(EventArgs e)
{
if (RequestHelper.IsPostBack())
{
setFilter();
}
base.OnPreRender(e);
}
private void SetupControl()
{
if (this.StopProcessing)
{
this.Visible = false;
}
else if (!RequestHelper.IsPostBack())
{
InitializeCategory();
}
}
private void InitializeCategory()
{
CustomTableItemProvider customTableProvider = ne CustomTableItemProvider(CMSContext.CurrentUser);
string where = "";
string tableName = "customtable.EventCategory";
DataClassInfo customTable = DataClassInfoProvider.GetDataClass(tableName);
if (customTable != null)
{
DataSet dataSet = customTableProvider.GetItems(tableName, where, null);
if (!DataHelper.DataSourceIsEmpty(dataSet))
{
this.drpCategory.DataSource = dataSet;
this.drpCategory.DataTextField = "CategoryName";
this.drpCategory.DataValueField = "ItemGUID";
this.drpCategory.DataBind();
this.drpCategory.Items.Insert(0, new ListItem("(all)", "##ALL##"));
}
}
}
private void setFilter()
{
string where = null;
if (this.drpCategory.SelectedValue != null)
{
Guid itemGUID = ValidationHelper.GetGuid(this.drpCategory.SelectedValue, Guid.Empty );
if (itemGUID != Guid.Empty)
{
where = "EventCategory LIKE \'%" + itemGUID.ToString() + "%\'";
}
}
if (where != null)
{
this.WhereCondition = where;
}
this.RaiseOnFilterChanged();
}
}
This filter works great using a basic repeater and a document data source. When I use the event calendar it does not. I'm using Kentico version 6.0.30
The problem is in the different lifecycle of the EventCalendar, based on the CMSCalendar control which is based on standard .Net Calendar.
First of all, our developers discovered a way to fix this and allow your scenario to run by default. This fix will be included in the 6.0.33 hotfix (scheduled to go out on Friday 25th).
I'm sorry for this inconvenience.
Aside from this upcoming fix, it's also possible to make the EventCalendar to filter its results by modifying (cloning) the web part, integrating the filter controls directly into that web part and set the calendar's Where condition in the OnPreRender before the DataBind as
protected override void OnPreRender(EventArgs e)
{
calItems.WhereCondition = "some filtering condition";
...
If you can hotfix your CMS instance, it would be certainly less effort.
Regards,
Zdenek / Kentico Support

Preventing TabControl selection in Silverlight

Is there any way to prevent the change of a tab in TabControl in Silverlight 4?
A simple case is when I've got a form with some data, and I want to ask the user if he/she wants to save this data before actually changing the tab.
I've seen code examples on how to do this in WPF, but not in Silverlight.
What can I do to stop the tab from changing?
Bind SelectedIndex to a property on your data context.
<sdk:TabControl SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}">
<sdk:TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</sdk:TabItem>
<sdk:TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</sdk:TabItem>
</sdk:TabControl>
In the SET accessor, write your code to check whether the user really wants to do what they're trying to do.
public class Context : INotifyPropertyChanged
{
int _SelectedIndex = 0;
public int SelectedIndex
{
get
{
return _SelectedIndex;
}
set
{
MessageBoxResult result = MessageBox.Show("Do you want to save?", "Really?", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
_SelectedIndex = value;
}
RaisePropertyChanged("SelectedIndex");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
Net effect is, if the user chooses 'cancel' on the dialogue the private variable is never changed - the PropertyChanged event fires, rebinding the selected index to the existing value.
Hope this is what you were trying to accomplish.
UPDATE (11/10/2012) - Alternate method (possibly for SL5?). Write code behind to tie into SelectionChanged event of your TabControl, reset the tab control's selected item property based on your test.
private void TabControl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count > 0)
{
MessageBoxResult result = MessageBox.Show("Do you want to save?", "Really?", MessageBoxButton.OKCancel);
if (result != MessageBoxResult.OK)
{
((TabControl)sender).SelectionChanged -= new SelectionChangedEventHandler(TabControl_SelectionChanged);
((TabControl)sender).SelectedItem = e.RemovedItems[0];
((TabControl)sender).SelectionChanged += new SelectionChangedEventHandler(TabControl_SelectionChanged);
}
}
}

Java ME - Multiple forms, moving from one screen to the next

I am using Java Micro Edition and I am trying to create a simple login form with a record store. When the user enters the details I'd like to check them against the ones stored and then move onto another screen like a welcome area.
I have a feeling it has something to do with the form element and switching between it but I can't seem to get anywhere with google
try this
form = new Form("login");
form.addCommand(getExitCommand());
form.addCommand(getOkCommand());
form.setCommandListener(this);
public void commandAction(Command command, Displayable displayable) {
if (displayable == form) {
if (command == exitCommand) {
exitMIDlet();
} else if (command == okCommand) {
display.setCurrent(getWelcomeForm());
}
} else if (displayable == form1) {
if (command == backCommand) {
// do something else
}
}
}
display is something that should be created in the constructor and also above it i.e.
public class YourMidlet extends MIDlet implements CommandListener {
private Display display;
private Form form1;
private Form form2;
public YourMidlet
{
display = Display.getDisplay(this);
form 1 = new Form("hello form this is form 1");
form 2 = new Form("hello form 2");
display.setCurrent(form1);
}
}
you then do:
display.setCurrent(form2);
to switch to form 2

Resources