Personal tools

Views

Flex Scheduling Framework:ScheduleViewer Adding Navigation and Zooming Sample

From Adobe Labs

This sample shows how to add a navigation and a zooming tool. In order to achieve maximum flexibility both features are meant to be driven by external components that talk to an API of ScheduleViewer.

[edit]

Zooming

The ScheduleViewer's zoom property can be manipulated in order to achieve zooming. You could use, for example, an HSlider component that manipulates zoom on each change event as the ScheduleViewerSample3 example shows. A zoom value of 100 always shows the complete dataProvider on the currently visible canvas (no scrollbars should appear).

[edit]

Navigation

The ScheduleViewer's xPosition and yPosition properties can be manipulated in order to navigate along ScheduleViewer's content. You can use the scroll events of the Timeline component and the pixelScroll events of ScheduleViewer to connect both components and achieve navigation via Timeline.

ScheduleViewer offers APIs to navigate (and animate) to specific entries or times. See the gotoNow and gotoSelectedEntry methods of the sample below.

You can select single or multiple entries via the selectedItem and selectedItems property. To switch to the multiple selection mode, set the allowMultipleSelection to true.

Furthermore, in this sample, the background color of each schedule entry shall be data provider driven. To make this possible, we've specified another supplied entry renderer: com.adobe.ac.controls.scheduleClasses.renderers.ColoredGradientScheduleEntryRenderer. This entry renderer accepts only com.adobe.ac.controls.scheduleClasses.ColoredScheduleEntry objects. We've extracted the creation of schedule entries (of type ColoredScheduleEntry) into a separate class. Check out com.adobe.ac.scheduling.samples.ScheduleData in the downloadable source for more details.

<?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();" verticalGap="0">
   
   <mx:Script>
      <![CDATA[
         import com.adobe.ac.scheduling.samples.ScheduleData;
         import com.adobe.ac.controls.scheduleClasses.SimpleScheduleEntry;
         import com.adobe.ac.util.DateUtil;
         import mx.collections.ArrayCollection;
         import mx.controls.Alert;
         import mx.events.ScrollEvent;
         import mx.events.ScrollEventDirection;         
         
         [Bindable]         
         private var dataProvider : ArrayCollection;         
         [Bindable]
         private var startDate : Date;
         [Bindable]
         private var endDate : Date;
         [Bindable]
         private var zoom : Number;
         [Bindable]
         private var scheduleViewerWidth : Number = 600;
            
         private function onCreationComplete() : void
         {
            setTimeframe();
            initDataProvider();
         }
         
         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().createRandomColoredScheduleEntries( 200 );
         }
         
         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 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." );
         }
      ]]>
   </mx:Script>
   
   <controls:Timeline 
      id="timeline" 
      width="{ scheduleViewerWidth }" 
      startDate="{ startDate }" endDate="{ endDate }" 
      zoom="{ zoom }" 
      scroll="onScrollTimeline( event.position );" 
      />
         
   <controls:ScheduleViewer 
      id="scheduleViewer"  
      width="{ scheduleViewerWidth }" height="400" 
      dataProvider="{ dataProvider }"    
      startDate="{ startDate }" endDate="{ endDate }" 
      zoom="{ zoom }" 
      horizontalScrollPolicy="off" 
      entryRenderer="com.adobe.ac.controls.scheduleClasses.renderers.ColoredGradientScheduleEntryRenderer"       
      pixelScroll="onScrollScheduleViewer( 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="100"  
         snapInterval="1" liveDragging="true" 
         change="onZoom( zoomSlider.value );" 
         />
   </mx:HBox>   
         
</mx:Application>
Retrieved from "http://labs.adobe.com/wiki/index.php/Flex_Scheduling_Framework:ScheduleViewer_Adding_Navigation_and_Zooming_Sample"