Personal tools

Views

Flex Scheduling Framework:ScheduleViewer Row Based Schedulers Sample

From Adobe Labs

This sample shows how you can synchronize a List control with ScheduleViewer.

The way to do this in the current release of the framework is not as elegant as we'd desire, so this is one are of the current framework that we'll revisit in future.

<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) 2006. Adobe Systems Incorporated.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
  * Neither the name of Adobe Systems Incorporated nor the names of its
    contributors may be used to endorse or promote products derived from this
    software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

@ignore
-->
<mx:Application 
   xmlns:mx="http://www.adobe.com/2006/mxml" 
   xmlns:controls="com.adobe.ac.controls.*" 
   creationComplete="onCreationComplete();" layout="horizontal" horizontalGap="0">
   
   <mx:Script>
      <![CDATA[
         import com.adobe.ac.scheduling.samples.ScheduleData;
         import com.adobe.ac.controls.scheduleClasses.SimpleScheduleEntry;
         import com.adobe.ac.controls.scheduleClasses.BackgroundItem;
         import com.adobe.ac.controls.scheduleClasses.LayoutUpdateEvent;
         import com.adobe.ac.util.DateUtil;
         import mx.collections.IList;
         import mx.collections.ArrayCollection;
         import mx.controls.Alert;
         import mx.events.ScrollEvent;
         import mx.events.ScrollEventDirection;         
         
         [Bindable]
         private var dataProvider : IList;
         [Bindable]
         private var rowDataProvider : IList;
         [Bindable]
         private var startDate : Date;
         [Bindable]
         private var endDate : Date;
         [Bindable]
         private var zoom : Number;
         [Bindable]
         private var scheduleViewerWidth : Number = 600;
         [Bindable]
         private var scheduleViewerHeight : Number = 400;
                     
         private function onCreationComplete() : void
         {
            setTimeframe();
            initDataProvider();
            initBackgroundColors();
            zoom = 250;
         }
         
         private function setTimeframe() : void
         {
            startDate = DateUtil.clearTime( new Date() );
            endDate = getEndDate( startDate );
         }
         
         private function getEndDate( startDate : Date ) : Date
         {
            var duration : Number = DateUtil.DAY_IN_MILLISECONDS * 1;
            var endDate : Date = new Date( startDate.getTime() + duration );
            return endDate;
         }
         
         private function initDataProvider() : void
         {
            dataProvider = new ScheduleData().createRowsOfSequentialColoredEntries( 150, 15 );            
            rowDataProvider = new ArrayCollection();
            for( var i : int = 0; i < dataProvider.length; i++ )
            {
               rowDataProvider.addItem( { label: "Row " + ( i + 1 ) } );         
            }      
         }
         
         private function onZoom( value : Number ) : void
         {
            zoom = value;
         }
         
         private function onScrollTimeline( position : Number ) : void
         {
            scheduleViewer.xPosition = position;
         }
         
         private function onScrollScheduleViewer( event : ScrollEvent ) : void
         {
            if( event.direction == ScrollEventDirection.HORIZONTAL )
            {
               timeline.xPosition = event.position;
            }
         }
         
         private function scrollList( event : LayoutUpdateEvent ) : void
         {
            if( event.direction == ScrollEventDirection.VERTICAL )
            {
               rowsList.verticalScrollPosition = event.position;      
            }
         }         
         
         private function gotoNow() : void
         {
            var time : Date = new Date();
            scheduleViewer.moveToTime( time.getTime() - startDate.getTime() );
         }
         
         private function gotoSelectedEntry() : void
         {
            if( scheduleViewer.selectedItem != null )
            {
               scheduleViewer.moveToEntry( scheduleViewer.selectedItem );
            }
            else
            {
               warnAboutNoSelection();
            }
         }
         
         private function warnAboutNoSelection() : void
         {
            Alert.show( "Please select an entry." );
         }
         
         private function initBackgroundColors() : void
         {
            var result : ArrayCollection = new ArrayCollection();
            
            var duration : Number = endDate.getTime() - startDate.getTime();
            var days : Number = duration / DateUtil.DAY_IN_MILLISECONDS;
            
            for( var i : int; i < days; i++ )
            {
               var currentDate : Number = DateUtil.DAY_IN_MILLISECONDS * i;
               var backgroundItem : BackgroundItem;
               backgroundItem = createBackgroundItem( "office closed", 0xD5D4D0, 
                                                   currentDate, 0, 0, 24, 0 );         
               result.addItem( backgroundItem );
               
               backgroundItem = createBackgroundItem( "generell office hours", 0xFFFFFF, 
                                                      currentDate, 9, 0, 17, 0 );
               result.addItem( backgroundItem );
               
               backgroundItem = createBackgroundItem( "lunchtime", 0xEEEDE9, 
                                                      currentDate, 12, 0, 13, 0 );
               result.addItem( backgroundItem );         
            }            
            scheduleViewer.backgroundItems = result;
         }
         
         private function createBackgroundItem( description : String, 
                                                color : Number, currentDate : Number,  
                                                startHours : Number, startMinutes : Number, 
                                                endHours : Number, endMinutes : Number ) : BackgroundItem
         {
            var backgroundItem : BackgroundItem = new BackgroundItem();
            backgroundItem.description = description;
            backgroundItem.color = color;
            backgroundItem.startDate = new Date( currentDate + createTime( startHours, startMinutes ) );
            backgroundItem.endDate = new Date( currentDate + createTime( endHours, endMinutes ) );
            return backgroundItem;   
         }
         
         private function createTime( hours : Number, minutes : Number ) : Number
         {
            var result : Number = (( hours * 60 ) + minutes ) * 60 * 1000;
            return result;
         }
      ]]>
   </mx:Script>

   <mx:VBox verticalGap="0">
      <mx:VBox height="{ timeline.height }" verticalGap="0">
         <mx:Label text="Rows"/>
      </mx:VBox>
      <mx:List 
         id="rowsList" width="60" height="{ scheduleViewerHeight }" 
         dataProvider="{ rowDataProvider }" 
         rowHeight="{ scheduleViewer.rowHeight }" 
         verticalScrollPolicy="off"/>
   </mx:VBox>
      
   <mx:VBox verticalGap="0">
      <controls:Timeline 
         id="timeline" 
         width="{ scheduleViewerWidth }" borderStyle="none"  
         startDate="{ startDate }" endDate="{ endDate }" 
         zoom="{ zoom }" 
         scroll="onScrollTimeline( event.position );" 
         />
      
      <controls:ScheduleViewer 
         id="scheduleViewer"  
         width="{ scheduleViewerWidth }" height="{ scheduleViewerHeight }" borderStyle="none"
         dataProvider="{ dataProvider }"    
         startDate="{ startDate }" endDate="{ endDate }" 
         zoom="{ zoom }" 
         horizontalScrollPolicy="off" 
         entryRenderer="com.adobe.ac.controls.scheduleClasses.renderers.ColoredGradientScheduleEntryRenderer" 
         entryLayout="com.adobe.ac.controls.scheduleClasses.layout.SimpleLayout" 
         pixelScroll="onScrollScheduleViewer( event );" itemScroll="scrollList( event )" 
         />   
      <mx:HBox width="{ scheduleViewerWidth }" paddingTop="6">      
         <mx:Label text="Goto:"/>
         <mx:Button label="Now" click="gotoNow();"/>
         <mx:Button label="Selected" click="gotoSelectedEntry();"/>
         <mx:Spacer width="100%"/>
         <mx:Label text="Zoom:"/>
         <mx:HSlider 
            id="zoomSlider" 
            minimum="0" maximum="1000" value="{ zoom }" 
            snapInterval="1" liveDragging="true" 
            change="onZoom( zoomSlider.value );" />
      </mx:HBox>
   </mx:VBox>
</mx:Application>
Retrieved from "http://labs.adobe.com/wiki/index.php/Flex_Scheduling_Framework:ScheduleViewer_Row_Based_Schedulers_Sample"