filebrowser.monkey2 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. Namespace mojox
  2. #rem monkeydoc The FileBrowser class.
  3. #end
  4. Class FileBrowser Extends TreeView
  5. #rem monkeydoc Invoked when a file is clicked.
  6. #end
  7. Field FileClicked:Void( path:String )
  8. #rem monkeydoc Invoked when a file is right clicked.
  9. #end
  10. Field FileRightClicked:Void( path:String )
  11. #rem monkeydoc Invoked when a file is double clicked.
  12. #end
  13. Field FileDoubleClicked:Void( path:String )
  14. #rem monkeydoc Creates a new FileBrowser.
  15. #end
  16. Method New( rootPath:String="." )
  17. Style=GetStyle( "FileBrowser" )
  18. GetFileTypeIcons()
  19. _rootNode=New Node( Null )
  20. RootPath=rootPath
  21. NodeClicked=OnNodeClicked
  22. NodeRightClicked=OnNodeRightClicked
  23. NodeDoubleClicked=OnNodeDoubleClicked
  24. NodeExpanded=OnNodeExpanded
  25. NodeCollapsed=OnNodeCollapsed
  26. RootNode=_rootNode
  27. Update()
  28. End
  29. #rem monkeydoc Root path of browser.
  30. #end
  31. Property RootPath:String()
  32. Return _rootPath
  33. Setter( path:String )
  34. _rootPath=path
  35. _rootNode._path=path
  36. _rootNode.Text=_rootPath
  37. End
  38. #rem monkeydoc Updates the browser.
  39. #end
  40. Method Update()
  41. UpdateNode( _rootNode,_rootPath,True )
  42. End
  43. Protected
  44. Method OnValidateStyle() Override
  45. Super.OnValidateStyle()
  46. GetFileTypeIcons()
  47. _dirIcon=_fileTypeIcons["._dir"]
  48. _fileIcon=_fileTypeIcons["._file"]
  49. End
  50. Private
  51. Class Node Extends TreeView.Node
  52. Method New( parent:Node )
  53. Super.New( "",parent )
  54. End
  55. Private
  56. Field _path:String
  57. End
  58. Field _rootNode:Node
  59. Field _rootPath:String
  60. Field _dirIcon:Image
  61. Field _fileIcon:Image
  62. Method OnNodeClicked( tnode:TreeView.Node )
  63. Local node:=Cast<Node>( tnode )
  64. If Not node Return
  65. FileClicked( node._path )
  66. End
  67. Method OnNodeRightClicked( tnode:TreeView.Node )
  68. Local node:=Cast<Node>( tnode )
  69. If Not node Return
  70. FileRightClicked( node._path )
  71. End
  72. Method OnNodeDoubleClicked( tnode:TreeView.Node )
  73. Local node:=Cast<Node>( tnode )
  74. If Not node Return
  75. FileDoubleClicked( node._path )
  76. End
  77. Method OnNodeExpanded( tnode:TreeView.Node )
  78. Local node:=Cast<Node>( tnode )
  79. If Not node Return
  80. UpdateNode( node,node._path,True )
  81. End
  82. Method OnNodeCollapsed( tnode:TreeView.Node )
  83. Local node:=Cast<Node>( tnode )
  84. If Not node Return
  85. For Local child:=Eachin node.Children
  86. child.RemoveAllChildren()
  87. Next
  88. End
  89. Method UpdateNode( node:Node,path:String,recurse:Bool )
  90. If Not path.EndsWith( "/" ) path+="/"
  91. Local dir:=filesystem.LoadDir( path )
  92. Local dirs:=New Stack<String>
  93. Local files:=New Stack<String>
  94. For Local f:=Eachin dir
  95. Local fpath:=path+f
  96. Select GetFileType( fpath )
  97. Case FileType.Directory
  98. dirs.Push( f )
  99. Default
  100. files.Push( f )
  101. End
  102. Next
  103. dirs.Sort()
  104. files.Sort()
  105. Local i:=0,children:=node.Children
  106. While i<dir.Length
  107. Local f:=""
  108. If i<dirs.Length f=dirs[i] Else f=files[i-dirs.Length]
  109. Local child:Node
  110. If i<children.Length
  111. child=Cast<Node>( children[i] )
  112. Else
  113. child=New Node( node )
  114. Endif
  115. Local fpath:=path+f
  116. child.Text=f
  117. child._path=fpath
  118. Local icon:=GetFileTypeIcon( fpath )
  119. If i<dirs.Length
  120. If Not icon icon=_dirIcon
  121. child.Icon=icon
  122. If child.Expanded Or recurse
  123. UpdateNode( child,fpath,child.Expanded )
  124. Endif
  125. Else
  126. If Not icon icon=_fileIcon
  127. child.Icon=icon
  128. child.RemoveAllChildren()
  129. Endif
  130. i+=1
  131. Wend
  132. node.RemoveChildren( i )
  133. End
  134. Function GetFileTypeIcons:StringMap<Image>()
  135. If _fileTypeIcons Return _fileTypeIcons
  136. _fileTypeIcons=New StringMap<Image>
  137. Local dir:="theme::filetype_icons/"
  138. Local types:=stringio.LoadString( dir+"filetypes.txt" ).Split( "~n" )
  139. For Local type:=Eachin types
  140. type=type.Trim()
  141. If Not type Continue
  142. Local icon:=Image.Load( dir+type )
  143. If Not icon Continue
  144. icon.Scale=App.Theme.Scale
  145. _fileTypeIcons[ "."+StripExt(type) ]=icon
  146. Next
  147. App.ThemeChanged+=Lambda()
  148. For Local image:=Eachin _fileTypeIcons.Values
  149. image.Scale=App.Theme.Scale
  150. Next
  151. End
  152. Return _fileTypeIcons
  153. End
  154. Protected
  155. Method GetFileTypeIcon:Image( path:String ) Virtual
  156. Local ext:=ExtractExt( path )
  157. If Not ext Return Null
  158. Return GetFileTypeIcons()[ ext.ToLower() ]
  159. End
  160. Private
  161. Global _fileTypeIcons:StringMap<Image>
  162. End