 
   # triggered by clicking a tab's close button
   closeTab: (tab, event) =>
-    remove = (restore = false) =>
-      tab.restore() if restore
-      tabIndex = @tabs.indexOf(tab)
-      selectOther = tab.active()
-      @tabs.remove(tab)
-      if selectOther
-        otherTab = @tabs()[tabIndex]
-        if not otherTab? and tabIndex > 0
-          otherTab = @tabs()[tabIndex-1]
-        @selectTab(otherTab) if otherTab?
-
-      if @tabs().length == 0
-        @selectedTab null
-
     if tab.dirty()
       bootbox.confirm "Are you sure you want to close this #{tab.type}?  Your unsaved changes will be lost.", (close) =>
-        remove(true) if close
+        @removeTab(tab) if close
     else
-      remove()
+      @removeTab tab
 
     event.stopPropagation()
 

    @socket.emit 'get_tree', null, (tree) =>
      @objects tree.map (o) => new TreeNode o, @

  findInTree: (id) ->
    find = (id, o) ->
      if o.id == id


  new_object: (obj) =>
    obj.children = []
    @insertIntoTree obj

  rm_object: (id) =>
    @removeFromTree id
    @removeFromTabs id
    if @selectedObject().id is id
      @selectedObject null

  object_parent_changed: (spec) =>
    o = @removeFromTree spec.id
    o.parent_id = spec.parent_id
    @insertIntoTree o

  object_name_changed: (spec) =>
    o = @findInTree spec.id
