text: /**********************************************************************
  *  Copyright (c) 2008-2014, Alliance for Sustainable Energy.  
  *  All rights reserved.
  *  
  *  This library is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU Lesser General Public
  *  License as published by the Free Software Foundation; either
  *  version 2.1 of the License, or (at your option) any later version.
  *  
  *  This library is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  *  Lesser General Public License for more details.
  *  
  *  You should have received a copy of the GNU Lesser General Public
  *  License along with this library; if not, write to the Free Software
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  **********************************************************************/
  
  #include <openstudio_lib/SpaceTypesController.hpp>
  #include <openstudio_lib/SpaceTypesView.hpp>
  
  #include <model/Model.hpp>
  #include <model/SpaceType.hpp>
  #include <model/SpaceType_Impl.hpp>
  #include <model/Space.hpp>
  #include <model/Component.hpp>
  #include <model/Component_Impl.hpp>
  #include <model/ComponentData.hpp>
  #include <model/ComponentData_Impl.hpp>
  
  #include <utilities/core/Assert.hpp>
  
  namespace openstudio {
  
  SpaceTypesController::SpaceTypesController(const model::Model& model)
    : ModelSubTabController(new SpaceTypesView(model), model)
  {
  }
  
  
  void SpaceTypesController::onAddObject(const openstudio::IddObjectType& iddObjectType)
  {
    OS_ASSERT(IddObjectType::OS_SpaceType == iddObjectType.value());
    openstudio::model::SpaceType(this->model());
  }
  
  void SpaceTypesController::onCopyObject(const openstudio::model::ModelObject& modelObject)
  {
    modelObject.clone(this->model());
  }
  
  void SpaceTypesController::onRemoveObject(openstudio::model::ModelObject modelObject)
  {
    modelObject.remove();
  }
  
  void SpaceTypesController::onReplaceObject(openstudio::model::ModelObject modelObject, const OSItemId& replacementItemId)
  {
    // not yet implemented
  }
  
  void SpaceTypesController::onPurgeObjects(const openstudio::IddObjectType& iddObjectType)
  {
    BOOST_FOREACH(model::SpaceType spaceType, this->model().getConcreteModelObjects<model::SpaceType>()){
      if (spaceType.spaces().empty()){
        spaceType.remove();
      }
    }
  }
  
  void SpaceTypesController::onDrop(const OSItemId& itemId)
  {
    boost::optional<model::ModelObject> modelObject = this->getModelObject(itemId);
    if (modelObject){
      if (modelObject->optionalCast<model::SpaceType>()){
        if (this->fromComponentLibrary(itemId)){
          modelObject = modelObject->clone(this->model());
        }
      }
    }else{
      boost::optional<model::Component> component = this->getComponent(itemId);
      if (component){
        if (component->primaryObject().optionalCast<model::SpaceType>()){
          boost::optional<model::ComponentData> componentData = this->model().insertComponent(*component);
        }
      }
    }
  }
  
  void SpaceTypesController::onInspectItem(OSItem* item)
  {
  }
  
  } // openstudio
  
  