{"id":2289,"date":"2020-05-13T15:44:00","date_gmt":"2020-05-13T15:44:00","guid":{"rendered":"https:\/\/mindfusion.eu\/blog\/?p=2289"},"modified":"2021-01-25T16:13:08","modified_gmt":"2021-01-25T16:13:08","slug":"custom-painting-of-resources-in-java-scheduler","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/custom-painting-of-resources-in-java-scheduler\/","title":{"rendered":"Custom Painting of Resources in Java Scheduler"},"content":{"rendered":"<p>In this blog post we will explain how to color cells and resources in the Resource view of the calendar based on a certain criteria. In our case we take the &#8220;Resource Table&#8221; sample from the <a href=\"https:\/\/mindfusion.dev\/products\/java\/scheduling\/samples\">Samples for the Java Swing Scheduler<\/a> and we will edit its code to color the header and background of cells that correspond to given resources, in our sample it is an employee:<\/p>\n<p><img decoding=\"async\" title=\"Resource View: Custom Painting of Background\" src=\"https:\/\/mindfusion.dev\/samples\/java\/scheduler\/resource_table_coloring.png\" \/><\/p>\n<p>We will also add tooltips that show when the mouse is over cells that correspond to this employee:<\/p>\n<p><img decoding=\"async\" title=\"Resource View: Tooltips\" src=\"https:\/\/mindfusion.dev\/samples\/java\/scheduler\/resource_tooltip.png\" \/><\/p>\n<p><!--more--><\/p>\n<p><strong>I. General Settings<\/strong><\/p>\n<p>The code that we will demonstrate and explain is an extension to the &#8220;Resource Table&#8221; sample, which you can download from:<\/p>\n<p align=\"center\"><a href=\"http:\/\/mindfusion.dev\/samples\/java\/scheduler\/resourceTable.zip\">Download Resource Table Java Scheduler Sample<\/a><\/p>\n<p>The sample uses the Scheduling Library for Java Swing, which is included as a Jar reference to the project.<\/p>\n<p><img decoding=\"async\" title=\"Adding JPlanner Jar File as a Reference to an Eclipse Project\" src=\"https:\/\/mindfusion.dev\/samples\/java\/scheduler\/jplanner_jar.png\" \/><\/p>\n<p>We create an instance of the <a title=\"MindFusion Scheduling Library for Java Swing: Calendar\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_awt_AwtCalendar_Members.htm\">Calendar<\/a> class and set its current time, date and the end date &#8211; this is the final date that will be visible in the resource view:<\/p>\n<pre>calendar = new Calendar();\ncalendar.setCurrentTime(DateTime.now());\ncalendar.setDate(new DateTime(2020, 6, 8));\ncalendar.setEndDate(new DateTime(2020, 7, 7));\n<\/pre>\n<p>The <a title=\"MindFusion Scheduling Library for Java Swing: Calendar\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_awt_AwtCalendar_Members.htm\">Calendar<\/a> class exposes many methods for customizing the schedule. We first set the view to be <a title=\"MindFusion Scheduling Library for Java Swing: CalendarView\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_CalendarView_0.htm\">CalendarView<\/a> <a title=\"MindFusion Scheduling Library for Java Swing: itemSettings\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?M_com_mindfusion_scheduling_awt_AwtCalendar_getItemSettings_0.htm\">itemSettings<\/a> and <a title=\"MindFusion Scheduling Library for Java Swing: resourceViewSettings\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?M_com_mindfusion_scheduling_awt_AwtCalendar_getResourceViewSettings_0.htm\">resourceViewSettings<\/a> classes provide us with lots of options to customize the calendar look. We use them to twist the appearance of our resource table:<\/p>\n<pre>calendar.getItemSettings().getSelectedItemStyle().setHeaderFont(new Font(\"Verdana\", Font.PLAIN, 9));\ncalendar.getItemSettings().getSelectedItemStyle().setHeaderTextAlignment(EnumSet.of(TextAlignment.MiddleLeft));\ncalendar.getItemSettings().getSelectedItemStyle().setHeaderTextShadowStyle(ShadowStyle.None);\n...........................\n...........................\ncalendar.getItemSettings().getStyle().setHeaderFont(new Font(\"Verdana\", Font.PLAIN, 9));\ncalendar.getItemSettings().getStyle().setHeaderTextAlignment(EnumSet.of(TextAlignment.MiddleLeft));\ncalendar.getItemSettings().getStyle().setHeaderTextShadowStyle(ShadowStyle.None);\ncalendar.getItemSettings().getStyle().setHeaderTextShadowOffset(0);\n...........................\n...........................\ncalendar.getResourceViewSettings().getBottomTimelineSettings().setFormat(\"EEE (MM\/dd)\");\ncalendar.getResourceViewSettings().getBottomTimelineSettings().setSize(15);\ncalendar.getResourceViewSettings().getBottomTimelineSettings().getStyle().setHeaderBrush(Brushes.White);\ncalendar.getResourceViewSettings().getBottomTimelineSettings().getStyle().setHeaderFont(new Font(\"Verdana\", Font.BOLD, 10));<\/pre>\n<p>The employees are instances of the <a title=\"MindFusion Scheduling Library for Java Swing: Contact\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_model_Contact_Members.htm\">Contact<\/a> class. We add them to the <a title=\"MindFusion Scheduling Library for Java Swing: contacts\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?M_com_mindfusion_scheduling_awt_AwtCalendar_getContacts_0.htm\">contacts<\/a> collection of the <a title=\"MindFusion Scheduling Library for Java Swing: Calendar,\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_awt_AwtCalendar_Members.htm\">Calendar,<\/a> once we&#8217;ve created them. It is important that we provide an id to each <a title=\"MindFusion Scheduling Library for Java Swing: Contact,\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_model_Contact_Members.htm\">Contact,<\/a> because that&#8217;s how we will identify them later in code:<\/p>\n<pre>Contact contact = new Contact();\ncontact.setFirstName(\"Mike\");\ncontact.setId(\"IdMike\");\ncontact.setName(\"Mike\");\ncalendar.getContacts().add(contact);<\/pre>\n<p>The items that represent tasks for the resources are <a title=\"MindFusion Scheduling Library for Java Swing: Appointment\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_model_Appointment_Members.htm\">Appointment<\/a> instances. We create them in code and add them to the <a title=\"MindFusion Scheduling Library for Java Swing: items\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?M_com_mindfusion_scheduling_CalendarListener_itemsModified_1_ItemsEvent.htm\">items<\/a> collection of the <a title=\"MindFusion Scheduling Library for Java Swing: Calendar\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_awt_AwtCalendar_Members.htm\">Calendar<\/a> app;<\/p>\n<pre>app = new Appointment();\napp.setStartTime(new DateTime(2006, 3, 27));\napp.setEndTime(new DateTime(2006, 3, 28));\napp.getContacts().add(calendar.getSchedule().getContacts().get(\"IdMike\"));\napp.setHeaderText(\"21965 Carbon Mesa Rd (1)\");\napp.setPriority(0);\ncalendar.getSchedule().getItems().add(app);<\/pre>\n<p>The resource view renders rows of cells that correspond to a given <a title=\"MindFusion Scheduling Library for Java Swing: Resource,\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_model_Resource_Members.htm\">Resource,<\/a> <a title=\"MindFusion Scheduling Library for Java Swing: Location,\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_model_Location_Members.htm\">Location,<\/a> <a title=\"MindFusion Scheduling Library for Java Swing: Item,\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_model_Item.htm\">Item,<\/a> <a title=\"MindFusion Scheduling Library for Java Swing: Task\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_model_Task_Members.htm\">Task<\/a> etc. The options available are members of the <a title=\"MindFusion Scheduling Library for Java Swing: GroupType\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_GroupType_0.htm\">GroupType<\/a> enumeration. In our sample we group the view by resources e.g. employees:<\/p>\n<pre>calendar.setGroupType(GroupType.GroupByContacts);<\/pre>\n<p>And with that we&#8217;ve finished with the general settings and we continue writing the code that will customize our application.<\/p>\n<p><strong>II. Custom Drawing<\/strong><\/p>\n<p>What we want to achieve as appearance of our resource table &#8211; selective coloring of the background of cells &#8211; can be done through custom drawing. Custom drawing provides us with means to color most elements of the calendar, depending on the view. The &#8220;Custom Draw Elements&#8221; sample gives us visual representation of the elements that correspond to the <a title=\"MindFusion Scheduling Library for Java Swing: CustomDrawElements\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?M_com_mindfusion_scheduling_Calendar_setCustomDraw_1_EnumSet{CustomDrawElements}_2.htm\">CustomDrawElements<\/a> enumeration, which determines what is to be custom drawn in a schedule:<\/p>\n<p><img decoding=\"async\" title=\"Presenting the Custom Drawing Options in Java Scheduling\" src=\"https:\/\/mindfusion.dev\/samples\/java\/scheduler\/custom_draw_elements.png\" \/><\/p>\n<p>The sample is available from this link:<\/p>\n<p><a title=\"Custom Drawn Elements in Scheduling for Java Swing\" href=\"https:\/\/mindfusion.dev\/samples\/java\/scheduler\/customDrawElements.zip\">Java Swing Scheduler: Sample that Demonstrates the Custom Draw Elements according to the Calendar View<\/a><\/p>\n<p>We want to color the resource header and the cells that correspond to this resource. So we use the <a title=\"MindFusion Scheduling Library for Java Swing: setCustomDraw\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?M_com_mindfusion_scheduling_Calendar_setCustomDraw_1_EnumSet{CustomDrawElements}_2.htm\">setCustomDraw<\/a> method to achieve that:<\/p>\n<pre>calendar.setCustomDraw(EnumSet.of(CustomDrawElements.ResourceViewRowHeader, CustomDrawElements.ResourceViewCellComplete));<\/pre>\n<p>The members of the <a title=\"MindFusion Scheduling Library for Java Swing: CustomDrawElements\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_CustomDrawElements_0.htm\">CustomDrawElements<\/a> enumeration allow bitwise combining. The drawing is done in an event handler for the <a title=\"MindFusion Scheduling Library for Java Swing: draw\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_CalendarAdapter_Members.htm\">draw<\/a> event:<\/p>\n<pre>calendar.addCalendarListener(new CalendarAdapter() {\n\tpublic void draw(CalendarDrawEvent e) {\n\t\tonCalendarDraw(e);\n\t}\n});<\/pre>\n<p>We use a <a title=\"MindFusion Scheduling Library for Java Swing: CalendarAdapter\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_CalendarAdapter_Members.htm\">CalendarAdapter<\/a> to subscribe to the <a title=\"MindFusion Scheduling Library for Java Swing: draw\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?M_com_mindfusion_scheduling_CalendarAdapter_draw_1_DrawEvent.htm\">draw<\/a> event, which we handle with the onCalendarDraw method. The <a title=\"MindFusion Scheduling Library for Java Swing: CalendarDrawEvent\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_CalendarDrawEvent_Members.htm\">CalendarDrawEvent<\/a> class exposes many properties that give us information about the element that is being drawn. We use the <a title=\"MindFusion Scheduling Library for Java Swing: getElement\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?M_com_mindfusion_scheduling_CalendarDrawEvent_getElement_0.htm\">getElement<\/a> method to check, which element is being drawn &#8211; the cell or the header. If it is the cell, we get the resource that correspond to it and if it is the right one, we paint a rectangle, which represents the whole area of the element that is painted at the moment. We get it with the <a title=\"MindFusion Scheduling Library for Java Swing: getBounds\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?M_com_mindfusion_scheduling_CalendarDrawEvent_getBounds_0.htm\">getBounds<\/a> method:<\/p>\n<pre>if (e.getElement() == CustomDrawElements.ResourceViewCellComplete)\n{\n\tRectangle bounds = new Rectangle(e.getBounds());\n\tbounds.x += 1;\n\nif (e.getResource().getId().equals(\"IdMike\") ||\n    e.getResource().getId().equals(\"IdChuck\") ||\n    e.getResource().getId().equals(\"IdTom\") ||\n    e.getResource().getId().equals(\"IdAlfredo\"))\n\t\t\t\t\n{\n\tg.fillRectangle(_brush3, bounds);\n\tg.drawString(\"Office\", _font, _textBrush, bounds, f);\n}<\/pre>\n<p>We will draw an outline to the resource header that correspond to the same resource, whose rows we colored with _brush3:<\/p>\n<pre>else if (e.getElement() == CustomDrawElements.ResourceViewRowHeader)\n\t{\n\t\tif (e.getResource().getId().equals(\"IdMike\") ||\n\t\t\te.getResource().getId().equals(\"IdChuck\") ||\n\t\t\te.getResource().getId().equals(\"IdTom\") ||\n\t\t\te.getResource().getId().equals(\"IdAlfredo\"))\n\t\t\t\t\t\n\t\t{\n\t\t\tBrush _brush3 = new SolidBrush(new Color(254, 249, 207, 100));\n\t\t\tg.fillRectangle(_brush3, e.getBounds());\n\t\t\tg.drawRectangle(new Pen(new Color(163, 198, 134, 255), 2),\n\t\t\t\te.getBounds().getMinX() + 1,\n\t\t\t\te.getBounds().getMinY() + 1,\n\t\t\t\te.getBounds().getMaxX() - 2,\n\t\t\t\te.getBounds().getMaxY() - 2);\n\t\t}\n\t\t\t\n\t}<\/pre>\n<p>Here we check if the custom draw element is <a title=\"MindFusion Scheduling Library for Java Swing: CustomDrawElements\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?M_com_mindfusion_scheduling_Calendar_setCustomDraw_1_EnumSet{CustomDrawElements}_2.htm\">CustomDrawElements<\/a> The <a title=\"MindFusion Scheduling Library for Java Swing: CalendarDrawEvent\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_CalendarDrawEvent_Members.htm\">CalendarDrawEvent<\/a> class, which provides data for the event gives us sufficient information to recognize the exact element that is being painted.<\/p>\n<p><strong>III. Tooltips Over Selected Resources<\/strong><\/p>\n<p>By default the <a title=\"MindFusion Scheduling Library for Java Swing: Calendar\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_awt_AwtCalendar_Members.htm\">Calendar<\/a> provides tooltips for items. The <a title=\"MindFusion Scheduling Library for Java Swing: ItemTooltipEvent\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_ItemTooltipEvent_Members.htm\">ItemTooltipEvent<\/a> provides more information about this. However, we want to show tooltips when the user hovers over cells that correspond to the elements that we&#8217;ve painted in section II. We can do that by using a MouseMotionListener and subscribing to the mouseMove event. Note that these are standard Java Swing events:<\/p>\n<pre>calendar.addMouseMotionListener(new MouseAdapter() {\n\tpublic void mouseMoved(MouseEvent e) {\n\t\tonCalendarMouseMoved(e);\n     }\n\t\t\n});<\/pre>\n<p>In the event handler method we use the <a title=\"MindFusion Scheduling Library for Java Swing: getResourceAt\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?M_com_mindfusion_scheduling_awt_AwtCalendar_getResourceAt_2_Int32_Int32.htm\">getResourceAt<\/a> method of the <a title=\"MindFusion Scheduling Library for Java Swing: Calendar\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jplanner\/index.htm?T_com_mindfusion_scheduling_awt_AwtCalendar_Members.htm\">Calendar<\/a> to learn the resource, over which the mouse is hovering. Then we use the id-s that we&#8217;ve assigned to our resources and check if the mouse is over the resources that we want to render tooltips:<\/p>\n<pre>private void onCalendarMouseMoved(MouseEvent e) {\n\t\t\n\t\tResource res = calendar.getResourceAt(e.getX(), e.getY());\n\t\t\n\t\tif (res.getId().equals(\"IdMike\") ||\n\t\t    res.getId().equals(\"IdChuck\") ||\n\t\t    res.getId().equals(\"IdTom\") ||\n\t\t    res.getId().equals(\"IdAlfredo\"))\n\t\t{\t\t\t\t\n\t\t\t   calendar.setToolTipText(res.getId());\t\t\t\t\n\t\t\t\t\n\t\t}\n\t\telse\n\t\t{\n\t\t     calendar.setToolTipText(\"\");\t\t    \n\t\t}\t\t\t\n\t\t\n}<\/pre>\n<p>When we detect that the mouse is over a resource that does not need to render tooltip, we set the tolltip text to be an empty string.<\/p>\n<p>With that we&#8217;ve finished customizing the resource sample. You can download the extended version from this link:<\/p>\n<p align=\"center\"><a href=\"https:\/\/mindfusion.dev\/samples\/java\/scheduler\/resourceTable1.zip\">Resource Table in Java Swing with Tooltips and Resource Coloring: Download<\/a><\/p>\n<p>You are welcome to post your questions and comments at the <a title=\"Java Swing Forum\" href=\"https:\/\/mindfusion.dev\/Forum\/YaBB.pl?board=scheduling_swing\">Online Forum for Scheduling for Java Swing<\/a>.<\/p>\n<p><em>About MindFusion Scheduling for Java Swing:<\/em> The library provides extensive feature-set for creating and customizing all sorts of calendars, task lists, time-management tables, resource allocation tables and other. It boasts various options for customizing appearance and numerous events for handling user actions. The distribution archive includes a lot of samples and detailed documentation. Learn more at <a href=\"https:\/\/mindfusion.dev\/java-scheduler.html\">https:\/\/mindfusion.dev\/java-scheduler.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post we will explain how to color cells and resources in the Resource view of the calendar based on a certain criteria. In our case we take the &#8220;Resource Table&#8221; sample from the Samples for the Java &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/custom-painting-of-resources-in-java-scheduler\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[535,74,104],"tags":[57,647,646,648,119,370],"class_list":["post-2289","post","type-post","status-publish","format-standard","hentry","category-java-swing","category-sample-code","category-scheduling-2","tag-java-swing","tag-resource-background","tag-resource-table","tag-sameple-code","tag-scheduler","tag-tooltips"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-AV","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=2289"}],"version-history":[{"count":4,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2289\/revisions"}],"predecessor-version":[{"id":2657,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2289\/revisions\/2657"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=2289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=2289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=2289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}