// ESBDSAddinImpl.cpp : Implementation of ESBDSAddinImpl
#include "stdafx.h"
#include "IESBDSAddin.h"
#include "DSAddin.h"
#include "CommandHandler.h"
#include "EventHandler.h"
#include "ComHelper.h"
#include <string>
using namespace std;
#include "ESBServer/src/ESBServer_i.c"
/////////////////////////////////////////////////////////////////////////////
// ESBDSAddinImpl
ESBDSAddinImpl::ESBDSAddinImpl()
: mApplication(NULL), mCommandHandler(NULL), mApplicationEventHandler(NULL),
mCookie(0), mClassRegistrationCookie(0), mIsEnabled(false)
{
DebugBox("ESBDSAddinImpl::ESBDSAddinImpl()");
}
HRESULT ESBDSAddinImpl::OnConnection(IApplication* application, VARIANT_BOOL isFirstTime, long dwCookie, VARIANT_BOOL* pbOnConnection)
{
const static string function = "ESBDSAddinImpl::OnConnection(): ";
DebugBox(function);
try {
*pbOnConnection = VARIANT_FALSE;
setupComStuff();
mApplication = application;
mCookie = dwCookie;
setupAddIn(isFirstTime == VARIANT_TRUE);
*pbOnConnection = VARIANT_TRUE;
return S_OK;
} catch(std::exception &e) {
DebugBox(function + e.what());
} catch(_com_error &e) {
DebugBox(function + asString(e));
} catch(...) {
DebugBox(function + " unknown error");
}
return E_FAIL;
}
HRESULT ESBDSAddinImpl::OnDisconnection(VARIANT_BOOL bLastTime)
{
DebugBox("ESBDSAddinImpl::OnDisconnection()");
CoRevokeClassObject(mClassRegistrationCookie);
mApplication = NULL;
mCommandHandler = NULL;
mApplicationEventHandler = NULL;
return TRUE;
}
HRESULT ESBDSAddinImpl::gotoLine(BSTR filename, long line)
{
const static string function = "ESBDSAddinImpl::gotoLine(): ";
DebugBox("ESBDSAddinImpl::gotoLine(): ");
try {
CComPtr< IDispatch> tmp = 0;
cex_ = getApplication()->get_Documents(&tmp.p);
CComQIPtr< IDocuments> documents = tmp;
tmp = 0;
cex_ = documents->Open( filename, CComVariant(), CComVariant(), &tmp.p);
CComQIPtr< ITextDocument> document = tmp;
tmp = 0;
cex_ = document->get_Selection( &tmp.p);
CComQIPtr< ITextSelection> selection = tmp;
cex_ = selection->GoToLine(line, CComVariant(1));
} catch(std::exception &e) {
DebugBox(function + e.what());
} catch(_com_error &e) {
DebugBox(function + asString(e));
} catch(...) {
DebugBox(function + " unknown error");
}
return E_FAIL;
}
void ESBDSAddinImpl::setupComStuff()
{
CComPtr<IUnknown> pIUnk = 0;
cex_ = _Module.GetClassObject( GetObjectCLSID(), IID_IUnknown, reinterpret_cast<void**>(&pIUnk));
cex_ = CoRegisterClassObject(GetObjectCLSID(), pIUnk, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &mClassRegistrationCookie);
}
void ESBDSAddinImpl::setupAddIn(bool isFirstTime)
{
DebugBox("ESBDSAddinImpl::setupAddIn()");
mCommandHandler = new_instance<CommandHandler>();
mCommandHandler->setAddIn(this);
mApplicationEventHandler = new_instance<ApplicationEventHandler>();
mApplicationEventHandler->setAddIn(this);
enable(false);
//tell vc about us....
cex_ = getApplication()->SetAddInInfo((long)_Module.GetModuleInstance(), mCommandHandler, IDR_TOOLBAR_MEDIUM, IDR_TOOLBAR_LARGE, mCookie);
// register our commands with VC
VARIANT_BOOL bRet;
const size_t commandCnt = 6;
string commands[6][3] = {
{"ESBProperties", "\n\n\nEnable/Disable","setProperties"},
{"ESBShowOutlineBrowser", "\n\n\nShow Outline", "showOutlineBrowser"},
{"ESBShowHierarchyBrowser", "\n\n\nShow Hierarchy", "showHierarchyBrowser"},
{"ESBShowTypeBrowser", "\n\n\nShow Types", "showTypeBrowser"},
{"ESBShowMethodBrowser", "\n\n\nShow Methods", "showMethodBrowser"},
{"ESBShowWorkspaceBrowser", "\n\n\nShow Workspace", "showWorkspaceBrowser"},
};
for(int i = 0; i < commandCnt; i++) {
cex_ = getApplication()->AddCommand(CComBSTR((commands[i][0]+ commands[i][1]).c_str()), CComBSTR(commands[i][2].c_str()), i, mCookie, &bRet);
if(isFirstTime)
{
cex_ = getApplication()->AddCommandBarButton(dsGlyph, CComBSTR(commands[i][0].c_str()), mCookie);
}
}
DebugBox("... completed ESBDSAddinImpl::setupAddIn()");
}
CComPtr<IESBServer> ESBDSAddinImpl::getESBServer()
{
CComPtr<IESBServer> esbServer;
cex_ = esbServer.CoCreateInstance(CLSID_ESBServerCoClass);
return esbServer;
}
void ESBDSAddinImpl::enable(bool enable)
{
mIsEnabled = enable;
if(isEnabled()) {
mApplicationEventHandler->Connect(getApplication());
} else {
mApplicationEventHandler->Disconnect(getApplication());
}
}
void ESBDSAddinImpl::workspaceLoaded(const std::string &workspace, const std::string &options)
{
getESBServer()->loadWorkspace(CComBSTR(workspace.c_str()), CComBSTR(options.c_str()));
}
void ESBDSAddinImpl::documentChanged(const std::string &filename)
{
getESBServer()->fileContentChanged(CComBSTR(filename.c_str()));
}
void ESBDSAddinImpl::documentActivated(const std::string &filename)
{
getESBServer()->showFileOutline(CComBSTR(filename.c_str()));
}