void CommandManager::SetHandlersByCommandId(const std::map<std::string, SmartPointer<IHandler> >& handlersByCommandId) { // Make that all the reference commands are created. for (std::map<std::string, SmartPointer<IHandler> >::const_iterator commandItr = handlersByCommandId.begin(); commandItr != handlersByCommandId.end(); ++commandItr) { this->GetCommand(commandItr->first); } // Now, set-up the handlers on all of the existing commands. for (HandleObjectsByIdMap::Iterator commandItr = handleObjectsById.begin(); commandItr != handleObjectsById.end(); ++commandItr) { const Command::Pointer command(commandItr->second.Cast<Command>()); const std::string commandId(command->GetId()); std::map<std::string, SmartPointer<IHandler> >::const_iterator handlerItr = handlersByCommandId.find(commandId); if (handlerItr != handlersByCommandId.end()) { command->SetHandler(handlerItr->second); } else { command->SetHandler(IHandler::Pointer(0)); } } }
void CommandContributionItem::CreateCommand(const QString &commandId, const QHash<QString,Object::Pointer> ¶meters) { if (commandId.isEmpty()) { // StatusManager.getManager().handle(StatusUtil.newStatus(IStatus.ERROR, // "Unable to create menu item \"" + getId() // + "\", no command id", null)); BERRY_ERROR << "Unable to create menu item \"" << this->GetId().toStdString() << "\", no command id"; return; } Command::Pointer cmd = commandService->GetCommand(commandId); if (!cmd->IsDefined()) { // StatusManager.getManager().handle(StatusUtil.newStatus( // IStatus.ERROR, "Unable to create menu item \"" + getId() // + "\", command \"" + commandId + "\" not defined", null)); BERRY_ERROR << "Unable to create menu item \"" << this->GetId().toStdString() << "\", command \"" << commandId.toStdString() << "\" not defined"; return; } command = ParameterizedCommand::GenerateCommand(cmd, parameters); }
void CommandManager::CommandListener::CommandChanged(const SmartPointer<const CommandEvent> commandEvent) { if (commandEvent->IsDefinedChanged()) { const Command::Pointer command(commandEvent->GetCommand()); const std::string commandId = command->GetId(); const bool commandIdAdded = command->IsDefined(); if (commandIdAdded) { commandManager->definedHandleObjects.insert(command); } else { commandManager->definedHandleObjects.erase(command); } CommandManagerEvent::Pointer event(new CommandManagerEvent(*commandManager, commandId, commandIdAdded, true, "", false, false)); commandManager->FireCommandManagerChanged(event); } }
void CommandService::RegisterElement(const SmartPointer<IElementReference>& elementReference) { QList<IElementReference::Pointer>& parameterizedCommands = commandCallbacks[elementReference->GetCommandId()]; parameterizedCommands.push_back(elementReference); // If the active handler wants to update the callback, it can do // so now Command::Pointer command = GetCommand(elementReference->GetCommandId()); if (command->IsDefined()) { if (IElementUpdater::Pointer updater = command->GetHandler().Cast<IElementUpdater>()) { updater->UpdateElement(elementReference->GetElement().GetPointer(), elementReference->GetParameters()); } } }
void CommandManager::AddExecutionListener(const SmartPointer<IExecutionListener> listener) { if (!listener) { throw Poco::NullPointerException( "Cannot add a null execution listener"); //$NON-NLS-1$ } if (executionEvents.IsEmpty()) { // Add an execution listener to every command. executionListener = new ExecutionListener(this); for (HandleObjectsByIdMap::Iterator itr = handleObjectsById.begin(); itr != handleObjectsById.end(); ++itr) { Command::Pointer command = itr->second.Cast<Command>(); if (command) { command->AddExecutionListener(executionListener); } } } executionEvents.AddListener(listener); }
void CommandService::RefreshElements(const QString& commandId, const QHash<QString, Object::Pointer>& filter) { Command::Pointer cmd = GetCommand(commandId); if (!cmd->IsDefined() || !(cmd->GetHandler().Cast<IElementUpdater>())) { return; } IElementUpdater::Pointer updater = cmd->GetHandler().Cast<IElementUpdater>(); if (commandCallbacks.isEmpty()) { return; } if(!commandCallbacks.contains(commandId)) { return; } foreach (IElementReference::Pointer callbackRef, commandCallbacks[commandId]) { struct _SafeRunnable : public ISafeRunnable { IElementUpdater* updater; IElementReference* callbackRef; _SafeRunnable(IElementUpdater* updater, IElementReference* callbackRef) : updater(updater), callbackRef(callbackRef) {} void HandleException(const std::exception& exc) { WorkbenchPlugin::Log(QString("Failed to update callback: ") + callbackRef->GetCommandId() + exc.what()); } void Run() { updater->UpdateElement(callbackRef->GetElement().GetPointer(), callbackRef->GetParameters()); } }; QHash<QString,Object::Pointer> parms = callbackRef->GetParameters(); ISafeRunnable::Pointer run(new _SafeRunnable(updater.GetPointer(), callbackRef.GetPointer())); if (filter.isEmpty()) { SafeRunner::Run(run); } else { bool match = true; QHashIterator<QString, Object::Pointer> i(filter); while (i.hasNext()) { i.next(); Object::Pointer value = parms[i.key()]; if (i.value() != value) { match = false; break; } } if (match) { SafeRunner::Run(run); } } } }