~ubuntu-dev/ubuntu/lucid/editobj/lucid-201002101902

« back to all changes in this revision

Viewing changes to demo/tree_demo2.py

  • Committer: Bazaar Package Importer
  • Author(s): Marc Dequènes (Duck)
  • Date: 2004-04-29 16:35:29 UTC
  • Revision ID: james.westby@ubuntu.com-20040429163529-nf7jugpi5b075se5
Tags: upstream-0.5.3b
Import upstream version 0.5.3b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# TreeWidget
 
4
# Copyright (C) 2001-2002 Jean-Baptiste LAMY -- jiba@tuxfamily
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
from Tkinter import *
 
21
from editobj.treewidget import *
 
22
import os, os.path, sys, string, imp
 
23
import editobj.treewidget
 
24
 
 
25
# Gets the default icons directory (shipped with TreeWidget).
 
26
iconsdir = IconsDir(os.path.join(os.path.dirname(editobj.treewidget.__file__), "icons"))
 
27
 
 
28
class FileNode(Node):
 
29
  """Example Node subclass -- browse the file system."""
 
30
  def __init__(self, parent, path):
 
31
    self.path = path
 
32
    Node.__init__(self, parent)
 
33
    
 
34
  def __str__(self): return os.path.basename(self.path) or self.path
 
35
 
 
36
  # One may explicitely choose the icon as following (TreeWidget use those as default) :
 
37
  
 
38
  def geticon(self):
 
39
    if self.isexpandable():
 
40
      if self.expanded: return iconsdir["openfolder.pgm"]
 
41
      else: return iconsdir["folder.pgm"]
 
42
    else: return iconsdir["python.pgm"]
 
43
    
 
44
  def iseditable(self): return os.path.basename(self.path) != ""
 
45
  def isexpandable(self): return os.path.isdir(self.path)
 
46
  def createchildren(self, oldchildren = None):
 
47
    try: files = os.listdir(self.path)
 
48
    except os.error: return []
 
49
    files.sort(lambda a, b: cmp(os.path.normcase(a), os.path.normcase(b)))
 
50
    children = []
 
51
    for file in files: children.append(FileNode(self, os.path.join(self.path, file)))
 
52
    return children
 
53
    
 
54
  def settext(self, text):
 
55
    newpath = os.path.dirname(self.path)
 
56
    newpath = os.path.join(newpath, text)
 
57
    if os.path.dirname(newpath) != os.path.dirname(self.path): return
 
58
    try:
 
59
      os.rename(self.path, newpath)
 
60
      self.path = newpath
 
61
    except os.error: print "Cannot rename !"
 
62
 
 
63
  
 
64
root = Tk()
 
65
tree = Tree(root)
 
66
tree.frame.pack(expand=1, fill="both")
 
67
 
 
68
node = FileNode(tree, os.curdir)
 
69
root.mainloop()
 
70