瀏覽代碼

Added supersimple ted2 xml document type based on impixi's demo code.

Mark Sibly 9 年之前
父節點
當前提交
8c05c6d4c8
共有 2 個文件被更改,包括 169 次插入0 次删除
  1. 3 0
      src/ted2/ted2.monkey2
  2. 166 0
      src/ted2/xmldocument.monkey2

+ 3 - 0
src/ted2/ted2.monkey2

@@ -6,6 +6,7 @@
 #Import "<std>"
 #Import "<mojo>"
 #Import "<mojox>"
+#Import "<tinyxml2>"
 
 #Import "mainwindow"
 #Import "documentmanager"
@@ -29,6 +30,7 @@
 #Import "imagedocument"
 #import "audiodocument"
 #import "jsondocument"
+#import "xmldocument"
 
 #import "textviewkeyeventfilter"
 
@@ -42,6 +44,7 @@ Namespace ted2
 Using std..
 Using mojo..
 Using mojox..
+Using tinyxml2..
 
 Function Main()
 

+ 166 - 0
src/ted2/xmldocument.monkey2

@@ -0,0 +1,166 @@
+
+Namespace ted2
+
+
+
+Class XmlDocumentView Extends DockingView
+
+	Method New( doc:TextDocument )
+	
+		_textView=New TextView( doc )
+		
+		doc.TextChanged+=Lambda()
+		
+			_treeView.RootNode.RemoveAllChildren()
+			
+			Local xml:=_textView.Text
+			
+			Local doc := New XMLDocument()
+			
+			If doc.Parse( xml ) <> XMLError.XML_SUCCESS
+				Return
+			Endif
+ 
+			_treeView.RootNode.Text = "XML Document"
+ 
+			AddXMLNodeToTree( doc,_treeView.RootNode )
+		
+		End
+		
+		_treeView=New TreeView
+
+		AddView( _treeView,"right",300,True )
+		
+		ContentView=_textView
+	End
+	
+	Property TextView:TextView()
+	
+		Return _textView
+	End
+	
+	Property TreeView:TreeView()
+	
+		Return _treeView
+	End
+	
+	Private
+	
+	Field _textView:TextView
+	
+	Field _treeView:TreeView
+	
+	Method AddXMLNodeToTree(xmlNode:XMLNode, parent:TreeView.Node)
+	
+		Local str := ""
+	
+		Local xmlElement := xmlNode.ToElement()
+		
+		If xmlElement
+		
+			str += "<" + xmlNode.Value()
+			
+			Local attrib := xmlElement.FirstAttribute()
+			While attrib 
+				str += " " + attrib.Name() + "=~q" + attrib.Value() + "~q "
+				attrib=attrib.NextAttribute()
+			wend
+			
+			str += ">"
+		Else
+			str += xmlNode.Value()
+		Endif
+ 
+		Local treeNode:TreeView.Node
+	
+		If str
+			treeNode = New TreeView.Node(str, parent)
+		Endif
+		
+		Local xmlChild := xmlNode.FirstChild()
+	
+		While xmlChild
+		
+			If Not xmlChild.NoChildren()
+				If treeNode Then parent = treeNode
+			Endif
+		
+			AddXMLNodeToTree(xmlChild, parent)
+			
+			xmlChild = xmlChild.NextSibling()
+	
+		Wend
+	
+	End
+
+End
+
+Class XmlDocument Extends Ted2Document
+
+	Method New( path:String )
+		Super.New( path )
+		
+		_doc=New TextDocument
+		
+		_doc.TextChanged+=Lambda()
+			Dirty=True
+		End
+		
+		_view=New XmlDocumentView( _doc )
+	End
+
+	Protected
+	
+	Method OnLoad:Bool() Override
+	
+		Local xml:=stringio.LoadString( Path )
+		
+		_doc.Text=xml
+		
+		Return True
+	End
+	
+	Method OnSave:Bool() Override
+	
+		Local xml:=_doc.Text
+		
+		Return stringio.SaveString( xml,Path )
+	End
+	
+	Method OnCreateView:View() Override
+	
+		Return _view
+	End
+	
+	Method OnGetTextView:TextView( view:View ) Override
+	
+		Return Cast<XmlDocumentView>( view ).TextView
+	End
+	
+	Private
+	
+	Field _doc:TextDocument
+	
+	Field _view:XmlDocumentView
+End
+
+Class XmlDocumentType Extends Ted2DocumentType
+
+	Protected
+	
+	Method New()
+		AddPlugin( Self )
+		
+		Extensions=New String[]( ".xml" )
+	End
+	
+	Method OnCreateDocument:Ted2Document( path:String ) Override
+	
+		Return New XmlDocument( path )
+	End
+	
+	Private
+	
+	Global _instance:=New XmlDocumentType
+	
+End