commandGroup.py :  » UML » Python-UML-Tool » pyut-1.4.0 » src » 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 » UML » Python UML Tool 
Python UML Tool » pyut 1.4.0 » src » commandGroup.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from historyUtils import *

class CommandGroup(object):
    """
    @author P. Dabrowski <przemek.dabrowski@destroy-display.com> (17.11.2005)
    This class is a part of the history system of PyUt. It brings together
    different commands used for doing a undo or redo. For example, when we
    select many shapes and we delete them, then there is a command 'created'
    for each one that is added to a CommandGroupe. This way, when we want
    to do an undo, all the deleted shapes will be reconstructed in one action.
    """
    
    def __init__(self, comment = ""):
        """
        Constructor.
        
        @param comment  String  :   a short description/comment in
                                    view to display in the menu or
                                    other GUI part.
        """

        #history to which belongs the group. Init when the group is added.
        self._history = None

        # list of commands belonging to the group
        self._commands = []
        
        self._comment = comment

        #to store informations that are common to all the commands of the
        #group. WARNING : the common data is NOT serialized, so the
        #common data are lost after an unserialization. You have to use
        #these data in a command before the serialization.
        self._commonData = []
        
    #>------------------------------------------------------------------------
        
    def addCommand(self, command):
        """
        Add the specified command to the group
        @param command Command : command to add
        """

        command.setGroup(self)
        self._commands.append(command)
        
    #>------------------------------------------------------------------------

    def removeCommand(self, command):
        """
        Remove the specified command from the group
        @param command Command : command to remove
        """
        
        self._commands.remove(command)
        
    #>------------------------------------------------------------------------
        
    def serialize(self):
        """
        Transform all the commands belonging to the group into strings in
        view to store them in a file.
        @return a string representing the command.
        """
        #add the begining informations of the group
        serializedGroup = (makeToken(GROUP_BEGIN_ID) +
                           makeValuatedToken(GROUP_COMMENT_ID, self._comment))

        #add the begining informations and setup informations of
        #each command. After that add the ending informations of
        #for each command.
        for command in self._commands:
            serializedGroup += (makeToken(COMMAND_BEGIN_ID) +
                                command.serialize() +
                                makeToken(COMMAND_END_ID))
            
        #add the ending informations of the group
        serializedGroup += makeToken(GROUP_END_ID)
        
        return serializedGroup

    #>------------------------------------------------------------------------

    def unserialize(self, serializedCommands):
        """
        unserialize the specified commands and add them to the group
        @param serializedCommands (string) :    a string representation
                                                of the commands belonging
                                                to the group.
        """

        #define the begining and ending token of a serialized command
        commandBegin = TOKEN_BEGIN + COMMAND_BEGIN_ID + TOKEN_END
        commandEnd = TOKEN_BEGIN + COMMAND_END_ID + TOKEN_END

        #looking for the begining of the first command
        cStart = serializedCommands.find(commandBegin)

        #while there is still a command begining token we can
        #proceed to the unserialization.
        while cStart > -1:
            
            #we don't need anymore of the begining token
            cStart += len(commandBegin)
            
            #find the ending token for this command
            cEnd = serializedCommands.find(commandEnd, cStart)

            #we work only on the useful data
            serialCommand = serializedCommands[cStart : cEnd]

            commandModuleName = getTokenValue(COMMAND_MODULE_ID, serialCommand)
            
            #get the name of the class of the command
            commandClassName = getTokenValue(COMMAND_CLASS_ID, serialCommand)

            #import the module which contains the command class an get that class
            commandClass = getattr(__import__(commandModuleName), commandClassName)

            #construction of an uninitialized command
            command = commandClass()
            command.setGroup(self)

            #unserialization and setup of the command
            command.unserialize(serialCommand)

            #add the command to the group
            self.addCommand(command)

            #looking for the next command begining token
            cStart = serializedCommands.find(commandBegin, cEnd)

    #>------------------------------------------------------------------------ 
        
    def redo(self):
        """
        Call the redo() method of all commands belonging to the group
        """
        for command in self._commands:
            command.redo()

    #>------------------------------------------------------------------------
    
    def undo(self):
        """
        Call the undo() method of all commands belonging to the group
        """
        for command in self._commands:
            command.undo()

    #>------------------------------------------------------------------------

    def execute(self):
        """
        Call the execute() method of all commands belonging to the group 
        """
        for command in self._commands:
            command.execute()

    #>------------------------------------------------------------------------
            
    def getHistory(self):
        """
        return the history to which belongs the group
        """

        return self._history
    
    #>------------------------------------------------------------------------

    def setHistory(self, history):
        """
        Set the history to which belongs the group. Avoid to call this method
        because it is called automaticaly when the group is added.
        @param history (HistoryManager) : history to which belongs the group
        """

        self._history = history
    
    #>------------------------------------------------------------------------

    def getComment(self):
        """
        return the comment/description of the group
        """

        return self._comment

    #>------------------------------------------------------------------------

    def setComment(self, comment):
        """
        set the comment/description of the group
        @param comment (string) : comment/description of group to display
        """

        self._comment = comment
        
    #>------------------------------------------------------------------------

    def addCommonData(self, commonData):
        """
        Add a data that is common to all the commands belonging to the group.
        A common data should contain a identificator so that a given command
        can get only the pertinent data for itself. For e.g. linkCommand will
        get only the tuples ("link", (shapeToLink, linkId)).
        WARNING : the common data is NOT serialized, so they are lost after
        an unserialization. You have to use these data in a command before
        the serialization of the group.

        @param commonData (tuple)   :   data that a command add to be used by
                                        an other command.
        """
        self._commonData.append(commonData)

    #>------------------------------------------------------------------------
    
    def getCommonData(self):
        """
        @return a list of common data, so a command can use informations
        produced by an other command in the same group.
        WARNING : the common data is NOT serialized, so they are lost after
        an unserialization. You have to use these data in a command before
        the serialization of the group.
        """
        return self._commonData
        
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.