Changeset 1239

Show
Ignore:
Timestamp:
04/20/07 02:02:47 (1 year ago)
Author:
arj..@yirdis.nl
Message:

Made ElementFactory? a service. Several updates that use the element_factory service instead of the resource(). Updates so the ui module doesn't depend on resource() anymore (except accelmap.py).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gaphor/trunk/gaphor/UML

    • Property svn:ignore set to
      uml2.py
  • gaphor/trunk/gaphor/UML/__init__.py

    r1121 r1239  
    44from gaphor.UML.elementfactory import ElementFactory 
    55 
    6 # Make one ElementFactory instance an application-wide resource 
    7 from gaphor import resource 
    8 _default_element_factory = resource(ElementFactory) 
    9 del resource 
    106 
    117# Make some elements of the default ElementFactory easely accessable 
     
    1410    """Create a new model element in the default ElementFactory. 
    1511    """ 
    16     return _default_element_factory.create(type) 
     12    from gaphor.application import Application 
     13    return Application.get_service('element_factory').create(type) 
    1714 
    1815def lookup(id): 
    1916    """Find the element with id in the default ElementFactory. 
    2017    """ 
    21     return _default_element_factory.lookup(id) 
     18    from gaphor.application import Application 
     19    return Application.get_service('element_factory').lookup(id) 
    2220 
    2321def select(expression=None): 
     
    2523    Returns an iterator. 
    2624    """ 
    27     return _default_element_factory.select(expression) 
     25    from gaphor.application import Application 
     26    return Application.get_service('element_factory').select(expression) 
    2827 
    2928def lselect(expression=None): 
     
    3130    Returns a list of elements. 
    3231    """ 
    33     return _default_element_factory.lselect(expression) 
     32    from gaphor.application import Application 
     33    return Application.get_service('element_factory').lselect(expression) 
    3434 
    3535def flush(): 
    36     _default_element_factory.flush() 
     36    from gaphor.application import Application 
     37    Application.get_service('element_factory').flush() 
    3738 
    3839def swap_element(element, new_class): 
    3940    """Swap the class for an element 
    4041    """ 
    41     _default_element_factory.swap_element(element, new_class) 
     42    from gaphor.application import Application 
     43    Application.get_service('element_factory').swap_element(element, new_class) 
    4244 
    4345if 0 and __debug__:  
  • gaphor/trunk/gaphor/actions/diagramactions.py

    r1219 r1239  
    77import gaphas 
    88 
    9 from gaphor import resource 
    109from gaphor import UML 
     10from gaphor.core import inject 
    1111from gaphor.transaction import Transaction, transactional 
    1212from gaphor.misc.action import Action, CheckAction, RadioAction 
     
    192192 
    193193 
     194copy_buffer = [] 
     195 
    194196class CopyAction(Action): 
    195197    """Copy/Cut/Paste functionality required a lot of thinking: 
    196198 
    197     Store a list of DiagramItems that have to be copied in resource 
     199    Store a list of DiagramItems that have to be copied in a global 
    198200    'copy-buffer'. 
    199201 
     
    227229                #i.save(save_func) 
    228230            if copy_items: 
    229                 resource.set('copy-buffer', copy_items) 
     231                global copy_buffer 
     232                copy_buffer = copy_items 
    230233        tab = self._window.get_current_diagram_tab() 
    231234 
     
    236239    """Copy/Cut/Paste functionality required a lot of thinking: 
    237240 
    238     Create a copy of DiagramItems that have to be copied in resource 
     241    Create a copy of DiagramItems that have to be copied in a global 
    239242    'copy-buffer'. 
    240243 
     
    252255    stock_id = 'gtk-paste' 
    253256 
    254     def init(self, window): 
    255         self._window = window 
    256  
    257     def update(self): 
    258         diagram_tab = self._window.get_current_diagram_tab() 
    259         self.sensitive = diagram_tab and resource('copy-buffer', []) 
     257    element_factory = inject('element_factory') 
     258 
     259    def init(self, window): 
     260        self._window = window 
     261 
     262    def update(self): 
     263        global copy_buffer 
     264        diagram_tab = self._window.get_current_diagram_tab() 
     265        self.sensitive = diagram_tab and copy_buffer 
    260266 
    261267    def _load_element(self, name, value): 
     
    268274            self._item.load(name, item) 
    269275        else: 
    270             item = self._factory.lookup(value.id) 
     276            item = self.element_factory.lookup(value.id) 
    271277            if item: 
    272278                self._item.load(name, item) 
     
    286292    @transactional 
    287293    def execute(self): 
     294        global copy_buffer 
    288295        view = self._window.get_current_diagram_view() 
    289296        diagram = self._window.get_current_diagram() 
     
    296303            return 
    297304 
    298         copy_items = [ c for c in resource('copy-buffer', []) if c.canvas ] 
     305        copy_items = [ c for c in copy_buffer if c.canvas ] 
    299306 
    300307        # Mapping original id -> new item 
    301308        self._new_items = {} 
    302         self._factory = resource(UML.ElementFactory) 
    303309 
    304310        # Create new id's that have to be used to create the items: 
  • gaphor/trunk/gaphor/actions/itemactions.py

    r1221 r1239  
    44""" 
    55 
    6 from gaphor import GaphorError, resource 
    76from gaphor import UML 
     7from gaphor.core import inject 
    88from gaphor.diagram import items 
    99from gaphor.transaction import transactional 
     
    1212import gaphas 
    1313 
    14 class NoFocusItemError(GaphorError): 
     14class NoFocusItemError(Exception): 
    1515    pass 
    1616 
     
    176176    tooltip = 'Create a new attribute' 
    177177 
     178    element_factory = inject('element_factory') 
     179 
    178180    def init(self, window): 
    179181        self._window = window 
     
    194196        subject = focus_item.subject 
    195197        assert isinstance(subject, (UML.Class, UML.Interface)) 
    196         elemfact = resource(UML.ElementFactory) 
    197198         
    198         attribute = elemfact.create(UML.Property) 
     199        attribute = self.element_factory.create(UML.Property) 
    199200        attribute.parse('new') 
    200201        subject.ownedAttribute = attribute 
     
    219220    tooltip = 'Create a new operation' 
    220221 
     222    element_factory = inject('element_factory') 
     223 
    221224    def init(self, window): 
    222225        self._window = window 
     
    237240        subject = focus_item.subject 
    238241        assert isinstance(subject, UML.Classifier) 
    239         elemfact = resource(UML.ElementFactory) 
    240  
    241         operation = elemfact.create(UML.Operation) 
     242 
     243        operation = self.element_factory.create(UML.Operation) 
    242244        operation.parse('new()') 
    243245        subject.ownedOperation = operation 
  • gaphor/trunk/gaphor/actions/mainactions.py

    r1237 r1239  
    227227 
    228228    def on_element_factory(self, *args): 
    229         #element_factory = resource(UML.ElementFactory) 
    230229        if self.element_factory.values(): 
    231230            self.sensitive = True 
     
    460459    stock_id = 'gaphor-diagram' 
    461460 
     461    element_factory = inject('element_factory') 
     462 
    462463    def init(self, window): 
    463464        self._window = window 
     
    469470    def execute(self): 
    470471        element = self._window.get_tree_view().get_selected_element() 
    471         diagram = resource(UML.ElementFactory).create(UML.Diagram) 
     472        diagram = self.element_factory.create(UML.Diagram) 
    472473        diagram.package = element 
    473474 
  • gaphor/trunk/gaphor/actions/placementactions.py

    r1233 r1239  
    8080    group = 'placementtools' 
    8181 
     82    element_factory = inject('element_factory') 
     83 
    8284    def init(self, window): 
    8385        self._window = window 
     
    9092        subject = None 
    9193        if self.subject_type: 
    92             subject = UML.create(self.subject_type) 
     94            subject = self.element_factory.create(self.subject_type) 
    9395        diagram = self._window.get_current_diagram() 
    9496        return diagram.create(self.type, subject=subject) 
  • gaphor/trunk/gaphor/actions/tests/test_itemactions.py

    r1121 r1239  
    33 
    44from gaphor import resource, UML 
     5from gaphor.application import Application 
    56from gaphor.ui.mainwindow import MainWindow 
    67from gaphor.diagram import items 
     
    1112class ItemActionTestCase(unittest.TestCase): 
    1213 
    13     main_window = resource(MainWindow) 
    14     try: 
    15         main_window.construct() 
    16     except: 
    17         pass 
     14#    main_window = resource(MainWindow) 
     15#    try: 
     16#        main_window.construct() 
     17#    except: 
     18#        pass 
    1819 
    1920    def setUp(self): 
    2021        self.diagram = UML.create(UML.Diagram) 
    2122        self.main_window.show_diagram(self.diagram) 
     23        Application.init() 
     24        self.main_window = Application.get_service('gui_manager').main_window 
     25 
    2226        self.class_ = self.diagram.create(items.ClassItem, subject=UML.create(UML.Class)) 
    2327 
  • gaphor/trunk/gaphor/actions/tests/test_placementactions.py

    r1121 r1239  
    22import unittest 
    33from gaphor import resource 
    4 from gaphor.ui.mainwindow import MainWindow 
     4resource('DataDir', '') 
     5from gaphor.application import Application 
     6#from gaphor.ui.mainwindow import MainWindow 
    57from gaphor.diagram.tool import PlacementTool 
    68from gaphor.actions import placementactions 
     
    1214Event = Context 
    1315 
    14  
    1516class PlacementToolTestCase(unittest.TestCase): 
    1617 
    17     main_window = resource(MainWindow
    18     try: 
    19         main_window.construct() 
    20     except: 
    21         pass 
     18#    main_window = MainWindow(
     19#    try: 
     20#        main_window.construct() 
     21#    except: 
     22#        pass 
    2223 
    2324    def setUp(self): 
    24         pass 
     25        Application.init() 
     26        self.main_window = Application.get_service('gui_manager').main_window 
    2527 
    2628    def tearDown(self): 
    27         pass 
     29        Application.shutdown() 
    2830 
    2931    def do_test_placement(self, action): 
  • gaphor/trunk/gaphor/adapters/connectors.py

    r1126 r1239  
    99from gaphas import constraint 
    1010from gaphor import UML 
     11from gaphor.core import inject 
    1112from gaphor.diagram.interfaces import IConnect 
    1213from gaphor.diagram import items 
     
    307308    """ 
    308309 
     310    element_factory = inject('element_factory') 
     311 
    309312    def relationship(self, required_type, head, tail): 
    310313        """ 
     
    367370        if not relation: 
    368371            line = self.line 
    369             relation = UML.create(type) 
     372            relation = self.element_factory.create(type) 
    370373            setattr(relation, head[0], line.head.connected_to.subject) 
    371374            setattr(relation, tail[0], line.tail.connected_to.subject) 
     
    737740            # Find all associations and determine if the properties on 
    738741            # the association ends have a type that points to the class. 
    739             for assoc in UML.select(): 
     742            for assoc in self.element_factory.select(): 
    740743                if isinstance(assoc, UML.Extension): 
    741744                    end1 = assoc.memberEnd[0] 
     
    753756            else: 
    754757                # Create a new Extension relationship 
    755                 relation = UML.create(UML.Extension) 
    756                 head_end = UML.create(UML.Property) 
    757                 tail_end = UML.create(UML.ExtensionEnd) 
     758                relation = self.element_factory.create(UML.Extension) 
     759                head_end = self.element_factory.create(UML.Property) 
     760                tail_end = self.element_factory.create(UML.ExtensionEnd) 
    758761                relation.package = element.canvas.diagram.namespace 
    759762                relation.memberEnd = head_end 
     
    828831            # Find all associations and determine if the properties on 
    829832            # the association ends have a type that points to the class. 
    830             for assoc in UML.select(): 
     833            for assoc in self.element_factory.select(): 
    831834                if isinstance(assoc, UML.Association): 
    832835                    end1 = assoc.memberEnd[0] 
     
    850853            else: 
    851854                # Create a new Extension relationship 
    852                 relation = UML.create(UML.Association) 
    853                 head_end = UML.create(UML.Property) 
    854                 head_end.lowerValue = UML.create(UML.LiteralSpecification) 
    855                 tail_end = UML.create(UML.Property) 
    856                 tail_end.lowerValue = UML.create(UML.LiteralSpecification) 
     855                relation = self.element_factory.create(UML.Association) 
     856                head_end = self.element_factory.create(UML.Property) 
     857                head_end.lowerValue = self.element_factory.create(UML.LiteralSpecification) 
     858                tail_end = self.element_factory.create(UML.Property) 
     859                tail_end.lowerValue = self.element_factory.create(UML.LiteralSpecification) 
    857860                relation.package = element.canvas.diagram.namespace 
    858861                relation.memberEnd = head_end 
     
    925928                        ('target', 'incoming')) 
    926929        if not relation.guard: 
    927             relation.guard = UML.create(UML.LiteralSpecification) 
     930            relation.guard = self.element_factory.create(UML.LiteralSpecification) 
    928931        line.subject = relation 
    929932        opposite = line.opposite(handle) 
     
    9981001        subject = element.subject 
    9991002        if len(subject.incoming) > 1 and len(subject.outgoing) < 2: 
    1000             UML.swap_element(subject, join_node_class) 
     1003            self.element_factory.swap_element(subject, join_node_class) 
    10011004            element.request_update() 
    10021005        elif len(subject.incoming) < 2 and len(subject.outgoing) > 1: 
    1003             UML.swap_element(subject, fork_node_class) 
     1006            self.element_factory.swap_element(subject, fork_node_class) 
    10041007            element.request_update() 
    10051008        elif not element.combined and len(subject.incoming) > 1 and len(subject.outgoing) > 1: 
     
    10121015                flow_class = UML.ControlFlow 
    10131016             
    1014             UML.swap_element(join_node, join_node_class) 
    1015             fork_node = UML.create(fork_node_class) 
     1017            self.element_factory.swap_element(join_node, join_node_class) 
     1018            fork_node = self.element_factory.create(fork_node_class) 
    10161019            for flow in list(join_node.outgoing): 
    10171020                flow.source = fork_node 
    1018             flow = UML.create(flow_class) 
     1021            flow = self.element_factory.create(flow_class) 
    10191022            flow.source = join_node 
    10201023            flow.target = fork_node 
     
    10481051                if len(join_node.outgoing) > 1: 
    10491052                    assert len(join_node.incoming) < 2 
    1050                     UML.swap_element(join_node, fork_node_class) 
     1053                    self.element_factory.swap_element(join_node, fork_node_class) 
    10511054                element.combined = None 
    10521055 
  • gaphor/trunk/gaphor/adapters/editors.py

    r1211 r1239  
    99from gaphas import constraint 
    1010from gaphor import UML 
     11from gaphor.core import inject 
    1112from gaphor.UML.umllex import render_attribute 
    1213from gaphor.diagram.interfaces import IEditor 
     
    251252    component.adapts(items.ForkNodeItem) 
    252253 
     254    element_factory = inject('element_factory') 
     255 
    253256    def __init__(self, item): 
    254257        self._item = item 
     
    276279        if not spec: 
    277280            from gaphor import resource 
    278             factory = resource(UML.ElementFactory) 
    279             spec = self._item.subject.joinSpec = factory.create(UML.LiteralSpecification) 
     281            spec = self._item.subject.joinSpec = self.element_factory.create(UML.LiteralSpecification) 
    280282            spec.value = text 
    281283 
  • gaphor/trunk/gaphor/adapters/tests/test_connector.py

    r1233 r1239  
    66from zope import component 
    77from gaphor import UML 
     8from gaphor.application import Application 
    89from gaphor.diagram import items 
    910from gaphor.diagram.interfaces import IConnect 
     
    1112# Ensure adapters are loaded 
    1213import gaphor.adapters 
     14 
     15Application.load_services() 
    1316 
    1417class ConnectorTestCase(unittest.TestCase): 
  • gaphor/trunk/gaphor/application.py

    r1232 r1239  
    3131        """ 
    3232        self.load_services() 
     33        self.init_all_services() 
    3334 
    3435    def load_services(self): 
     
    4950 
    5051        # HACK: implicitly add UML.ElementFactory for now. 
    51         from gaphor import UML 
    52         self._uninitialized_services['element_factory'] = resource(UML.ElementFactory) 
     52#        from gaphor import UML 
     53#        self._uninitialized_services['element_factory'] = resource(UML.ElementFactory) 
    5354        # /HACK 
    5455 
     56    def init_all_services(self): 
    5557        while self._uninitialized_services: 
    5658            self.init_service(self._uninitialized_services.iterkeys().next()) 
  • gaphor/trunk/gaphor/data/plugins/checkmetamodel/checkmodel.py

    r565 r1239  
    44from gaphor import storage 
    55from gaphor import UML 
     6from gaphor.core import inject 
    67from os import path 
    78 
  • gaphor/trunk/gaphor/services/guimanager.py

    r1221 r1239  
    3434        # Load stock items 
    3535        import gaphor.ui.stock 
     36        gaphor.ui.stock.load_stock_icons() 
    3637 
    3738    def init_main_window(self): 
     
    4142 
    4243        load_accel_map() 
    43         #self._main_window = MainWindow() 
     44        self._main_window = MainWindow() 
    4445        # Backwards compat 
    4546        # TODO: remove 
    4647        from gaphor import resource 
    47         self._main_window = resource(MainWindow) 
     48        resource.set(MainWindow, self._main_window) 
     49        resource.set('MainWindow', self._main_window) 
    4850        self._main_window.construct() 
    4951 
     
    5658 
    5759    def shutdown(self): 
     60        #self._main_window.close() 
    5861        from gaphor.ui.accelmap import save_accel_map 
    5962        save_accel_map() 
  • gaphor/trunk/gaphor/services/pluginmanager.py

    r1218 r1239  
    2727import glob 
    2828import sys 
     29import pkg_resources 
    2930from xml.sax import handler, make_parser 
    3031 
     
    4344# Directories to look for plugins. These sirectories are added to the 
    4445# search path. User provided plugins overrule system plugins. 
    45 DEFAULT_PLUGIN_DIRS = [os.path.join(resource('DataDir'), 'plugins'), 
    46                        os.path.join(resource('UserDataDir'), 'plugins')] 
     46# TODO: fix: 
     47#DEFAULT_PLUGIN_DIRS = [os.path.join(resource('DataDir'), 'plugins'), 
     48#                       os.path.join(resource('UserDataDir'), 'plugins')] 
    4749 
    4850#log.debug('sys.path=' + str(sys.path)) 
     
    9395        return True 
    9496 
    95     def import_plugin(self): 
     97    def import_plugin(self, importer): 
    9698        """ 
    9799        Do the actual import of the plugin module. 
     
    99101        #mod = __import__(self.path.split(os.sep)[-1], globals(), locals(), []) 
    100102        name = os.path.split(self.path)[1] 
    101         f, n, d = imp.find_module(name, DEFAULT_PLUGIN_DIRS) 
    102         mod = imp.load_module(MODULENS + name, f, n, d
     103 
     104        mod = importer(name
    103105        self.module = mod 
    104106        self.initialized = True 
     
    323325            return 
    324326 
     327        plugin_dir = pkg_resources.resource_filename('gaphor', 'data/plugins') 
     328 
    325329        # Load the plugins in reverse order, so the user plugins will 
    326330        # overwrite the default plugins. (they are imported in sys.path as 
    327331        # [user plugins, default plugins]). 
    328         for plugin_dir in DEFAULT_PLUGIN_DIRS: 
    329             self.load_plugins_from_dir(plugin_dir) 
     332        #for plugin_dir in plugin_dir: 
     333        self.load_plugins_from_dir(plugin_dir) 
     334 
     335        def plugin_importer(name): 
     336            f, n, d = imp.find_module(name, [plugin_dir]) 
     337            return imp.load_module(MODULENS + name, f, n, d) 
    330338 
    331339        import_done = True 
     
    335343                if plugin.requirements_met(self): 
    336344                    try: 
    337                         plugin.import_plugin(
     345                        plugin.import_plugin(plugin_importer
    338346                    except Exception, e: 
    339347                        plugin.status = 'Failed to load plugin %s: %s' % (plugin.name, e) 
  • gaphor/trunk/gaphor/ui/diagramtab.py

    r1233 r1239  
    55 
    66from gaphor import UML 
    7 from gaphor import resource 
    87from gaphor.core import inject 
    98from gaphor.i18n import _ 
  • gaphor/trunk/gaphor/ui/mainwindow.py

    r1233 r1239  
    44import gtk 
    55 
    6 from gaphor import resource 
    76from gaphor import UML 
    87from gaphor.core import inject 
     
    472471@component.adapter(IServiceEvent) 
    473472def on_undo(*args): 
    474     resource(MainWindow).execute_action('UndoStack') 
     473    from gaphor.application import Application 
     474    Application.get_service('gui_manager').main_window.execute_action('UndoStack') 
    475475 
    476476component.provideHandler(on_undo) 
  • gaphor/trunk/gaphor/ui/namespace.py

    r1233 r1239  
    1212 
    1313from gaphor import UML 
    14 from gaphor import resource 
    1514from gaphor.transaction import Transaction 
    1615 
  • gaphor/trunk/gaphor/ui/stock.py

    r930 r1239  
    33 
    44import os.path 
     5import pkg_resources 
    56from xml.sax import handler 
    67import gtk 
    78 
    8 from gaphor import resource 
    99from gaphor import UML 
    1010from gaphor.parser import ParserException 
     
    117117    from xml.sax import make_parser 
    118118    parser = make_parser() 
    119     data_dir = resource('DataDir') 
    120     icon_dir = os.path.join(data_dir, 'pixmaps') 
     119    icon_dir = os.path.abspath(pkg_resources.resource_filename('gaphor', 'data/pixmaps')) 
     120    log.info('Icon dir: %s' % icon_dir) 
     121    #icon_dir = 'gaphor/data/pixmaps' 
    121122    loader = StockIconLoader(icon_dir) 
    122123 
     
    124125    parser.setContentHandler(loader) 
    125126 
    126     filename = os.path.join(data_dir, 'icons.xml') 
     127    filename = pkg_resources.resource_filename('gaphor', 'data/icons.xml') 
    127128    if os.name == 'nt' and data_dir[1] == ':': 
    128129        # Make the filename a full URL 
    129130        filename = 'file:' + filename.replace('\\\\', '/') 
     131    #try: 
    130132    parser.parse(filename) 
     133    #except IOError, e: 
     134    #    log.error('Unable to load icons', e) 
    131135 
    132 load_stock_icons() 
     136#load_stock_icons() 
    133137 
    134138# vim:sw=4:et 
  • gaphor/trunk/setup.py

    r1228 r1239  
    8383            'gui_manager = gaphor.services.guimanager:GUIManager', 
    8484            'adapter_loader = gaphor.services.adapterloader:AdapterLoader', 
     85            'element_factory = gaphor.UML.elementfactory:ElementFactory', 
    8586        ], 
    8687        'gaphor.adapters': [