| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 Eran Ifrah |
| 5 |
// file name : databaseexplorer.cpp |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#include "databaseexplorer.h" |
| 27 |
|
| 28 |
#include "ErdPanel.h" |
| 29 |
#include "SqlCommandPanel.h" |
| 30 |
#include "clKeyboardManager.h" |
| 31 |
#include "detachedpanesinfo.h" |
| 32 |
#include "dockablepane.h" |
| 33 |
#include "event_notifier.h" |
| 34 |
#include "globals.h" |
| 35 |
#include "imanager.h" |
| 36 |
|
| 37 |
#include <wx/aboutdlg.h> |
| 38 |
#include <wx/wxsf/AutoLayout.h> |
| 39 |
#include <wx/xrc/xmlres.h> |
| 40 |
|
| 41 |
//#ifdef DBL_USE_MYSQL |
| 42 |
#include "MySqlDbAdapter.h" |
| 43 |
//#endif |
| 44 |
|
| 45 |
//#ifdef DBL_USE_SQLITE |
| 46 |
#include "SqliteDbAdapter.h" |
| 47 |
//#endif |
| 48 |
|
| 49 |
//#ifdef DBL_USE_POSTGRES |
| 50 |
#include "PostgreSqlDbAdapter.h" |
| 51 |
//#endif |
| 52 |
|
| 53 |
#define DBE_VERSION "0.5.3 Beta" |
| 54 |
|
| 55 |
static DatabaseExplorer* thePlugin = NULL; |
| 56 |
|
| 57 |
DbViewerPanel* DatabaseExplorer::m_dbViewerPanel = NULL; |
| 58 |
|
| 59 |
IManager* DatabaseExplorer::GetManager() { return thePlugin->m_mgr; } |
| 60 |
|
| 61 |
enum wxbuildinfoformat { short_f, long_f }; |
| 62 |
|
| 63 |
wxString wxbuildinfo(wxbuildinfoformat format) |
| 64 |
{ |
| 65 |
wxString wxbuild(wxVERSION_STRING); |
| 66 |
|
| 67 |
if(format == long_f) { |
| 68 |
#if defined(__WXMSW__) |
| 69 |
wxbuild << _T("-Windows"); |
| 70 |
#elif defined(__WXMAC__) |
| 71 |
wxbuild << _T("-Mac"); |
| 72 |
#elif defined(__UNIX__) |
| 73 |
wxbuild << _T("-Linux"); |
| 74 |
#endif |
| 75 |
|
| 76 |
#if wxUSE_UNICODE |
| 77 |
wxbuild << _T("-Unicode build"); |
| 78 |
#else |
| 79 |
wxbuild << _T("-ANSI build"); |
| 80 |
#endif // wxUSE_UNICODE |
| 81 |
} |
| 82 |
|
| 83 |
return wxbuild; |
| 84 |
} |
| 85 |
|
| 86 |
// Define the plugin entry point |
| 87 |
CL_PLUGIN_API IPlugin* CreatePlugin(IManager* manager) |
| 88 |
{ |
| 89 |
if(thePlugin == 0) { |
| 90 |
thePlugin = new DatabaseExplorer(manager); |
| 91 |
} |
| 92 |
return thePlugin; |
| 93 |
} |
| 94 |
|
| 95 |
CL_PLUGIN_API PluginInfo* GetPluginInfo() |
| 96 |
{ |
| 97 |
static PluginInfo info; |
| 98 |
info.SetAuthor("Peter Janků, Michal Bližňák, Tomas Bata University in Zlin, Czech Republic (www.fai.utb.cz)"); |
| 99 |
info.SetName("DatabaseExplorer"); |
| 100 |
info.SetDescription(_("DatabaseExplorer for CodeLite")); |
| 101 |
info.SetVersion(DBE_VERSION); |
| 102 |
return &info; |
| 103 |
} |
| 104 |
|
| 105 |
CL_PLUGIN_API int GetPluginInterfaceVersion() { return PLUGIN_INTERFACE_VERSION; } |
| 106 |
|
| 107 |
DatabaseExplorer::DatabaseExplorer(IManager* manager) |
| 108 |
: IPlugin(manager) |
| 109 |
{ |
| 110 |
|
| 111 |
// create tab (possibly detached) |
| 112 |
Notebook* book = m_mgr->GetWorkspacePaneNotebook(); |
| 113 |
wxWindow* editorBook = m_mgr->GetEditorPaneNotebook(); |
| 114 |
|
| 115 |
EventNotifier::Get()->Connect(wxEVT_TREE_ITEM_FILE_ACTIVATED, |
| 116 |
clCommandEventHandler(DatabaseExplorer::OnOpenWithDBE), NULL, this); |
| 117 |
EventNotifier::Get()->Bind(wxEVT_SHOW_WORKSPACE_TAB, &DatabaseExplorer::OnToggleTab, this); |
| 118 |
|
| 119 |
if(IsDbViewDetached()) { |
| 120 |
DockablePane* cp = new DockablePane(book->GetParent()->GetParent(), book, _("DbExplorer"), false, wxNOT_FOUND, |
| 121 |
wxSize(200, 200)); |
| 122 |
m_dbViewerPanel = new DbViewerPanel(cp, editorBook, m_mgr); |
| 123 |
cp->SetChildNoReparent(m_dbViewerPanel); |
| 124 |
|
| 125 |
} else { |
| 126 |
|
| 127 |
m_dbViewerPanel = new DbViewerPanel(book, editorBook, m_mgr); |
| 128 |
book->AddPage(m_dbViewerPanel, _("DbExplorer"), false); |
| 129 |
} |
| 130 |
m_mgr->AddWorkspaceTab(_("DbExplorer")); |
| 131 |
|
| 132 |
// configure autolayout algorithns |
| 133 |
wxSFAutoLayout layout; |
| 134 |
|
| 135 |
wxSFLayoutHorizontalTree* pHTreeAlg = |
| 136 |
wxDynamicCast(layout.GetAlgorithm("Horizontal Tree"), wxSFLayoutHorizontalTree); |
| 137 |
if(pHTreeAlg) { |
| 138 |
pHTreeAlg->SetHSpace(200); |
| 139 |
} |
| 140 |
|
| 141 |
wxSFLayoutVerticalTree* pVTreeAlg = wxDynamicCast(layout.GetAlgorithm("Vertical Tree"), wxSFLayoutVerticalTree); |
| 142 |
if(pVTreeAlg) { |
| 143 |
pVTreeAlg->SetVSpace(75); |
| 144 |
} |
| 145 |
|
| 146 |
m_longName = _("DatabaseExplorer for CodeLite"); |
| 147 |
m_shortName = "DatabaseExplorer"; |
| 148 |
|
| 149 |
clKeyboardManager::Get()->AddAccelerator("wxEVT_EXECUTE_SQL", _("Database Explorer"), _("Execute SQL"), "Ctrl-J"); |
| 150 |
wxTheApp->Bind(wxEVT_MENU, &DatabaseExplorer::OnExecuteSQL, this, XRCID("wxEVT_EXECUTE_SQL")); |
| 151 |
} |
| 152 |
|
| 153 |
DatabaseExplorer::~DatabaseExplorer() { wxSFAutoLayout::CleanUp(); } |
| 154 |
|
| 155 |
void DatabaseExplorer::CreateToolBar(clToolBarGeneric* toolbar) { wxUnusedVar(toolbar); } |
| 156 |
|
| 157 |
void DatabaseExplorer::CreatePluginMenu(wxMenu* pluginsMenu) |
| 158 |
{ |
| 159 |
|
| 160 |
// You can use the below code a snippet: |
| 161 |
wxMenu* menu = new wxMenu(); |
| 162 |
wxMenuItem* item(NULL); |
| 163 |
item = new wxMenuItem(menu, XRCID("dbe_about"), _("About..."), wxEmptyString, wxITEM_NORMAL); |
| 164 |
menu->Append(item); |
| 165 |
item = new wxMenuItem(menu, XRCID("wxEVT_EXECUTE_SQL"), _("Execute SQL"), wxEmptyString, wxITEM_NORMAL); |
| 166 |
menu->Append(item); |
| 167 |
pluginsMenu->Append(wxID_ANY, _("Database Explorer"), menu); |
| 168 |
m_mgr->GetTheApp()->Connect(XRCID("dbe_about"), wxEVT_COMMAND_MENU_SELECTED, |
| 169 |
wxCommandEventHandler(DatabaseExplorer::OnAbout), NULL, this); |
| 170 |
} |
| 171 |
|
| 172 |
void DatabaseExplorer::HookPopupMenu(wxMenu* menu, MenuType type) |
| 173 |
{ |
| 174 |
wxUnusedVar(menu); |
| 175 |
wxUnusedVar(type); |
| 176 |
} |
| 177 |
|
| 178 |
void DatabaseExplorer::OnExecuteSQL(wxCommandEvent& event) |
| 179 |
{ |
| 180 |
wxUnusedVar(event); |
| 181 |
// Pass the command to the active frame |
| 182 |
} |
| 183 |
|
| 184 |
void DatabaseExplorer::UnPlug() |
| 185 |
{ |
| 186 |
EventNotifier::Get()->Disconnect(wxEVT_TREE_ITEM_FILE_ACTIVATED, |
| 187 |
clCommandEventHandler(DatabaseExplorer::OnOpenWithDBE), NULL, this); |
| 188 |
EventNotifier::Get()->Unbind(wxEVT_SHOW_WORKSPACE_TAB, &DatabaseExplorer::OnToggleTab, this); |
| 189 |
int index = m_mgr->GetWorkspacePaneNotebook()->GetPageIndex(m_dbViewerPanel); |
| 190 |
if(index != wxNOT_FOUND) { |
| 191 |
m_mgr->GetWorkspacePaneNotebook()->RemovePage(index); |
| 192 |
} |
| 193 |
wxTheApp->Unbind(wxEVT_MENU, &DatabaseExplorer::OnExecuteSQL, this, XRCID("wxEVT_EXECUTE_SQL")); |
| 194 |
wxDELETE(m_dbViewerPanel); |
| 195 |
} |
| 196 |
|
| 197 |
bool DatabaseExplorer::IsDbViewDetached() |
| 198 |
{ |
| 199 |
wxASSERT(m_mgr); |
| 200 |
IConfigTool* configTool = m_mgr->GetConfigTool(); |
| 201 |
wxASSERT(configTool); |
| 202 |
|
| 203 |
DetachedPanesInfo dpi; |
| 204 |
configTool->ReadObject("DetachedPanesList", &dpi); |
| 205 |
const wxArrayString& detachedPanes = dpi.GetPanes(); |
| 206 |
|
| 207 |
return detachedPanes.Index(_("DbExplorer")) != wxNOT_FOUND; |
| 208 |
} |
| 209 |
|
| 210 |
void DatabaseExplorer::OnAbout(wxCommandEvent& e) |
| 211 |
{ |
| 212 |
wxString version = wxString::Format(DBE_VERSION); |
| 213 |
wxString desc = _("Cross platform database explorer\n\n"); |
| 214 |
desc << wxbuildinfo(long_f) << "\n\n"; |
| 215 |
|
| 216 |
wxAboutDialogInfo info; |
| 217 |
info.SetName(_("DatabaseExplorer")); |
| 218 |
info.SetVersion(version); |
| 219 |
info.SetDescription(desc); |
| 220 |
info.SetCopyright(_("2011 - 2015 (C) Tomas Bata University, Zlin, Czech Republic")); |
| 221 |
info.SetWebSite(_("http://www.fai.utb.cz")); |
| 222 |
info.AddDeveloper("Peter Janků"); |
| 223 |
info.AddDeveloper("Michal Bližňák"); |
| 224 |
|
| 225 |
wxAboutBox(info); |
| 226 |
} |
| 227 |
|
| 228 |
void DatabaseExplorer::OnOpenWithDBE(clCommandEvent& e) |
| 229 |
{ |
| 230 |
// get the file name |
| 231 |
e.Skip(); |
| 232 |
if(FileExtManager::IsFileType(e.GetFileName(), FileExtManager::TypeDatabase)) { |
| 233 |
e.Skip(false); |
| 234 |
// Open the databse file |
| 235 |
DoOpenFile(e.GetFileName()); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
void DatabaseExplorer::DoOpenFile(const wxFileName& filename) |
| 240 |
{ |
| 241 |
if(m_dbViewerPanel) { |
| 242 |
m_dbViewerPanel->OpenSQLiteFile(filename, true); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
void DatabaseExplorer::OnToggleTab(clCommandEvent& event) |
| 247 |
{ |
| 248 |
if(event.GetString() != _("DbExplorer")) { |
| 249 |
event.Skip(); |
| 250 |
return; |
| 251 |
} |
| 252 |
|
| 253 |
if(event.IsSelected()) { |
| 254 |
// show it |
| 255 |
clGetManager()->GetWorkspacePaneNotebook()->AddPage(m_dbViewerPanel, _("DbExplorer"), true); |
| 256 |
} else { |
| 257 |
int where = m_mgr->GetWorkspacePaneNotebook()->GetPageIndex(_("DbExplorer")); |
| 258 |
if(where != wxNOT_FOUND) { |
| 259 |
clGetManager()->GetWorkspacePaneNotebook()->RemovePage(where); |
| 260 |
} |
| 261 |
} |
| 262 |
} |