00001 /* 00002 * Copyright (C) 2003-2009 Olivier Boudeville 00003 * 00004 * This file is part of the Ceylan library. 00005 * 00006 * The Ceylan library is free software: you can redistribute it and/or modify 00007 * it under the terms of either the GNU Lesser General Public License or 00008 * the GNU General Public License, as they are published by the Free Software 00009 * Foundation, either version 3 of these Licenses, or (at your option) 00010 * any later version. 00011 * 00012 * The Ceylan library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License and the GNU General Public License 00016 * for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License and the GNU General Public License along with the Ceylan library. 00020 * If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 * Author: Olivier Boudeville (olivier.boudeville@esperide.com) 00023 * 00024 */ 00025 00026 00027 #include "CeylanGenericModel.h" 00028 00029 #include "CeylanGenericView.h" 00030 #include "CeylanOperators.h" 00031 00032 00033 using namespace Ceylan ; 00034 using namespace Ceylan::MVC ; 00035 00036 00037 using std::string ; 00038 00039 00040 00041 /* 00042 * In this file following classes are implemented: 00043 * 00044 * - BaseModel 00045 * - NoViewModel 00046 * - SingleViewModel 00047 * - MultipleViewModel 00048 * 00049 */ 00050 00051 00052 00053 00054 // BaseModel section. 00055 00056 00057 BaseModel::BaseModel() 00058 { 00059 00060 } 00061 00062 00063 00064 BaseModel::~BaseModel() throw() 00065 { 00066 00067 } 00068 00069 00070 00071 const string BaseModel::toString( Ceylan::VerbosityLevels level ) const 00072 { 00073 00074 return "Base model" ; 00075 00076 } 00077 00078 00079 00080 00081 00082 // NoViewModel section. 00083 00084 00085 NoViewModel::NoViewModel() 00086 { 00087 00088 } 00089 00090 00091 00092 NoViewModel::~NoViewModel() throw() 00093 { 00094 00095 } 00096 00097 00098 00099 void NoViewModel::addView( const BaseView & view ) const 00100 { 00101 00102 throw GenericMVCException( "NoViewModel::addView failed: " 00103 "no view supported by this model." ) ; 00104 00105 } 00106 00107 00108 00109 const string NoViewModel::toString( Ceylan::VerbosityLevels level ) const 00110 { 00111 00112 return "View-less model" ; 00113 00114 } 00115 00116 00117 00118 00119 00120 // Non-templated SingleViewModel section. 00121 00122 00123 00124 SingleViewModel::SingleViewModel( const BaseView & view ): 00125 _view( & view ) 00126 { 00127 00128 } 00129 00130 00131 00132 SingleViewModel::SingleViewModel(): 00133 _view( 0 ) 00134 { 00135 00136 } 00137 00138 00139 00140 SingleViewModel::~SingleViewModel() throw() 00141 { 00142 00143 if ( _view != 0 ) 00144 { 00145 00146 // View owned here: 00147 delete _view ; 00148 00149 } 00150 00151 } 00152 00153 00154 00155 void SingleViewModel::setView( const BaseView & view ) 00156 { 00157 00158 addView( view ) ; 00159 00160 } 00161 00162 00163 00164 void SingleViewModel::addView( const BaseView & view ) const 00165 { 00166 00167 if ( _view != 0 ) 00168 throw GenericMVCException( "SingleViewModel::addView failed: " 00169 "a view was already registered." ) ; 00170 00171 // Do as if this instance had not to be 'const': 00172 const_cast<SingleViewModel *>(this)->_view = & view ; 00173 00174 } 00175 00176 00177 00178 const string SingleViewModel::toString( Ceylan::VerbosityLevels level ) const 00179 { 00180 00181 if ( _view != 0 ) 00182 { 00183 00184 /* 00185 * Beware to infinite recursion: 00186 * (the view may display in turn this model) 00187 * 00188 */ 00189 00190 if ( level == Ceylan::low ) 00191 return "Single-view controller-less model owning a view" ; 00192 else 00193 return "Single-view controller-less model owning " 00194 + _view->toString( level ) ; 00195 00196 } 00197 else 00198 { 00199 00200 return "Single-view controller-less model " 00201 "not associated to any view" ; 00202 00203 } 00204 00205 } 00206 00207 00208 00209 00210 00211 // Non-templated MultipleViewModel section. 00212 00213 00214 00215 MultipleViewModel::MultipleViewModel( const BaseView & view ) 00216 { 00217 00218 _views.push_back( & view ) ; 00219 00220 } 00221 00222 00223 00224 MultipleViewModel::MultipleViewModel( 00225 const std::list<const BaseView *> & views ) 00226 { 00227 00228 _views = views ; 00229 00230 } 00231 00232 00233 00234 MultipleViewModel::MultipleViewModel() 00235 { 00236 00237 } 00238 00239 00240 00241 MultipleViewModel::~MultipleViewModel() throw() 00242 { 00243 00244 deleteViews() ; 00245 00246 } 00247 00248 00249 00250 void MultipleViewModel::setViews( 00251 const std::list<const BaseView *> & views ) 00252 { 00253 00254 if ( ! _views.empty() ) 00255 throw GenericMVCException( "MultipleViewModel::setViews failed: " 00256 "there was at least one view registered." ) ; 00257 00258 _views = views ; 00259 00260 } 00261 00262 00263 00264 void MultipleViewModel::addView( const BaseView & view ) const 00265 { 00266 00267 const_cast<MultipleViewModel *>( this )->_views.push_back( & view ) ; 00268 00269 } 00270 00271 00272 00273 const string MultipleViewModel::toString( Ceylan::VerbosityLevels level ) const 00274 { 00275 00276 if ( _views.empty() ) 00277 return "Multiple-view controller-less model " 00278 "not associated to any view" ; 00279 00280 /* 00281 * Beware to infinite recursion: 00282 * (the view may display in turn this model) 00283 * 00284 */ 00285 00286 if ( level == Ceylan::low ) 00287 return "Multiple-view controller-less model owning " 00288 + Ceylan::toString( _views.size() ) + " view(s)" ; 00289 00290 00291 std::list<std::string> res ; 00292 00293 for ( std::list<const BaseView *>::const_iterator it = 00294 _views.begin(); it != _views.end() ; it++ ) 00295 { 00296 00297 res.push_back( (*it)->toString( level ) ) ; 00298 00299 } 00300 00301 return "Multiple-view controller-less model " 00302 "associated to following view(s): " 00303 + Ceylan::formatStringList( res ) ; 00304 00305 } 00306 00307 00308 00309 void MultipleViewModel::deleteViews() 00310 { 00311 00312 for ( std::list<const BaseView *>::const_iterator it = 00313 _views.begin(); it != _views.end(); it++ ) 00314 { 00315 00316 delete *it ; 00317 00318 } 00319 00320 _views.clear() ; 00321 00322 } 00323