Repository: gaphor/gaphor
Issue: Exceptions when Renaming or Creating New Package or Diagram

### I'm submitting a...
- [X] Bug report  <!-- Please search GitHub for a similar issue or PR before submitting -->
- [ ] Feature request
- [ ] Documentation issue or request

### Current behavior
It doesn't work to rename elements, or create new diagrams or packages in the Namespace.

### Expected behavior
Operations work as expected.

### Minimal reproduction of the problem with instructions
Create a new model. Right click on the root package, and select New package.

```
gaphor.transaction ERROR Transaction terminated due to an exception, performing a rollback
Traceback (most recent call last):
  File "/app/lib/python3.7/site-packages/gaphor/transaction.py", line 27, in _transactional
    r = func(*args, **kwargs)
  File "/app/lib/python3.7/site-packages/gaphor/ui/namespace.py", line 600, in tree_view_create_diagram
    self.select_element(diagram)
  File "/app/lib/python3.7/site-packages/gaphor/ui/namespace.py", line 552, in select_element
    self._namespace.get_model().path_from_element(element)
AttributeError: 'TreeModelSort' object has no attribute 'path_from_element'
Traceback (most recent call last):
  File "/app/lib/python3.7/site-packages/gaphor/action.py", line 179, in _action_activate
    method()
  File "/app/lib/python3.7/site-packages/gaphor/transaction.py", line 27, in _transactional
    r = func(*args, **kwargs)
  File "/app/lib/python3.7/site-packages/gaphor/ui/namespace.py", line 600, in tree_view_create_diagram
    self.select_element(diagram)
  File "/app/lib/python3.7/site-packages/gaphor/ui/namespace.py", line 552, in select_element
    self._namespace.get_model().path_from_element(element)
AttributeError: 'TreeModelSort' object has no attribute 'path_from_element'
``

### System Information
OS:
- [X] Linux (Please put in notes the specific distro)
- [ ] MacOS
- [ ] Windows 10
- [ ] Windows 7

NOTES:


Changed files:
gaphor/ui/namespace.py, gaphor/ui/mainwindow.py

diff --git a/gaphor/ui/mainwindow.py b/gaphor/ui/mainwindow.py
index c447233d2..78c539c1c 100644
--- a/gaphor/ui/mainwindow.py
+++ b/gaphor/ui/mainwindow.py
@@ -582,7 +582,6 @@ class Diagrams(UIComponent, ActionProvider):
             for page in range(0, self._notebook.get_n_pages()):
                 widget = self._notebook.get_nth_page(page)
                 if event.element is widget.diagram_page.diagram:
-                    print("Name change", event.__dict__)
                     self._notebook.set_tab_label(
                         widget, self.tab_label(event.new_value, widget)
                     )
diff --git a/gaphor/ui/namespace.py b/gaphor/ui/namespace.py
index 5a0f40b24..a41780e9f 100644
--- a/gaphor/ui/namespace.py
+++ b/gaphor/ui/namespace.py
@@ -398,6 +398,15 @@ class Namespace(UIComponent, ActionProvider):
         return scrolled_window
 
     def iter_for_element(self, element, old_namespace=0):
+        """Get the Gtk.TreeIter for an element in the Namespace.
+
+        Args:
+            element: The element contained in the in the Namespace.
+            old_namespace: The old namespace containing the element, optional.
+
+        Returns: Gtk.TreeIter object
+        """
+
         # Using `0` as sentinel
         if old_namespace != 0:
             parent_iter = self.iter_for_element(old_namespace)
@@ -466,7 +475,8 @@ class Namespace(UIComponent, ActionProvider):
         element = event.element
         if type(element) in self.filter:
             iter = self.iter_for_element(element)
-            # iter should be here, unless we try to delete an element who's parent element is already deleted, so let's be lenient.
+            # iter should be here, unless we try to delete an element who's
+            # parent element is already deleted, so let's be lenient.
             if iter:
                 self.model.remove(iter)
 
@@ -546,18 +556,22 @@ class Namespace(UIComponent, ActionProvider):
         self.close()
 
     def select_element(self, element):
-        """
-        Select an element from the Namespace view.
+        """Select an element from the Namespace view.
+
         The element is selected. After this an action may be executed,
         such as OpenModelElement, which will try to open the element (if it's
         a Diagram).
         """
-        path = Gtk.TreePath.new_from_indices(
-            self._namespace.get_model().path_from_element(element)
-        )
-        # Expand the first row:
-        if len(path.get_indices()) > 1:
-            self._namespace.expand_row(path=path, open_all=False)
+
+        tree_iter = self.iter_for_element(element)
+        path = self.model.get_path(tree_iter)
+        path_indices = path.get_indices()
+
+        # Expand the parent row
+        if len(path_indices) > 1:
+            parent_path = Gtk.TreePath.new_from_indices(path_indices[:-1])
+            self._namespace.expand_row(path=parent_path, open_all=False)
+
         selection = self._namespace.get_selection()
         selection.select_path(path)
         self._on_view_cursor_changed(self._namespace)
@@ -577,7 +591,9 @@ class Namespace(UIComponent, ActionProvider):
         view = self._namespace
         element = view.get_selected_element()
         if element is not None:
-            path = view.get_model().path_from_element(element)
+            selection = view.get_selection()
+            model, iter = selection.get_selected()
+            path = model.get_path(iter)
             column = view.get_column(0)
             cell = column.get_cells()[1]
             cell.set_property("editable", 1)