Changeset 336

Show
Ignore:
Timestamp:
06/03/04 14:27:07 (5 years ago)
Author:
wrobell
Message:

- added support for moving up/down operations and attributes

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/gaphor/gaphor/UML/collection.py

    r332 r336  
    6161            raise AttributeError, '%s not in collection' % value 
    6262 
    63     def moveUp(self, value): 
    64         items = self.items 
    65         i = items.index(value) 
    66         i = i - 1 
    67         if i >= 0: 
    68             items.remove(value) 
    69             items.insert(i, value) 
    70         # Send a notification that this list has changed 
    71         print self.property, value 
    72         self.property.notify(self.object) 
    73         #self.owner.notify(self.property.name, pspec=self.property) 
    7463 
    7564    def index(self, key): 
     
    7766        collection.""" 
    7867        return self.items.index(key) 
    79      
     68 
     69 
    8070    # OCL members (from SMW by Ivan Porres, http://www.abo.fi/~iporres/smw) 
    8171 
     
    200190                    c=c-1 
    201191        return False 
     192 
     193 
     194    def moveUp(self, value): 
     195        """ 
     196        Move element up. Owner is notified about the change. 
     197        """ 
     198        i1 = self.items.index(value) 
     199        i2 = i1 - 1 
     200        if i2 >= 0: 
     201            self.items[i1], self.items[i2] = self.items[i2], self.items[i1] 
     202            self.property.notify(self.object) # send a notification that this list has changed 
     203        else: 
     204            log.warning('Cannot move up first element') 
     205 
     206 
     207    def moveDown(self, value): 
     208        """ 
     209        Move element down. Owner is notified about the change. 
     210        """ 
     211        i1 = self.items.index(value) 
     212        i2 = i1 + 1 
     213        if i2 < len(self.items): 
     214            self.items[i1], self.items[i2] = self.items[i2], self.items[i1] 
     215            self.property.notify(self.object) # send a notification that this list has changed 
     216        else: 
     217            log.warning('Cannot move down last element') 
  • trunk/gaphor/gaphor/diagram/attribute.py

    r332 r336  
    1515        'DeleteAttribute', 
    1616        'MoveUp', 
     17        'MoveDown', 
    1718        'separator', 
    18         'CreateAttribute' 
     19        'CreateAttribute', 
    1920    ) 
    2021 
     
    4546    on_subject_notify__taggedValue_value = on_subject_notify__name 
    4647 
     48 
    4749initialize_item(AttributeItem) 
  • trunk/gaphor/gaphor/diagram/itemactions.py

    r333 r336  
    616616 
    617617 
    618 class MoveUpAction(Action): 
     618class MoveAction(Action): 
     619    """ 
     620    Move attribute/operation down or up on the list. 
     621    """ 
     622    def _getItem(self): 
     623        return self._window.get_current_diagram_view() \ 
     624            .focus_item.item 
     625 
     626 
     627    def _getParent(self): 
     628        return get_parent_focus_item(self._window) 
     629 
     630 
     631    def _getElements(self, cls, item): 
     632        if isinstance(item, AttributeItem): 
     633            collection = cls.ownedAttribute 
     634        elif isinstance(item, OperationItem): 
     635            collection = cls.ownedOperation 
     636        return collection 
     637 
     638 
     639    def init(self, window): 
     640        self._window = window 
     641 
     642 
     643    def update(self): 
     644        try: 
     645            cls_item = self._getParent() 
     646            item = self._getItem() 
     647        except NoFocusItemError: 
     648            pass 
     649        else: 
     650            if isinstance(item, (AttributeItem, OperationItem)): 
     651                self.active = item.subject  
     652                self.sensitive = self._isSensitive(cls_item.subject, item) 
     653 
     654 
     655    def execute(self): 
     656        cls = self._getParent().subject 
     657        item = self._getItem() 
     658 
     659        log.debug('%s: %s.%s (%s)' \ 
     660            % (self.move_action, cls.name, item.subject.name, item.subject.__class__)) 
     661 
     662        # get method to move the element: moveUp or moveDown 
     663        move = getattr(self._getElements(cls, item), self.move_action) 
     664        move(item.subject) 
     665        self._window.execute_action('ItemFocus') 
     666 
     667 
     668class MoveUpAction(MoveAction): 
    619669    id = 'MoveUp' 
    620670    label = 'Move Up' 
    621671    tooltip = 'Move Up' 
    622  
    623     def _get_item(self): 
    624         return self._window.get_current_diagram_view().focus_item.item 
    625  
    626     def init(self, window): 
    627         self._window = window 
    628  
    629     def execute(self): 
    630         feature = self._get_item() 
    631         subject = get_parent_focus_item(self._window).subject 
    632         if isinstance(feature, AttributeItem): 
    633             subject.ownedAttribute.moveUp(feature.subject) 
    634         elif isinstance(feature, OperationItem): 
    635             subject.ownedOperation.moveUp(feature.subject) 
     672    move_action = 'moveUp' # name of method to move the element 
     673 
     674    def _isSensitive(self, cls, item): 
     675        collection = self._getElements(cls, item) 
     676        return len(collection) > 0 and collection[0] != item.subject 
    636677 
    637678register_action(MoveUpAction, 'ItemFocus') 
     679             
     680 
     681class MoveDownAction(MoveAction): 
     682    id = 'MoveDown' 
     683    label = 'Move Down' 
     684    tooltip = 'Move Down' 
     685    move_action = 'moveDown' # name of method to move the element 
     686 
     687    def _isSensitive(self, cls, item): 
     688        collection = self._getElements(cls, item) 
     689        return len(collection) > 0 and collection[-1] != item.subject 
     690 
     691register_action(MoveDownAction, 'ItemFocus') 
  • trunk/gaphor/gaphor/diagram/klass.py

    r335 r336  
    4141            save_func(None, item) 
    4242 
    43     def reorder(self, features): 
    44         """Reorder the items in this compartment so they have the same 
    45         order as the items in the features list. 
    46         """ 
    47         items = list(self) 
    48         tmp = [(item.subject, item) for item in self] 
    49         newlist = [] 
    50         for f in features: 
    51             for t in tmp: 
    52                 if t[0] is f: 
    53                     newlist.append(t[1]) 
    54                     tmp.remove(t) 
    55                     break 
    56         if newlist != items: 
    57             print self[:] 
    58             print 'new list', newlist 
    59             self[:] = newlist 
    60             self.owner.request_update() 
    6143 
    6244    def pre_update(self, width, height, affine): 
     
    137119        self._stereotype.set_text(STEREOTYPE_OPEN + 'stereotype' + STEREOTYPE_CLOSE) 
    138120 
     121 
    139122    def save(self, save_func): 
    140123        # Store the show- properties *before* the width/height properties, 
     
    184167        self.add(new) 
    185168         
    186     def sync_features(self, features, compartment, creator=None): 
     169    def sync_uml_elements(self, elements, compartment, creator=None): 
    187170        """Common function for on_subject_notify__ownedAttribute() and 
    188171        on_subject_notify__ownedOperation(). 
    189         - features: the list of attributes or operations in the model 
     172        - elements: the list of attributes or operations in the model 
    190173        - compartment: our local representation 
    191174        - creator: factory method for creating new attr. or oper.'s 
    192175        """ 
    193         # Extract the UML elements from the compartment 
    194         my_features = map(getattr, compartment, 
    195                       ['subject'] * len(compartment)) 
    196  
    197         for a in [f for f in features if f not in my_features]: 
    198             creator(a) 
    199  
    200         tmp = [f for f in my_features if f not in features] 
    201         if tmp: 
    202             # Create a feature->item mapping 
    203             mapping = dict(zip(my_features, compartment)) 
    204             for a in tmp: 
    205                 self.remove(mapping[a]) 
    206  
    207         compartment.reorder(features) 
     176        # extract the UML elements from the compartment 
     177        local_elements = [f.subject for f in compartment] 
     178 
     179        # map local element with compartment element 
     180        mapping = dict(zip(local_elements, compartment)) 
     181 
     182        try: 
     183            import sets 
     184            to_add = sets.Set(elements) - sets.Set(local_elements) 
     185        except ImportError: 
     186            to_add = [el for el in elements if el not in local_elements] 
     187 
     188        # sync local elements with elements 
     189        del compartment[:] 
     190 
     191        for el in elements: 
     192            if el in to_add: 
     193                creator(el) 
     194            else: 
     195                compartment.append(mapping[el]) 
     196 
     197        log.debug('elements order in model: %s' % [f.name for f in elements]) 
     198        log.debug('elements order in diagram: %s' % [f.subject.name for f in compartment]) 
     199        assert tuple([f.subject for f in compartment]) == tuple(elements) 
     200 
     201        self.request_update() 
     202             
    208203 
    209204    def sync_attributes(self): 
     
    212207        """ 
    213208        owned_attributes = [a for a in self.subject.ownedAttribute if not a.association] 
    214         self.sync_features(owned_attributes, self._attributes, 
     209        self.sync_uml_elements(owned_attributes, self._attributes, 
    215210                           self._create_attribute) 
    216211 
     
    219214        in self.subject. 
    220215        """ 
    221         self.sync_features(self.subject.ownedOperation, self._operations, 
     216        self.sync_uml_elements(self.subject.ownedOperation, self._operations, 
    222217                           self._create_operation) 
    223218 
    224219    def on_subject_notify(self, pspec, notifiers=()): 
    225         #log.debug('Class.on_subject_notify(%s, %s)' % (pspec, notifiers)) 
     220        log.debug('Class.on_subject_notify(%s, %s)' % (pspec, notifiers)) 
    226221        NamedItem.on_subject_notify(self, pspec, ('ownedAttribute', 'ownedOperation', 'isAbstract')) 
    227222        # Create already existing attributes and operations: 
     
    240235        """Called when the ownedAttribute property of our subject changes. 
    241236        """ 
    242         #log.debug('on_subject_notify__ownedAttribute') 
     237        log.debug('on_subject_notify__ownedAttribute') 
    243238        # Filter attributes that are connected to an association: 
    244239        self.sync_attributes() 
  • trunk/gaphor/gaphor/diagram/operation.py

    r332 r336  
    1616        'DeleteOperation', 
    1717        'MoveUp', 
     18        'MoveDown', 
    1819        'separator', 
    1920        'CreateOperation'