Menu

[r9]: / Tools / Doc-Tool / DocumentsViewController.itcl  Maximize  Restore  History

Download this file

339 lines (305 with data), 10.2 kB

import UIViewController
import UIAnimator
import UIFileListController
import XMLParser
import XMLElement
import provide DocumentsViewController

itcl::class DocumentsViewController {

	inherit UIViewController

	protected variable messageLabel
	protected variable animator
	protected variable filelist
	protected variable workfile
	protected variable xmlTree
	protected variable currentProperty
	protected variable currentOMethod
	protected variable currentCMethod

	protected variable editArea

	protected variable workfileL
	protected variable versionL
	protected variable versionT
	protected variable creatorL
	protected variable creatorT
	protected variable modifierL
	protected variable modifierT
	protected variable classnameL
	protected variable classnameT
	protected variable inheritanceL
	protected variable inheritanceT
	protected variable purposeL
	protected variable purposeT

	protected variable propertyLB
	protected variable propertyL
	protected variable propNameT
	protected variable propTypeT
	protected variable propDescT
	protected variable rbRW
	protected variable rbRO
	protected variable rbRT
	protected variable rbAS

	protected variable cMethodsLB
	protected variable oMethodsLB

	method setXmlTree: {Value}
	method xmlTree {}
	method setWorkfile: {Value}
	method workfile {}

	constructor {} {
		set currentProperty nil
		set currentOMethod nil
		set currentCMethod nil
		set workfile nil
		set xmlTree nil
		set animator [alloc UIAnimator]
		$animator setDuration: 2

		set varView [$this initWithXMLFile: DocumentsViewController.ixml]
		foreach {VAR VIEW} $varView {set $VAR $VIEW}

		$propDescT setTextString: ""
		set [$rbRT group] -1
		set [$rbRO group] -1

	}

	destructor {
		puts "DocumentsViewController destroyed"
		$xmlTree release
		$animator release
	}

	method selectedFile: {Controller} {
		$this __writeCurrentWorkfile

		set currentProperty nil
		set currentOMethod nil
		set currentCMethod nil
		$propDescT setTextString: ""
		set [$rbRT group] -1
		set [$rbRO group] -1
		$propNameT setTextString: ""
		$propTypeT setTextString: ""

		set workfile [$Controller selectedFile]
		$workfileL setTextString: [file tail $workfile]
		set if1 [open $workfile]
		set contents [read $if1]
		close $if1
		set parser [alloc XMLParser]
		$parser parseXML: $contents
		$this setXmlTree: [$parser rootXMLElement]
		$parser release
		if {$xmlTree == {nil}} {
			$this __createNewDocument
		}
		$this makeChildIfNotThere: creator
		$this makeChildIfNotThere: modifier
		$this makeChildIfNotThere: version
		$this makeChildIfNotThere: nameOfClass
		$this makeChildIfNotThere: purpose
		$this makeChildIfNotThere: inheritance
		$this makeChildIfNotThere: properties
		$this makeChildIfNotThere: classMethods
		$this makeChildIfNotThere: objectMethods

		$creatorT setTextString: [[$xmlTree childNamed: creator] text]
		$modifierT setTextString: [[$xmlTree childNamed: modifier] text]
		$versionT setTextString: [[$xmlTree childNamed: version] text]
		$classnameT setTextString: [[$xmlTree childNamed: nameOfClass] text]
		$purposeT setTextString: [[$xmlTree childNamed: purpose] text]
		$inheritanceT setTextString: [[$xmlTree childNamed: inheritance] text]

		$propertyLB reloadData
	}


	method __currentPropertyToUI {} {
		$propNameT setTextString: [[$currentProperty childNamed: name] text]
		$propTypeT setTextString: [[$currentProperty childNamed: type] text]
		$propDescT setTextString: [[$currentProperty childNamed: description] text]
		if {[[$currentProperty childNamed: assign] text] != {nil} && [[$currentProperty childNamed: assign] text] != ""} {
			set [$rbRT group] [[$currentProperty childNamed: assign] text]
		} else {
			set [$rbRT group] -1
		}
		if {[[$currentProperty childNamed: access] text] != {nil} && [[$currentProperty childNamed: access] text] != ""} {
			set [$rbRW group] [[$currentProperty childNamed: access] text]
		} else {
			set [$rbRW group] -1
		}
		if {[$propNameT textString] == {nil}} {$propNameT setTextString: ""}
		if {[$propTypeT textString] == {nil}} {$propTypeT setTextString: ""}
		if {[$propDescT textString] == {nil}} {$propDescT setTextString: ""}
	}
	method __uiToCurrentProperty {} {
		$this propNameChanged: $propNameT
		$this propTypeChanged: $propTypeT
		$this propDescChanged: $propDescT
		$this propAssiChanged: $rbRT
		$this propAcceChanged: $rbRW
	}
	method __uiToXML {} {
		$this __uiToCurrentProperty
		$this versionChanged: $versionT
		$this creatorChanged: $creatorT
		$this modifierChanged: $modifierT
		$this inheritanceChanged: $inheritanceT
		$this classnameChanged: $classnameT
		$this purposeChanged: $purposeT
	}

	method propNameChanged: {TF} {
		[$currentProperty childNamed: name] setText: [$TF textString]
		$propertyLB reloadData
	}
	method propTypeChanged: {TF} {
		[$currentProperty childNamed: type] setText: [$TF textString]
		$propertyLB reloadData
	}
	method propDescChanged: {TF} {
		[$currentProperty childNamed: description] setText: [$TF textString]
	}
	method propAssiChanged: {RBG} {
		[$currentProperty childNamed: assign] setText: [set [$RBG group]]
	}
	method propAcceChanged: {RBG} {
		[$currentProperty childNamed: access] setText: [set [$RBG group]]
	}


	method versionChanged: {TF} {
		[$xmlTree childNamed: "version"] setText: [$TF textString]
	}
	method creatorChanged: {TF} {
		[$xmlTree childNamed: "creator"] setText: [$TF textString]
	}
	method modifierChanged: {TF} {
		[$xmlTree childNamed: "modifier"] setText: [$TF textString]
	}
	method inheritanceChanged: {TF} {
		[$xmlTree childNamed: "inheritance"] setText: [$TF textString]
	}
	method classnameChanged: {TF} {
		[$xmlTree childNamed: "nameOfClass"] setText: [$TF textString]
	}
	method purposeChanged: {TE} {
		[$xmlTree childNamed: "purpose"] setText: [$TE textString]
	}
	method listForListPicker: {Picker} {
		if {$Picker == $propertyLB} {
			set names [$xmlTree elementsWithPath: "properties/property/name"]
			set proplist {}
			foreach name $names {
				if {$name != {nil}} {
					set str "([[$name siblingNamed: type] text])"
					append str [$name text]
					lappend proplist $str
				}
			}
			return $proplist
		}
		return {}
	}
	method listPickerSelected: {Picker} {
		set index [$Picker selectedIndex]
		if {$Picker == $propertyLB} {
			set i 0
			$this __uiToCurrentProperty
			set element [[$xmlTree childNamed: properties] childNamed: property]
			while {$i < $index} {
				incr i
				set element [$element sibling]
			}
			set currentProperty $element
			$this __makePropertyHierarchy: $element
			$this __currentPropertyToUI
			focus [$propNameT widget]
		}
	}
	method addProperty: {Sender} {
		puts "adding new property"
		set propEl [$xmlTree childNamed: properties]
		if {[$propEl child] == {nil}} {
			$this makeElement:childOf: [$this elementWithName: property] $propEl
			set propEl [$propEl lastChild]
		} else {
			set propEl [$propEl lastChild]
			$this makeElement:siblingOf: [$this elementWithName: property] $propEl
			set propEl [$propEl lastSibling]
		}
		$this makeElement:childOf: [$this elementWithName:text:attributes: name "property" {}] $propEl 
		set currentProperty [$propEl lastSibling]
		$this __makePropertyHierarchy: $currentProperty
		$propertyLB reloadData
		$this __currentPropertyToUI
		focus [$propNameT widget]
	}
	method removeProperty: {Sender} {
		$currentProperty retain
		set p [$currentProperty prevSibling]
		set n [$currentProperty sibling]

		if {[[$currentProperty parent] child] == $currentProperty} {
			[$currentProperty parent] setChild: $n
		}
		$n setPrevSibling: $p
		$p setSibling: $n
		$currentProperty release
		set currentProperty nil
		$propertyLB reloadData
		$this __currentPropertyToUI
	}


	method __createNewDocument {} {
		set root [alloc XMLElement]
		$this setXmlTree: [$this elementWithName: root]
	}

	method __writeCurrentWorkfile {} {
		if {$workfile != {nil}} {
			$this __uiToXML
			set of1 [open $workfile w]
			puts -nonewline $of1 [$xmlTree encodeXML]
			close $of1
		}
	}

	method makeChildIfNotThere: {name} {
		if {[$xmlTree childNamed: $name] == {nil}} {
			$this makeElement:childOf: [$this elementWithName: $name] $xmlTree
			puts "$name element created"
		} 
	}

	method makeChildOfParent:ifNotThere:text: {parent name text} {
		if {[$parent childNamed: $name] == {nil}} {
			$this makeElement:childOf: [$this elementWithName: $name] $parent
			[$parent childNamed: $name] setText: $text
			puts "$name element created"
		} 
	}

	method __makePropertyHierarchy: {propertyElement} {
		$this makeChildOfParent:ifNotThere:text: $propertyElement name ""
		$this makeChildOfParent:ifNotThere:text: $propertyElement type ""
		$this makeChildOfParent:ifNotThere:text: $propertyElement description ""
		$this makeChildOfParent:ifNotThere:text: $propertyElement access 0
		$this makeChildOfParent:ifNotThere:text: $propertyElement assign 0
	}

	# should be class methods... but the names got soooo long!
	method elementWithName: {Name} {
		return [$this elementWithName:text:attributes: $Name]
	}

	method elementWithName:text:attributes: {Name {Text {}} {Attribs {}}} {
		# creates a new XMLElement
		# Attribs is a flat list like {width 25 height 38 id 8}
		set element [alloc XMLElement]
		$element setName: $Name
		$element setText: $Text
		foreach {key val} $Attribs {
			$element setValue:forAttribute: $val $key
		}
		return [$element autorelease]
	}
	
	method makeElement:siblingOf: {newElement oldElement} {
		# properly connects elements - inserts if needs
		set oldElementsSibling [$oldElement sibling]
		$newElement setPrevSibling: $oldElement
		$newElement setSibling: $oldElementsSibling
		$oldElementsSibling setPrevSibling: $newElement
		$oldElement setSibling: $newElement
		$newElement setParent: [$oldElement parent]
	}

	method makeElement:childOf: {newElement parentElement} {
		if {[$parentElement child] == {nil}} {
			$newElement setParent: $parentElement
			$parentElement setChild: $newElement
		} else {
			set el [$parentElement lastChild]
			$el setSibling: $newElement
			$newElement setPrevSibling: $el
			$newElement setParent: $parentElement
		}
	}
}
synthesize DocumentsViewController xmlTree XMLElement retain
synthesize DocumentsViewController workfile
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.