36 CommandManager::ExecutionListener::ExecutionListener(CommandManager* commandManager) : commandManager(commandManager)
41 void CommandManager::ExecutionListener::NotDefined(
const QString& commandId,
const NotDefinedException* exception)
43 commandManager->executionEvents.notDefined(commandId, exception);
46 void CommandManager::ExecutionListener::NotEnabled(
const QString& commandId,
const NotEnabledException* exception)
48 commandManager->executionEvents.notEnabled(commandId, exception);
51 void CommandManager::ExecutionListener::NotHandled(
const QString& commandId,
const NotHandledException* exception)
53 commandManager->executionEvents.notHandled(commandId, exception);
56 void CommandManager::ExecutionListener::PostExecuteFailure(
const QString& commandId,
57 const ExecutionException* exception)
59 commandManager->executionEvents.postExecuteFailure(commandId, exception);
62 void CommandManager::ExecutionListener::PostExecuteSuccess(
const QString& commandId,
65 commandManager->executionEvents.postExecuteSuccess(commandId, returnValue);
68 void CommandManager::ExecutionListener::PreExecute(
const QString& commandId,
71 commandManager->executionEvents.preExecute(commandId, event);
74 CommandManager::CommandCategoryListener::CommandCategoryListener(CommandManager* commandManager)
75 : commandManager(commandManager)
82 if (categoryEvent->IsDefinedChanged())
85 const QString categoryId(category->GetId());
86 const bool categoryIdAdded = category->IsDefined();
89 commandManager->definedCategoryIds.insert(categoryId);
93 commandManager->definedCategoryIds.remove(categoryId);
96 false,
false, categoryId, categoryIdAdded,
true));
97 commandManager->FireCommandManagerChanged(event);
102 CommandManager::CommandListener::CommandListener(CommandManager* commandManager)
103 : commandManager(commandManager)
108 void CommandManager::CommandListener::CommandChanged(
const SmartPointer<const CommandEvent>& commandEvent)
110 if (commandEvent->IsDefinedChanged())
113 const QString commandId = command->GetId();
114 const bool commandIdAdded = command->IsDefined();
117 commandManager->definedHandleObjects.insert(command);
121 commandManager->definedHandleObjects.remove(command);
124 commandId, commandIdAdded,
true,
"",
false,
false));
125 commandManager->FireCommandManagerChanged(event);
130 CommandManager::ParameterTypeListener::ParameterTypeListener(CommandManager* commandManager)
131 : commandManager(commandManager)
136 void CommandManager::ParameterTypeListener::ParameterTypeChanged(
139 if (parameterTypeEvent->IsDefinedChanged())
142 ->GetParameterType());
143 const QString parameterTypeId = parameterType->GetId();
144 const bool parameterTypeIdAdded = parameterType->IsDefined();
145 if (parameterTypeIdAdded)
147 commandManager->definedParameterTypeIds.insert(parameterTypeId);
151 commandManager->definedParameterTypeIds.remove(parameterTypeId);
155 parameterTypeId, parameterTypeIdAdded,
true));
156 commandManager->FireCommandManagerChanged(event);
169 : categoryListener(new CommandCategoryListener(this)),
170 commandListener(new CommandListener(this)),
171 parameterTypeListener(new ParameterTypeListener(this))
183 if (listener ==
nullptr)
185 throw ctkInvalidArgumentException(
186 "Cannot add a null execution listener");
192 executionListener.reset(
new ExecutionListener(
this));
199 command->AddExecutionListener(executionListener.data());
208 const QString& description)
211 category->Define(name, description);
215 const QString& serializedParameterizedCommand)
218 const int lparenPosition = this->UnescapedIndexOf(
221 QString commandIdEscaped;
222 QString serializedParameters;
223 if (lparenPosition == -1)
225 commandIdEscaped = serializedParameterizedCommand;
229 commandIdEscaped = serializedParameterizedCommand.left(lparenPosition);
231 if (serializedParameterizedCommand
234 throw SerializationException(
235 "Parentheses must be balanced in serialized ParameterizedCommand");
238 serializedParameters = serializedParameterizedCommand.mid(
240 serializedParameterizedCommand.size() - lparenPosition - 2);
244 const QString commandId(this->Unescape(commandIdEscaped));
246 const QList<IParameter::Pointer> parameters(command->GetParameters());
247 const QList<Parameterization>parameterizations(this->GetParameterizations(
248 serializedParameters, parameters));
256 QList<Command::Pointer> result;
262 result.push_back(cmd);
270 if (categoryId.isEmpty())
281 categoriesById[categoryId] = category;
282 category->AddCategoryListener(categoryListener.data());
295 command =
new Command(commandId);
297 command->AddCommandListener(commandListener.data());
299 if (executionListener)
301 command->AddExecutionListener(executionListener.data());
310 QList<CommandCategory::Pointer> categories;
311 for (QSet<QString>::Iterator itr = definedCategoryIds.begin();
312 itr != definedCategoryIds.end(); ++itr)
321 return definedCategoryIds;
331 QList<Command::Pointer> result;
336 result.push_back(cmd);
343 return definedParameterTypeIds;
348 QList<ParameterType::Pointer> parameterTypes;
349 for (QSet<QString>::ConstIterator itr = definedParameterTypeIds.begin();
350 itr != definedParameterTypeIds.end(); ++itr)
354 return parameterTypes;
360 if (!command->IsDefined())
362 throw NotDefinedException(
"The command is not defined. "
371 QHash<WeakPointer<IHandler>, QString>::ConstIterator itr =
372 helpContextIdsByHandler.find(weakHandler);
373 if (itr != helpContextIdsByHandler.end() && !itr.value().isEmpty())
380 return command->GetHelpContextId();
385 this->
CheckId(parameterTypeId);
391 parameterTypesById[parameterTypeId] = parameterType;
392 parameterType->AddListener(parameterTypeListener.data());
395 return parameterType;
407 throw ctkInvalidArgumentException(
"Cannot remove a null listener");
424 command->RemoveExecutionListener(executionListener.data());
426 executionListener.reset();
434 commandItr != handlersByCommandId.end(); ++commandItr)
444 const QString commandId(command->GetId());
445 QHash<QString, SmartPointer<IHandler> >::const_iterator handlerItr = handlersByCommandId.find(commandId);
446 if (handlerItr != handlersByCommandId.end())
448 command->SetHandler(handlerItr.value());
458 const QString& helpContextId)
462 throw ctkInvalidArgumentException(
"The handler cannot be null");
465 if (helpContextId.isEmpty())
467 helpContextIdsByHandler.remove(weakHandler);
471 helpContextIdsByHandler[weakHandler] = helpContextId;
477 executionEvents.
notEnabled(commandId, exception);
482 executionEvents.
notDefined(commandId, exception);
496 const ExecutionException* exception)
501 QString CommandManager::Unescape(
const QString& escapedText)
508 for (QString::const_iterator itr = escapedText.begin();
509 itr != escapedText.end(); ++itr, ++pos)
511 QString::value_type c = *itr;
515 if (!buffer.isEmpty())
522 if (buffer.isEmpty())
524 buffer = escapedText.left(pos);
527 if (++itr != escapedText.end())
538 QString msg(
"Invalid character '");
540 msg +=
"' in escape sequence";
541 throw SerializationException(msg);
546 throw SerializationException(
547 "Unexpected termination of escape sequence");
553 if (buffer.isEmpty())
561 void CommandManager::FireCommandManagerChanged(
const SmartPointer<const CommandManagerEvent> event)
565 throw ctkInvalidArgumentException(
"Event cannot be null.");
571 QList<Parameterization> CommandManager::GetParameterizations(
572 const QString& serializedParams,
const QList<SmartPointer<IParameter> >& parameters)
const
575 QString serializedParameters(serializedParams);
577 if (serializedParameters.isEmpty() || parameters.isEmpty())
579 return QList<Parameterization>();
582 QList<Parameterization> paramList;
587 commaPosition = this->UnescapedIndexOf(serializedParameters,
',');
589 QString idEqualsValue;
590 if (commaPosition == -1)
593 idEqualsValue = serializedParameters;
598 idEqualsValue = serializedParameters.left(commaPosition);
601 serializedParameters = serializedParameters.mid(commaPosition + 1);
604 int equalsPosition = this->UnescapedIndexOf(idEqualsValue,
'=');
607 QString parameterValue;
608 if (equalsPosition == -1)
611 parameterId = this->Unescape(idEqualsValue);
615 parameterId = this->Unescape(idEqualsValue.left(equalsPosition));
616 parameterValue = this->Unescape(idEqualsValue.mid(equalsPosition + 1));
619 for (
int i = 0; i < parameters.size(); i++)
622 if (parameter->GetId() == parameterId)
624 paramList.push_back(Parameterization(parameter,
630 }
while (commaPosition != -1);
635 int CommandManager::UnescapedIndexOf(
const QString& escapedText,
const char ch)
const
638 int pos = escapedText.indexOf(ch);
655 pos = escapedText.indexOf(ch, pos + 1);
berry::SmartPointer< const Self > ConstPointer
friend class ParameterizedCommand
static MsgHandler handler
QList< SmartPointer< Command > > GetAllCommands() const
Message2< const QString &, const NotEnabledException * > notEnabled
static const char PARAMETER_START_CHAR
SmartPointer< Command > GetCommand(const QString &commandId)
SmartPointer< CommandCategory > GetCategory(const QString &categoryId)
void FireNotDefined(const QString &commandId, const NotDefinedException *exception)
void FirePreExecute(const QString &commandId, const SmartPointer< const ExecutionEvent > event)
void SetHandlersByCommandId(const QHash< QString, SmartPointer< IHandler > > &handlersByCommandId)
QSet< QString > GetDefinedCommandIds() const
Event commandManagerChanged
QList< SmartPointer< CommandCategory > > GetDefinedCategories()
static const char ESCAPE_CHAR
Message2< const QString &, const ExecutionException * > postExecuteFailure
void FirePostExecuteSuccess(const QString &commandId, Object::Pointer returnValue)
void SetHelpContextId(const SmartPointer< IHandler > handler, const QString &helpContextId)
QSet< QString > GetDefinedHandleObjectIds() const
void RemoveCommandManagerListener(ICommandManagerListener *listener)
berry::SmartPointer< Self > Pointer
QList< SmartPointer< ParameterType > > GetDefinedParameterTypes()
void FireNotEnabled(const QString &commandId, const NotEnabledException *exception)
static const char PARAMETER_END_CHAR
void RemoveListener(IExecutionListener *listener) override
void FirePostExecuteFailure(const QString &commandId, const ExecutionException *exception)
QString GetHelpContextId(const SmartPointer< const Command > command) const
SmartPointer< ParameterizedCommand > Deserialize(const QString &serializedParameterizedCommand)
Message2< const QString &, const NotDefinedException * > notDefined
void DefineUncategorizedCategory(const QString &name, const QString &description)
QSet< QString > GetDefinedParameterTypeIds() const
void CheckId(const QString &id) const
static const char PARAMETER_SEPARATOR_CHAR
void RemoveListener(ICommandManagerListener *listener)
static const QString AUTOGENERATED_CATEGORY_ID
HandleObjectsByIdMap handleObjectsById
Message2< const QString &, const Object::Pointer & > postExecuteSuccess
static const char ID_VALUE_CHAR
QList< SmartPointer< Command > > GetDefinedCommands() const
QSet< QString > GetDefinedCategoryIds() const
SmartPointer< ParameterType > GetParameterType(const QString ¶meterTypeId)
SmartPointer< Other > Cast() const
bool IsEmpty() const override
void RemoveExecutionListener(IExecutionListener *listener)
void AddCommandManagerListener(ICommandManagerListener *listener)
void AddListener(ICommandManagerListener *listener)
void AddExecutionListener(IExecutionListener *listener)
Message2< const QString &, const SmartPointer< const ExecutionEvent > & > preExecute
HandleObjectsSet definedHandleObjects
void AddListener(IExecutionListener *listener) override