so as I mention in the last video we're gonna add an improvement to our user
interface and that's too highlight items in the list that got a deadline on today's date
and we are doing that so the users doesn't have to click on every item
to find out what they need to prioritize now theirs couple ways to do that
so one way to highlight todays items would be just a color them red so to do this we
were going to use what's called a cell factory
each item in the list view is being displayed in a cell and what we can do is we
can customize how those cell will look by assigning a custom cell factory to
the list view so right now its using the default cell factory and this
default factory sets the text to whatever items toString method
returns as you've seen on screen but also you might have noticed that the
background color of the cell alternates between white and a very light gray
depending on the operating system that your running this on so we are going to change all of this and
more so when we create a cell factory we need to provide the call back or
the method that the ListView will call each time it wants to paint one of its cells so lets add the
code to set the cell factory and declared the callback method we are gonna do
that in the initialization method of the main
window's controller and that's over at controllers.java so gonna add this as
the last line and within their we're gonna do....
...and I just pressed
enter their it should populate the rest of the code for us so basically
created the callback method for us automatically and all we really need to
do then do is actually updated it so we got a call so we need to add some code in here so within
this code itself we are gonna add a list...
....
....and then we going then override a method so within their we are going to do
an override and hold down CTRL and press O or command o depending on your
operating system and we can do a search for update item so type...
press Enter its gonna override that method as you can see their and
we will come back to that in a minute we are not gonna leave that there but we're gonna
come back to that in a minute for now we're going to go to here and down here and then
before that we need to return something so return the cell which will
update so from their...
...so lets talk about what we've just done so in the
code we've set to cell factory by calling the list views set cell factory method
and we pass an anonymous class that implements the callback interface
now this interface is part of the JavaFX API and the interface itself has got 2 type parameters the
first parameter is the type of argument provided by the call method and
that's a single method within the interface in our case we are passing it
the ListView controller as you can see on the screen and the second parameters
is the types that its gonna be returned from the call method in our case an instance of the
class list cell is gonna be returned you can see we returning that cell on line 63 we also used generics
to be more specific about what type of objects the list view cell will contain hence the
the use of to do item which you've seen on line 54 and 55 we've also used an
anonymous class to create our list cell instances
and we are overriding the update items as you can see in the list cell class and that
methods can run whenever the list view wants to paint a single cell
so our call method is gonna return the cell instance that we created and the list view will used that
instance to paint the cell as a result this allows us
to paint every cell based on the to-do item that it contained which is pretty cool
we've got the basic code skeleton in place we are gonna add the code to update item
that's going to color items with the deadline on today in red so our anonymous classes extending
the list cell class as can see and that class has got the label class in its
parent hierarchy and controllers that descend from the label class can
display text and graphics now the label control also has this class in
its parents hierarchy many of the methods that are calling when working with the
label controls are actually in the label class for example the set text set
graphic and set text field methods are all in the label class itself so in the update
item method we are going to first call the super method which is already there as you can see we don't need
to change anything for that and we are doing that because we want to keep most of the
default appearance provided by the parent class for example we may still
want the alternating background colors and since we're providing the cell factory
we have to set the text by calling the set text method and we are gonna do that we
are gonna put that code index to do that and before doing that we have to first
check for empty cells and if its empty we need to set its text to null
and if a cell isn't empty then we have to set the text to the to
-do items short description and will then used the set text fill method
to set the color of the text to red if the items due today so we do this by
we figure it out by comparing the items deadline with value local date.now and
because that returns todays date so lets implement that code and its not a lot of code to do that
so we still wanna keep the super call in as I mention because we want to keep most
of the default appearance provided by its parent class and that's what that does
but then we override that accent the extra so we test here we are gonna put...
....
...and will just do a test to see whether it's today's date
so put....
....
...and if we run that we should find that our due item will now show in red and I'm not sure why I didn't end up
saving that other long item I thought I saved it but for some reason its showing so lets
just try setting another creating a new record again I think I didn't close it down
properly and if you remember that the saving only happens on closed down so
probably didn't save the entries permanently and when I started up again that data is not
there so we can put in a Test record set the deadline to today's date just to see what
happens which is the current date their click OK and bingo its actually working its showing in
red so that's what we wanted to do so if its today's date and you substitute
the date when you selected with date that you're running
applications is now showing in red and that proves that the code is being executed
the callback code in our update item method and switch back over to the interface again lets just add
that really long item so click on new.....go back to the page again and
right click and copy and paste that in there and set the deadline also today's
date number click ok and I've got 2 entries in red they're also going to close down normally and
run it again and we should find those entries are back in there again today their we go they are back in so obviously when
I closed down at the end of the previous video I didn't close down properly and
the code that was writing it back to a file the
to do items wasn't executed so that is good we know its working now
moving on can you remember when we change the to do string method to return the short
description so that the ListView will display rather than the object reference
at that point we could have added a cell factory instead and because we've added
one their and it sets the text to the item short description we no longer need to
override the to do String method in the to do item class so we go
back to the to-do item class remembering the to do string here we no longer need this because
controller code the updates cell method is adding that for us in the item update method from our cell factory
we can just comment that out if you want you should find that everything works as per normal
and its working because it's using a custom cell factory that we implemented so when we don't want to use
a cell factory for whatever reason overriding the to do String method in
our data objects can be quick way to have a custom or have control display what we
want but they will be time where we want to override the to do string method and print the values of all instance variables
or do something else other than return the value of 1 field and that is where the
custom cell factory will come in handy and now we know how to create custom cell
factory we want to override the to do String method for the purposes of
customizing our control displays but in addition to covering items
today in red it would be nice if the user could also see what would be due
tomorrow so let's first add a to do item with a deadline of tomorrow and add some
code into the it with a different color so I'm gonna create new to do item
....
....confirming that it's not showing in red or anything so we know its not been processed
as todays date its tomorrow date close that down that normally so its saves close down the run window and if you go
back to our controller that's actually update the update item method to show
those items that are due tomorrow in a different color what we can do is this
test here that is checking to see when the deadline is now will put an else after that
because we want to test for different conditions so....
.....
takes the current day and adds one to it...
....
....so lets run it and we can see that it has done something we can see a bit of yellow their but that is
really a bad choice of color we can't really read the item anymore other than
knowing that's something for tomorrow so it really is quite a bad color so lets change
yellow and try something a bit better maybe brown
try brown and see what that looks like and that is a bit better but their is a bug in our code I'm not sure if you have spotted it but to illustrate it lets add an item that was due last week so.....
....
...save that and notice there's its not highlighted in anyway even though it's a
overdue so we assumed that our users are super efficient and always completely theirs items on time
so that's color our overdue items in red
so we are gonna change the condition in our cell factory so that it checks for dates earlier
or equal to today's date so lets go ahead and do that so close it down and looking at the code at the moment
we are doing
if item.getdeadlines is equal to localdate.now instead of doing a
test for just .equals now we are gonna put...
....what we are
testing for now is to see whether there's a date a deadline and that
date is before the current to today + 1 so it has to be earlier than tomorrow so any
date is equal to today's date or prior to that date should now be flag in red so lets run that and
see what happens and I've you've got a lot of items in red and all these date should
be dates in the past you can see some of this go back to march april may 13 but anything from this date onward that one by
the way may 17 was today so that's working today or prior is working
we still got tomorrow date in the future that's working for tomorrow but also dates
that are more than one day in the future
are colored in the normal colors because they're outside of just being tomorrow
so we now see that our code is working properly and as a result at this point
the user can view to do items and add new to do items but in a real world application
they really should be able to edit and delete to-do items as well so we are not gonna cover the edit case
at the moment because that is really going to be very similar to the other case we could
even use the same FXML file because we want our dialogue to look the same
and the instructions we display in the dialogue might be different but which could
lead could be an argument for putting into each dialog header text rather
than into the dialogues header text and we also want to initialize the value of each
control with the values from the to do item the user wants to edit but other than
that the UI would pretty much be the same so its probably something you could
figure out so instead of actually looking at the edit in the next lecture
let's start looking at the ability to delete to do items and then make a few
other enhancements to the UI see you in the next video