31 #include "internal/berryCommandUtils.h"
33 #include <QTextStream>
53 throw ctkInvalidArgumentException(
"Cannot add a null command listener");
60 if (!executionListener)
62 throw ctkInvalidArgumentException(
"Cannot add a null execution listener");
69 NamedHandleObjectWithState::AddState(
id, state);
73 stateHandler->AddState(
id, state);
79 const Command* castedObject =
dynamic_cast<const Command*
>(object);
80 int compareTo = CommandUtils::CompareObj(category, castedObject->category);
89 compareTo = CommandUtils::CompareObj(handler, castedObject->handler);
92 compareTo = CommandUtils::Compare(
id, castedObject->
id);
95 compareTo = CommandUtils::Compare(
name, castedObject->
name);
98 compareTo = CommandUtils::Compare(parameters,
99 castedObject->parameters);
106 return compareTo < 0;
111 const QList<IParameter::Pointer>& parameters,
113 const QString& helpContextId)
117 throw ctkInvalidArgumentException(
"The name of a command cannot be empty");
122 throw ctkInvalidArgumentException(
"The category of a command cannot be null");
125 const bool definedChanged = !this->
defined;
128 const bool nameChanged = this->name !=
name;
131 const bool descriptionChanged = this->description !=
description;
134 const bool categoryChanged = this->category != category;
135 this->category = category;
137 const bool parametersChanged = !CommandUtils::Equals(this->parameters,
139 this->parameters = parameters;
141 const bool returnTypeChanged = this->returnType != returnType;
142 this->returnType = returnType;
144 const bool helpContextIdChanged = this->helpContextId != helpContextId;
145 this->helpContextId = helpContextId;
148 definedChanged, descriptionChanged,
false, nameChanged,
149 parametersChanged, returnTypeChanged, helpContextIdChanged));
150 this->FireCommandChanged(event);
155 this->FirePreExecute(event);
160 const NotDefinedException exception(
161 "Trying to execute a command that is not defined. " + this->
GetId());
162 this->FireNotDefined(&exception);
167 if (handler && handler->IsHandled())
169 this->
SetEnabled(event->GetApplicationContext());
171 const NotEnabledException exception(
172 "Trying to execute the disabled command " + this->
GetId());
173 this->FireNotEnabled(&exception);
180 this->FirePostExecuteSuccess(returnValue);
183 catch (
const ExecutionException* e)
185 this->FirePostExecuteFailure(e);
190 const NotHandledException e(
191 "There is no handler to execute for command " + this->
GetId());
192 this->FireNotHandled(&e);
200 throw ctkInvalidArgumentException(
"Cannot fire a null event");
213 void Command::FireNotDefined(
const NotDefinedException* e)
219 +
"not defined: id=" + this->
GetId() +
"; exception=" + e->what());
225 void Command::FireNotEnabled(
const NotEnabledException* e)
231 +
"not enabled: id=" + this->
GetId() +
"; exception=" + e->what());
237 void Command::FireNotHandled(
const NotHandledException* e)
242 +
"not handled: id=" + this->
GetId() +
"; exception=" + e->what());
248 void Command::FirePostExecuteFailure(
const ExecutionException* e)
254 +
"failure: id=" + this->
GetId() +
"; exception=" + e->what());
260 void Command::FirePostExecuteSuccess(
const Object::Pointer returnValue)
266 +
"success: id=" + this->
GetId() +
"; returnValue=" +
267 (returnValue.IsNull() ? QString(
"NULL") : returnValue->
ToString()));
279 +
"starting: id=" + this->
GetId() +
"; event=" + event->ToString());
292 return helpContextId;
299 throw NotDefinedException(
300 "Cannot get a parameter from an undefined command. " +
id);
303 for (
int i = 0; i < parameters.size(); i++)
306 if (parameter->GetId() == parameterId) {
318 throw NotDefinedException(
319 "Cannot get the parameters from an undefined command. " +
id);
330 return parameterWithType->GetParameterType();
339 throw NotDefinedException(
340 "Cannot get the return type of an undefined command. " +
id);
353 return handler->IsEnabled();
360 handler->SetEnabled(evaluationContext);
371 return handler->IsHandled();
376 if (!commandListener)
378 throw ctkInvalidArgumentException(
379 "Cannot remove a null command listener");
394 if (!executionListener)
396 throw Poco::NullPointerException(
397 "Cannot remove a null execution listener");
406 stateHandler->RemoveState(stateId);
413 if (handler == this->handler)
419 const QList<QString> stateIds(this->
GetStateIds());
420 for (
int i = 0; i < stateIds.size(); ++i)
422 const QString stateId = stateIds[i];
425 stateHandler->RemoveState(stateId);
430 stateHandler->AddState(stateId, stateToAdd);
437 this->handler->RemoveHandlerListener(this->GetHandlerListener());
441 this->handler = handler;
444 this->handler->AddHandlerListener(this->GetHandlerListener());
452 QString buffer(
"Command('");
453 buffer +=
id +
"') has changed to ";
455 buffer +=
"no handler";
457 buffer +=
"\'" + handler->ToString() +
"' as its handler";
464 false,
false,
false,
false, enabled != this->
IsEnabled()));
465 this->FireCommandChanged(cmdEvent);
475 void Command::HandlerChanged(
const SmartPointer<HandlerEvent>& handlerEvent)
477 bool enabledChanged = handlerEvent->IsEnabledChanged();
478 bool handledChanged = handlerEvent->IsHandledChanged();
480 false,
false, handledChanged,
false,
false,
false,
481 false, enabledChanged));
482 this->FireCommandChanged(cmdEvent);
489 QTextStream buffer(&
str);
491 buffer <<
"Command(" <<
id <<
',' <<
name <<
",\n\t\t";
492 buffer <<
description <<
",\n\t\t" << (category ? category->ToString() : QString(
""));
493 buffer <<
",\n\t\t" << (handler ? handler->ToString() :
"");
494 buffer <<
",\n\t\t" <<
"[";
495 for (
int i = 0; i < parameters.size(); ++i)
497 buffer << parameters[i]->GetId();
499 buffer <<
"]," << (returnType ? returnType->ToString() :
"");
500 buffer <<
"," <<
defined <<
")";
511 const bool definedChanged =
defined;
514 const bool nameChanged = !
name.isEmpty();
517 const bool descriptionChanged = !
description.isEmpty();
520 const bool categoryChanged = category;
523 const bool parametersChanged = !parameters.empty();
526 const bool returnTypeChanged = returnType;
527 returnType =
nullptr;
529 const QList<QString> stateIds(this->
GetStateIds());
532 for (
int i = 0; i < stateIds.size(); i++)
534 const QString stateId(stateIds[i]);
535 handlerWithState->RemoveState(stateId);
544 for (
int i = 0; i < stateIds.size(); ++i)
546 const QString stateId(stateIds[i]);
554 definedChanged, descriptionChanged,
false, nameChanged,
555 parametersChanged, returnTypeChanged,
false, enabledChanged));
556 this->FireCommandChanged(cmdEvent);
berry::SmartPointer< const Self > ConstPointer
void RemoveState(const QString &id) override
SmartPointer< ParameterType > GetParameterType(const QString ¶meterId) const
static MsgHandler handler
Message2< const QString &, const NotEnabledException * > notEnabled
Object::Pointer ExecuteWithChecks(const SmartPointer< const ExecutionEvent > event)
void AddExecutionListener(IExecutionListener *executionListener)
void RemoveListener(ICommandListener *listener)
Light weight base class for most BlueBerry classes.
void AddState(const QString &id, const SmartPointer< State > &state) override
SmartPointer< IHandler > GetHandler() const
Message2< const QString &, const ExecutionException * > postExecuteFailure
void AddCommandListener(ICommandListener *commandListener)
static void PrintTrace(const QString &component, const QString &message)
bool operator<(const Object *object) const override
static const QString SEPARATOR
SmartPointer< IParameter > GetParameter(const QString ¶meterId) const
QList< QString > GetStateIds() const override
static bool DEBUG_HANDLERS
berry::SmartPointer< Self > Pointer
void RemoveListener(IExecutionListener *listener) override
void Define(const QString &name, const QString &description, const SmartPointer< CommandCategory > category, const QList< SmartPointer< IParameter > > ¶meters=QList< SmartPointer< IParameter > >(), const SmartPointer< ParameterType > &returnType=SmartPointer< ParameterType >(nullptr), const QString &helpContextId="")
const SmartPointer< State > & state
QString ToString() const override
Message2< const QString &, const NotDefinedException * > notDefined
void RemoveExecutionListener(IExecutionListener *executionListener)
static bool DEBUG_COMMAND_EXECUTION
QString GetHelpContextId() const
Message2< const QString &, const Object::Pointer & > postExecuteSuccess
void RemoveState(const QString &stateId) override
bool SetHandler(const SmartPointer< IHandler > handler)
void RemoveCommandListener(ICommandListener *commandListener)
void AddListener(ICommandListener *listener)
SmartPointer< Other > Cast() const
static QString DEBUG_HANDLERS_COMMAND_ID
SmartPointer< State > GetState(const QString &stateId) const override
QList< SmartPointer< IParameter > > GetParameters() const
Message2< const QString &, const SmartPointer< const ExecutionEvent > & > preExecute
Command(const QString &id)
Message2< const QString &, const NotHandledException * > notHandled
void AddListener(IExecutionListener *listener) override
SmartPointer< ParameterType > GetReturnType() const
void SetEnabled(const Object::Pointer &evaluationContext)