Replace Error notification with exception for Tags model

This commit is contained in:
Simon Robertshaw 2012-03-29 16:17:30 +01:00
parent 1f388e4ca0
commit 289556ac70
8 changed files with 74 additions and 27 deletions

View File

@ -10,7 +10,7 @@
#include "login/LoginController.h"
#include "interface/Point.h"
#include "dialogues/ErrorMessage.h"
#include "SaveLoadException.h"
#include "GameModelException.h"
using namespace std;
@ -39,7 +39,7 @@ public:
{
cc->gameModel->SetSave(new Save(*(cc->search->GetLoadedSave())));
}
catch(SaveLoadException & ex)
catch(GameModelException & ex)
{
new ErrorMessage("Cannot open save", ex.what());
}

View File

@ -8,7 +8,7 @@
#include "EllipseBrush.h"
#include "client/Client.h"
#include "game/DecorationTool.h"
#include "SaveLoadException.h"
#include "GameModelException.h"
GameModel::GameModel():
activeTools({NULL, NULL, NULL}),
@ -262,7 +262,7 @@ void GameModel::SetSave(Save * newSave)
if(returnVal){
delete currentSave;
currentSave = NULL;
throw SaveLoadException(returnVal==2?"Save from newer version":"Save data corrupt");
throw GameModelException(returnVal==2?"Save from newer version":"Save data corrupt");
}
}
notifySaveChanged();

View File

@ -0,0 +1,26 @@
/*
* SaveLoadException.h
*
* Created on: Mar 29, 2012
* Author: Simon
*/
#ifndef SAVELOADEXCEPTION_H_
#define SAVELOADEXCEPTION_H_
#include <string>
#include <exception>
using namespace std;
struct GameModelException: public exception {
string message;
public:
GameModelException(string message_): message(message_) {}
const char * what() const throw()
{
return message.c_str();
}
~GameModelException() throw() {};
};
#endif /* SAVELOADEXCEPTION_H_ */

View File

@ -8,6 +8,7 @@
#include "TagsModel.h"
#include "TagsView.h"
#include "client/Client.h"
#include "TagsModelException.h"
TagsModel::TagsModel():
save(NULL)
@ -40,8 +41,7 @@ void TagsModel::RemoveTag(string tag)
}
else
{
lastError = Client::Ref().GetLastError();
notifyError();
throw TagsModelException(Client::Ref().GetLastError());
}
}
}
@ -59,8 +59,7 @@ void TagsModel::AddTag(string tag)
}
else
{
lastError = Client::Ref().GetLastError();
notifyError();
throw TagsModelException(Client::Ref().GetLastError());
}
}
}
@ -79,14 +78,6 @@ void TagsModel::notifyTagsChanged()
}
}
void TagsModel::notifyError()
{
for(int i = 0; i < observers.size(); i++)
{
observers[i]->NotifyError(this);
}
}
TagsModel::~TagsModel() {
// TODO Auto-generated destructor stub
}

View File

@ -14,10 +14,8 @@
class TagsView;
class TagsModel {
Save * save;
string lastError;
std::vector<TagsView*> observers;
void notifyTagsChanged();
void notifyError();
public:
TagsModel();
void AddObserver(TagsView * observer);
@ -25,7 +23,6 @@ public:
void RemoveTag(string tag);
void AddTag(string tag);
Save * GetSave();
string GetLastError(){ return lastError; }
virtual ~TagsModel();
};

View File

@ -0,0 +1,23 @@
/*
* TagsModelException.h
*
* Created on: Mar 29, 2012
* Author: Simon
*/
#ifndef TAGSMODELEXCEPTION_H_
#define TAGSMODELEXCEPTION_H_
#include <string>
#include <exception>
using namespace std;
class TagsModelException {
string message;
public:
TagsModelException(string message_): message(message_) {};
const char * what() const throw() { return message.c_str(); };
~TagsModelException() throw() {};
};
#endif /* TAGSMODELEXCEPTION_H_ */

View File

@ -11,6 +11,7 @@
#include "dialogues/ErrorMessage.h"
#include "TagsController.h"
#include "TagsModel.h"
#include "TagsModelException.h"
TagsView::TagsView():
ui::Window(ui::Point(-1, -1), ui::Point(195, 250))
@ -46,11 +47,6 @@ void TagsView::OnDraw()
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255);
}
void TagsView::NotifyError(TagsModel * sender)
{
new ErrorMessage("Error", sender->GetLastError());
}
void TagsView::NotifyTagsChanged(TagsModel * sender)
{
for(int i = 0; i < tags.size(); i++)
@ -69,7 +65,14 @@ void TagsView::NotifyTagsChanged(TagsModel * sender)
DeleteTagAction(TagsView * _v, string tag) { v = _v; this->tag = tag; }
void ActionCallback(ui::Button * sender)
{
v->c->RemoveTag(tag);
try
{
v->c->RemoveTag(tag);
}
catch(TagsModelException & ex)
{
new ErrorMessage("Could not remove tag", ex.what());
}
}
};
@ -108,7 +111,15 @@ void TagsView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
case KEY_RETURN:
if(IsFocused(tagInput))
{
c->AddTag(tagInput->GetText());
try
{
c->AddTag(tagInput->GetText());
}
catch(TagsModelException & ex)
{
new ErrorMessage("Could not add tag", ex.what());
}
tagInput->SetText("");
}
break;

View File

@ -25,7 +25,6 @@ class TagsView: public ui::Window {
public:
TagsView();
virtual void OnDraw();
void NotifyError(TagsModel * sender);
void AttachController(TagsController * c_) { c = c_; };
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
void NotifyTagsChanged(TagsModel * sender);