CommonCtrl.FileExplorerCtrl
a control that displaying folder/files using a treeview control
| Title |
a control that displaying folder/files using a treeview control |
| Author(s) |
LiXizhi |
| Date |
2008/4/24 |
| File |
script/ide/FileExplorerCtrl.lua |
Description
Sample Code
NPL.load("(gl)script/ide/FileExplorerCtrl.lua");
local ctl = CommonCtrl.FileExplorerCtrl:new{
name = "FileExplorerCtrl1",
alignment = "_lt",left=0, top=0,width = 256,height = 300,
parent = _parent,
rootfolder = "temp",
filter = "*.png;*.jpg",
AllowFolderSelection = true,
OnSelect = function(filepath) _guihelper.MessageBox(filepath); end,
OnDoubleClick = nil,
};
ctl:Show(true);
-- alternatively one can open multiple folders by setting rootfolder to nil and add child nodes as below.
local ctl = CommonCtrl.FileExplorerCtrl:new{
name = "FileExplorerCtrl1",
alignment = "_lt",left=0, top=0,width = 256,height = 300,
parent = _parent,
rootfolder = nil,
filter = "*.*",
OnSelect = function(filepath) _guihelper.MessageBox(filepath); end,
};
-- node.rootfolder property should contain the rootfolder path, you can even add owner draw to these nodes
ctl.RootNode:AddChild(CommonCtrl.TreeNode:new({Text = "My documents", rootfolder = "temp"}));
ctl.RootNode:AddChild(CommonCtrl.TreeNode:new({Text = "My worlds", rootfolder = "worlds/", Expanded = false,}));
ctl:Show(true);
Member Functions
FileExplorerCtrl:Destroy
common control library
NPL.load("(gl)script/ide/common_control.lua");
NPL.load("(gl)script/ide/TreeView.lua");
local FileExplorerCtrl = commonlib.inherit(CommonCtrl.TreeView, {
name = "FileExplorerCtrl1",
-- appearance
alignment = "_lt",
left = 0,
top = 0,
width = 500,
height = 500,
parent = nil,
container_bg = "Texture/3DMapSystem/common/ThemeLightBlue/container_bg.png: 4 4 4 4",
-- default item height
DefaultNodeHeight = 22,
DefaultIndentation = 16,
FolderOpenIcon = "Texture/3DMapSystem/common/Folder_open.png",
FolderIcon = "Texture/3DMapSystem/common/Folder.png",
-- Icon for display file extensions
Icons = {
-- file without extension
["none"]="Texture/3DMapSystem/common/Folder.png",
-- unknown file without extension
["unknown"]="Texture/3DMapSystem/common/page_white.png",
-- media files
["wav"]="Texture/3DMapSystem/common/music.png",
["dds"]="Texture/3DMapSystem/common/image.png",
["jpg"]="Texture/3DMapSystem/common/image.png",
["bmp"]="Texture/3DMapSystem/common/image.png",
["png"]="Texture/3DMapSystem/common/image.png",
["tga"]="Texture/3DMapSystem/common/image.png",
["avi"]="Texture/3DMapSystem/common/film.png",
["flv"]="Texture/3DMapSystem/common/film.png",
["swf"]="Texture/3DMapSystem/common/film.png",
["x"]="Texture/3DMapSystem/common/file_parax.png",
-- modules
["db"]="Texture/3DMapSystem/common/page_white_database.png",
["config"]="Texture/3DMapSystem/common/page_white_gear.png",
["max"]="Texture/3DMapSystem/common/plugin.png",
["dll"]="Texture/3DMapSystem/common/script_gear.png",
["exe"]="Texture/3DMapSystem/common/application_xp.png",
["zip"]="Texture/3DMapSystem/common/page_white_zip.png",
["pkg"]="Texture/3DMapSystem/common/page_white_zip.png",
-- script files
["lua"]="Texture/3DMapSystem/common/script_code_red.png",
["txt"]="Texture/3DMapSystem/common/page_white_text.png",
["xml"]="Texture/3DMapSystem/common/page_code.png",
["html"]="Texture/3DMapSystem/common/page_code.png",
["c"]="Texture/3DMapSystem/common/page_white_c.png",
["cpp"]="Texture/3DMapSystem/common/page_white_c.png",
["h"]="Texture/3DMapSystem/common/page_white_h.png",
-- open world file
["worldconfig.txt"]="Texture/3DMapSystem/common/page_world.png",
["world.zip"]="Texture/3DMapSystem/common/page_world.png",
["world.pkg"]="Texture/3DMapSystem/common/page_world.png",
},
-- only used when CheckBoxes is true,
checked_bg = "Texture/3DMapSystem/common/ThemeLightBlue/checkbox.png",
-- only used when CheckBoxes is true,
unchecked_bg = "Texture/3DMapSystem/common/ThemeLightBlue/uncheckbox.png",
-- only used when CheckBoxes is true,
checkmixed_bg = "Texture/3DMapSystem/common/ThemeLightBlue/checkbox_mixed.png",
-- default is FileExplorerCtrl.DrawNormalNodeHandler
DrawNodeHandler = nil,
-- parameters --
-- initial root folder name, such as "temp", "script/test",
-- if this is nil, the first level children of rootNode will be expanded as child node.
-- The childNode.Name should contain the rootfolder instead. One can also implement owner draw function
-- for the first level root, such as in the open file dialog.
rootfolder = nil,
-- the files to be displayed, such as ".*", ".jpg;*.x". if nil, only folder will be displayed.
filter = nil,
-- boolean: whether allow folder selection. if true, a folder node can be selected and select event is fired
AllowFolderSelection = nil,
-- boolean: if true, folder can not be expanded, otherwise it can.
DisableFolderExpand = nil,
-- boolean: if true, folder will be hidden. However if self.filter contains folder, they will be displayed anyway.
HideFolder = nil,
-- max number of items per folder
MaxItemPerFolder = 300,
-- currently selected folder path
SelectedPath = nil,
-- boolean: Gets or sets a value indicating whether check boxes are displayed next to the tree nodes in the tree view control
-- A check box is displayed to the left of both the tree node label and tree node Icon, if any. Check boxes allow the user to select more than one tree node at a time
CheckBoxes=nil,
-- event --
-- called when user click on a folder or file item. it is a string or a function(filepath) end
OnSelect = nil,
-- called when user double clicked on an file item. it is a string or a function(filepath) end
OnDoubleClick = nil,
-- called when user checks or unchecks a folder or file item. it is a function(treeNode, filepath, Checked) end
OnCheck = nil,
-- called when a new folder or file node is added. it is a function(treeNode, filepath) end
-- usually one can alter the Text, Icon, TextColor property of the created treeNode according to treeNode.Name
OnCreateNode = nil,
-- private
selectedNodePath = nil,
treeViewName = nil,
--记录所有被选中的目录
CheckedPathList = {},
--已经存在的一个路径列表
fromTxtPathList = {},
filterList = {"*.x","*.png","*.dds","*.lua","*.htm","*.html","*.xml","*.jpg","*.jpeg","*.gif","*.swf","*.avi","*.mp3"}
})
FileExplorerCtrl? =
FileExplorerCtrl;
Destroy the UI control
syntax
function FileExplorerCtrl:Destroy ()
FileExplorerCtrl:Show
display control.
syntax
function FileExplorerCtrl:Show(bShow)
parameters
FileExplorerCtrl:GetNodeNamePath
private functions:
get node file path.
syntax
function FileExplorerCtrl:GetNodeNamePath(node)
parameters
FileExplorerCtrl.ValidateFolderRootNode
cononicalize root folder
syntax
function FileExplorerCtrl.ValidateFolderRootNode(node)
parameters
FileExplorerCtrl:ResetTreeView
rebuild tree view for the current root directory.
syntax
function FileExplorerCtrl:ResetTreeView()
FileExplorerCtrl:RefreshNode
refresh the node according to its folder path. It will automatically add the first level child if any.
syntax
function FileExplorerCtrl:RefreshNode(node)
parameters
FileExplorerCtrl:GetIcon
get the icon path for a given file name
syntax
function FileExplorerCtrl:GetIcon(filename)
parameters
FileExplorerCtrl.SearchFiles
only return the sub folders of the current folder
- param rootfolder : the folder which will be searched.
- param nMaxFilesNum : one can limit the total number of files in the search result. Default value is 50. the search will stop at this value even there are more matching files.
- param filter : if nil, it defaults to "*."
- return a : table array containing relative to rootfolder file name.
syntax
function FileExplorerCtrl.SearchFiles(output, rootfolder,nMaxFilesNum, filter)
parameters
| output |
|
| rootfolder |
the folder which will be searched. |
| nMaxFilesNum |
|
| filter |
if nil, it defaults to "*." |
FileExplorerCtrl.DrawNormalNodeHandler
owner draw function of treeview
syntax
function FileExplorerCtrl.DrawNormalNodeHandler(_parent,treeNode)
parameters
FileExplorerCtrl.OnClick
event handlers:
event handler: user clicks a node
syntax
function FileExplorerCtrl.OnClick(sCtrlName, nodePath)
parameters
FileExplorerCtrl:ClickNode
this function can be called from outside
syntax
function FileExplorerCtrl:ClickNode(node)
parameters
FileExplorerCtrl.OnToggleNode
toggle folder node, it will cause node to be rebuilt.
syntax
function FileExplorerCtrl.OnToggleNode(sCtrlName, nodePath)
parameters
FileExplorerCtrl.OnToggleNodeChecked
toggle folder node, it will cause node to be rebuilt.
syntax
function FileExplorerCtrl.OnToggleNodeChecked(sCtrlName, nodePath)
parameters
FileExplorerCtrl:SetCheckedPath
记录被点击节点的路径
syntax
function FileExplorerCtrl:SetCheckedPath(nodePath,checked)
parameters
FileExplorerCtrl:UpdateByPathList
更新FileExplorerCtrl
syntax
function FileExplorerCtrl:UpdateByPathList(nodePath,pathList)
parameters
FileExplorerCtrl:FindTxtPath
从已经存在的列表(self.fromTxtPathList)中找 某个节点,如果找到,返回true
syntax
function FileExplorerCtrl:FindTxtPath(list,nodeTxt)
parameters
FileExplorerCtrl:ClearFolderFilter
转换某个目录下面的通配符为具体文件路径
通配符只可能出现在self.fromTxtPathList里面
a/*.jpg ==> a/1.jpg,a/2.jpg ......
syntax
function FileExplorerCtrl:ClearFolderFilter(parentNode,parentNodePath)
parameters
| parentNode |
|
| parentNodePath |
|
FileExplorerCtrl:OurChildenIsAllUnChecked
校验某个目录,假如它的子目录全部没有被选中,通知它和它的父节点
在使用同配符的时候,有可能出现这种情况 父目录是选中状态,但是打开子目录后发现没有一个是匹配的
syntax
function FileExplorerCtrl:OurChildenIsAllUnChecked(sCtrlName, nodePath)
parameters
FileExplorerCtrl:IsFolder
判断一个路径是否为文件夹
syntax
function FileExplorerCtrl:IsFolder(filePath)
parameters
FileExplorerCtrl:HasFilter
syntax
function FileExplorerCtrl:HasFilter(filePath)
parameters
FileExplorerCtrl:GetText
get the current file path
syntax
function FileExplorerCtrl:GetText()
FileExplorerCtrl:SetValue
TODO:
syntax
function FileExplorerCtrl:SetValue(filepath)
parameters
FileExplorerCtrl:CreateFolder
TODO: create a new folder
- return true : if succeed.
syntax
function FileExplorerCtrl:CreateFolder(folderName, rootDir)
parameters
| folderName |
|
| rootDir |
|
| return |
if succeed. |