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

TIP 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/Encoding.lua");
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

bShow  

FileExplorerCtrl:GetNodeNamePath


private functions:

get node file path.

syntax

function  FileExplorerCtrl:GetNodeNamePath(node)

parameters

node  

FileExplorerCtrl.ValidateFolderRootNode

cononicalize root folder

syntax

function FileExplorerCtrl.ValidateFolderRootNode(node)

parameters

node  

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

node  

FileExplorerCtrl:GetIcon

get the icon path for a given file name

syntax

function FileExplorerCtrl:GetIcon(filename)

parameters

filename  

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

parent  
treeNode  

FileExplorerCtrl.OnClick


event handlers:

event handler: user clicks a node

syntax

function FileExplorerCtrl.OnClick(sCtrlName, nodePath)

parameters

sCtrlName  
nodePath  

FileExplorerCtrl:ClickNode

this function can be called from outside

syntax

function FileExplorerCtrl:ClickNode(node)

parameters

node  

FileExplorerCtrl.OnToggleNode

toggle folder node, it will cause node to be rebuilt.

syntax

function FileExplorerCtrl.OnToggleNode(sCtrlName, nodePath)

parameters

sCtrlName  
nodePath  

FileExplorerCtrl.OnToggleNodeChecked

toggle folder node, it will cause node to be rebuilt.

syntax

function FileExplorerCtrl.OnToggleNodeChecked(sCtrlName, nodePath)

parameters

sCtrlName  
nodePath  

FileExplorerCtrl:SetCheckedPath

璁板綍琚偣鍑昏妭鐐圭殑璺緞

syntax

function FileExplorerCtrl:SetCheckedPath(nodePath,checked)

parameters

nodePath  
checked  

FileExplorerCtrl:UpdateByPathList

鏇存柊FileExplorerCtrl

syntax

function FileExplorerCtrl:UpdateByPathList(nodePath,pathList)

parameters

nodePath  
pathList  

FileExplorerCtrl:FindTxtPath

浠庡凡缁忓瓨鍦ㄧ殑鍒楄〃(self.fromTxtPathList)涓壘 鏌愪釜鑺傜偣锛屽鏋滄壘鍒帮紝杩斿洖true

syntax

function FileExplorerCtrl:FindTxtPath(list,nodeTxt)

parameters

list  
nodeTxt  

FileExplorerCtrl:ClearFolderFilter

杞崲鏌愪釜鐩綍涓嬮潰鐨勯€氶厤绗︿负鍏蜂綋鏂囦欢璺緞 閫氶厤绗﹀彧鍙兘鍑虹幇鍦╯elf.fromTxtPathList閲岄潰 a/*.jpg ==> a/1.jpg,a/2.jpg ......

syntax

function FileExplorerCtrl:ClearFolderFilter(parentNode,parentNodePath)

parameters

parentNode  
parentNodePath  

FileExplorerCtrl:HasFilter

鏍¢獙鏌愪釜鐩綍锛屽亣濡傚畠鐨勫瓙鐩綍鍏ㄩ儴娌℃湁琚€変腑锛岄€氱煡瀹冨拰瀹冪殑鐖惰妭鐐? --鍦ㄤ娇鐢ㄥ悓閰嶇鐨勬椂鍊欙紝鏈夊彲鑳藉嚭鐜拌繖绉嶆儏鍐?鐖剁洰褰曟槸閫変腑鐘舵€侊紝浣嗘槸鎵撳紑瀛愮洰褰曞悗鍙戠幇娌℃湁涓€涓槸鍖归厤鐨? function FileExplorerCtrl:OurChildenIsAllUnChecked(sCtrlName, nodePath) local self, node = GetCtl(sCtrlName, nodePath) local reallyUnchecked = true; if(node) then local nSize = table.getn(node.Nodes); local i, childnode; for i=1, nSize do childnode = node.Nodes[i]; if(childnode.Checked == true)then reallyUnchecked = false; end end if(reallyUnchecked)then node.Checked = false; self:BeepParentNode(sCtrlName, nodePath); end end end

鍒ゆ柇涓€涓矾寰勬槸鍚︿负鏂囦欢澶? function FileExplorerCtrl:IsFolder(filePath) local rootPath = GetCurDirectory(0); local path = rootPath..filePath; local search_result = SearchFiles(path.."/","*", "", 0, 1, 0); local nCount = search_result:GetNumOfResult(); if(nCount>0) then return true; else return false; end end

syntax

function FileExplorerCtrl:HasFilter(filePath)

parameters

filePath  

FileExplorerCtrl:GetText

get the current file path

syntax

function FileExplorerCtrl:GetText()

FileExplorerCtrl:SetValue

TODO:

syntax

function FileExplorerCtrl:SetValue(filepath)

parameters

filepath  

FileExplorerCtrl:CreateFolder

TODO: create a new folder

  • return true : if succeed.

syntax

function FileExplorerCtrl:CreateFolder(folderName, rootDir)

parameters

folderName  
rootDir  
return if succeed.
Topic revision: r1 - 2008-02-29 - LiXizhi
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback