This tutorial shows how to create a custom schedule item class. For simplicity, the sample demonstrates that by inheriting a class from Appointment.
Follow steps 1 and 2 from Tutorial 1: Getting Started.
Create a new class in the project (for example MyApp). Inherit the new class from Appointment.
The custom class will support a new property of type boolean (called Kept) that will indicate whether the appointment has been kept or cancelled. The implementation of the property is trivial. Simply add a private field of type boolean and a property getter and setter. The following code illustrates this:
Java Copy Code |
---|
public class MyApp inherits Appointment |
Note |
---|
You must provide constructor that takes no argument with your custom item class. JPlanner creates item class instances using reflection and an exception will be thrown if there is no such constructor available. |
By default the Calendar control instantiates items of type Appointment when the user creates items interactively (by typing). To make the calendar create items of the new class, we need to add the following line of code:
Java Copy Code |
---|
calendar.setInteractiveItemType(MyApp.class); |
Now all items created by the user at runtime will be instances of MyApp.
To test whether the calendar actually creates MyApp instances, listen to the control's itemClick event. Add the following as a body of the event method:
Java Copy Code |
---|
if (e.getItem() instanceof MyApp) |
Clicking on an item now should display the message box.
Serialization of custom items requires additional efforts. Attempts to save a schedule containing items of type MyApp will result in an InvalidOperationException exception. In order to support serialization, we have to register MyApp with the schedule. For this purpose, add the following line of code somewhere in the window's constructor:
Java Copy Code |
---|
Schedule.registerItemClass(MyApp.class, "myapp", 1); |
Now the exception won't be thrown, but we still need to override the saveTo and loadFrom methods of the Appointment class in order to serialize the custom property Kept. The code below illustrates how this is done.
Java Copy Code |
---|
@Override |
Recurrence of items from a custom class is not handled wholly automatically. Let's take a closer look at how recurrence works in JPlanner. When a recurrence pattern is associated with an existing item, the item is said to become the master of the recurrence. Every time the occurrences of a master item must be displayed in a given time range, they are generated by instantiating new objects of the same type as that of the master. This is necessary in order to support infinite recurrences, where the generation of all recurrence instances is not possible. When the occurrences are generated, their properties are copied from the master item. This is done automatically for the built-in item properties, but not for those added by a derived item class. In order to copy the custom properties from the master item to a particular occurrence, you need to override the copyOccurrence method. The following code sample shows how to do this.
Java Copy Code |
---|
@Override |
In addition, it is recommended to mark an item from a recurrent series as an exception when the value of a custom property changes. Otherwise, the change will be lost the next time the instance is generated.
Java Copy Code |
---|
//... |
The users can clone items by holding down a particular key (the SHIFT key by default). To enable your custom items to be cloned interactively, you have to override the clone method, create a new object from your custom type and duplicate the fields of the prototype item. If you do not override the clone method, JPlanner will still create copies of the item, but they will be of type Appointment instead of the custom item type. Here is a sample implementation of the clone method.
Java Copy Code |
---|
//... |
In order to enable undo/redo of changes done on MyApp, create an ItemState-derived class that contains member corresponding to the additional properties defined by MyApp:
Java Copy Code |
---|
class MyAppState extends ItemState |
Finally, override the createState, saveState and restoreState methods in the custom appointment class:
Java Copy Code |
---|
@Override |