PageRenderTime 30ms CodeModel.GetById 18ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/name/carter/mark/flex/project/mdoc/MSuperNodeProperties.as

http://transcriptstudio4isha.googlecode.com/
ActionScript | 220 lines | 175 code | 25 blank | 20 comment | 50 complexity | 67b2a81fd9b083b1af534ce732230438 MD5 | raw file
  1/*
  2   Transcript Studio for Isha Foundation: An XML based application that allows users to define 
  3   and store contextual metadata for contiguous sections within a text document. 
  4
  5   Copyright 2008 Mark Carter, Swami Kevala
  6
  7   This file is part of Transcript Studio for Isha Foundation.
  8
  9   Transcript Studio for Isha Foundation is free software: you can redistribute it and/or modify it 
 10   under the terms of the GNU General Public License as published by the Free Software 
 11   Foundation, either version 3 of the License, or (at your option) any later version.
 12
 13   Transcript Studio for Isha Foundation is distributed in the hope that it will be useful, but 
 14   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 15   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 16
 17   You should have received a copy of the GNU General Public License along with 
 18   Transcript Studio for Isha Foundation. If not, see http://www.gnu.org/licenses/.
 19*/
 20
 21package name.carter.mark.flex.project.mdoc
 22{
 23	import name.carter.mark.flex.util.XMLUtils;
 24	
 25	public class MSuperNodeProperties {
 26		public static const MARKUP_TYPE_TAG_NAME:String = "markupType";
 27		public static const MARKUP_CATEGORY_TAG_NAME:String = "markupCategory";
 28		public static const MARKUP_CATEGORY_SUGGESTION_TAG_NAME:String = "markupCategorySuggestion";
 29		public static const ADDITIONAL_CONCEPT_TAG_NAME:String = "concept";
 30		public static const SUMMARY_PROP_NAME:String = "summary";
 31		public static const NOTES_PROP_NAME:String = "notes";
 32		public static const RATING_PROP_NAME:String = "rating";
 33		public static const RATING_PROP_DEFAULT:int = -1;
 34		
 35		private var superNode:MSuperNode;
 36		
 37		public function MSuperNodeProperties(superNode:MSuperNode) {
 38			this.superNode = superNode;
 39		}
 40		
 41		public function overwrite(props:MSuperNodeProperties):void {
 42			markupTypeId = props.markupTypeId;
 43			markupCategoryId = props.markupCategoryId;
 44			markupCategorySuggestion = props.markupCategorySuggestion;
 45			additionalConcepts = props.additionalConcepts;
 46			summary = props.summary;
 47			notes = props.notes;
 48			rating = props.rating;
 49		}
 50		
 51		[Bindable]
 52		public function set markupTypeId(newValue:String):void {
 53			setSingletonTag(superNode, MARKUP_TYPE_TAG_NAME, newValue);
 54		}
 55		
 56		public function get markupTypeId():String {
 57			return getFirstTagValue(superNode, MARKUP_TYPE_TAG_NAME);
 58		}
 59		
 60		[Bindable]
 61		public function set markupCategoryId(newValue:String):void {
 62			setSingletonTag(superNode, MARKUP_CATEGORY_TAG_NAME, newValue);			
 63		}
 64		
 65		public function get markupCategoryId():String {
 66			return getFirstTagValue(superNode, MARKUP_CATEGORY_TAG_NAME);			
 67		}
 68		
 69		[Bindable]
 70		public function set markupCategorySuggestion(newValue:String):void {
 71			setSingletonTag(superNode, MARKUP_CATEGORY_SUGGESTION_TAG_NAME, newValue);			
 72		}
 73		
 74		public function get markupCategorySuggestion():String {
 75			return getFirstTagValueOrBlank(superNode, MARKUP_CATEGORY_SUGGESTION_TAG_NAME);			
 76		}
 77		
 78		[Bindable]
 79		public function set additionalConcepts(newValues:Array):void {
 80			setTags(superNode, ADDITIONAL_CONCEPT_TAG_NAME, newValues);
 81		}
 82		
 83		public function get additionalConcepts():Array {
 84			return superNode.getTagValues(ADDITIONAL_CONCEPT_TAG_NAME);
 85		}
 86		
 87		[Bindable]
 88		public function set summary(newValue:String):void {
 89			superNode.setElementProperty(SUMMARY_PROP_NAME, newValue);
 90		}
 91		
 92		public function get summary():String {
 93			return superNode.getElementPropertyValue(SUMMARY_PROP_NAME, "");
 94		}
 95		
 96		[Bindable]
 97		public function set notes(newValue:String):void {
 98			superNode.setElementProperty(NOTES_PROP_NAME, newValue);
 99		}
100		
101		public function get notes():String {
102			return superNode.getElementPropertyValue(NOTES_PROP_NAME, "");
103		}
104		
105		[Bindable]
106		public function set rating(newValue:int):void {
107			var strValue:String = newValue > RATING_PROP_DEFAULT ? newValue.toString() : null;
108			superNode.setProperty(RATING_PROP_NAME, strValue);
109		}
110		
111		public function get rating():int {
112			var ratingStr:String = superNode.getPropertyValue(RATING_PROP_NAME);
113			if (ratingStr == null) {
114				return RATING_PROP_DEFAULT;
115			}
116			else {
117				return new int(ratingStr);
118			}
119		}
120		
121		public function equals(obj:Object):Boolean {
122			if (obj == this) {
123				return true;
124			}
125			if (!(obj is MSuperNodeProperties)) {
126				return false;
127			}
128			var guest:MSuperNodeProperties = obj as MSuperNodeProperties;
129			if (this.markupTypeId != guest.markupTypeId) {
130				return false;
131			}
132			if (this.markupCategoryId != guest.markupCategoryId) {
133				return false;
134			}
135			if (this.markupCategorySuggestion != guest.markupCategorySuggestion) {
136				return false;
137			}
138			if (!arrayEquals(this.additionalConcepts, guest.additionalConcepts)) {
139				return false;
140			}
141			if (this.summary != guest.summary) {
142				return false;
143			}
144			if (this.notes != guest.notes) {
145				return false;
146			}
147			if (this.rating != guest.rating) {
148				return false;
149			}
150			return true;
151		}
152		
153		private static function arrayEquals(arr1:Array, arr2:Array):Boolean {
154			if (arr1 == arr2) {
155				return true;
156			}
157			if (arr1 == null || arr2 == null) {
158				return false;
159			}
160			// both non-null
161			if (arr1.length != arr2.length) {
162				return false;
163			}
164			for (var i:int = 0; i < arr1.length; i++) {
165				if (arr1[i] != arr2[i]) {
166					return false;
167				}
168			}
169			return true;
170		}
171		
172		private static function getFirstTagValue(node:TaggableMNode, tagType:String, throwException:Boolean = false):String {
173			var values:Array = node.getTagValues(tagType);
174			if (values.length == 0) {
175				if (throwException) {
176					throw new ArgumentError("Could not find tag type '" + tagType + "' for node: " + node);
177				}
178				else {
179					return null;
180				}
181			}
182			return values[0];
183		}
184		
185		private static function getFirstTagValueOrBlank(node:TaggableMNode, tagType:String):String {
186			var result:String = getFirstTagValue(node, tagType);
187			if (result == null) {
188				return "";
189			}
190			else {
191				return result;
192			}
193		}
194		
195		private static function setTags(taggable:TaggableMNode, type:String, values:Array):void {
196			taggable.removeAllTags(type);
197			if (values != null) {
198				for each (var value:* in values) {
199					if (value == null) {
200						continue;
201					}
202					var valueStr:String = value.toString();
203					if (valueStr != null && valueStr.length > 0) {
204						taggable.addTag(type, valueStr);
205					}
206				}
207			}
208		}
209		
210		private static function setSingletonTag(taggable:TaggableMNode, type:String, value:Object):void {
211			taggable.removeAllTags(type);
212			if (value != null) {
213				var valueStr:String = value.toString();
214				if (valueStr != null && valueStr.length > 0) {
215					taggable.addTag(type, valueStr);
216				}
217			}
218		}
219	}
220}