public function replace(SwatWidget $widget, SwatWidget $new_widget)
    {
        foreach ($this->children as $key => $child_widget) {
            if ($child_widget === $widget) {
                $this->children[$key] = $new_widget;
                $new_widget->parent = $this;
                $widget->parent = null;

                if ($widget->id !== null) {
                    unset($this->children_by_id[$widget->id]);
                }

                if ($new_widget->id !== null) {
                    $this->children_by_id[$new_widget->id] = $new_widget;
                }

                return $widget;
            }
        }
        return null;
    }