| 1 |
""" |
|---|
| 2 |
""" |
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
import gtk |
|---|
| 6 |
from zope import interface, component |
|---|
| 7 |
|
|---|
| 8 |
from gaphor.core import _ |
|---|
| 9 |
from gaphor.UML.interfaces import IAssociationChangeEvent |
|---|
| 10 |
from gaphor.UML import Presentation |
|---|
| 11 |
from interfaces import IPropertyPage, IDiagramSelectionChange |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
class PropertyEditor(object): |
|---|
| 15 |
""" |
|---|
| 16 |
The Property Editor pane. |
|---|
| 17 |
""" |
|---|
| 18 |
|
|---|
| 19 |
def __init__(self): |
|---|
| 20 |
super(PropertyEditor, self).__init__() |
|---|
| 21 |
self._current_item = None |
|---|
| 22 |
self._last_tab = _('Properties') |
|---|
| 23 |
|
|---|
| 24 |
def construct(self): |
|---|
| 25 |
self.notebook = gtk.Notebook() |
|---|
| 26 |
self.notebook.set_scrollable(True) |
|---|
| 27 |
self.notebook.set_show_border(True) |
|---|
| 28 |
self.notebook.set_tab_pos(gtk.POS_LEFT) |
|---|
| 29 |
self.notebook.connect('switch-page', self._on_switch_page) |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
component.provideHandler(self._selection_change) |
|---|
| 33 |
component.provideHandler(self._element_changed) |
|---|
| 34 |
|
|---|
| 35 |
return self.notebook |
|---|
| 36 |
|
|---|
| 37 |
def create_tabs_for_item(self, item): |
|---|
| 38 |
""" |
|---|
| 39 |
Load all tabs that can operate on the given item. |
|---|
| 40 |
""" |
|---|
| 41 |
last_tab = self._last_tab |
|---|
| 42 |
for name, adapter in component.getAdapters([item,], IPropertyPage): |
|---|
| 43 |
self.notebook.prepend_page(adapter.construct(), gtk.Label(name)) |
|---|
| 44 |
self.notebook.show_all() |
|---|
| 45 |
|
|---|
| 46 |
self._last_tab = last_tab |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
for page_num in range(0, self.notebook.get_n_pages()): |
|---|
| 50 |
page = self.notebook.get_nth_page(page_num) |
|---|
| 51 |
label_text = self.notebook.get_tab_label_text(page) |
|---|
| 52 |
if label_text == self._last_tab: |
|---|
| 53 |
self.notebook.set_current_page(page_num) |
|---|
| 54 |
break |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
def clear_all_tabs(self): |
|---|
| 59 |
""" |
|---|
| 60 |
Remove all tabs from the notebook. |
|---|
| 61 |
""" |
|---|
| 62 |
last_tab = self._last_tab |
|---|
| 63 |
while self.notebook.get_n_pages(): |
|---|
| 64 |
self.notebook.remove_page(0) |
|---|
| 65 |
self._last_tab = last_tab |
|---|
| 66 |
|
|---|
| 67 |
def _on_switch_page(self, notebook, page, page_num): |
|---|
| 68 |
page = self.notebook.get_nth_page(page_num) |
|---|
| 69 |
label_text = self.notebook.get_tab_label_text(page) |
|---|
| 70 |
self._last_tab = label_text |
|---|
| 71 |
|
|---|
| 72 |
@component.adapter(IDiagramSelectionChange) |
|---|
| 73 |
def _selection_change(self, event): |
|---|
| 74 |
""" |
|---|
| 75 |
Called when a diagram item receives focus. |
|---|
| 76 |
|
|---|
| 77 |
This reloads all tabs based on the current selection. |
|---|
| 78 |
""" |
|---|
| 79 |
item = event.focused_item |
|---|
| 80 |
if item is self._current_item: |
|---|
| 81 |
return |
|---|
| 82 |
|
|---|
| 83 |
self._current_item = item |
|---|
| 84 |
self.clear_all_tabs() |
|---|
| 85 |
|
|---|
| 86 |
if item is None: |
|---|
| 87 |
return |
|---|
| 88 |
self.create_tabs_for_item(item) |
|---|
| 89 |
|
|---|
| 90 |
@component.adapter(IAssociationChangeEvent) |
|---|
| 91 |
def _element_changed(self, event): |
|---|
| 92 |
element = event.element |
|---|
| 93 |
if event.property is Presentation.subject: |
|---|
| 94 |
if element is self._current_item: |
|---|
| 95 |
self.clear_all_tabs() |
|---|
| 96 |
self.create_tabs_for_item(self._current_item) |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|