Guest User

ValidatedComboBox

a guest
Feb 2nd, 2019
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.51 KB | None | 0 0
  1. package Classes.Input
  2. {
  3. import flash.events.DataEvent;
  4. import flash.events.Event;
  5. import flash.events.FocusEvent;
  6. import flash.events.KeyboardEvent;
  7. import flash.events.MouseEvent;
  8. import flash.ui.Keyboard;
  9. import flash.utils.Dictionary;
  10.  
  11. import mx.controls.ComboBox;
  12. import mx.events.ListEvent;
  13.  
  14. /**
  15. * postValidate Code to execute after validateData method completes.
  16. *
  17. **/
  18.  
  19. [Event(name="postValidate", type="flash.events.DataEvent")]
  20.  
  21. /**
  22. * Create an extended version of the ComboBox that allows the definition of invalid indexes and
  23. * multiple keystroke selection.
  24. *
  25. * <pre>
  26. * The developer defines one or more invalid indexes in a comma separated list using the property badIndexes.
  27. * Most commonly the developer would specify "0" which is the first element in the list.
  28. *
  29. * New properties are;
  30. * badIndexes - A string that allows the developer to define a comma separated list of invalid indexes.
  31. * isVald - A boolean that defines whether the field is valid.
  32. *
  33. * New Methods are;
  34. * validateData() - This method checks if the data is valid and returns true if valid, false if invalid.
  35. *
  36. * Enhanced Properties;
  37. * value - When the property value is set, the combobox will attempt to find the appropriate value in the
  38. * dataprovider, and set the selectedIndex to that item. It will set the value to -1 if not found.
  39. *
  40. * When the user types it searches all the characters in the LABEL not just the first character.
  41. *
  42. * </pre>
  43. */
  44. public class ValidatedComboBox extends ComboBox
  45. {
  46. /** Bad indexes - A comma separated list of invalid indexes. */
  47. private var _badIndexes:String = "";
  48.  
  49. /** Bad Data - A comma separated list of invalid Data. */
  50. private var _badData:String = "";
  51.  
  52. /** Has this field passed validation */
  53. private var _isValid:Boolean = true;
  54.  
  55. /** value */
  56. private var _value:Object;
  57.  
  58. /** should we validate data */
  59. private var _doValidateData:Boolean = true;
  60.  
  61. /** promptLabel */
  62. private var _promptLabel:String;
  63.  
  64.  
  65. /** dataType */
  66. private var _dataType:String = "string";
  67.  
  68. /** dataField */
  69. private var _dataField:String = "DATA";
  70.  
  71. /** toolTipField */
  72. private var _toolTipField:String = "";
  73.  
  74. /** Default value A literal that represents the value that will replace the "value"
  75. * property when the method setDefault is executed */
  76. private var _defaultValue:String = "";
  77.  
  78. private var _eventHandler:Function = this["checkData"];
  79.  
  80. private var _tabOut:Function = null;
  81.  
  82. private var _focusOutTab:Boolean = false;
  83.  
  84. private var _typedText:String = "";
  85.  
  86. public function ValidatedComboBox()
  87. {
  88. //TODO: implement function
  89. super();
  90. this.addEventListener(Event.CHANGE,_eventHandler,false,0,true)
  91. }
  92.  
  93. /**
  94. * badIndexes- A comma seperated list of indexes that will be considered invalid.
  95. *
  96. * @return the specified bad indexes
  97. */
  98. [Inspectable( type="String" , defaultValue="" )]
  99. public function get badIndexes():String
  100. {
  101. return this._badIndexes;
  102. }
  103. /**
  104. * Sets the the specified bad indexes
  105. */
  106. public function set badIndexes( badIndexes:String ):void
  107. {
  108. this._badIndexes = badIndexes;
  109. if (_badIndexes.length < 1)
  110. {
  111. errorString = "";
  112. _isValid = true;
  113. }
  114. }
  115.  
  116. /**
  117. * badData- A comma seperated list of Data that will be considered invalid.
  118. *
  119. * @return the specified bad Data
  120. */
  121. [Inspectable( type="String" , defaultValue="" )]
  122. public function get badData():String
  123. {
  124. return this._badData;
  125. }
  126. /**
  127. * Sets the the specified bad Data
  128. */
  129. public function set badData( badData:String ):void
  130. {
  131. this._badData = badData;
  132. if (_badData.length < 1)
  133. {
  134. errorString = "";
  135. _isValid = true;
  136. }
  137. }
  138.  
  139. /**
  140. * promptLabel - Specify a human readable label for the field.
  141. *
  142. * @return the specified promptLabel
  143. */
  144.  
  145. [Inspectable( type="String" , defaultValue="" )]
  146. public function get promptLabel():String
  147. {
  148. return this._promptLabel;
  149. }
  150. /**
  151. * Sets the the specified promptLabel
  152. */
  153. public function set promptLabel( promptLabel:String ):void
  154. {
  155. this._promptLabel = promptLabel;
  156. }
  157.  
  158. /**
  159. * dataType - specify whether to use numeric or string matching when setting
  160. * the value of this field.
  161. *
  162. * <p>
  163. * Use this field when the drop down's data is a number. Particularly useful if
  164. * the current field in the database is string and can contain leading zeroes.
  165. * </p>
  166. *
  167. * @return the specified validate data flag.
  168. */
  169. [Inspectable( type="String" , defaultValue=false, enumeration="string,number" )]
  170. public function get dataType():String
  171. {
  172. return this._dataType;
  173. }
  174.  
  175. /**
  176. * Sets the specified validate data flag.
  177. */
  178. public function set dataType( dataType:String ):void
  179. {
  180. this._dataType = dataType;
  181. }
  182.  
  183. /**
  184. * doValidateData - specify the whether this field should be validated. Default is true.
  185. *
  186. * @return the specified validate data flag.
  187. */
  188. [Inspectable( type="Boolean" , defaultValue=false, enumeration="true,false" )]
  189. public function get doValidateData():Boolean
  190. {
  191. return this._doValidateData;
  192. }
  193.  
  194. /**
  195. * Sets the specified validate data flag.
  196. */
  197. public function set doValidateData( doValidateData:Boolean ):void
  198. {
  199. this._doValidateData = doValidateData;
  200. }
  201.  
  202. /**
  203. * toolTipField - Specify a field in the dataProvider whose value will be used as the
  204. * to populate the toolTip.
  205. *
  206. * @return the specified toolTipField
  207. */
  208. [Inspectable( type="String" , defaultValue="" )]
  209. public function get toolTipField():String
  210. {
  211. return this._toolTipField;
  212. }
  213. /**
  214. * Sets the the specified toolTipField
  215. */
  216. public function set toolTipField( toolTipField:String ):void
  217. {
  218. this._toolTipField = toolTipField;
  219. }
  220.  
  221. /**
  222. * defaultValue - allows the specification of a default value for a field.
  223. * To set the current text/value properties use the the setDefault() method.
  224. *
  225. * @return the specified defaultValue value
  226. */
  227. [Inspectable( type="String" , defaultValue="" )]
  228. public function get defaultValue():String
  229. {
  230. return this._defaultValue;
  231. }
  232. /**
  233. * Sets the specified Default value
  234. */
  235. public function set defaultValue( defaultValue:String ):void
  236. {
  237. this._defaultValue = defaultValue;
  238. var event:Event;
  239. }
  240.  
  241. /**
  242. * dataField - the field to be used as the data field from the dataProvider.
  243. *
  244. * The value of the field defined here is what the value is set to based
  245. * on the selection.
  246. *
  247. * @return the specified dataField
  248. */
  249. [Inspectable( type="String" , defaultValue="DATA" )]
  250. public function get dataField():String
  251. {
  252. return this._dataField;
  253. }
  254. /**
  255. * Sets the the specified dataField
  256. */
  257. public function set dataField( dataField:String ):void
  258. {
  259. this._dataField = dataField;
  260. }
  261.  
  262. /**
  263. * tabOut - A function to execute if the tabkey is pressed..
  264. *
  265. * @return the specified tabOut Function
  266. */
  267. [Inspectable( type="Function" , defaultValue="" )]
  268. public function get tabOut():Function
  269. {
  270. return this._tabOut;
  271. }
  272. /**
  273. * Sets the the specified tabOut
  274. */
  275. public function set tabOut( tabOut:Function ):void
  276. {
  277. this._tabOut = tabOut;
  278. }
  279.  
  280. /**
  281. * focusOutTab - A boolean defining if the last focus out was a tab.
  282. *
  283. * @return focusOutTab
  284. */
  285. public function get focusOutTab():Boolean
  286. {
  287. return this._focusOutTab;
  288. }
  289. /**
  290. * Sets the the specified focusOutTab
  291. */
  292. public function set focusOutTab( focusOutTab:Boolean ):void
  293. {
  294. this._focusOutTab = focusOutTab;
  295. }
  296.  
  297. /**
  298. * isValid - Returns a boolean that defines whether the current value is valid.
  299. */
  300.  
  301. public function get isValid():Boolean
  302. {
  303. return _isValid;
  304. }
  305.  
  306. override public function get value():Object
  307. {
  308. return _value;
  309. }
  310.  
  311. override public function set dataProvider(dpValue:Object):void
  312. {
  313. var objCurrentValue:Object = _value;
  314. super.dataProvider = dpValue;
  315. if (this.hasOwnProperty("dropdown"))
  316. if (this.dropdown != null)this.dropdown.dataProvider = dpValue;
  317. _value = objCurrentValue;
  318. if (dpValue != null)
  319. {
  320. if (dpValue.length > 0)
  321. {
  322. if (_value != null)
  323. {
  324. if(_value.toString().length > 0) setSelectedItem(_value);
  325. }
  326. else
  327. {
  328. if (this.selectedItem != null) _value = this.selectedItem[dataField];
  329. }
  330. if (this.selectedIndex == -1)
  331. {
  332. this.selectedIndex = 0;
  333. _value = this.selectedItem[dataField];
  334. if (this.selectedItem != null)
  335. if (this.selectedItem.hasOwnProperty(dataField))
  336. {
  337. _value = this.selectedItem[dataField];
  338. dispatchEvent( new ListEvent( ListEvent.CHANGE ) );
  339. }
  340. }
  341. }
  342. }
  343. dpValue = null;
  344. objCurrentValue = null;
  345. }
  346.  
  347. override public function set selectedIndex(intValue:int):void
  348. {
  349. // if (this.hasOwnProperty("id")) trace("id=" + this.id + " set intValue=" + intValue);
  350. super.selectedIndex = intValue;
  351. if (intValue > -1)
  352. {
  353. if (this.selectedItem != null)
  354. {
  355. if (this.selectedItem.hasOwnProperty(dataField))
  356. {
  357. _value = this.selectedItem[dataField];
  358. }
  359. }
  360. }
  361. }
  362.  
  363. /**
  364. * Sets the the specified value based on the item selected.
  365. */
  366.  
  367. public function set value(value:Object): void
  368. {
  369. // if (this.hasOwnProperty("id")) trace("id=" + this.id + " set value=" + value);
  370. // trace("ValidatedComboBox-" + this.id + "Set value=" + value);
  371. this.selectedIndex = 0;
  372. if (value != null)
  373. {
  374. setSelectedItem(value);
  375. _value = value;
  376. }
  377. else
  378. {
  379. _value = "";
  380. }
  381. if (this.selectedIndex > -1)
  382. {
  383. if (this.selectedItem != null)
  384. if (this.selectedItem.hasOwnProperty(dataField))
  385. {
  386. _value = this.selectedItem[dataField];
  387. }
  388. }
  389. }
  390.  
  391. private function checkData(event:Event):void
  392. {
  393. this.removeEventListener(Event.CHANGE,_eventHandler)
  394. if (this.selectedItem)
  395. {
  396. this.
  397. _value = this.selectedItem[dataField];
  398. validateData();
  399. }
  400. this.addEventListener(Event.CHANGE,_eventHandler,false,0,true)
  401. }
  402.  
  403. /**
  404. * setDefault - Method that will replace the value of the value property with the
  405. * value of the defaultValue property.
  406. */
  407.  
  408. public function setDefault():void
  409. {
  410. _value = _defaultValue;
  411. setSelectedItem(_value);
  412. }
  413.  
  414. /**
  415. * getDefault - Method that will return the value of the defaultValue property.
  416. *
  417. * This method returns the defaultValue it exists to provide a consistenet api
  418. * between all "validated" components.
  419. */
  420.  
  421. public function getDefault():String
  422. {
  423. return _defaultValue;
  424. }
  425.  
  426. /**
  427. * validateData - Method that will validate the data that has been entered.
  428. */
  429.  
  430. public function validateData():Boolean
  431. {
  432. if (_badIndexes.length == 0 && _badData.length == 0)
  433. {
  434. this.dispatchEvent(new DataEvent("postValidate"));
  435. return true;
  436. }
  437. var aryIndexesTemp:Array = _badIndexes.split(",");
  438. if (_doValidateData)
  439. {
  440. _isValid = ((aryIndexesTemp.indexOf(this.selectedIndex.toString()) == -1) &&
  441. (this.selectedIndex > -1));
  442. if (_isValid && _badData.length > 0)
  443. {
  444. var aryDataTemp:Array = _badData.split(",");
  445. if (this.selectedIndex > -1) _isValid = (aryDataTemp.indexOf(this.dataProvider[this.selectedIndex][dataField].toString()) == -1);
  446. }
  447. if (!_isValid)
  448. {
  449. this.errorString = '"' + this.selectedLabel + '" is an invalid choice. ' +
  450. this.selectedLabel.toString();
  451. }
  452. else
  453. {
  454. this.errorString = "";
  455. }
  456. }
  457. this.dispatchEvent(new DataEvent("postValidate"));
  458. return _isValid;
  459. }
  460.  
  461.  
  462. private function setSelectedItem(strFindItem:Object):void
  463. {
  464. var strDataItem:String = "";
  465. if (this.dataProvider != null)
  466. {
  467. this.selectedIndex = -1;
  468. if (_dataType == "number")
  469. {
  470. strFindItem = String(parseInt(strFindItem.toString(),10));
  471. }
  472. for (var i:int=0;i<this.dataProvider.length;i++)
  473. {
  474. if (this.dataProvider[i] != null)
  475. {
  476. if (_dataType == "number")
  477. {
  478. strDataItem = String(parseInt(this.dataProvider[i][dataField].toString(),10));
  479. }
  480. else
  481. {
  482. strDataItem = this.dataProvider[i][dataField].toString();
  483. }
  484. if (strDataItem == strFindItem && strDataItem.length == strFindItem.toString().length)
  485. {
  486. this.selectedIndex = i;
  487. if (_toolTipField.length > 0)
  488. {
  489. this.toolTip = this.dataProvider[i][_toolTipField]
  490. }
  491. // dispatchEvent( new ListEvent( ListEvent.CHANGE ) );
  492. break;
  493. }
  494. }
  495. }
  496. }
  497. }
  498.  
  499. override protected function focusOutHandler(event:FocusEvent):void
  500. {
  501. super.focusOutHandler(event);
  502. _typedText = "";
  503. validateData()
  504. }
  505.  
  506. override protected function focusInHandler(event:FocusEvent):void
  507. {
  508. this.removeEventListener(KeyboardEvent.KEY_DOWN,this.keyDownHandler);
  509. super.focusInHandler(event);
  510. _typedText = "";
  511. _focusOutTab = false;
  512. this.addEventListener(KeyboardEvent.KEY_DOWN,this.keyDownHandler,false,0,true);
  513. }
  514.  
  515. override protected function textInput_changeHandler(event:Event):void
  516. {
  517. _typedText += this.textInput.text;
  518. // trace('_typedText=' + _typedText);
  519. if (!findFirstItem(_typedText))
  520. {
  521. _typedText = _typedText.substr(0,_typedText.length -1);
  522. findFirstItem(_typedText);
  523. }
  524. // this.dispatchEvent(new Event(Event.CHANGE,true));
  525. }
  526. override protected function keyDownHandler(event:KeyboardEvent):void
  527. {
  528. // trace("event="+event.toString());
  529. this.removeEventListener(KeyboardEvent.KEY_DOWN,this.keyDownHandler);
  530. event.preventDefault();
  531. if (event.keyCode == Keyboard.TAB)_focusOutTab = true;
  532. if ((event.keyCode == Keyboard.TAB && _tabOut == null) || event.keyCode == Keyboard.ENTER) return;
  533. if (event.keyCode == Keyboard.TAB)
  534. {
  535. _tabOut();
  536. }
  537. if(!event.ctrlKey)
  538. {
  539. if (event.keyCode == Keyboard.BACKSPACE || event.keyCode == Keyboard.DELETE)
  540. {
  541. _typedText = _typedText.substr(0,_typedText.length -1);
  542. findFirstItem(_typedText);
  543. }
  544. if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.RIGHT)
  545. {
  546. _typedText = "";
  547. if (this.dropdown.selectedIndex < this.dataProvider.length - 1)
  548. {
  549. this.selectedIndex++;
  550. this.dropdown.selectedIndex++;
  551. this.dropdown.scrollToIndex(this.dropdown.selectedIndex);
  552. _value = this.selectedItem[dataField];
  553. }
  554. }
  555. if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.LEFT)
  556. {
  557. _typedText = "";
  558. if (this.dropdown.selectedIndex > 0)
  559. {
  560. this.selectedIndex--;
  561. this.dropdown.selectedIndex--;
  562. this.dropdown.scrollToIndex(this.dropdown.selectedIndex);
  563. _value = this.selectedItem[dataField];
  564. }
  565. }
  566. if ((event.charCode > 31) && (event.charCode < 128))
  567. {
  568. _typedText += String.fromCharCode(event.charCode);
  569. if (!findFirstItem(_typedText))
  570. {
  571. _typedText = _typedText.substr(0,_typedText.length -1);
  572. }
  573. }
  574. }
  575. // trace("_typedText=" + _typedText);
  576. this.addEventListener(KeyboardEvent.KEY_DOWN,this.keyDownHandler,false,0,true);
  577. // super.keyDownHandler(event);
  578. }
  579.  
  580. private function findFirstItem(strFindItem:String):Boolean
  581. {
  582. if (this.dataProvider != null)
  583. {
  584. if (strFindItem.length == 0)
  585. {
  586. this.selectedIndex = 0;
  587. this.dropdown.selectedIndex = 0;
  588. this.dropdown.scrollToIndex(0);
  589. _value = this.selectedItem[dataField];
  590. dispatchEvent( new ListEvent( ListEvent.CHANGE ) );
  591. return true;
  592. }
  593. this.dispatchEvent(new MouseEvent("mouseOut"));
  594. for (var i:int=0;i<this.dataProvider.length;i++)
  595. {
  596. if (this.dataProvider[i][this.labelField].toString().substr(0,strFindItem.length).toUpperCase() == strFindItem.toUpperCase())
  597. {
  598. this.selectedIndex = i;
  599. this.dropdown.selectedIndex = i;
  600. this.dropdown.scrollToIndex(i);
  601. _value = this.selectedItem[dataField];
  602. if (_toolTipField.length > 0)
  603. {
  604. this.toolTip = this.dataProvider[i][_toolTipField]
  605. this.dispatchEvent(new MouseEvent("mouseOver"));
  606. }
  607. dispatchEvent( new ListEvent( ListEvent.CHANGE ) );
  608. return true;
  609. }
  610. }
  611. }
  612. return false;
  613. }
  614.  
  615. }
  616. }
Add Comment
Please, Sign In to add comment