HelpTree.py :  » GUI » PmwContribD » PmwContribD-r2_0_2 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » GUI » PmwContribD 
PmwContribD » PmwContribD r2_0_2 » HelpTree.py
#!/usr/bin/env python
#
# $Id: HelpTree.py,v 1.3 2001/11/03 11:05:22 doughellmann Exp $
#
# Copyright 2001 Doug Hellmann.
#
#
#                         All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

"""Use the NavigableTree class to build a HelpTree class.

"""

__rcs_info__ = {
    #
    #  Creation Information
    #
    'module_name'  : '$RCSfile: HelpTree.py,v $',
    'rcs_id'       : '$Id: HelpTree.py,v 1.3 2001/11/03 11:05:22 doughellmann Exp $',
    'creator'      : 'Doug Hellmann <doug@hellfly.net>',
    'project'      : 'PmwContribD',
    'created'      : 'Sun, 01-Apr-2001 17:49:50 EDT',

    #
    #  Current Information
    #
    'author'       : '$Author: doughellmann $',
    'version'      : '$Revision: 1.3 $',
    'date'         : '$Date: 2001/11/03 11:05:22 $',
}

#
# Import system modules
#
import Tkinter, Pmw
import string


#
# Import Local modules
#
import TreeExplorer
import NavigableTree


#
# Module
#


def move_text_left(input_text):
    """Un-indent help messages.

    Since multi-line strings embedded in programs will have unnatural
    (and potentially undesired) indention, they need to be adjusted
    before they are added to the help system.

    To prevent a help string from losing all indention, place a
    vertical bar character ('|') at the point on the line where the
    left margin should be.  The | will be replaced with a space
    character.
    """
    if not input_text: return input_text
    lines = string.split(input_text, '\n')
    lines = map(string.strip, lines)
    formatted_lines = []
    for line in lines:
        if not line:
            pass
        elif line[0] == '|':
                line = ' %s' % line[1:]
        formatted_lines.append(line)
    return string.join(formatted_lines, '\n')

class HelpTree(NavigableTree.NavigableTree):
    """A data structure to hold help text for a GUI application.
    """
    
    nodePathSep=':'
    
    def format_text(self, input_text):
        """Fix up the text so it can be displayed.
        
        Remove the first whitespace character from the input text, since it
        probably comes from code that was indented.
        """
        return move_text_left(input_text)
    
    def insert_node(self, node_path, help_text=''):
        """Insert a help text node into the tree.

        The new node is returned.

        Arguments

          'node_path' -- A string with node names separated by the
          nodePathSep value for the class.  The path is assumed to
          start at the current node, so the name of the current node
          should not be inserted into the beginning of the path.
        
          'help_text' -- A string which should be displayed as the
          help string for the node.
        
        """
        #print 'inserting "%s"->%s' % (node_path, help_text)
        if not node_path: return
        #
        # Split up the path
        #
        node_path_parts = string.split(node_path, self.nodePathSep)
        if not node_path_parts: return
        remaining_path = string.join(node_path_parts[1:], self.nodePathSep) 
        #
        # Find the insertion point
        #
        children = self.children()
        insertion_point = filter(lambda x, n=node_path_parts[0]:x.name == n, children)
        if not insertion_point:
            # create a new node
            if remaining_path:
                newNode = self.__class__(name=node_path_parts[0], parent=self)
            else:
                formatted_help_text = self.format_text(help_text)
                newNode = self.__class__(name=node_path_parts[0],
                                         data=formatted_help_text, parent=self)
            self.add_children([ newNode ])
        else:
            newNode = insertion_point[0]
            if not remaining_path:
                formatted_help_text = self.format_text(help_text)
                if newNode.data:
                    newNode.data = '%s\n%s' % (newNode.data, formatted_help_text)
                else:
                    newNode.data = self.format_text(help_text)

        # insert below this node
        if remaining_path:
            newNode.insert_node(remaining_path, help_text)

class HelpExplorer(TreeExplorer.TreeExplorer):
    """A widget for viewing a HelpTree.
    """
    
    def createInterior(self):
        "Create the components."
        interior = self.interior()
        self.textDisplay = self.createcomponent('textdisplay', (), None,
                                                Pmw.ScrolledText,
                                                (interior,),
                                                text_state='disabled',
                                                text_wrap='none',
                                                )
        self.textDisplay.pack(side=Tkinter.TOP, expand=Tkinter.YES, fill=Tkinter.BOTH)
        self.panes.configurepane('datapane', min=500, size=500)
        return
    
    def select_node(self, node):
        "Called when a node is selected."
        if not node: return
        if node.data:
            newText = node.data
        else: 
            newText = 'No help for %s' % string.join(map(lambda x: x.name,
                                                         node.getpath()), ':')
        try:
            self.textDisplay.settext(newText)
        except AttributeError:
            # not initialized yet
            pass
        return
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.