topical media & game development

talk show tell print

#springgraph-thesaurus.mx

#springgraph-thesaurus.mx [swf] flex


  <?xml version="1.0" encoding="utf-8"?>
  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:fc="http://www.adobe.com/2006/fc" 
      layout="absolute" backgroundGradientColors="[#334488, #8899cc]"
      creationComplete="startup()" 
      >
      
      <!--
  

//////////////////////////////////////////////////////////////////////////

// // Copyright (C) 2006 Adobe Macromedia Software LLC and its licensors. // All Rights Reserved. The following is Source Code and is subject to all // restrictions on such code as contained in the End User License Agreement // accompanying this product. //

//////////////////////////////////////////////////////////////////////////

--> <!-- Application for roaming through a thesaurus. This app was written in Flex 2, using FlexBuilder. Most of the work is done by the Roamer component, avaialble from http://mark-shepherd.com. The data for this app is a graph containing 2 kinds of objects that are linked to each other: A "Word" is simply a string of characters. A "Meaning" is a single concept, as represented by a main word, a part of speech (such as noun or verb) and a definition. In the graph, Words are linked to the various Meanings that the word has, and Meanings are linked to the various Words that express that meaning. Words are never linked to other Words, and Meanings are never linked to other meanings. The underlying thesaurus data comes from http://thesaurus.reference.com. We use http://www.dappit.com to screenscrape the data into a simple XML document; the template name is thesaurusreferencecom2. The script lookup.php fetches the data and relays it to this Flex UI; this script provides a way to work within the Flash Player's security sandbox restrictions; it doesn't process the data in any way. Written by Mark Shepherd, Dec 2006 --> <!-- The UI --> <!-- the Roamer covers the entire area --> <fc:Roamer id="roamer" bottom="0" left="0" top="0" right="0" viewFactory="{new ax_springgraph_thesaurus_ViewFactory()}" autoFit="true" maxDistanceFromCurrent="2" itemLimit="9999" showHistory="false" tidyHistory="true" dataProvider="{graph}"> <fc:removeItemEffect> <mx:Fade alphaFrom="1" alphaTo="0" duration="1000"/> <!-- todo: these fades don't work very well because we're using system fonts. should switch to embedded fonts for displayed text. --> </fc:removeItemEffect> <fc:addItemEffect> <mx:Fade alphaFrom="0" alphaTo="1" duration="1000"/> </fc:addItemEffect> </fc:Roamer> <!-- The rest of the UI --> <mx:Style> .dropDown {background-color: #b7c8d5;} </mx:Style> <mx:Label x="10" y="37" text="Type in a word, then click 'Look up'" id="help" color="#65ff8e"/> <mx:Label x="235" y="65" id="message" color="#fbff4c"/> <mx:Label text="thesaurus data from" click="navigateToURL(new URLRequest('http://thesaurus.com/'), '_blank')" toolTip="Visit thesaurus.com" rollOver="event.currentTarget.setStyle('color', 0xffffff)" rollOut="event.currentTarget.setStyle('color', 0xdddddd)" textAlign="right" color="0xdddddd" fontSize="9" right="10" bottom="24"/> <mx:Label x="179" y="65" text="Look up" fontWeight="bold" click="newWord(textinput.text); help.text = 'searching...'" toolTip="{'Display meanings and synonyns for &quot;' + textinput.text + '&quot;.'}" rollOver="event.currentTarget.setStyle('color', 0xffffff)" rollOut="event.currentTarget.setStyle('color', 0xdddddd)" textAlign="right" color="0xdddddd" fontSize="10"/> <mx:Label x="188" y="121" text="more..." fontWeight="bold" click="navigateToURL(new URLRequest('http://thesaurus.reference.com/browse/' + lastSearchWord), '_blank')" toolTip="{'See more synonyms for &quot;' + lastSearchWord + '&quot; at thesaurus.com'}" rollOver="event.currentTarget.setStyle('color', 0xffffff)" rollOut="event.currentTarget.setStyle('color', 0xdddddd)" textAlign="right" color="0xdddddd" fontSize="9"/> <mx:Label text="mark-shepherd.com" fontWeight="bold" click="navigateToURL(new URLRequest('http://mark-shepherd.com/'), '_blank')" toolTip="Visit mark-shepherd.com" rollOver="event.currentTarget.setStyle('color', 0xffffff)" rollOut="event.currentTarget.setStyle('color', 0xdddddd)" textAlign="left" color="0xdddddd" fontSize="10" bottom="10" left="10"/> <mx:Label text="thesaurus.com" click="navigateToURL(new URLRequest('http://thesaurus.com/'), '_blank')" toolTip="Visit thesaurus.com" rollOver="event.currentTarget.setStyle('color', 0xffffff)" rollOut="event.currentTarget.setStyle('color', 0xdddddd)" textAlign="right" color="0xdddddd" fontSize="11" right="10" bottom="10" fontWeight="bold"/> <mx:Label text="Thesaurus Roamer" click="doAbout()" toolTip="Click for more information about Thesaurus Roamer" rollOver="event.currentTarget.setStyle('color', 0xffffff)" rollOut="event.currentTarget.setStyle('color', 0xdddddd)" textAlign="left" color="0xdddddd" fontSize="16" left="10" fontWeight="bold" top="10" height="25"/> <mx:TextInput id="textinput" text="help" x="11" y="62" enter="newWord(textinput.text)" width="159" color="#ffffff" themeColor="#637bae" backgroundColor="#b7c8d5" backgroundAlpha="0.5"/> <mx:CheckBox x="12" y="122" label="Show History" selected="false" click="roamer.showHistory = event.currentTarget.selected;" fontSize="9" color="#dddddd" fontWeight="bold"/> <mx:ComboBox x="10" y="93" width="218" rowCount="{Math.min(roamer.history.length, 12)}" change="roamer.historyIndex = (roamer.history.length - 1) - event.currentTarget.selectedIndex" dataProvider="{roamer.history.slice().reverse()}" selectedIndex="{getHistoryIndex(roamer.history, roamer.historyIndex)}" dropdownStyleName="dropDown" color="#222233"/> <!-- fillColors="[#b7c8d5,#b7c8d5]" --> <mx:Image x="120" y="124" source="@Embed('springgraph-thesaurus-LeftButton.png')" click="roamer.back()" alpha="{getButtonAlpha(roamer.backOK)}" rollOver="event.currentTarget.alpha = roamer.backOK ? 1.0 : getButtonAlpha(roamer.backOK)" rollOut ="event.currentTarget.alpha = getButtonAlpha(roamer.backOK)"/> <mx:Image x="144" y="124" source="@Embed('springgraph-thesaurus-RightButton.png')" click="roamer.forward()" alpha="{getButtonAlpha(roamer.forwardOK)}" rollOver="event.currentTarget.alpha = roamer.forwardOK ? 1.0 : getButtonAlpha(roamer.forwardOK)" rollOut ="event.currentTarget.alpha = getButtonAlpha(roamer.forwardOK)"/> <!-- we use this component to access the thesaurus data. The data is provided by 'lookup.php', which takes a single URL parameter 'q', whose value is the word to lookup. --> <mx:HTTPService id="thesaurus" showBusyCursor="true" resultFormat="e4x" result="gotData(event)" url="http://mark-shepherd.com/thesaurus/lookup.php"/> <!-- url=<lookup.php>/> --> <mx:Script> <![CDATA[ import flash.net.navigateToURL; import mx.managers.SystemManager; import mx.managers.PopUpManager; import mx.core.IFlexDisplayObject; import com.adobe.flex.extras.controls.springgraph.Item; import com.adobe.flex.extras.controls.springgraph.Graph; import mx.rpc.events.ResultEvent; /* On startup, maybe we'll lookup a word, so the screen doesn't look too empty. */ private function startup(): void { newWord("image"); } /* Calculates the combobox index that corresponds to the history index. We need to do this because the history is displayed in reverse (newest first). */ private function getHistoryIndex(history: Array, historyIndex: int): int { return (history.length - 1) - historyIndex; } /* this gets called when the results of a lookup request are received */ private function gotData(event: ResultEvent): void { loadThesaurusData(event.currentTarget.lastResult); if(firstTime) { help.text = 'Double-click any item to see more synonyms.'; firstTime = false; } else { help.text = ""; } } /* we visually indicate the available/unavailable status of the back/forward buttons by using a reduced alpha to indicate not available. */ private function getButtonAlpha(available: Boolean): Number { return available ? 0.8 : 0.4; } /* loads a block of XML which contains the thesaurus lookup results. * For a sample of the XML, try http://www.dappit.com/RunDapp?dappName=thesaurusreferencecom2&v=1&applyToUrl=http%3A%2F%2Fthesaurus.reference.com%2Fbrowse%2Frock */ private function loadThesaurusData(data: XML): void { var word: String = data.word; var wordItem: ax_springgraph_thesaurus_Word = getWord(word); var item: Item; var count: int = 0; for each (var entry: XML in data..entry) { if(entry.mainEntry.toString() != "") { var meaningItem: ax_springgraph_thesaurus_Meaning = getMeaning(entry.mainEntry, entry.partOfSpeech, entry.definition); graph.link(wordItem, meaningItem); graph.link(meaningItem, getWord(entry.mainEntry)); graph.link(meaningItem, getWord(entry.definition)); var synonymArray: Array = entry.synonyms.toString().split(/ *, */); for(var i: int = 0; i < synonymArray.length; i++) { graph.link(meaningItem, getWord(synonymArray[i])); count++; } } } if(count > 0) { message.text = ""; roamer.currentItem = wordItem; } else { message.text = "'" + lastSearchWord + "' not found."; // start a timer to get rid of the message in a few seconds //message.alpha = 0.99; message.visible = true; messageTimer = new Timer(3000, 1); messageTimer.addEventListener(TimerEvent.TIMER_COMPLETE, messageTimerExpired); messageTimer.start(); } } /* called when the message timer expires */ public function messageTimerExpired(evt:TimerEvent):void { message.visible = false; //messageHider.play(); } /* Find a Meaning object corresponding to the given meaning. If we've already created a Meaning object for that meaning, we return it. Otherwise we create a new Meaning object. */ private function getMeaning(word: String, partOfSpeech: String, definition: String): ax_springgraph_thesaurus_Meaning { var key: String = word + "/" + partOfSpeech + "/" + definition; if(!meanings.hasOwnProperty(key)) { var newMeaning: ax_springgraph_thesaurus_Meaning = new ax_springgraph_thesaurus_Meaning(word, partOfSpeech, definition); meanings[key] = newMeaning; graph.add(newMeaning); } return meanings[key]; } /* Find a Word object corresponding to the given word. If we've already created a Word object for that word, we return it. Otherwise we create a new Word object. */ private function getWord(word: String): ax_springgraph_thesaurus_Word { word = word.replace("*", ""); if(!words.hasOwnProperty(word)) { var newWord: ax_springgraph_thesaurus_Word = new ax_springgraph_thesaurus_Word(word); words[word] = newWord; graph.add(newWord); } return words[word]; } // makes the selected word the current word. private function newWord(word: String): void { //roamer.resetHistory(); textinput.text = ""; lastSearchWord = word; thesaurus.send({q: word}); } // Make the selected word the current work. Asks the server for info about the word. public function showWord(word: String): void { hideHelp(); lastSearchWord = word; thesaurus.send({q: word}); } // Make the selected item be the current item. public function setCurrentItem(item: Item): void { message.text = ""; lastSearchWord = (item is ax_springgraph_thesaurus_Word) ? (item as ax_springgraph_thesaurus_Word).id : (item as ax_springgraph_thesaurus_Meaning).word; roamer.currentItem = item; hideHelp(); }
Returns the one-and-only instance of Thesaurus (i.e. the main application)

  
              public static function get instance(): springgraph_thesaurus {
                  return Application.application as springgraph_thesaurus;
              }
              
              
              /* for use by itemRenderers, determines their glow filters */
              public function calcFilters(currentItem: Item, item: Item): Array {
                  if(roamer.currentItem == item) {
                      return [new GlowFilter(0xffffff, 0.6, 10, 10, 2, 1)];
                  } else if(roamer.hasBeenCurrentItem(item)) {
                      return [new GlowFilter(0x0000FF, 0.3, 6, 6, 2, 1)];
                  } else {
                      return null;
                  }
              }
              
              /* for use by itemRenderers, determines their alpha */
              public function getItemAlpha(visibleHistoryItems: Object, id: String): Number {
                  return visibleHistoryItems.hasOwnProperty(id) ? 0.7 : 1.0;
              }
  
              /* for use by itemRenderers, determines their text color */
              public function getColor(item: Object, d: Object): int {
                  if (item == d)
                      return 0x0000ff;
                  else
                      return 0x222222;
              }
              
              /* for use by itemRenderers, determines their text weight */
              public function getWeight(item: Object, d: Object): String {
                  if (item == d)
                      return "bold";
                  else
                      return "bold"; //"normal";
              }
  
              /* turns off the help. called when the user no longer needs help. */
              private function hideHelp(): void {
                  help.visible = false;
              }
  
              
pops up the about box

  
              private function doAbout(): void {
                  var aboutWindow: IFlexDisplayObject = new springgraph_thesaurus_AboutWindow();
                  PopUpManager.addPopUp(aboutWindow, this, true);        
                  PopUpManager.centerPopUp(aboutWindow);        
              }
              
              [Bindable]
              // the graph of Words and Meanings
              public var graph: Graph = new Graph();
              
              // all the Meanings we've come across so far
              private var meanings: Object = new Object();
              
              // all the Words we've come across so far.
              private var words: Object = new Object();
              
              // the word of the most recent search
              [Bindable]
              private var lastSearchWord: String = "roaming";
              
              private var messageTimer: Timer;
              private var firstTime: Boolean = true;
          ]]>
      </mx:Script>
  
      <!--<mx:Fade id="messageHider2" alphaFrom="0.99" alphaTo="0" duration="1000" target="{message}"/>
      <mx:Move id="messageHider" xTo="999" duration="1000" target="{message}"/>-->
  
  </mx:Application>
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.