File: wb_template_list.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (225 lines) | stat: -rw-r--r-- 7,235 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "mforms/menu.h"
#include "mforms/toolbar.h"
#include "mforms/scrollpanel.h"

#include "grts/structs.db.h"

#include "workbench/wb_context.h"
#include "wb_template_list.h"
#include "wb_context_model.h"
#include "wb_model_diagram_form.h"
#include "wb_component_physical.h"

using namespace mforms;
using namespace base;


size_t TableTemplateList::count()
{
  return (int)grt::BaseListRef::cast_from(_grt->get("/wb/options/options/TableTemplates")).count();
}

bool TableTemplateList::get_field(const bec::NodeId &node, ColumnId column, std::string &value)
{
  grt::BaseListRef templates(grt::BaseListRef::cast_from(_grt->get("/wb/options/options/TableTemplates")));
  if (node[0] < templates.count())
  {
    db_TableRef table = db_TableRef::cast_from(templates[node[0]]);

    switch (column)
    {
      case 0:
        value = table->name();
        return true;
      case 1:
      {
        for (size_t c = table->columns().count(), i = 0; i < c; i++)
        {
          if (!value.empty())
            value.append(", ");
          value.append(table->columns()[i]->name());
        }
        return true;
      }
    }
  }
  return false;
}


void TableTemplateList::refresh()
{
}


std::string TableTemplateList::get_selected_template()
{
  std::string name;
  get_field(selected_index(), 0, name);
  return name;
}

//------------------------------------------------------------------------------------------------

void TableTemplateList::prepare_context_menu()
{
  _context_menu = manage(new Menu());
  _context_menu->set_handler(boost::bind(&TableTemplatePanel::on_action, _owner, _1));
  _context_menu->signal_will_show()->connect(boost::bind(&TableTemplateList::menu_will_show, this));

  _context_menu->add_item("New Table from Template", "use_template");
  _context_menu->add_separator();
  _context_menu->add_item("Edit Template...", "edit_templates");

}

//------------------------------------------------------------------------------------------------

void TableTemplateList::menu_will_show()
{
}

//------------------------------------------------------------------------------------------------


TableTemplateList::TableTemplateList(grt::GRT *grt, TableTemplatePanel *owner)
: BaseSnippetList("snippet_mwb.png", this), _grt(grt), _owner(owner)
{
  prepare_context_menu();
  refresh_snippets();
}

//------------------------------------------------------------------------------------------------

TableTemplateList::~TableTemplateList()
{
  _context_menu->release();
}

//------------------------------------------------------------------------------------------------

bool TableTemplateList::mouse_double_click(mforms::MouseButton button, int x, int y)
{
  BaseSnippetList::mouse_double_click(button, x, y);

  if (button == MouseButtonLeft)
  {
    Snippet* snippet = snippet_from_point(x, y);
    if (snippet != NULL && snippet == _selected_snippet)
    {
      _owner->on_action("use_template");
      return true;
    }
  }

  return false;
}

//------------------------------------------------------------------------------------------------

TableTemplatePanel::TableTemplatePanel(grt::GRT *grt, wb::WBContextModel *cmodel)
: mforms::Box(false), _grt(grt), _templates(grt, this), _context(cmodel)
{
#ifdef _WIN32
  set_padding(3, 3, 3, 3);
  _templates.set_back_color(base::Color::get_application_color_as_string(AppColorPanelContentArea, false));
#else
  _templates.set_back_color("#f2f2f2");
#endif
  _scroll_panel = manage(new mforms::ScrollPanel());
  _scroll_panel->add(&_templates);

  _toolbar = mforms::manage(new mforms::ToolBar(mforms::PaletteToolBar));

  mforms::ToolBarItem *item = mforms::manage(new mforms::ToolBarItem(mforms::ActionItem));
  item->set_name("edit_templates");
  item->set_icon(mforms::App::get()->get_resource_path("edit_table_templates.png"));
  item->set_tooltip("Open the table template editor.");
  scoped_connect(item->signal_activated(), boost::bind(&TableTemplatePanel::toolbar_item_activated, this, _1));
  _toolbar->add_item(item);

  item = mforms::manage(new mforms::ToolBarItem(mforms::SeparatorItem));
  _toolbar->add_item(item);

  /*
  item = mforms::manage(new mforms::ToolBarItem(mforms::ActionItem));
  item->set_icon(mforms::App::get()->get_resource_path("snippet_add.png"));
  item->set_name("add_template");
  item->set_tooltip("Create a table template from the selected table object.");
  scoped_connect(item->signal_activated(), boost::bind(&TableTemplatePanel::toolbar_item_activated, this, _1));
  _toolbar->add_item(item);
*/
  item = mforms::manage(new mforms::ToolBarItem(mforms::ActionItem));
  item->set_name("use_template");
  item->set_icon(mforms::App::get()->get_resource_path("tiny_new_table.png"));
  item->set_tooltip("Create a new table based on the selected table template.");
  scoped_connect(item->signal_activated(), boost::bind(&TableTemplatePanel::toolbar_item_activated, this, _1));
  _toolbar->add_item(item);

  add(_toolbar, false, true);
  add(_scroll_panel, true, true);
}


void TableTemplatePanel::on_action(const std::string &action)
{
  if (action == "edit_templates")
  {
    grt::BaseListRef args(_grt);
    args.ginsert(grt::StringRef(_templates.get_selected_template()));
    _grt->call_module_function("WbTableUtils", "openTableTemplateEditorFor", args);
    _templates.refresh_snippets();
  }
  else if (action == "use_template")
  {
    if (!_templates.get_selected_template().empty())
    {
      grt::BaseListRef args(_grt);
      args.ginsert(workbench_physical_ModelRef::cast_from(_context->get_active_model(true))->catalog()->schemata()[0]);
      args.ginsert(grt::StringRef(_templates.get_selected_template()));
      db_TableRef table(db_TableRef::cast_from(_grt->call_module_function("WbTableUtils", "createTableFromTemplate", args)));
      if (table.is_valid())
      {
        model_DiagramRef d = _context->get_active_model_diagram(true);
        if (d.is_valid())
        {
          wb::ModelDiagramForm *diagram = _context->get_diagram_form_for_diagram_id(d.id());
          if (diagram)
          {
            std::list<GrtObjectRef> objects;
            objects.push_back(table);
            diagram->perform_drop(10, 10, WB_DBOBJECT_DRAG_TYPE, objects);
          }
        }
      }
    }
    else
      mforms::Utilities::show_message("Empty Selection", "Please select template to be used.", "Ok");

  }
}

void TableTemplatePanel::toolbar_item_activated(mforms::ToolBarItem *item)
{
  on_action(item->get_name());
}