32 CommandManager::ExecutionListener::ExecutionListener(CommandManager* commandManager) : commandManager(commandManager)
37 void CommandManager::ExecutionListener::NotDefined(
const QString& commandId,
const NotDefinedException* exception)
39 commandManager->executionEvents.notDefined(commandId, exception);
42 void CommandManager::ExecutionListener::NotEnabled(
const QString& commandId,
const NotEnabledException* exception)
44 commandManager->executionEvents.notEnabled(commandId, exception);
47 void CommandManager::ExecutionListener::NotHandled(
const QString& commandId,
const NotHandledException* exception)
49 commandManager->executionEvents.notHandled(commandId, exception);
52 void CommandManager::ExecutionListener::PostExecuteFailure(
const QString& commandId,
53 const ExecutionException* exception)
55 commandManager->executionEvents.postExecuteFailure(commandId, exception);
58 void CommandManager::ExecutionListener::PostExecuteSuccess(
const QString& commandId,
61 commandManager->executionEvents.postExecuteSuccess(commandId, returnValue);
64 void CommandManager::ExecutionListener::PreExecute(
const QString& commandId,
67 commandManager->executionEvents.preExecute(commandId, event);
70 CommandManager::CommandCategoryListener::CommandCategoryListener(CommandManager* commandManager)
71 : commandManager(commandManager)
78 if (categoryEvent->IsDefinedChanged())
81 const QString categoryId(category->GetId());
82 const bool categoryIdAdded = category->IsDefined();
85 commandManager->definedCategoryIds.insert(categoryId);
89 commandManager->definedCategoryIds.remove(categoryId);
92 false,
false, categoryId, categoryIdAdded,
true));
93 commandManager->FireCommandManagerChanged(event);
98 CommandManager::CommandListener::CommandListener(CommandManager* commandManager)
99 : commandManager(commandManager)
104 void CommandManager::CommandListener::CommandChanged(
const SmartPointer<const CommandEvent>& commandEvent)
106 if (commandEvent->IsDefinedChanged())
109 const QString commandId = command->GetId();
110 const bool commandIdAdded = command->IsDefined();
113 commandManager->definedHandleObjects.insert(command);
117 commandManager->definedHandleObjects.remove(command);
120 commandId, commandIdAdded,
true,
"",
false,
false));
121 commandManager->FireCommandManagerChanged(event);
126 CommandManager::ParameterTypeListener::ParameterTypeListener(CommandManager* commandManager)
127 : commandManager(commandManager)
132 void CommandManager::ParameterTypeListener::ParameterTypeChanged(
135 if (parameterTypeEvent->IsDefinedChanged())
138 ->GetParameterType());
139 const QString parameterTypeId = parameterType->GetId();
140 const bool parameterTypeIdAdded = parameterType->IsDefined();
141 if (parameterTypeIdAdded)
143 commandManager->definedParameterTypeIds.insert(parameterTypeId);
147 commandManager->definedParameterTypeIds.remove(parameterTypeId);
151 parameterTypeId, parameterTypeIdAdded,
true));
152 commandManager->FireCommandManagerChanged(event);
165 : categoryListener(new CommandCategoryListener(this)),
166 commandListener(new CommandListener(this)),
167 parameterTypeListener(new ParameterTypeListener(this))
179 if (listener ==
nullptr)
181 throw ctkInvalidArgumentException(
182 "Cannot add a null execution listener");
188 executionListener.reset(
new ExecutionListener(
this));
195 command->AddExecutionListener(executionListener.data());
204 const QString& description)
207 category->Define(name, description);
211 const QString& serializedParameterizedCommand)
214 const int lparenPosition = this->UnescapedIndexOf(
217 QString commandIdEscaped;
218 QString serializedParameters;
219 if (lparenPosition == -1)
221 commandIdEscaped = serializedParameterizedCommand;
225 commandIdEscaped = serializedParameterizedCommand.left(lparenPosition);
227 if (serializedParameterizedCommand
230 throw SerializationException(
231 "Parentheses must be balanced in serialized ParameterizedCommand");
234 serializedParameters = serializedParameterizedCommand.mid(
236 serializedParameterizedCommand.size() - lparenPosition - 2);
240 const QString commandId(this->Unescape(commandIdEscaped));
242 const QList<IParameter::Pointer> parameters(command->GetParameters());
243 const QList<Parameterization>parameterizations(this->GetParameterizations(
244 serializedParameters, parameters));
252 QList<Command::Pointer> result;
258 result.push_back(cmd);
266 if (categoryId.isEmpty())
277 categoriesById[categoryId] = category;
278 category->AddCategoryListener(categoryListener.data());
291 command =
new Command(commandId);
293 command->AddCommandListener(commandListener.data());
295 if (executionListener)
297 command->AddExecutionListener(executionListener.data());
306 QList<CommandCategory::Pointer> categories;
307 for (QSet<QString>::Iterator itr = definedCategoryIds.begin();
308 itr != definedCategoryIds.end(); ++itr)
317 return definedCategoryIds;
327 QList<Command::Pointer> result;
332 result.push_back(cmd);
339 return definedParameterTypeIds;
344 QList<ParameterType::Pointer> parameterTypes;
345 for (QSet<QString>::ConstIterator itr = definedParameterTypeIds.begin();
346 itr != definedParameterTypeIds.end(); ++itr)
350 return parameterTypes;
356 if (!command->IsDefined())
358 throw NotDefinedException(
"The command is not defined. " 367 QHash<WeakPointer<IHandler>, QString>::ConstIterator itr =
368 helpContextIdsByHandler.find(weakHandler);
369 if (itr != helpContextIdsByHandler.end() && !itr.value().isEmpty())
376 return command->GetHelpContextId();
381 this->
CheckId(parameterTypeId);
387 parameterTypesById[parameterTypeId] = parameterType;
388 parameterType->AddListener(parameterTypeListener.data());
391 return parameterType;
403 throw ctkInvalidArgumentException(
"Cannot remove a null listener");
420 command->RemoveExecutionListener(executionListener.data());
422 executionListener.reset();
430 commandItr != handlersByCommandId.end(); ++commandItr)
440 const QString commandId(command->GetId());
441 QHash<QString, SmartPointer<IHandler> >::const_iterator handlerItr = handlersByCommandId.find(commandId);
442 if (handlerItr != handlersByCommandId.end())
444 command->SetHandler(handlerItr.value());
454 const QString& helpContextId)
458 throw ctkInvalidArgumentException(
"The handler cannot be null");
461 if (helpContextId.isEmpty())
463 helpContextIdsByHandler.remove(weakHandler);
467 helpContextIdsByHandler[weakHandler] = helpContextId;
473 executionEvents.
notEnabled(commandId, exception);
478 executionEvents.
notDefined(commandId, exception);
492 const ExecutionException* exception)
497 QString CommandManager::Unescape(
const QString& escapedText)
504 for (QString::const_iterator itr = escapedText.begin();
505 itr != escapedText.end(); ++itr, ++pos)
507 QString::value_type c = *itr;
511 if (!buffer.isEmpty())
518 if (buffer.isEmpty())
520 buffer = escapedText.left(pos);
523 if (++itr != escapedText.end())
534 QString msg(
"Invalid character '");
536 msg +=
"' in escape sequence";
537 throw SerializationException(msg);
542 throw SerializationException(
543 "Unexpected termination of escape sequence");
549 if (buffer.isEmpty())
561 throw ctkInvalidArgumentException(
"Event cannot be null.");
567 QList<Parameterization> CommandManager::GetParameterizations(
571 QString serializedParameters(serializedParams);
573 if (serializedParameters.isEmpty() || parameters.isEmpty())
575 return QList<Parameterization>();
578 QList<Parameterization> paramList;
583 commaPosition = this->UnescapedIndexOf(serializedParameters,
',');
585 QString idEqualsValue;
586 if (commaPosition == -1)
589 idEqualsValue = serializedParameters;
594 idEqualsValue = serializedParameters.left(commaPosition);
597 serializedParameters = serializedParameters.mid(commaPosition + 1);
600 int equalsPosition = this->UnescapedIndexOf(idEqualsValue,
'=');
603 QString parameterValue;
604 if (equalsPosition == -1)
607 parameterId = this->Unescape(idEqualsValue);
611 parameterId = this->Unescape(idEqualsValue.left(equalsPosition));
612 parameterValue = this->Unescape(idEqualsValue.mid(equalsPosition + 1));
615 for (
int i = 0; i < parameters.size(); i++)
618 if (parameter->GetId() == parameterId)
626 }
while (commaPosition != -1);
631 int CommandManager::UnescapedIndexOf(
const QString& escapedText,
const char ch)
const 634 int pos = escapedText.indexOf(ch);
651 pos = escapedText.indexOf(ch, pos + 1);
berry::SmartPointer< const Self > ConstPointer
friend class ParameterizedCommand
static MsgHandler handler
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 > GetDefinedHandleObjectIds() const
SmartPointer< Other > Cast() 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)
QList< SmartPointer< Command > > GetAllCommands() const
void RemoveCommandManagerListener(ICommandManagerListener *listener)
QSet< QString > GetDefinedParameterTypeIds() const
QList< SmartPointer< Command > > GetDefinedCommands() const
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
QSet< QString > GetDefinedCommandIds() const
void FirePostExecuteFailure(const QString &commandId, const ExecutionException *exception)
SmartPointer< ParameterizedCommand > Deserialize(const QString &serializedParameterizedCommand)
Message2< const QString &, const NotDefinedException * > notDefined
QString GetHelpContextId(const SmartPointer< const Command > command) const
void DefineUncategorizedCategory(const QString &name, const QString &description)
QSet< QString > GetDefinedCategoryIds() 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
SmartPointer< ParameterType > GetParameterType(const QString ¶meterTypeId)
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 CheckId(const QString &id) const
void AddListener(IExecutionListener *listener) override