Changeset 2116

Show
Ignore:
Timestamp:
09/10/07 13:12:44 (8 months ago)
Author:
arj..@yirdis.nl
Message:
  • Updated some unit tests.
  • Migrated some diagram item classes to new event handler code.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gaphor/trunk/gaphor/diagram/classifier.py

    r2090 r2116  
    164164 
    165165        self._drawing_style = ClassifierItem.DRAW_NONE 
     166        self.add_watch(UML.Classifier.isAbstract, self.on_classifier_is_abstract) 
    166167 
    167168 
     
    175176    def postload(self): 
    176177        NamedItem.postload(self) 
    177         self.on_subject_notify__isAbstract(self.subject
     178        self.on_classifier_is_abstract(None
    178179 
    179180    @observed 
     
    259260 
    260261 
    261     def on_subject_notify(self, pspec, notifiers=()): 
    262         #log.debug('Class.on_subject_notify(%s, %s)' % (pspec, notifiers)) 
    263         NamedItem.on_subject_notify(self, pspec, ('isAbstract',) + notifiers) 
    264         # Create already existing attributes and operations: 
    265         if self.subject: 
    266             self.on_subject_notify__isAbstract(self.subject) 
    267         self.request_update() 
    268  
    269     def on_subject_notify__isAbstract(self, subject, pspec=None): 
    270         self._name.font = subject.isAbstract \ 
     262    def on_classifier_is_abstract(self, event): 
     263        self._name.font = (self.subject and self.subject.isAbstract) \ 
    271264                and font.FONT_ABSTRACT_NAME or font.FONT_NAME 
    272265        self.request_update() 
  • gaphor/trunk/gaphor/diagram/diagramitem.py

    r2009 r2116  
    44""" 
    55 
     6from zope import component 
    67from gaphor import UML 
     8from gaphor.application import Application 
    79from gaphor.misc import uniqueid 
    810from gaphor.diagram import DiagramItemMeta 
     
    279281        # properties, which should be saved in file 
    280282        self._persistent_props = set() 
    281  
     283        self._watched_properties = dict() 
     284 
     285        self.add_watch(UML.Element.appliedStereotype, self.on_element_applied_stereotype) 
     286        self.add_watch(UML.Presentation.subject, self.on_presentation_subject) 
    282287 
    283288    id = property(lambda self: self._id, doc='Id') 
     
    313318                log.warning('%s has no property named %s (value %s)' % (self, name, value)) 
    314319 
     320 
    315321    def postload(self): 
    316322        if self.subject: 
     
    324330        save_func(name, getattr(self, name.replace('-', '_'))) 
    325331 
     332 
    326333    def save_properties(self, save_func, *names): 
    327334        """ 
     
    330337        for name in names: 
    331338            self.save_property(save_func, name) 
     339 
    332340 
    333341    def unlink(self): 
     
    345353 
    346354 
     355    def request_update(self): 
     356        """ 
     357        Placeholder for gaphor.Item's request_update() method. 
     358        """ 
     359        pass 
     360 
     361 
    347362    def draw(self, context): 
    348363        EditableTextSupport.draw(self, context) 
     
    352367        return self 
    353368 
    354     def on_subject_notify__appliedStereotype(self, subject, pspec=None): 
     369 
     370    def on_subject_notify(self, pspec, notifiers=()): 
     371        SubjectSupport.on_subject_notify(self, pspec, notifiers) 
     372 
     373    def on_presentation_subject(self, event): 
     374        for prop, handler in self._watched_properties.iteritems(): 
     375            if handler: 
     376                # Provide event? 
     377                handler(None) 
     378 
     379    def on_element_applied_stereotype(self, event): 
    355380        if self.subject: 
    356381            self.update_stereotype() 
    357382            self.request_update() 
    358383 
    359     def request_update(self): 
    360         """ 
    361         Placeholder for gaphor.Item's request_update() method. 
    362         """ 
    363         pass 
    364  
    365     def on_subject_notify(self, pspec, notifiers=()): 
    366         SubjectSupport.on_subject_notify(self, pspec, notifiers + ('appliedStereotype',)) 
     384 
     385    def add_watch(self, property, handler=None): 
     386        """ 
     387        Add a property (umlproperty) to be watched. a handler may be provided 
     388        that will be called with the event as argument (handler(event)). 
     389        """ 
     390        assert isinstance(property, UML.properties.umlproperty) 
     391        self._watched_properties[property] = handler 
     392 
     393 
     394    def register_handlers(self): 
     395        Application.register_handler(self.on_element_change) 
     396 
     397 
     398    def unregister_handlers(self): 
     399        Application.unregister_handler(self.on_element_change) 
     400 
     401 
     402    @component.adapter(UML.interfaces.IElementChangeEvent) 
     403    def on_element_change(self, event): 
     404        """ 
     405        Called when a model element has changed. 
     406        """ 
     407        if event.property in self._watched_properties: 
     408            handler = self._watched_properties[event.property] 
     409            if handler: 
     410                handler(event) 
     411            elif self.subject: 
     412                log.debug('on_element_changed') 
     413                self.request_update() 
    367414 
    368415 
  • gaphor/trunk/gaphor/diagram/diagramline.py

    r2013 r2116  
    2626    head = property(lambda self: self._handles[0]) 
    2727    tail = property(lambda self: self._handles[-1]) 
     28 
     29 
     30    def setup_canvas(self): 
     31        gaphas.Line.setup_canvas() 
     32        self.register_handlers() 
     33 
     34 
     35    def teardown_canvas(self): 
     36        gaphas.Line.teardown_canvas() 
     37        self.unregister_handlers() 
    2838 
    2939 
  • gaphor/trunk/gaphor/diagram/elementitem.py

    r1747 r2116  
    2424        self.auto_resize = 0 
    2525 
     26 
    2627    def save(self, save_func): 
    2728        save_func('matrix', tuple(self.matrix)) 
     
    3031        DiagramItem.save(self, save_func) 
    3132 
     33 
    3234    def load(self, name, value): 
    3335        if name == 'matrix': 
     
    3537        else: 
    3638            DiagramItem.load(self, name, value) 
     39 
     40 
     41    def setup_canvas(self): 
     42        gaphas.Element.setup_canvas() 
     43        self.register_handlers() 
     44 
     45 
     46    def teardown_canvas(self): 
     47        gaphas.Element.teardown_canvas() 
     48        self.unregister_handlers() 
     49 
    3750 
    3851    def pre_update(self, context): 
  • gaphor/trunk/gaphor/diagram/klass.py

    r2090 r2116  
    3434        self._attributes = self.create_compartment('attributes') 
    3535        self._operations = self.create_compartment('operations') 
     36 
     37        self.add_watch(UML.Class.ownedAttribute) 
     38        self.add_watch(UML.Class.ownedOperation) 
    3639 
    3740    def save(self, save_func): 
  • gaphor/trunk/gaphor/diagram/nameditem.py

    r1998 r2116  
    3535        # size of stereotype, namespace and name text 
    3636        self._header_size = 0, 0 
    37  
     37        self.add_watch(UML.NamedElement.namespace, self.on_named_element_namespace) 
     38        self.add_watch(UML.Namespace.name, self.on_named_element_namespace) 
    3839 
    3940    def is_namespace_info_visible(self): 
     
    5960 
    6061 
    61     def on_subject_notify(self, pspec, notifiers=()): 
    62         #log.debug('Class.on_subject_notify(%s, %s)' % (pspec, notifiers)) 
    63         ElementItem.on_subject_notify(self, pspec, 
    64                 ('namespace', 'namespace.name') + notifiers) 
    65         if self.subject: 
    66             self.on_subject_notify__namespace(self.subject) 
    67                                      
    68  
    69     def on_subject_notify__namespace(self, subject, pspec=None): 
     62    def on_named_element_namespace(self, event): 
    7063        """ 
    7164        Add a line '(from ...)' to the class item if subject's namespace 
     
    7871            self._from.text = '' 
    7972        self.request_update() 
    80  
    81  
    82     def on_subject_notify__namespace_name(self, subject, pspec=None): 
    83         """ 
    84         Change the '(from ...)' line if the namespace's name changes. 
    85         """ 
    86         self.on_subject_notify__namespace(subject, pspec) 
    8773 
    8874 
  • gaphor/trunk/gaphor/services/tests/test_filemanager.py

    r2061 r2116  
    88 
    99    def setUp(self): 
    10         Application.init(services=['file_manager', 'element_factory', 'properties', 'gui_manager']) 
     10        Application.init(services=['file_manager', 'element_factory', 'properties', 'gui_manager', 'action_manager']) 
    1111        self.recent_files_backup = Application.get_service('properties').get('recent-files') 
    1212 
  • gaphor/trunk/gaphor/tests/test_application.py

    r2060 r2116  
    1616        Load services. At lease the undo_manager should be available after that. 
    1717        """ 
    18         Application.init(['undo_manager', 'file_manager']) 
     18        Application.init(['undo_manager', 'file_manager', 'properties']) 
    1919 
    2020        self.assertTrue(Application.get_service('undo_manager') is not None) 
  • gaphor/trunk/gaphor/tests/test_storage.py

    r2052 r2116  
    1515from cStringIO import StringIO 
    1616 
    17 __module__ = 'test_storage' 
     17#__module__ = 'test_storage' 
    1818 
    1919class PseudoFile(object): 
  • gaphor/trunk/gaphor/ui/mainwindow.py

    r2100 r2116  
    470470    def quit(self): 
    471471        self.ask_to_close() and gtk.main_quit() 
     472        self._tree_view.get_model().close() 
     473        Application.unregister_handler(self._action_executed) 
    472474 
    473475    @action(name='tree-view-open', label='_Open') 
  • gaphor/trunk/gaphor/ui/namespace.py

    r2098 r2116  
    1212from zope import component 
    1313 
     14from gaphor.application import Application 
    1415from gaphor import UML 
    1516from gaphor.UML.event import ModelFactoryEvent, FlushFactoryEvent 
     
    7677        self.filter = _default_filter_list 
    7778 
    78         component.provideHandler(self.flush) 
    79         component.provideHandler(self.refresh) 
    80         component.provideHandler(self._on_element_change) 
    81         component.provideHandler(self._on_element_create) 
    82         component.provideHandler(self._on_element_delete) 
    83         component.provideHandler(self._on_association_set) 
     79        Application.register_handler(self.flush) 
     80        Application.register_handler(self.refresh) 
     81        Application.register_handler(self._on_element_change) 
     82        Application.register_handler(self._on_element_create) 
     83        Application.register_handler(self._on_element_delete) 
     84        Application.register_handler(self._on_association_set) 
    8485 
    8586        self._build_model() 
     87 
     88 
     89    def close(self): 
     90        """ 
     91        Close the namespace model, unregister handlers. 
     92        """ 
     93        Application.unregister_handler(self.flush) 
     94        Application.unregister_handler(self.refresh) 
     95        Application.unregister_handler(self._on_element_change) 
     96        Application.unregister_handler(self._on_element_create) 
     97        Application.unregister_handler(self._on_element_delete) 
     98        Application.unregister_handler(self._on_association_set) 
     99 
    86100 
    87101    def path_from_element(self, e): 
     
    96110            return () 
    97111 
     112 
    98113    def element_from_path(self, path): 
    99114        """ 
     
    109124            return None 
    110125 
     126 
    111127    @component.adapter(IAttributeChangeEvent) 
    112128    @catchall 
     
    148164                                        [original] * len(parent_nodes), 
    149165                                        parent_nodes)) 
     166 
    150167 
    151168    def _add_elements(self, element): 
     
    257274                self._remove_element(element) 
    258275 
     276 
    259277    @component.adapter(ModelFactoryEvent) 
    260278    def refresh(self, event=None): 
    261279        self.flush() 
    262280        self._build_model() 
     281 
    263282 
    264283    @component.adapter(FlushFactoryEvent) 
     
    268287        self._nodes = {None: []} 
    269288 
     289 
    270290    def _build_model(self): 
    271291        toplevel = self.factory.select(lambda e: isinstance(e, UML.Namespace) and not e.namespace) 
     
    274294            self._add_elements(element) 
    275295 
     296 
    276297    # TreeModel methods: 
    277298 
     
    282303        return 0 
    283304 
     305 
    284306    def on_get_n_columns(self): 
    285307        """ 
     
    288310        return 1 
    289311 
     312 
    290313    def on_get_column_type(self, index): 
    291314        """ 
     
    293316        """ 
    294317        return gobject.TYPE_PYOBJECT 
     318 
    295319 
    296320    def on_get_path(self, node): 
     
    300324        path = self.path_from_element(node) 
    301325        return path 
     326 
    302327 
    303328    def on_get_iter(self, path): 
     
    309334        return self.element_from_path(path) 
    310335 
     336 
    311337    def on_get_value(self, node, column): 
    312338        """ 
     
    315341        assert column == 0, 'column can only be 0' 
    316342        return node 
     343 
    317344 
    318345    def on_iter_next(self, node): 
     
    327354        except IndexError, e: 
    328355            return None 
     356 
    329357         
    330358    def on_iter_has_child(self, node): 
     
    334362        n = self._nodes.get(node) 
    335363        return n or len(n) > 0 
     364 
    336365 
    337366    def on_iter_children(self, node): 
     
    344373            pass 
    345374 
     375 
    346376    def on_iter_n_children(self, node): 
    347377        """ 
     
    349379        """ 
    350380        return len(self._nodes[node]) 
     381 
    351382 
    352383    def on_iter_nth_child(self, node, n): 
     
    359390        except TypeError, e: 
    360391            return None 
     392 
    361393 
    362394    def on_iter_parent(self, node): 
     
    435467        self.connect('drag-data-delete', NamespaceView.on_drag_data_delete) 
    436468 
     469 
    437470    def get_selected_element(self): 
    438471        selection = self.get_selection() 
     
    442475        return model.get_value(iter, 0) 
    443476 
     477 
    444478    def expand_root_nodes(self): 
    445479        self.expand_row((0,), False) 
     480 
    446481 
    447482    def _set_pixbuf(self, column, cell, model, iter, data): 
     
    491526            log.error('Could not create path from string "%s"' % path_str) 
    492527 
     528 
    493529#    def do_drag_begin (self, context): 
    494530#        print 'do_drag_begin' 
     531 
    495532 
    496533    def on_drag_data_get(self, context, selection_data, info, time): 
     
    509546                selection_data.set(selection_data.target, 8, element.name) 
    510547 
     548 
    511549    def on_drag_data_delete (self, context): 
    512550        """ 
     
    514552        """ 
    515553        self.emit_stop_by_name('drag-data-delete') 
     554 
    516555 
    517556    # Drop 
     
    569608                selection.select_path(path) 
    570609 
     610 
    571611    def on_drag_drop(self, context, x, y, time): 
    572612        """ 
     
    577617        return 1 
    578618 
     619 
    579620gobject.type_register(NamespaceModel) 
    580621gobject.type_register(NamespaceView) 
  • gaphor/trunk/gaphor/ui/tests/test_handletool.py

    r2051 r2116  
    2121 
    2222    def setUp(self): 
    23         Application.init(services=['adapter_loader', 'element_factory', 'gui_manager', 'properties_manager']) 
     23        Application.init(services=['adapter_loader', 'element_factory', 'gui_manager', 'properties_manager', 'action_manager', 'properties']) 
    2424        self.main_window = Application.get_service('gui_manager').main_window 
    2525 
  • gaphor/trunk/gaphor/ui/tests/test_namespace.py

    r1994 r2116  
    134134        m.name = 'm' 
    135135        assert ns._nodes.has_key(m) 
    136         assert ns.path_from_element(m) == (0,) 
    137         assert ns.element_from_path((0,)) is m 
     136        assert ns.path_from_element(m) == (1,) 
     137        assert ns.element_from_path((1,)) is m 
    138138 
    139139        a = factory.create(UML.Package) 
     
    142142        assert a in ns._nodes[None] 
    143143        assert m in ns._nodes 
    144         assert ns.path_from_element(a) == (0,), ns.path_from_element(a) 
    145         assert ns.path_from_element(m) == (1,), ns.path_from_element(m) 
     144        assert ns.path_from_element(a) == (1,), ns.path_from_element(a) 
     145        assert ns.path_from_element(m) == (2,), ns.path_from_element(m) 
    146146 
    147147        a.package = m 
     
    153153        assert a in m.ownedMember 
    154154        assert a.namespace is m 
    155         assert ns.path_from_element(a) == (0, 0), ns.path_from_element(a) 
     155        assert ns.path_from_element(a) == (1, 0), ns.path_from_element(a) 
    156156 
    157157        c = factory.create(UML.Class) 
    158158        c.name = 'c' 
    159159        assert c in ns._nodes 
    160         assert ns.path_from_element(c) == (0,)  
    161         assert ns.path_from_element(m) == (1,), ns.path_from_element(m) 
    162         assert ns.path_from_element(a) == (1, 0), ns.path_from_element(a) 
     160        assert ns.path_from_element(c) == (1,), ns.path_from_element(c) 
     161        assert ns.path_from_element(m) == (2,), ns.path_from_element(m) 
     162        assert ns.path_from_element(a) == (2, 0), ns.path_from_element(a) 
    163163 
    164164        c.package = m